Path.h
1 /*
2  * Copyright (C) 2006-2014 Marc Boris Duerner
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public
6  * License as published by the Free Software Foundation; either
7  * version 2.1 of the License, or (at your option) any later version.
8  *
9  * As a special exception, you may use this file as part of a free
10  * software library without restriction. Specifically, if other files
11  * instantiate templates or use macros or inline functions from this
12  * file, or you compile this file and link it with other files to
13  * produce an executable, this file does not by itself cause the
14  * resulting executable to be covered by the GNU General Public
15  * License. This exception does not however invalidate any other
16  * reasons why the executable file might be covered by the GNU Library
17  * General Public License.
18  *
19  * This library is distributed in the hope that it will be useful,
20  * but WITHOUT ANY WARRANTY; without even the implied warranty of
21  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
22  * Lesser General Public License for more details.
23  *
24  * You should have received a copy of the GNU Lesser General Public
25  * License along with this library; if not, write to the Free Software
26  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
27  */
28 
29 #ifndef Pt_System_Path_h
30 #define Pt_System_Path_h
31 
32 #include <Pt/System/Api.h>
33 #include <Pt/String.h>
34 #include <Pt/Types.h>
35 #include <string>
36 
37 namespace Pt {
38 
39 namespace System {
40 
41 class PathImpl;
42 
47 class PT_SYSTEM_API Path
48 {
49  public:
52  Path();
53 
56  Path(const Path& p);
57 
60  Path(const Pt::String& s);
61 
64  Path(const char* s);
65 
68  Path(const char* s, std::size_t n);
69 
72  ~Path();
73 
74  // Assignments
75 
78  Path& assign(const Path& p);
79 
82  Path& assign(const Pt::String& s);
83 
86  Path& assign(const char* s);
87 
90  Path& assign(const char* s, std::size_t n);
91 
94  Path& operator=(const Path& p)
95  { return assign(p); }
96 
100  { return assign(s); }
101 
104  Path& operator=(const char* s)
105  { return assign(s); }
106 
107  // Appends
108 
113  Path& append(const Path& p);
114 
119  Path& append(const Pt::String& s);
120 
125  Path& append(const char* s);
126 
131  Path& append(const char* s, std::size_t n);
132 
137  Path& operator/=(const Path& p)
138  { return append(p); }
139 
145  { return append(s); }
146 
151  Path& operator/=(const char* s)
152  { return append(s); }
153 
154  // Concatenation
155 
160  Path& concat(const Path& p);
161 
166  Path& concat(const Pt::String& s);
167 
172  Path& concat(const char* s);
173 
178  Path& concat(const char* s, std::size_t n);
179 
182  Path& operator+=(const Path& p)
183  { return concat(p); }
184 
190  { return concat(s); }
191 
196  Path& operator+=(const char* s)
197  { return concat(s); }
198 
199  // Modifiers
200 
203  void clear();
204 
205  // Query
206 
209  bool empty() const;
210 
211  // TODO: return Path objects
212 
216 
220 
224 
228 
229  // Comparison
230 
233  int compare(const Path& p) const;
234 
235  // Conversion
236 
240 
243  std::string toLocal() const;
244 
251 
252  // Others
253 
256  static Pt::String dirsep();
257 
260  static Pt::String curdir();
261 
264  static Pt::String updir();
265 
266  // Implementation
267 
268  const PathImpl* impl() const
269  { return _impl; }
270 
271  PathImpl* impl()
272  { return _impl; }
273 
274  private:
275  PathImpl* _impl;
276  std::string _pathData;
277 };
278 
283 inline Path operator/(const Path& a, const Path& b)
284 {
285  return Path(a) /= b;
286 }
287 
292 inline Path operator/(const Path& a, const Pt::String& b)
293 {
294  return Path(a) /= b;
295 }
296 
301 inline Path operator/(const Path& a, const char* b)
302 {
303  return Path(a) /= b;
304 }
305 
310 inline bool operator==(const Path& a, const Path& b)
311 {
312  return a.compare(b) == 0;
313 }
314 
319 inline bool operator!=(const Path& a, const Path& b)
320 {
321  return a.compare(b) != 0;
322 }
323 
328 inline bool operator<(const Path& a, const Path& b)
329 {
330  return a.compare(b) < 0;
331 }
332 
333 } // namespace System
334 
335 } // namespace Pt
336 
337 #endif // Pt_System_Path_h
Path & append(const Path &p)
Appends a path name.
void clear()
Clears the path.
Pt::String toGeneric() const
Returns the path name with generic separators.
Core module.
Definition: Allocator.h:33
bool empty() const
Returns true if path is empty.
~Path()
Destructor.
Pt::String extension() const
Returns the file name extension.
Path()
Default constructor.
Path & assign(const Path &p)
Assigns a path name.
bool operator==(const Path &a, const Path &b)
Compares two paths.
Definition: Path.h:310
Path & assign(const char *s)
Assigns an UTF-8 encoded path name.
Path operator/(const Path &a, const Path &b)
Appends a paths to another.
Definition: Path.h:283
Path & assign(const Pt::String &s)
Assigns a path name.
bool operator!=(const Path &a, const Path &b)
Compares two paths.
Definition: Path.h:319
Path(const Path &p)
Copy constructor.
Path & operator/=(const char *s)
Appends an UTF-8 encoded path name.
Definition: Path.h:151
Path & append(const char *s)
Appends an UTF-8 encoded path name.
Pt::String dirName() const
Returns the directory part.
Path & operator+=(const Path &p)
Concatenates a path name.
Definition: Path.h:182
Pt::String fileName() const
Returns the file name without the directroy part.
Path & operator=(const char *s)
Assigns an UTF-8 encoded path name.
Definition: Path.h:104
Path & concat(const Path &p)
Concatenates a path name.
Path(const char *s)
Construct from an UTF-8 encoded path name.
Path & operator/=(const Pt::String &s)
Appends a path name.
Definition: Path.h:144
Path & append(const Pt::String &s)
Appends a path name.
static Pt::String dirsep()
Returns the directory separator string.
Path & concat(const char *s, std::size_t n)
Appends an UTF-8 encoded path name.
Path & operator=(const Path &p)
Assignment operator.
Definition: Path.h:94
Path & append(const char *s, std::size_t n)
Appends an UTF-8 encoded path name.
static Pt::String curdir()
Returns the current directory string.
Path operator/(const Path &a, const Pt::String &b)
Appends a paths to another.
Definition: Path.h:292
Path & operator+=(const Pt::String &s)
Concatenates a path name.
Definition: Path.h:189
bool operator<(const Path &a, const Path &b)
Compares two paths.
Definition: Path.h:328
Represents a path in the file-system.
Definition: Path.h:48
Path & concat(const Pt::String &s)
Concatenates a path name.
Path & operator+=(const char *s)
Concatenates a path name.
Definition: Path.h:196
Path operator/(const Path &a, const char *b)
Appends a paths to another.
Definition: Path.h:301
Path & operator/=(const Path &p)
Appends a path name.
Definition: Path.h:137
Pt::String toString() const
Returns the path name.
Path & assign(const char *s, std::size_t n)
Assigns an UTF-8 encoded path name.
static Pt::String updir()
Returns the parent directory string.
Pt::String baseName() const
Returns the file name without directory and extension.
Unicode capable basic_string.
Definition: Api-String.h:44
int compare(const Path &p) const
Compares two paths (with strcmp semantics).
Path & operator=(const Pt::String &s)
Assigns a path name.
Definition: Path.h:99
Path(const Pt::String &s)
Construct from a path name.
Path(const char *s, std::size_t n)
Construct from an UTF-8 encoded path name.
std::string toLocal() const
Returns the path name in local encoding.
Path & concat(const char *s)
Concatenates a path name.