directory.h
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2006-2008 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 #ifndef CXXTOOLS_DIRECTORY_H
29 #define CXXTOOLS_DIRECTORY_H
30 
31 #include <cxxtools/fileinfo.h>
32 #include <cxxtools/ioerror.h>
33 #include <string>
34 
35 namespace cxxtools {
36 
51  : public std::iterator<std::forward_iterator_tag, std::string>
52 {
53  public:
56 
58  DirectoryIterator(const std::string& path, bool skipHiden = false);
59 
62 
65 
68 
71 
73  bool operator==(const DirectoryIterator& it) const
74  { return _impl == it._impl; }
75 
77  bool operator!=(const DirectoryIterator& it) const
78  { return _impl != it._impl; }
79 
81  const std::string& path() const;
82 
84  const std::string& operator*() const;
85 
86  const std::string* operator->() const;
87 
88  private:
90  class DirectoryIteratorImpl* _impl;
91 };
92 
108 {
109  public:
111 
112  public:
114  Directory();
115 
121  explicit Directory(const std::string& path);
122 
128  explicit Directory(const FileInfo& fi);
129 
131  Directory(const Directory& dir);
132 
134  ~Directory();
135 
137  Directory& operator=(const Directory& dir);
138 
144  const std::string& path() const
145  { return _path; }
146 
148  std::size_t size() const;
149 
158  std::string dirName() const;
159 
161  std::string name() const;
162 
167  void remove();
168 
174  void move(const std::string& to);
175 
177  const_iterator begin(bool skipHiden = false) const;
178 
180  const_iterator end() const;
181 
182  public:
184  static Directory create(const std::string& path);
185 
187  static bool exists(const std::string& path);
188 
190  static void chdir(const std::string& path);
191 
193  static std::string cwd();
194 
196  static std::string curdir();
197 
199  static std::string updir();
200 
205  static std::string rootdir();
206 
214  static std::string tmpdir();
215 
217  static std::string sep();
218 
219  private:
221  std::string _path;
222 };
223 
224 inline bool operator<(const Directory& a, const Directory& b)
225 {
226  return a.path() < b.path();
227 }
228 
229 inline bool operator==(const Directory& a, const Directory& b)
230 {
231  return a.path() == b.path();
232 }
233 
234 inline bool operator!=(const Directory& a, const Directory& b)
235 {
236  return !(a == b);
237 }
238 
239 }
240 
241 #endif