Elm  2
ELM is a library providing generic data structures, OS-independent interface, plugins and XML.
Path.h
1 /*
2  * Path class interface
3  *
4  * This file is part of OTAWA
5  * Copyright (c) 2005-8, IRIT UPS.
6  *
7  * OTAWA is free software; you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License as published by
9  * the Free Software Foundation; either version 2 of the License, or
10  * (at your option) any later version.
11  *
12  * OTAWA is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with OTAWA; if not, write to the Free Software
19  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
20  */
21 #ifndef ELM_SYS_PATH_H
22 #define ELM_SYS_PATH_H
23 
24 #include <elm/io/InStream.h>
25 #include <elm/io/Output.h>
26 #include <elm/PreIterator.h>
27 #include <elm/string.h>
28 #include <elm/sys/SystemException.h>
29 
30 namespace elm { namespace sys {
31 
32 // Path class
33 class Path {
34 public:
35 # if defined(__WIN32) || defined(__WIN64)
36  static const char SEPARATOR = '\\';
37  static const char PATH_SEPARATOR = ';';
38  static inline bool isSeparator(char c) { return c == SEPARATOR || c == '/'; }
39 # else
40  static const char SEPARATOR = '/';
41  static const char PATH_SEPARATOR = ':';
42  static inline bool isSeparator(char c) { return c == SEPARATOR; }
43 # endif
44  static const string BACK_PATH;
45 
46  // Constructors
47  inline Path(void) { }
48  inline Path(const char *path): buf(path) { }
49  inline Path(CString path): buf(path) { }
50  inline Path(const String& path): buf(path) { }
51  inline Path(const Path& path): buf(path.buf) { }
52  Path canonical(void) const;
53  Path absolute(void) const;
54  static void setCurrent(Path& path);
55  Path append(Path path) const;
56  Path parent(void) const;
57  Path setExtension(CString new_extension) const;
58  inline Path setExt(cstring ext) const { return setExtension(ext); }
59  Path relativeTo(Path base) const;
60  Path withoutExt(void) const;
61 
62  // Accessors
63  inline const String& toString(void) const { return buf; }
64  String namePart(void) const;
65  sys::Path dirPart(void) const;
66  Path basePart(void) const;
67  String extension(void) const;
68  bool isEmpty(void) const { return buf.isEmpty(); }
69  bool isAbsolute(void) const;
70  bool isRelative(void) const;
71  bool isHomeRelative(void) const;
72  inline bool equals(Path& path) const { return buf == path.buf; }
73  inline bool subPathOf(const Path& path) const { return buf.startsWith(path.buf); }
74  inline bool isPrefixOf(const Path& path) const
75  { return path.buf.startsWith(buf) && (path.buf.length() == buf.length() || path.buf[buf.length()] == SEPARATOR); }
76  inline bool prefixedBy(const Path& path) const { return path.isPrefixOf(*this); }
77  static Path current(void);
78  static Path home(void);
79  static Path temp(void);
80  inline const char *asSysString() const { return buf.toCString().chars(); }
81 
82  // path testing
83  bool exists(void) const;
84  bool isFile(void) const;
85  bool isDir(void) const;
86  bool isReadable(void) const;
87  bool isWritable(void) const;
88  bool isExecutable(void) const;
89 
90  // file handling
91  void remove(void);
92  void makeDir(void);
93  io::InStream *read(void);
94  io::OutStream *write(void);
95  io::OutStream *append(void);
96 
97  // Operator
98  inline Path& operator=(const char *str) { buf = str; return *this; }
99  inline Path& operator=(CString str) { buf = str; return *this; }
100  inline Path& operator=(const String& str) { buf = str; return *this; }
101  inline Path& operator=(const Path& path) { buf = path.buf; return *this; }
102 
103  inline bool operator==(Path path) const { return equals(path); }
104  inline bool operator!=(Path path) const { return !equals(path); }
105  inline Path operator/(const Path& path) const { return append(path); }
106  inline operator const String& (void) const { return toString(); }
107  inline operator bool (void) const { return buf; }
108 
109  // Path iterator
110  class PathIter: public PreIterator<PathIter, string> {
111  friend class Path;
112  public:
113  inline PathIter(void): p(0), n(-1) { }
114  inline PathIter(string paths): s(paths), p(0), n(-1) { look(); }
115  inline bool ended(void) const { return p >= s.length(); }
116  inline Path item(void) const { return s.substring(p, n - p); }
117  inline void next(void) { p = n; if(p < s.length()) { p++; look(); } }
118  inline bool equals(const PathIter& i) const { return s == i.s && p == i.p; }
119  private:
120  inline PathIter(string paths, int p): s(paths), p(p), n(-1) { look(); }
121  void look(void);
122  string s;
123  int p, n;
124  };
125 
126  class PathSplit {
127  public:
128  inline PathSplit(string p): _p(p) { }
129  inline PathIter begin(void) const { return PathIter(_p); }
130  inline PathIter end(void) const { return PathIter(_p, _p.length()); }
131  private:
132  string _p;
133  };
134  inline static PathSplit splitPaths(string paths) { return PathSplit(paths); }
135 
136  // directory reading
137  class DirIter {
138  public:
139  typedef cstring t;
140 
141  inline DirIter(void): _dir(nullptr) { }
142  DirIter(Path path);
143 
144  inline bool ended(void) const { return _dir == nullptr; }
145  inline cstring item(void) const { return _cur; }
146  void next(void);
147  inline bool equals(const DirIter& i) const { return _dir == i._dir && _cur == i._cur; }
148 
149  inline operator bool() const { return !ended(); }
150  inline operator cstring() const { return item(); }
151  inline cstring operator*() const { return item(); }
152  inline DirIter& operator++() { next(); return *this; }
153  inline DirIter operator++(int) { DirIter o = *this; next(); return o; }
154  inline bool operator==(const DirIter& i) const { return equals(i); }
155  inline bool operator!=(const DirIter& i) const { return !equals(i); }
156 
157  private:
158  void *_dir;
159  cstring _cur;
160  };
161 
162  class DirReader {
163  public:
164  inline DirReader(const Path& p): _p(p) { }
165  inline DirIter begin(void) { return DirIter(_p); }
166  inline DirIter end(void) { return DirIter(); }
167  private:
168  const Path& _p;
169  };
170 
171  inline DirReader readDir(void) const { return DirReader(*this); }
172  void makeDirs(void) const;
173 
174  // deprecated
175  inline bool contains(const Path& path) const { return path.subPathOf(*this); }
176 
177 private:
178  int nextSeparator(int start = 0) const;
179  int lastSeparator(void) const;
180  String buf;
181 };
182 
183 inline io::Output& operator<<(io::Output& out, const Path& path)
184  { out << path.toString(); return out; }
185 
186 } // system
187 
189 
190 } // elm
191 
192 #endif // ELM_SYS_PATH_H
elm::sys::Path::DirIter::operator!=
bool operator!=(const DirIter &i) const
Definition: Path.h:155
elm::sys::Path::PathIter::equals
bool equals(const PathIter &i) const
Definition: Path.h:118
elm::sys::Path::isFile
bool isFile(void) const
Definition: system_Path.cpp:559
elm::sys::Path::PathIter::ended
bool ended(void) const
Definition: Path.h:115
elm::sys::Path::equals
bool equals(Path &path) const
Definition: Path.h:72
elm::sys::Path::extension
String extension(void) const
Definition: system_Path.cpp:495
elm::t::out
typename type_info< T >::out_t out
Definition: type_info.h:284
elm::sys::Path::operator=
Path & operator=(const char *str)
Definition: Path.h:98
elm::sys::Path::PathIter::item
Path item(void) const
Definition: Path.h:116
elm::io::p
Printable< T, M > p(const T &data, const M &man)
Definition: Output.h:302
elm::sys::Path::BACK_PATH
static const string BACK_PATH
Definition: Path.h:44
elm::sys::Path::setCurrent
static void setCurrent(Path &path)
Definition: system_Path.cpp:200
elm::sys::Path::readDir
DirReader readDir(void) const
Definition: Path.h:171
elm::sys::Path
Definition: Path.h:33
elm::sys::Path::absolute
Path absolute(void) const
Definition: system_Path.cpp:186
elm::sys::Path::makeDirs
void makeDirs(void) const
Definition: system_Path.cpp:687
elm::sys::Path::DirIter::DirIter
DirIter(void)
Definition: Path.h:141
elm::sys::Path::SEPARATOR
static const char SEPARATOR
Definition: Path.h:40
elm::sys::Path::contains
bool contains(const Path &path) const
Definition: Path.h:175
elm::sys::Path::PathIter
Definition: Path.h:110
elm::sys::Path::DirReader::begin
DirIter begin(void)
Definition: Path.h:165
elm::sys::Path::Path
Path(CString path)
Definition: Path.h:49
elm::sys::Path::setExt
Path setExt(cstring ext) const
Definition: Path.h:58
elm::sys::Path::isRelative
bool isRelative(void) const
Definition: system_Path.cpp:294
elm::sys::Path::append
io::OutStream * append(void)
Definition: system_Path.cpp:659
elm::sys::Path::toString
const String & toString(void) const
Definition: Path.h:63
elm::sys::Path::DirReader::DirReader
DirReader(const Path &p)
Definition: Path.h:164
elm::io::base
IntFormat base(int base, IntFormat fmt)
Definition: Output.h:256
elm::sys::Path::isAbsolute
bool isAbsolute(void) const
Definition: system_Path.cpp:274
elm::sys::Path::Path
Path(const char *path)
Definition: Path.h:48
elm::sys::Path::current
static Path current(void)
Definition: system_Path.cpp:349
elm::sys::Path::DirReader
Definition: Path.h:162
elm::sys::Path::splitPaths
static PathSplit splitPaths(string paths)
Definition: Path.h:134
elm::sys::Path::PathSplit
Definition: Path.h:126
elm::sys::Path::DirIter::operator*
cstring operator*() const
Definition: Path.h:151
elm::cstring
CString cstring
Definition: CString.h:62
elm::sys::Path::DirIter::equals
bool equals(const DirIter &i) const
Definition: Path.h:147
elm::sys::Path::operator/
Path operator/(const Path &path) const
Definition: Path.h:105
elm::sys::Path::operator=
Path & operator=(const String &str)
Definition: Path.h:100
elm::sys::Path::subPathOf
bool subPathOf(const Path &path) const
Definition: Path.h:73
elm::sys::Path::prefixedBy
bool prefixedBy(const Path &path) const
Definition: Path.h:76
void
elm::sys::Path::DirIter::t
cstring t
Definition: Path.h:139
bool
elm::sys::Path::makeDir
void makeDir(void)
Definition: system_Path.cpp:626
elm::sys::Path::canonical
Path canonical(void) const
Definition: system_Path.cpp:134
elm::CString
Definition: CString.h:17
elm::sys::Path::operator==
bool operator==(Path path) const
Definition: Path.h:103
elm::sys::Path::DirIter::item
cstring item(void) const
Definition: Path.h:145
elm::sys::Path::operator=
Path & operator=(CString str)
Definition: Path.h:99
elm::sys::Path::isDir
bool isDir(void) const
Definition: system_Path.cpp:572
elm::sys::Path::relativeTo
Path relativeTo(Path base) const
Definition: system_Path.cpp:114
elm::sys::Path::parent
Path parent(void) const
Definition: system_Path.cpp:228
elm::sys::Path::Path
Path(void)
Definition: Path.h:47
elm::String::length
int length(void) const
Definition: String.h:75
elm::sys::Path::isPrefixOf
bool isPrefixOf(const Path &path) const
Definition: Path.h:74
elm::sys::Path::DirIter::operator==
bool operator==(const DirIter &i) const
Definition: Path.h:154
elm::sys::Path::asSysString
const char * asSysString() const
Definition: Path.h:80
elm::sys::Path::dirPart
sys::Path dirPart(void) const
Definition: system_Path.cpp:261
elm
Definition: adapter.h:26
elm::sys::Path::isWritable
bool isWritable(void) const
Definition: system_Path.cpp:595
elm::sys::Path::isEmpty
bool isEmpty(void) const
Definition: Path.h:68
elm::CString::chars
const char * chars(void) const
Definition: CString.h:27
elm::sys::Path::read
io::InStream * read(void)
Definition: system_Path.cpp:636
elm::sys::Path::isSeparator
static bool isSeparator(char c)
Definition: Path.h:42
elm::sys::Path::remove
void remove(void)
Definition: system_Path.cpp:617
elm::sys::Path::Path
Path(const Path &path)
Definition: Path.h:51
elm::sys::Path::PathSplit::end
PathIter end(void) const
Definition: Path.h:130
elm::sys::Path::DirIter::ended
bool ended(void) const
Definition: Path.h:144
elm::sys::Path::isExecutable
bool isExecutable(void) const
Definition: system_Path.cpp:604
elm::String::substring
String substring(int _off) const
Definition: String.h:96
elm::sys::Path::PathSplit::begin
PathIter begin(void) const
Definition: Path.h:129
elm::io::OutStream
Definition: OutStream.h:30
elm::Path
elm::sys::Path Path
Definition: Path.h:188
elm::sys::Path::setExtension
Path setExtension(CString new_extension) const
Definition: system_Path.cpp:510
elm::sys::Path::withoutExt
Path withoutExt(void) const
Definition: system_Path.cpp:535
elm::sys::Path::PathIter::next
void next(void)
Definition: Path.h:117
elm::sys::Path::DirIter
Definition: Path.h:137
elm::sys::Path::DirIter::operator++
DirIter & operator++()
Definition: Path.h:152
elm::sys::Path::PathIter::PathIter
PathIter(void)
Definition: Path.h:113
elm::sys::operator<<
io::Output & operator<<(io::Output &out, const Path &path)
Definition: Path.h:183
elm::sys::Path::write
io::OutStream * write(void)
Definition: system_Path.cpp:647
elm::sys::Path::DirIter::operator++
DirIter operator++(int)
Definition: Path.h:153
elm::sys::Path::PATH_SEPARATOR
static const char PATH_SEPARATOR
Definition: Path.h:41
elm::sys::Path::PathSplit::PathSplit
PathSplit(string p)
Definition: Path.h:128
elm::sys::Path::PathIter::PathIter
PathIter(string paths)
Definition: Path.h:114
elm::String
Definition: String.h:30
elm::sys::Path::DirIter::next
void next(void)
Definition: system_Path.cpp:716
elm::sys::Path::basePart
Path basePart(void) const
Definition: system_Path.cpp:481
elm::String::startsWith
bool startsWith(const char *str) const
Definition: String.h:112
elm::str
string str(const char *s)
Definition: String.h:150
elm::sys::Path::DirReader::end
DirIter end(void)
Definition: Path.h:166
elm::sys::Path::temp
static Path temp(void)
Definition: system_Path.cpp:385
elm::String::toCString
CString toCString(void) const
Definition: String.h:90
elm::String::isEmpty
bool isEmpty(void) const
Definition: String.h:87
elm::PreIterator
Definition: iter.h:28
elm::io::Output
Definition: Output.h:179
elm::sys::Path::operator=
Path & operator=(const Path &path)
Definition: Path.h:101
elm::sys::Path::operator!=
bool operator!=(Path path) const
Definition: Path.h:104
elm::sys::Path::namePart
String namePart(void) const
Definition: system_Path.cpp:248
elm::sys::Path::home
static Path home(void)
Definition: system_Path.cpp:366
elm::sys::Path::isReadable
bool isReadable(void) const
Definition: system_Path.cpp:585
elm::sys::Path::exists
bool exists(void) const
Definition: system_Path.cpp:549
elm::io::InStream
Definition: InStream.h:29
elm::sys::Path::Path
Path(const String &path)
Definition: Path.h:50
elm::sys::Path::isHomeRelative
bool isHomeRelative(void) const
Definition: system_Path.cpp:303