Sleeping Wombat GUI  0.100
swGUI
Path.h
Go to the documentation of this file.
1 #pragma once
2 
13 #include <filesystem>
14 #include <string>
15 #include <codecvt>
16 
17 namespace filesystem
18 {
19 
20 namespace experimental = std::tr2::sys;
21 
22 class Path
23 {
24 private:
25  experimental::path m_path;
26 
27 public:
28 
29  explicit Path ();
30  Path ( const Path& path );
31  Path ( Path&& path );
32  Path ( const std::wstring& path );
33 
34  template< class Source >
35  Path ( const Source& source );
36 
37 
38  Path& operator= ( const Path& other );
39  Path& operator= ( Path&& other );
40 
41  Path& operator/= ( const Path& other );
42  bool operator== ( const Path& other ) const;
43  bool Compare ( const Path& path1, const Path& path2 );
44 
45  friend Path operator/ ( const Path& path1, const Path& path2 );
46 
47  std::string String () const;
48 
49 
50  std::string GetFileName () const;
51  std::string GetExtension () const;
52  Path GetParent () const;
53  Path GetDirectory () const;
54 
55  void Normalize ();
56 
57 
58  bool HasRoot () const;
59  bool HasFileName () const;
60  bool HasExtension () const;
61 
62  bool IsRelative () const;
63  bool IsAbsolut () const;
64 
66  bool Exists () const;
67 
68  static Path WorkingDirectory();
69 
70 public:
71  const experimental::path& GetStdPath () const;
72 };
73 
74 
76 template< class Source >
77 inline Path::Path ( const Source& source )
78  : m_path( experimental::path( source ) )
79 {}
80 
82 inline Path::Path()
83 {}
84 
86 inline Path::Path ( const Path& path )
87  : m_path( path.m_path )
88 {}
89 
91 inline Path::Path ( Path&& path )
92  : m_path( std::move( path.m_path ) )
93 {}
94 
96 inline Path::Path ( const std::wstring& path )
97 {
98  typedef std::codecvt_utf8< wchar_t > ConvertType;
99  std::wstring_convert< ConvertType, wchar_t > converter;
100  auto pathStr = converter.to_bytes( path );
101 
102  m_path = experimental::path( pathStr );
103 }
104 
106 inline Path& Path::operator= ( const Path& other )
107 {
108  m_path = other.m_path;
109  return *this;
110 }
111 
113 inline Path& Path::operator= ( Path&& other )
114 {
115  m_path = std::move( other.m_path );
116  return *this;
117 }
118 
120 inline Path& Path::operator/= ( const Path& other )
121 {
122  m_path /= other.m_path;
123  return *this;
124 }
125 
127 inline bool Path::operator== ( const Path& other ) const
128 {
129  return experimental::equivalent( m_path, other.m_path );
130 }
131 
133 inline bool Path::Compare ( const Path& path1, const Path& path2 )
134 {
135  return experimental::equivalent( path1.m_path, path2.m_path );
136 }
137 
139 inline std::string Path::String() const
140 {
141  return m_path.string();
142 }
143 
145 inline std::string Path::GetFileName() const
146 {
147  return m_path.filename().string();
148 }
149 
151 inline std::string Path::GetExtension() const
152 {
153  return m_path.extension().string();
154 }
155 
157 inline Path Path::GetParent() const
158 {
159  return m_path.parent_path();
160 }
161 
163 inline Path Path::GetDirectory() const
164 {
165  if( m_path.has_filename() )
166  return GetParent();
167  else
168  return Path( *this );
169 }
170 
173 inline void Path::Normalize()
174 {
175  //m_path = experimental::complete( m_path );
176 }
177 
179 inline bool Path::HasRoot() const
180 {
181  return m_path.has_root_directory();
182 }
183 
185 inline bool Path::HasFileName() const
186 {
187  return m_path.has_filename();
188 }
189 
191 inline bool Path::HasExtension() const
192 {
193  return !GetExtension().empty();
194 }
195 
197 inline bool Path::IsRelative() const
198 {
199  return !IsAbsolut();
200 }
201 
203 inline bool Path::IsAbsolut() const
204 {
205  return m_path.has_root_name() && m_path.has_root_directory();
206 }
207 
209 inline bool Path::Exists() const
210 {
211  return experimental::exists( m_path );
212 }
213 
214 // ================================ //
215 //
217 {
218  return experimental::current_path();
219 }
220 
222 inline Path operator/( const Path& path1, const Path& path2 )
223 {
224  Path newPath = path1;
225  newPath /= path2;
226  return newPath;
227 }
228 
231 inline const experimental::path& Path::GetStdPath() const
232 {
233  return m_path;
234 }
235 
236 }
Path GetParent() const
Definition: Path.h:157
bool HasFileName() const
Definition: Path.h:185
Definition: Path.h:22
experimental::path m_path
Definition: Path.h:25
bool IsAbsolut() const
Definition: Path.h:203
Path()
Definition: Path.h:82
std::string GetExtension() const
Definition: Path.h:151
static Path WorkingDirectory()
Definition: Path.h:216
const experimental::path & GetStdPath() const
Returns standard library path. Use only if you must.
Definition: Path.h:231
bool HasExtension() const
Definition: Path.h:191
bool HasRoot() const
Definition: Path.h:179
Path & operator/=(const Path &other)
Definition: Path.h:120
bool Compare(const Path &path1, const Path &path2)
Porównuje ścieżki. Przed porównaniem ścieżki są normalizowane.
Definition: Path.h:133
std::string String() const
Definition: Path.h:139
Path & operator=(const Path &other)
Definition: Path.h:106
bool Exists() const
Checks if file or directory exists on filesystem.
Definition: Path.h:209
void Normalize()
Normalizuje ścieżkę.
Definition: Path.h:173
Path operator/(const Path &path1, const Path &path2)
Definition: Path.h:222
bool operator==(const Path &other) const
Definition: Path.h:127
Path GetDirectory() const
Definition: Path.h:163
bool IsRelative() const
Definition: Path.h:197
Definition: File.h:12
std::string GetFileName() const
Definition: Path.h:145
friend Path operator/(const Path &path1, const Path &path2)
Definition: Path.h:222