-
Notifications
You must be signed in to change notification settings - Fork 2
/
fake_filesystem.hpp
129 lines (98 loc) · 2.88 KB
/
fake_filesystem.hpp
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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
//
// fake_filesystem.hpp
// bugmatic
//
// Created by Uli Kusterer on 19/11/2016.
// Copyright © 2016 Uli Kusterer. All rights reserved.
//
/*
Until C++ 17's filesystem class ships, provide a minimal,
source-code-compatible version of the parts of the API
we need.
*/
#ifndef fake_filesystem_hpp
#define fake_filesystem_hpp
#include <string>
#include <dirent.h>
namespace fake
{
namespace filesystem
{
class path
{
public:
path() = default;
path( const path& ) = default;
path( path&& ) = default;
path( const char* inStr ) : mPath(inStr) {}
path( const std::string inStr ) : mPath(inStr) {}
path& operator = ( const path& ) = default;
bool operator == ( const path& inOther ) const { return mPath.compare( inOther.mPath ) == 0; }
bool operator != ( const path& inOther ) const { return mPath.compare( inOther.mPath ) != 0; }
path& operator /= ( const path& );
path operator / ( const path& ) const;
path filename() const;
path parent_path() const;
std::string string() const { return mPath; }
operator const std::string() const { return mPath; }
protected:
std::string mPath;
};
bool exists( const path& inPath );
std::ostream& operator << ( std::ostream& inOutputStream, const path& inPath );
class directory_entry
{
public:
directory_entry() = default;
directory_entry( const directory_entry& ) = default;
directory_entry( directory_entry&& ) = default;
explicit directory_entry( const path& inPath ) : mPath(inPath) {}
directory_entry& operator = ( const directory_entry& ) = default;
bool operator == ( const directory_entry& inOther ) const
{
return mPath == inOther.mPath;
}
bool operator != ( const directory_entry& inOther ) const
{
return mPath != inOther.mPath;
}
path path() const { return mPath; }
protected:
fake::filesystem::path mPath;
friend class directory_iterator;
};
class directory_iterator
{
protected:
class dir
{
public:
explicit dir( DIR* inDir ) : mDir(inDir) {}
dir* acquire() { ++mRefCount; return this; }
void release() { if( --mRefCount == 0 ) delete this; }
DIR* get_dir() const { return mDir; }
protected:
~dir() {}
DIR* mDir;
size_t mRefCount = 1;
};
public:
directory_iterator() = default;
directory_iterator( path inPath );
directory_iterator( const directory_iterator& inOriginal );
~directory_iterator();
directory_entry operator *() { return mEntry; }
directory_iterator operator ++ ();
directory_iterator& operator =( const directory_iterator& inOriginal );
bool operator == ( const directory_iterator& inOther ) const;
bool operator != ( const directory_iterator& inOther ) const;
protected:
directory_entry mEntry;
fake::filesystem::path mPath;
dir* mDir = nullptr;
};
bool create_directory( const path& p );
bool create_directories( const path& p );
}
}
#endif /* fake_filesystem_hpp */