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