Sleeping Wombat Common Library  0.50.0
swCommonLibrary
Path.h
1 #pragma once
2 
13 //#include <filesystem>
14 #include "Filesystem/filesystem/path.h"
15 #include <string>
16 #include <codecvt>
17 
18 #include "swCommonLib/Common/Converters.h"
19 
20 
21 namespace filesystem
22 {
23 
24 //namespace experimental = std::tr2::sys;
25 
26 class Path
27 {
28 private:
29  path_impl m_path;
30 
31 public:
32 
33  explicit Path ();
34  Path ( const Path& path ) = default;
35  Path ( Path&& path ) = default;
36  Path ( const std::wstring& path );
37 
38  template< class Source >
39  Path ( const Source& source );
40 
41 
42  Path& operator= ( const Path& other );
43  Path& operator= ( Path&& other );
44 
45  Path& operator/= ( const Path& other );
46  bool operator== ( const Path& other ) const;
47  bool operator!= ( const Path& other ) const;
48  bool operator< ( const Path& other ) const;
49  bool operator<= ( const Path& other ) const;
50  bool operator> ( const Path& other ) const;
51  bool operator>= ( const Path& other ) const;
52  //bool Compare ( const Path& path1, const Path& path2 );
53 
54  friend Path operator/ ( const Path& path1, const Path& path2 );
55 
56  std::string String () const;
57  std::wstring WString () const;
58 
59 
60  std::string GetFileName () const;
61  std::string GetExtension () const;
62  Path GetParent () const;
63  Path GetDirectory () const;
64 
65  void Normalize ();
66 
67 
68  //bool HasRoot () const;
69  bool HasFileName () const;
70  bool HasExtension () const;
71 
72  bool IsRelative () const;
73  bool IsAbsolut () const;
74 
76  bool Exists () const;
77 
78  static Path WorkingDirectory();
79 
80 public:
81  //const experimental::path& GetStdPath () const;
82 };
83 
84 
85 // ================================ //
86 //
87 template< class Source >
88 inline Path::Path ( const Source& source )
89  : m_path( path_impl( source ) )
90 {}
91 
92 // ================================ //
93 //
94 inline Path::Path()
95 {}
96 
97 // ================================ //
98 //
99 inline Path::Path ( const std::wstring& path )
100 {
101  //typedef std::codecvt_utf8< wchar_t > ConvertType;
102  //std::wstring_convert< ConvertType, wchar_t > converter;
103  //auto pathStr = converter.to_bytes( path );
104 
105  m_path = path_impl( path );
106 }
107 
108 // ================================ //
109 //
110 inline Path& Path::operator= ( const Path& other )
111 {
112  m_path = other.m_path;
113  return *this;
114 }
115 
116 // ================================ //
117 //
118 inline Path& Path::operator= ( Path&& other )
119 {
120  m_path = std::move( other.m_path );
121  return *this;
122 }
123 
124 // ================================ //
125 //
126 inline Path& Path::operator/= ( const Path& other )
127 {
128  m_path = m_path / other.m_path;
129  return *this;
130 }
131 
132 // ================================ //
133 //
134 inline bool Path::operator== ( const Path& other ) const
135 {
136  return m_path == other.m_path;
137 }
138 
139 // ================================ //
140 //
141 inline bool Path::operator!= ( const Path& other ) const
142 {
143  return m_path != other.m_path;
144 }
145 
146 // ================================ //
147 //
148 inline bool Path::operator< ( const Path& other ) const
149 {
150  return m_path.str() < other.m_path.str();
151 }
152 
153 // ================================ //
154 //
155 inline bool Path::operator<= ( const Path& other ) const
156 {
157  return m_path.str() <= other.m_path.str();
158 }
159 
160 // ================================ //
161 //
162 inline bool Path::operator> ( const Path& other ) const
163 {
164  return m_path.str() > other.m_path.str();
165 }
166 
167 // ================================ //
168 //
169 inline bool Path::operator>= ( const Path& other ) const
170 {
171  return m_path.str() >= other.m_path.str();
172 }
173 
175 //Paths must exist on file system.*/
176 //inline bool Path::Compare ( const Path& path1, const Path& path2 )
177 //{
178 // return experimental::equivalent( path1.m_path, path2.m_path );
179 //}
180 
182 inline std::string Path::String() const
183 {
184  return m_path.str();
185 }
186 
188 inline std::wstring Path::WString() const
189 {
190  return m_path.wstr();
191 }
192 
194 inline std::string Path::GetFileName() const
195 {
196  return m_path.filename();
197 }
198 
200 inline std::string Path::GetExtension() const
201 {
202  return m_path.extension();
203 }
204 
206 inline Path Path::GetParent() const
207 {
208  return m_path.parent_path();
209 }
210 
212 inline Path Path::GetDirectory() const
213 {
214  if( HasFileName() )
215  return GetParent();
216  else
217  return Path( *this );
218 }
219 
222 inline void Path::Normalize()
223 {
224  //m_path = experimental::complete( m_path );
225 }
226 
228 //inline bool Path::HasRoot() const
229 //{
230 // return m_path.has_root_directory();
231 //}
232 
234 inline bool Path::HasFileName() const
235 {
236  return !m_path.str().empty();
237 }
238 
240 inline bool Path::HasExtension() const
241 {
242  return !GetExtension().empty();
243 }
244 
246 inline bool Path::IsRelative() const
247 {
248  return !IsAbsolut();
249 }
250 
252 inline bool Path::IsAbsolut() const
253 {
254  return m_path.is_absolute();
255 }
256 
258 inline bool Path::Exists() const
259 {
260  return m_path.exists();
261 }
262 
263 // ================================ //
264 //
265 inline Path Path::WorkingDirectory()
266 {
267  return path_impl::getcwd();
268 }
269 
271 inline Path operator/( const Path& path1, const Path& path2 )
272 {
273  Path newPath = path1;
274  newPath /= path2;
275  return newPath;
276 }
277 
279 //Use only if you must.*/
280 //inline const experimental::path& Path::GetStdPath() const
281 //{
282 // return m_path;
283 //}
284 
285 }
Definition: Path.h:26
std::string String() const
Porównuje ścieżki. Przed porównaniem ścieżki są normalizowane. Paths must exist on file system...
Definition: Path.h:182
bool Exists() const
Checks if file or directory exists on filesystem.
Definition: Path.h:258
void Normalize()
Normalizuje ścieżkę.
Definition: Path.h:222
Definition: Dir.cpp:12
Simple class for manipulating paths on Linux/Windows/Mac OS.
Definition: path.h:33