Elm  1.0
ELM is a library providing generic data structures, OS-independent interface, plugins and XML.
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
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/string.h>
25 #include <elm/io.h>
26 #include <elm/PreIterator.h>
27 
28 namespace elm { namespace sys {
29 
30 // Path class
31 class Path {
32  String buf;
33 public:
34 # if defined(__WIN32) || defined(__WIN64)
35  static const char SEPARATOR = '\\';
36  static const char PATH_SEPARATOR = ';';
37  static inline bool isSeparator(char c) { return c == SEPARATOR || c == '/'; }
38 # else
39  static const char SEPARATOR = '/';
40  static const char PATH_SEPARATOR = ':';
41  static inline bool isSeparator(char c) { return c == SEPARATOR; }
42 # endif
43 
44  // Constructors
45  inline Path(void) { }
46  inline Path(const char *path): buf(path) { }
47  inline Path(CString path): buf(path) { }
48  inline Path(const String& path): buf(path) { }
49  inline Path(const Path& path): buf(path.buf) { }
50  Path canonical(void) const;
51  Path absolute(void) const;
52  static void setCurrent(Path& path);
53  Path append(Path path) const;
54  Path parent(void) const;
55  Path setExtension(CString new_extension) const;
56 
57  // Accessors
58  inline const String& toString(void) const { return buf; }
59  String namePart(void) const;
60  sys::Path dirPart(void) const;
61  Path basePart(void) const;
62  String extension(void) const;
63  bool isAbsolute(void) const;
64  bool isRelative(void) const;
65  bool isHomeRelative(void) const;
66  inline bool equals(Path& path) const { return buf == path.buf; }
67  inline bool contains(Path& path) const { return path.buf.startsWith(path.buf); }
68  static Path current(void);
69  static Path home(void);
70 
71  // path testing
72  bool exists(void) const;
73  bool isFile(void) const;
74  bool isDir(void) const;
75  bool isReadable(void) const;
76  bool isWritable(void) const;
77  bool isExecutable(void) const;
78 
79  // Operator
80  inline Path& operator=(const char *str) { buf = str; return *this; }
81  inline Path& operator=(CString str) { buf = str; return *this; }
82  inline Path& operator=(const String& str) { buf = str; return *this; }
83  inline Path& operator=(const Path& path) { buf = path.buf; return *this; }
84 
85  inline bool operator==(Path path) const { return equals(path); }
86  inline bool operator!=(Path path) const { return !equals(path); }
87  inline Path operator/(const Path& path) const { return append(path); }
88  inline operator const String& (void) const { return toString(); }
89  inline operator bool (void) const { return buf; }
90  inline const char *operator&(void) const { return &buf; };
91 
92  // Path iterator
93  class PathIter: public PreIterator<PathIter, string> {
94  public:
95  inline PathIter(const string& str);
96  inline PathIter(const PathIter& iter);
97  inline bool ended(void) const;
98  inline Path item(void) const;
99  inline void next(void);
100  private:
101  inline void look(void);
102  const string& s;
103  int p, n;
104  };
105 
106 private:
107  int nextSeparator(int start = 0) const;
108  int lastSeparator(void) const;
109 };
110 
111 inline io::Output& operator<<(io::Output& out, const Path& path)
112  { out << path.toString(); return out; }
113 
114 } // system
115 
117 
118 Path::PathIter::PathIter(const string& str)
119 : s(str), p(0), n(-1) {
120  look();
121 }
122 
124 : s(iter.s), p(iter.p), n(iter.n) {
125 }
126 
127 bool Path::PathIter::ended(void) const {
128  return p >= s.length();
129 }
130 
132  return s.substring(p, n - p);
133 }
134 
136  p = n + 1;
137  look();
138 }
139 
140 void Path::PathIter::look(void) {
141  if(p >= s.length())
142  return;
143  n = s.indexOf(Path::PATH_SEPARATOR, n + 1);
144  if(n < 0)
145  n = s.length();
146 }
147 
148 } // elm
149 
150 #endif // ELM_SYS_PATH_H
Definition: Path.h:93
Definition: CString.h:17
Path absolute(void) const
Definition: system_Path.cpp:147
String substring(int _off) const
Definition: String.h:93
Path(const Path &path)
Definition: Path.h:49
Definition: PreIterator.h:29
void next(void)
Definition: Path.h:135
bool isDir(void) const
Definition: system_Path.cpp:418
Path(CString path)
Definition: Path.h:47
bool isFile(void) const
Definition: system_Path.cpp:405
Definition: Path.h:31
String namePart(void) const
Definition: system_Path.cpp:209
Definition: Output.h:161
static const char SEPARATOR
Definition: Path.h:39
elm::sys::Path Path
Definition: Path.h:116
Path & operator=(const Path &path)
Definition: Path.h:83
bool exists(void) const
Definition: system_Path.cpp:395
bool isWritable(void) const
Definition: system_Path.cpp:441
bool isReadable(void) const
Definition: system_Path.cpp:431
Path & operator=(const String &str)
Definition: Path.h:82
bool isHomeRelative(void) const
Definition: system_Path.cpp:257
Path setExtension(CString new_extension) const
Definition: system_Path.cpp:378
sys::Path dirPart(void) const
Definition: system_Path.cpp:222
Path operator/(const Path &path) const
Definition: Path.h:87
Path & operator=(const char *str)
Definition: Path.h:80
PathIter(const string &str)
Definition: Path.h:118
static const char PATH_SEPARATOR
Definition: Path.h:40
const char * operator&(void) const
Definition: Path.h:90
bool startsWith(const char *str) const
Definition: String.h:127
static Path current(void)
Definition: system_Path.cpp:284
bool isAbsolute(void) const
Definition: system_Path.cpp:235
sys::SystemOutStream & out
Definition: system_SystemIO.cpp:101
bool isExecutable(void) const
Definition: system_Path.cpp:450
Path(void)
Definition: Path.h:45
bool operator==(Path path) const
Definition: Path.h:85
int indexOf(char chr) const
Definition: String.h:99
bool isRelative(void) const
Definition: system_Path.cpp:248
bool ended(void) const
Definition: Path.h:127
int length(void) const
Definition: String.h:73
Path parent(void) const
Definition: system_Path.cpp:189
Definition: String.h:31
Path(const char *path)
Definition: Path.h:46
Path item(void) const
Definition: Path.h:131
Path & operator=(CString str)
Definition: Path.h:81
Path(const String &path)
Definition: Path.h:48
io::Output & operator<<(io::Output &out, const Path &path)
Definition: Path.h:111
bool contains(Path &path) const
Definition: Path.h:67
Path append(Path path) const
Definition: system_Path.cpp:172
Path basePart(void) const
Definition: system_Path.cpp:349
bool equals(Path &path) const
Definition: Path.h:66
String extension(void) const
Definition: system_Path.cpp:363
bool operator!=(Path path) const
Definition: Path.h:86
const String & toString(void) const
Definition: Path.h:58
static Path home(void)
Definition: system_Path.cpp:301
Path canonical(void) const
Definition: system_Path.cpp:91
static bool isSeparator(char c)
Definition: Path.h:41
static void setCurrent(Path &path)
Definition: system_Path.cpp:161