Doxygen
Loading...
Searching...
No Matches
dir.cpp
Go to the documentation of this file.
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#include <utility>
22
23namespace fs = ghc::filesystem;
24
25
26//-----------------------------------------------------------------------------------------------
27
29{
30 fs::directory_entry entry;
31};
32
33DirEntry::DirEntry() : p(std::make_unique<Private>())
34{
35}
36
37DirEntry::DirEntry(const DirEntry &de) : p(std::make_unique<Private>())
38{
39 p->entry = de.p->entry;
40}
41
43{
44 if (this!=&de)
45 {
46 p = std::make_unique<Private>();
47 p->entry = de.p->entry;
48 }
49 return *this;
50}
51
52DirEntry::DirEntry(DirEntry &&de) : p(std::make_unique<Private>())
53{
54 p = std::move(de.p);
55}
56
58{
59 p = std::move(de.p);
60 return *this;
61}
62
63DirEntry::~DirEntry() = default;
64
66{
67 return p->entry.is_directory();
68}
69
71{
72 return p->entry.is_regular_file();
73}
74
76{
77 return p->entry.is_symlink();
78}
79
80std::string DirEntry::path() const
81{
82 return p->entry.path().string();
83}
84
85//-----------------------------------------------------------------------------------------------
86
88{
89 Private() : it() {}
90 Private(const std::string &path) : it(path,ec) {}
91 fs::directory_iterator it;
92 std::error_code ec;
94};
95
96DirIterator::DirIterator() : p(std::make_unique<Private>())
97{
98}
99
100DirIterator::DirIterator(const std::string &path) : p(std::make_unique<Private>(path))
101{
102}
103
104DirIterator::DirIterator(const DirIterator &it) : p(std::make_unique<Private>())
105{
106 p->it = it.p->it;
107 p->ec = it.p->ec;
108 p->current = it.p->current;
109}
110
112{
113 if (&it!=this)
114 {
115 p->it = it.p->it;
116 p->ec = it.p->ec;
117 p->current = it.p->current;
118 }
119 return *this;
120}
121
122DirIterator::DirIterator(DirIterator &&it) : p(std::make_unique<Private>())
123{
124 std::exchange(p->it,it.p->it);
125 std::exchange(p->ec,it.p->ec);
126 std::exchange(p->current,it.p->current);
127}
128
130{
131 std::exchange(p->it,it.p->it);
132 std::exchange(p->ec,it.p->ec);
133 std::exchange(p->current,it.p->current);
134 return *this;
135}
136
140
142{
143 DirIterator result;
144 result.p->it = ++p->it;
145 return result;
146}
147
149{
150 p->current.p->entry = *p->it;
151 return p->current;
152}
153
155{
156 p->current.p->entry = *p->it;
157 return &p->current;
158}
159
160bool operator==(const DirIterator &it1,const DirIterator &it2)
161{
162 return it1.p->it == it2.p->it;
163}
164
165bool operator!=(const DirIterator &it1,const DirIterator &it2)
166{
167 return it1.p->it!=it2.p->it;
168}
169
171{
172 return it;
173}
174
175DirIterator end(const DirIterator &) noexcept
176{
177 return DirIterator();
178}
179
180
181//-----------------------------------------------------------------------------------------------
182
183
185{
186 fs::path path;
187};
188
189Dir::Dir() : p(std::make_unique<Private>())
190{
191 std::error_code ec;
192 p->path = fs::current_path(ec);
193}
194
195Dir::Dir(const Dir &d) : p(std::make_unique<Private>())
196{
197 p->path = d.p->path;
198}
199
201{
202 if (&d!=this)
203 {
204 p->path = d.p->path;
205 }
206 return *this;
207}
208
209Dir::Dir(Dir &&d) : p(std::make_unique<Private>())
210{
211 std::exchange(p->path,d.p->path);
212}
213
215{
216 std::exchange(p->path,d.p->path);
217 return *this;
218}
219
220Dir::Dir(const std::string &path) : p(std::make_unique<Private>())
221{
222 setPath(path);
223}
224
226{
227}
228
229void Dir::setPath(const std::string &path)
230{
231 p->path = path;
232}
233
234std::string Dir::path() const
235{
236 return p->path.string();
237}
238
240{
241 return DirIterator(p->path.string());
242}
243
244static void correctPath(std::string &s)
245{
246 std::replace( s.begin(), s.end(), '\\', '/' );
247}
248
249bool Dir::exists(const std::string &path,bool acceptsAbsPath) const
250{
251 std::string result = filePath(path,acceptsAbsPath);
252 std::error_code ec;
253 bool exist = fs::exists(fs::path(result),ec);
254 return !ec && exist;
255}
256
257bool Dir::exists() const
258{
259 FileInfo fi(p->path.string());
260 return fi.exists() && fi.isDir();
261}
262
263bool Dir::isEmpty(const std::string subdir) const
264{
265 fs::path pth = path();
266 pth /= subdir;
267 return fs::is_empty(pth);
268}
269
270bool Dir::isRelative() const
271{
272 return isRelativePath(p->path.string());
273}
274
275bool Dir::isRelativePath(const std::string &path)
276{
277 return fs::path(path).is_relative();
278}
279
280std::string Dir::filePath(const std::string &path,bool acceptsAbsPath) const
281{
282 std::string result;
283 if (acceptsAbsPath && !isRelativePath(path))
284 {
285 result = path;
286 }
287 else
288 {
289 result = (p->path / path).string();
290 }
291 correctPath(result);
292 return result;
293}
294
295bool Dir::mkdir(const std::string &path,bool acceptsAbsPath) const
296{
297 std::error_code ec;
298 std::string result = filePath(path,acceptsAbsPath);
299 if (exists(path,acceptsAbsPath))
300 {
301 return true;
302 }
303 else
304 {
305 return fs::create_directory(result,ec);
306 }
307}
308
309bool Dir::rmdir(const std::string &path,bool acceptsAbsPath) const
310{
311 return remove(path,acceptsAbsPath);
312}
313
314bool Dir::remove(const std::string &path,bool acceptsAbsPath) const
315{
316 std::error_code ec;
317 std::string result = filePath(path,acceptsAbsPath);
318 return fs::remove(result,ec);
319}
320
321bool Dir::rename(const std::string &orgName,const std::string &newName,bool acceptsAbsPath) const
322{
323 std::error_code ec;
324 std::string fn1 = filePath(orgName,acceptsAbsPath);
325 std::string fn2 = filePath(newName,acceptsAbsPath);
326 fs::rename(fn1,fn2,ec);
327 return !ec;
328}
329
330bool Dir::copy(const std::string &srcName,const std::string &dstName,bool acceptsAbsPath) const
331{
332 const auto copyOptions = fs::copy_options::overwrite_existing;
333 std::error_code ec;
334 std::string sn = filePath(srcName,acceptsAbsPath);
335 std::string dn = filePath(dstName,acceptsAbsPath);
336 fs::copy(sn,dn,copyOptions,ec);
337 return !ec;
338}
339
341{
342 std::error_code ec;
343 std::string result = fs::current_path(ec).string();
344 correctPath(result);
345 return result;
346}
347
348bool Dir::setCurrent(const std::string &path)
349{
350 std::error_code ec;
351 fs::current_path(path,ec);
352 return !ec;
353}
354
355std::string Dir::cleanDirPath(const std::string &path)
356{
357 std::error_code ec;
358 std::string result = fs::path(path).lexically_normal().string();
359 correctPath(result);
360 return result;
361}
362
363std::string Dir::absPath() const
364{
365 std::error_code ec;
366 std::string result = fs::absolute(p->path,ec).string();
367 correctPath(result);
368 return result;
369}
370
Definition dir.h:25
~DirEntry()
DirEntry(const DirEntry &)
Definition dir.cpp:37
bool is_directory() const
Definition dir.cpp:65
bool is_regular_file() const
Definition dir.cpp:70
std::string path() const
Definition dir.cpp:80
std::unique_ptr< Private > p
Definition dir.h:40
DirEntry & operator=(const DirEntry &)
Definition dir.cpp:42
bool is_symlink() const
Definition dir.cpp:75
DirEntry()
Definition dir.cpp:33
static std::string currentDirPath()
Definition dir.cpp:340
Dir()
Definition dir.cpp:189
bool isEmpty(const std::string subdir) const
Definition dir.cpp:263
Dir & operator=(const Dir &d)
Definition dir.cpp:200
std::string absPath() const
Definition dir.cpp:363
bool mkdir(const std::string &path, bool acceptsAbsPath=true) const
Definition dir.cpp:295
void setPath(const std::string &path)
Definition dir.cpp:229
static bool isRelativePath(const std::string &path)
Definition dir.cpp:275
~Dir()
Definition dir.cpp:225
bool isRelative() const
Definition dir.cpp:270
bool remove(const std::string &path, bool acceptsAbsPath=true) const
Definition dir.cpp:314
bool copy(const std::string &src, const std::string &dest, bool acceptsAbsPath=true) const
Definition dir.cpp:330
std::string filePath(const std::string &path, bool acceptsAbsPath=true) const
Definition dir.cpp:280
DirIterator iterator() const
Definition dir.cpp:239
bool rmdir(const std::string &path, bool acceptsAbsPath=true) const
Definition dir.cpp:309
bool rename(const std::string &orgName, const std::string &newName, bool acceptsAbsPath=true) const
Definition dir.cpp:321
static std::string cleanDirPath(const std::string &path)
Definition dir.cpp:355
static bool setCurrent(const std::string &path)
Definition dir.cpp:348
std::string path() const
Definition dir.cpp:234
bool exists() const
Definition dir.cpp:257
std::unique_ptr< Private > p
Definition dir.h:110
DirIterator()
Definition dir.cpp:96
DirEntry value_type
Definition dir.h:46
DirIterator(const DirIterator &it)
Definition dir.cpp:104
DirIterator operator++()
Definition dir.cpp:141
~DirIterator()
Definition dir.cpp:137
const value_type & operator*() const
Definition dir.cpp:148
const value_type * operator->() const
Definition dir.cpp:154
std::unique_ptr< Private > p
Definition dir.h:70
DirIterator & operator=(const DirIterator &it)
Definition dir.cpp:111
Minimal replacement for QFileInfo.
Definition fileinfo.h:23
bool exists() const
Definition fileinfo.cpp:30
bool isDir() const
Definition fileinfo.cpp:70
static void correctPath(std::string &s)
Definition dir.cpp:244
bool operator==(const DirIterator &it1, const DirIterator &it2)
Definition dir.cpp:160
DirIterator begin(DirIterator it) noexcept
Definition dir.cpp:170
bool operator!=(const DirIterator &it1, const DirIterator &it2)
Definition dir.cpp:165
DirIterator end(const DirIterator &) noexcept
Definition dir.cpp:175
fs::path path
Definition dir.cpp:186
Definition dir.cpp:29
fs::directory_entry entry
Definition dir.cpp:30
Private(const std::string &path)
Definition dir.cpp:90
std::error_code ec
Definition dir.cpp:92
fs::directory_iterator it
Definition dir.cpp:91
DirEntry current
Definition dir.cpp:93