Doxygen
dir.cpp
浏览该文件的文档.
1 /******************************************************************************
2  *
3  * Copyright (C) 1997-2021 by Dimitri van Heesch.
4  *
5  * Permission to use, copy, modify, and distribute this software and its
6  * documentation under the terms of the GNU General Public License is hereby
7  * granted. No representations are made about the suitability of this software
8  * for any purpose. It is provided "as is" without express or implied warranty.
9  * See the GNU General Public License for more details.
10  *
11  * Documents produced by Doxygen are derivative works derived from the
12  * input used in their production; they are not affected by this license.
13  *
14  */
15 
16 #define NOMINMAX
17 #define WIN32_LEAN_AND_MEAN
18 #include "filesystem.hpp"
19 #include "dir.h"
20 
21 namespace fs = ghc::filesystem;
22 
23 //-----------------------------------------------------------------------------------------------
24 
26 {
27  fs::directory_entry entry;
28 };
29 
30 DirEntry::DirEntry() : p(std::make_unique<Private>())
31 {
32 }
33 
35 {
36 }
37 
39 {
40  return p->entry.is_directory();
41 }
42 
44 {
45  return p->entry.is_regular_file();
46 }
47 
49 {
50  return p->entry.is_symlink();
51 }
52 
53 std::string DirEntry::path() const
54 {
55  return p->entry.path().string();
56 }
57 
58 //-----------------------------------------------------------------------------------------------
59 
61 {
62  Private() : it() {}
63  Private(const std::string &path) : it(path) {}
64  fs::directory_iterator it;
65  mutable DirEntry current;
66 };
67 
68 DirIterator::DirIterator() : p(std::make_unique<Private>())
69 {
70 }
71 
72 DirIterator::DirIterator(const std::string &path) : p(std::make_unique<Private>(path))
73 {
74 }
75 
76 DirIterator::DirIterator(const DirIterator &it) : p(std::make_unique<Private>())
77 {
78  p->it = it.p->it;
79 }
80 
82 {
83 }
84 
86 {
87  if (&it!=this)
88  {
89  p->it = it.p->it;
90  }
91  return *this;
92 }
93 
95 {
96  DirIterator result;
97  result.p->it = ++p->it;
98  return result;
99 }
100 
102 {
103  p->current.p->entry = *p->it;
104  return p->current;
105 }
106 
108 {
109  p->current.p->entry = *p->it;
110  return &p->current;
111 }
112 
113 bool operator==(const DirIterator &it1,const DirIterator &it2)
114 {
115  return it1.p->it == it2.p->it;
116 }
117 
118 bool operator!=(const DirIterator &it1,const DirIterator &it2)
119 {
120  return it1.p->it!=it2.p->it;
121 }
122 
124 {
125  return it;
126 }
127 
128 DirIterator end(const DirIterator &) noexcept
129 {
130  return DirIterator();
131 }
132 
133 
134 //-----------------------------------------------------------------------------------------------
135 
136 
138 {
139  fs::path path;
140 };
141 
142 Dir::Dir() : p(std::make_unique<Private>())
143 {
144  std::error_code ec;
145  p->path = fs::current_path(ec);
146 }
147 
148 Dir::Dir(const Dir &d) : p(std::make_unique<Private>())
149 {
150  p->path = d.p->path;
151 }
152 
154 {
155  if (&d!=this)
156  {
157  p->path = d.p->path;
158  }
159  return *this;
160 }
161 
162 Dir::Dir(const std::string &path) : p(std::make_unique<Private>())
163 {
164  setPath(path);
165 }
166 
168 {
169 }
170 
171 void Dir::setPath(const std::string &path)
172 {
173  p->path = path;
174 }
175 
176 std::string Dir::path() const
177 {
178  return p->path.string();
179 }
180 
182 {
183  return DirIterator(p->path.string());
184 }
185 
186 static void correctPath(std::string &s)
187 {
188  std::replace( s.begin(), s.end(), '\\', '/' );
189 }
190 
191 bool Dir::exists(const std::string &path,bool acceptsAbsPath) const
192 {
193  std::string result = filePath(path,acceptsAbsPath);
194  std::error_code ec;
195  bool exist = fs::exists(fs::path(result),ec);
196  return !ec && exist;
197 }
198 
199 bool Dir::exists() const
200 {
201  FileInfo fi(p->path.string());
202  return fi.exists() && fi.isDir();
203 }
204 
205 bool Dir::isEmpty(const std::string subdir) const
206 {
207  fs::path pth = path();
208  pth /= subdir;
209  return fs::is_empty(pth);
210 }
211 
212 bool Dir::isRelative() const
213 {
214  return isRelativePath(p->path.string());
215 }
216 
217 bool Dir::isRelativePath(const std::string &path)
218 {
219  return fs::path(path).is_relative();
220 }
221 
222 std::string Dir::filePath(const std::string &path,bool acceptsAbsPath) const
223 {
224  std::string result;
225  if (acceptsAbsPath && !isRelativePath(path))
226  {
227  result = path;
228  }
229  else
230  {
231  result = (p->path / path).string();
232  }
233  correctPath(result);
234  return result;
235 }
236 
237 bool Dir::mkdir(const std::string &path,bool acceptsAbsPath) const
238 {
239  std::error_code ec;
240  std::string result = filePath(path,acceptsAbsPath);
241  if (exists(path,acceptsAbsPath))
242  {
243  return true;
244  }
245  else
246  {
247  return fs::create_directory(result,ec);
248  }
249 }
250 
251 bool Dir::rmdir(const std::string &path,bool acceptsAbsPath) const
252 {
253  return remove(path,acceptsAbsPath);
254 }
255 
256 bool Dir::remove(const std::string &path,bool acceptsAbsPath) const
257 {
258  std::error_code ec;
259  std::string result = filePath(path,acceptsAbsPath);
260  return fs::remove(result,ec);
261 }
262 
263 bool Dir::rename(const std::string &orgName,const std::string &newName,bool acceptsAbsPath) const
264 {
265  std::error_code ec;
266  std::string fn1 = filePath(orgName,acceptsAbsPath);
267  std::string fn2 = filePath(newName,acceptsAbsPath);
268  fs::rename(fn1,fn2,ec);
269  return !ec;
270 }
271 
272 bool Dir::copy(const std::string &srcName,const std::string &dstName,bool acceptsAbsPath) const
273 {
274  const auto copyOptions = fs::copy_options::overwrite_existing;
275  std::error_code ec;
276  std::string sn = filePath(srcName,acceptsAbsPath);
277  std::string dn = filePath(dstName,acceptsAbsPath);
278  fs::copy(sn,dn,copyOptions,ec);
279  return !ec;
280 }
281 
282 std::string Dir::currentDirPath()
283 {
284  std::error_code ec;
285  std::string result = fs::current_path(ec).string();
286  correctPath(result);
287  return result;
288 }
289 
290 bool Dir::setCurrent(const std::string &path)
291 {
292  std::error_code ec;
293  fs::current_path(path,ec);
294  return !ec;
295 }
296 
297 std::string Dir::cleanDirPath(const std::string &path)
298 {
299  std::error_code ec;
300  std::string result = fs::path(path).lexically_normal().string();
301  correctPath(result);
302  return result;
303 }
304 
305 std::string Dir::absPath() const
306 {
307  std::error_code ec;
308  std::string result = fs::absolute(p->path,ec).string();
309  correctPath(result);
310  return result;
311 }
312 
Dir::currentDirPath
static std::string currentDirPath()
Definition: dir.cpp:282
DirIterator::p
std::unique_ptr< Private > p
Definition: dir.h:63
Dir::copy
bool copy(const std::string &src, const std::string &dest, bool acceptsAbsPath=true) const
Definition: dir.cpp:272
Dir::Private
Definition: dir.cpp:137
reg::replace
std::string replace(const std::string &str, const Ex &re, const std::string &replacement)
Searching in a given input string for parts that match regular expression re and replaces those parts...
Definition: regex.cpp:740
DirIterator::Private
Definition: dir.cpp:60
Dir::filePath
std::string filePath(const std::string &path, bool acceptsAbsPath=true) const
Definition: dir.cpp:222
Dir::remove
bool remove(const std::string &path, bool acceptsAbsPath=true) const
Definition: dir.cpp:256
Dir
Class representing a directory in the file system
Definition: dir.h:68
DirIterator::Private::current
DirEntry current
Definition: dir.cpp:65
DirEntry::is_regular_file
bool is_regular_file() const
Definition: dir.cpp:43
DirEntry::p
std::unique_ptr< Private > p
Definition: dir.h:48
DirIterator::~DirIterator
~DirIterator()
Definition: dir.cpp:81
DirEntry
Definition: dir.h:24
DirEntry::Private
Definition: dir.cpp:25
DirEntry::path
std::string path() const
Definition: dir.cpp:53
DirEntry::DirEntry
DirEntry()
Definition: dir.cpp:30
DirIterator::operator->
const value_type * operator->() const
Definition: dir.cpp:107
DirIterator::operator++
DirIterator operator++()
Definition: dir.cpp:94
begin
DirIterator begin(DirIterator it) noexcept
Definition: dir.cpp:123
end
DirIterator end(const DirIterator &) noexcept
Definition: dir.cpp:128
Dir::isRelativePath
static bool isRelativePath(const std::string &path)
Definition: dir.cpp:217
operator!=
bool operator!=(const DirIterator &it1, const DirIterator &it2)
Definition: dir.cpp:118
Dir::rename
bool rename(const std::string &orgName, const std::string &newName, bool acceptsAbsPath=true) const
Definition: dir.cpp:263
FileInfo::exists
bool exists() const
Definition: fileinfo.cpp:30
Dir::path
std::string path() const
Definition: dir.cpp:176
Dir::~Dir
~Dir()
Definition: dir.cpp:167
DirEntry::Private::entry
fs::directory_entry entry
Definition: dir.cpp:27
Dir::absPath
std::string absPath() const
Definition: dir.cpp:305
DirIterator::operator*
const value_type & operator*() const
Definition: dir.cpp:101
DirEntry::~DirEntry
~DirEntry()
Definition: dir.cpp:34
Dir::Dir
Dir()
Definition: dir.cpp:142
Dir::setCurrent
static bool setCurrent(const std::string &path)
Definition: dir.cpp:290
DirIterator::operator=
DirIterator & operator=(const DirIterator &it)
Definition: dir.cpp:85
DirEntry::is_directory
bool is_directory() const
Definition: dir.cpp:38
FileInfo::isDir
bool isDir() const
Definition: fileinfo.cpp:70
DirIterator
Definition: dir.h:39
operator==
bool operator==(const DirIterator &it1, const DirIterator &it2)
Definition: dir.cpp:113
Dir::isEmpty
bool isEmpty(const std::string subdir) const
Definition: dir.cpp:205
DirIterator::Private::Private
Private()
Definition: dir.cpp:62
Dir::operator=
Dir & operator=(const Dir &d)
Definition: dir.cpp:153
correctPath
static void correctPath(std::string &s)
Definition: dir.cpp:186
DirEntry::is_symlink
bool is_symlink() const
Definition: dir.cpp:48
DirIterator::Private::it
fs::directory_iterator it
Definition: dir.cpp:64
FileInfo
Minimal replacement for QFileInfo.
Definition: fileinfo.h:22
Dir::Private::path
fs::path path
Definition: dir.cpp:139
Dir::isRelative
bool isRelative() const
Definition: dir.cpp:212
Dir::setPath
void setPath(const std::string &path)
Definition: dir.cpp:171
Dir::exists
bool exists() const
Definition: dir.cpp:199
Dir::iterator
DirIterator iterator() const
Definition: dir.cpp:181
Dir::rmdir
bool rmdir(const std::string &path, bool acceptsAbsPath=true) const
Definition: dir.cpp:251
DirIterator::DirIterator
DirIterator()
Definition: dir.cpp:68
dir.h
Dir::mkdir
bool mkdir(const std::string &path, bool acceptsAbsPath=true) const
Definition: dir.cpp:237
Dir::cleanDirPath
static std::string cleanDirPath(const std::string &path)
Definition: dir.cpp:297
Dir::p
std::unique_ptr< Private > p
Definition: dir.h:101
DirIterator::Private::Private
Private(const std::string &path)
Definition: dir.cpp:63