summaryrefslogtreecommitdiffstats
path: root/src/dir.h
blob: 0a1cc12c3de91b4d703e5bd55ebd381b05d4bd05 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
/******************************************************************************
 *
 * Copyright (C) 1997-2021 by Dimitri van Heesch.
 *
 * Permission to use, copy, modify, and distribute this software and its
 * documentation under the terms of the GNU General Public License is hereby
 * granted. No representations are made about the suitability of this software
 * for any purpose. It is provided "as is" without express or implied warranty.
 * See the GNU General Public License for more details.
 *
 * Documents produced by Doxygen are derivative works derived from the
 * input used in their production; they are not affected by this license.
 *
 */

#ifndef DIR_H
#define DIR_H

#include <string>
#include <memory>

#include "fileinfo.h"

class DirEntry
{
  public:
   ~DirEntry();
    bool is_directory() const;
    bool is_regular_file() const;
    bool is_symlink() const;
    std::string path() const;
  private:
    friend class DirIterator;
    DirEntry();
    struct Private;
    std::unique_ptr<Private> p;
};

class DirIterator
{
  public:
    using value_type = DirEntry;
    using difference_type = std::ptrdiff_t;
    using pointer = value_type*;
    using reference = value_type&;
    using iterator_category = std::input_iterator_tag;
    DirIterator(const DirIterator &it);
    DirIterator &operator=(const DirIterator &it);
    DirIterator operator++();
    const value_type &operator*() const;
    const value_type *operator->() const;

    friend bool operator==(const DirIterator &it1,const DirIterator &it2);
    friend bool operator!=(const DirIterator &it1,const DirIterator &it2);
    friend DirIterator begin(DirIterator it) noexcept;
    friend DirIterator end(const DirIterator &) noexcept;
   ~DirIterator();

  private:
    friend class Dir;
    DirIterator();
    DirIterator(const std::string &path);
    struct Private;
    std::unique_ptr<Private> p;
};

/** Class representing a directory in the file system */
class Dir final
{
  public:
    Dir();
    Dir(const std::string &path);
    Dir(const Dir &d);
    Dir &operator=(const Dir &d);
   ~Dir();
    void setPath(const std::string &path);
    std::string path() const;

    DirIterator iterator() const;

    bool exists() const;
    std::string filePath(const std::string &path,bool acceptsAbsPath=true) const;
    bool exists(const std::string &path,bool acceptsAbsPath=true) const;
    bool mkdir(const std::string &path,bool acceptsAbsPath=true) const;
    bool rmdir(const std::string &path,bool acceptsAbsPath=true) const;
    bool remove(const std::string &path,bool acceptsAbsPath=true) const;
    bool rename(const std::string &orgName,const std::string &newName,
                bool acceptsAbsPath=true) const;
    bool copy(const std::string &src,const std::string &dest,bool acceptsAbsPath=true) const;
    std::string absPath() const;

    bool isRelative() const;

    static bool isRelativePath(const std::string &path);
    static std::string currentDirPath();
    static bool setCurrent(const std::string &path);
    static std::string cleanDirPath(const std::string &path);

  private:
    struct Private;
    std::unique_ptr<Private> p;
};

#endif