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 
58  // TODO: explicit? Path p = "x"; not allowed?
59 
62  explicit Path(const Pt::String& s);
63 
66  explicit Path(const char* s);
67 
70  explicit Path(const char* s, std::size_t n);
71 
74  ~Path();
75 
76  // Assignments
77 
80  Path& assign(const Path& p);
81 
84  Path& assign(const Pt::String& s);
85 
88  Path& assign(const char* s);
89 
92  Path& assign(const char* s, std::size_t n);
93 
96  Path& operator=(const Path& p)
97  { return assign(p); }
98 
102  { return assign(s); }
103 
106  Path& operator=(const char* s)
107  { return assign(s); }
108 
109  // Appends
110 
115  Path& append(const Path& p);
116 
121  Path& append(const Pt::String& s);
122 
127  Path& append(const char* s);
128 
133  Path& append(const char* s, std::size_t n);
134 
139  Path& operator/=(const Path& p)
140  { return append(p); }
141 
147  { return append(s); }
148 
153  Path& operator/=(const char* s)
154  { return append(s); }
155 
156  // Concatenation
157 
162  Path& concat(const Path& p);
163 
168  Path& concat(const Pt::String& s);
169 
174  Path& concat(const char* s);
175 
180  Path& concat(const char* s, std::size_t n);
181 
184  Path& operator+=(const Path& p)
185  { return concat(p); }
186 
192  { return concat(s); }
193 
198  Path& operator+=(const char* s)
199  { return concat(s); }
200 
201  // Modifiers
202 
205  void clear();
206 
207  // Query
208 
211  bool empty() const;
212 
213  // TODO: return Path objects
214 
217  Pt::String fileName() const;
218 
221  Pt::String dirName() const;
222 
225  Pt::String baseName() const;
226 
229  Pt::String extension() const;
230 
231  // Comparison
232 
235  int compare(const Path& p) const;
236 
237  // Conversion
238 
241  Pt::String toString() const;
242 
245  std::string toLocal() const;
246 
247  // Others
248 
251  static Pt::String dirsep();
252 
255  static Pt::String curdir();
256 
259  static Pt::String updir();
260 
261  // Implementation
262 
263  const PathImpl* impl() const
264  { return _impl; }
265 
266  PathImpl* impl()
267  { return _impl; }
268 
269  private:
270  PathImpl* _impl;
271  std::string _pathData;
272 };
273 
278 inline Path operator/(const Path& a, const Path& b)
279 {
280  return Path(a) /= b;
281 }
282 
287 inline Path operator/(const Path& a, const Pt::String& b)
288 {
289  return Path(a) /= b;
290 }
291 
296 inline Path operator/(const Path& a, const char* b)
297 {
298  return Path(a) /= b;
299 }
300 
305 inline bool operator==(const Path& a, const Path& b)
306 {
307  return a.compare(b) == 0;
308 }
309 
314 inline bool operator!=(const Path& a, const Path& b)
315 {
316  return a.compare(b) != 0;
317 }
318 
323 inline bool operator<(const Path& a, const Path& b)
324 {
325  return a.compare(b) < 0;
326 }
327 
328 } // namespace System
329 
330 } // namespace Pt
331 
332 #endif // Pt_System_Path_h
bool operator!=(const Path &a, const Path &b)
Compares two paths.
Definition: Path.h:314
Path operator/(const Path &a, const Path &b)
Appends a paths to another.
Definition: Path.h:278
int compare(const Path &p) const
Compares two paths (with strcmp semantics).
Represents a path in the file-system.
Definition: Path.h:47
Path & operator/=(const char *s)
Appends an UTF-8 encoded path name.
Definition: Path.h:153
Path operator/(const Path &a, const char *b)
Appends a paths to another.
Definition: Path.h:296
Path & operator/=(const Path &p)
Appends a path name.
Definition: Path.h:139
Path & operator/=(const Pt::String &s)
Appends a path name.
Definition: Path.h:146
Path & operator=(const Path &p)
Assignment operator.
Definition: Path.h:96
Path operator/(const Path &a, const Pt::String &b)
Appends a paths to another.
Definition: Path.h:287
Path & operator+=(const Pt::String &s)
Concatenates a path name.
Definition: Path.h:191
Unicode capable basic_string.
Definition: String.h:42
Path & operator=(const char *s)
Assigns an UTF-8 encoded path name.
Definition: Path.h:106
Path & operator+=(const char *s)
Concatenates a path name.
Definition: Path.h:198
bool operator==(const Path &a, const Path &b)
Compares two paths.
Definition: Path.h:305
Path & operator=(const Pt::String &s)
Assigns a path name.
Definition: Path.h:101
bool operator<(const Path &a, const Path &b)
Compares two paths.
Definition: Path.h:323
Path & operator+=(const Path &p)
Concatenates a path name.
Definition: Path.h:184