1 #ifndef FILEZILLA_ENGINE_SERVERPATH_HEADER
2 #define FILEZILLA_ENGINE_SERVERPATH_HEADER
3 
4 #include "server.h"
5 
6 #include <libfilezilla/optional.hpp>
7 #include <libfilezilla/shared.hpp>
8 
9 #include <vector>
10 
11 class FZC_PUBLIC_SYMBOL CServerPathData final
12 {
13 public:
14 	std::vector<std::wstring> m_segments;
15 	fz::sparse_optional<std::wstring> m_prefix;
16 
17 	bool operator==(const CServerPathData& cmp) const;
18 };
19 
20 class FZC_PUBLIC_SYMBOL CServerPath final
21 {
22 public:
23 	CServerPath();
24 	explicit CServerPath(std::wstring const& path, ServerType type = DEFAULT);
25 	CServerPath(CServerPath const& path, std::wstring subdir); // Ignores parent on absolute subdir
26 	CServerPath(CServerPath const& path) = default;
27 	CServerPath(CServerPath && path) noexcept = default;
28 
29 	CServerPath& operator=(CServerPath const& op) = default;
30 	CServerPath& operator=(CServerPath && op) noexcept = default;
31 
32 	explicit operator bool() const { return !empty(); }
empty()33 	bool empty() const { return !m_data; }
34 	void clear();
35 
36 	bool SetPath(std::wstring newPath);
37 	bool SetPath(std::wstring& newPath, bool isFile);
38 	bool SetSafePath(std::wstring const& path);
39 
40 	// If ChangePath returns false, the object will be left
41 	// empty.
42 	bool ChangePath(std::wstring const& subdir);
43 	bool ChangePath(std::wstring &subdir, bool isFile);
44 
45 	std::wstring GetPath() const;
46 	std::wstring GetSafePath() const;
47 
48 	bool HasParent() const;
49 	CServerPath GetParent() const;
50 	CServerPath& MakeParent();
51 	std::wstring GetFirstSegment() const;
52 	std::wstring GetLastSegment() const;
53 
54 	CServerPath GetCommonParent(CServerPath const& path) const;
55 
56 	bool SetType(ServerType type);
57 	ServerType GetType() const;
58 
59 	bool IsSubdirOf(CServerPath const& path, bool cmpNoCase, bool allowEqual = false) const;
60 	bool IsParentOf(CServerPath const& path, bool cmpNoCase, bool allowEqual = false) const;
61 
62 	bool operator==(CServerPath const& op) const;
63 	bool operator!=(CServerPath const& op) const;
64 	bool operator<(CServerPath const& op) const;
65 
66 	int CmpNoCase(CServerPath const& op) const;
67 
68 	// omitPath is just a hint. For example dataset member names on MVS servers
69 	// always use absolute filenames including the full path
70 	std::wstring FormatFilename(std::wstring const& filename, bool omitPath = false) const;
71 
72 	// Returns identity on all but VMS. On VMS it escapes dots
73 	std::wstring FormatSubdir(std::wstring const& subdir) const;
74 
75 	bool AddSegment(std::wstring const& segment);
76 
77 	size_t SegmentCount() const;
78 
79 	static CServerPath GetChanged(CServerPath const& oldPath, CServerPath const& newPath, std::wstring const& newSubdir);
80 private:
81 	bool FZC_PRIVATE_SYMBOL IsSeparator(wchar_t c) const;
82 
83 	bool FZC_PRIVATE_SYMBOL DoSetSafePath(std::wstring const& path);
84 	bool FZC_PRIVATE_SYMBOL DoChangePath(std::wstring &subdir, bool isFile);
85 
86 	typedef std::vector<std::wstring> tSegmentList;
87 	typedef tSegmentList::iterator tSegmentIter;
88 	typedef tSegmentList::const_iterator tConstSegmentIter;
89 
90 	bool FZC_PRIVATE_SYMBOL Segmentize(std::wstring const& str, tSegmentList& segments);
91 	bool FZC_PRIVATE_SYMBOL SegmentizeAddSegment(std::wstring & segment, tSegmentList& segments, bool& append);
92 	bool FZC_PRIVATE_SYMBOL ExtractFile(std::wstring& dir, std::wstring& file);
93 
94 	fz::shared_optional<CServerPathData> m_data;
95 	ServerType m_type;
96 };
97 
98 #endif
99