-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfilesystem.cpp
More file actions
259 lines (220 loc) · 6.21 KB
/
filesystem.cpp
File metadata and controls
259 lines (220 loc) · 6.21 KB
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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
// Copyright (c) 2014 Intel Corporation.
// Author: LI,Binfei (binfeix.li@intel.com)
#include "filesystem.h"
#include <string.h>
#include <iostream>
#include <string>
#ifndef _WIN32
#include <unistd.h>
#include <fcntl.h>
#include <dirent.h>
#else
#include <io.h>
#define access _access
#define PATH_EOF -1
#endif
using namespace std;
//-------------------------------------------------------------------------------------------------
namespace avatar {
namespace utils {
//-------------------------------------------------------------------------------------------------
path::path(
const string &file, const ::std::string parent, bool isfolder)
: file_(file), parent_(parent), isfolder_(isfolder) {
}
//-------------------------------------------------------------------------------------------------
bool path2::exists() const {
return !access(file_.c_str(), 0);
}
//-------------------------------------------------------------------------------------------------
#ifndef _WIN32
class filesystem_entry {
public:
filesystem_entry() : handle_(nullptr), pathInfo_(nullptr) {
}
explicit filesystem_entry(const ::std::string& file) {
handle_ = opendir(file.c_str());
if (handle_ != nullptr) {
//strip "." and ".."
//pathInfo_ = readdir(handle_); // strip "."
//pathInfo_ = readdir(handle_); // strip ".."
pathInfo_ = readdir(handle_);
stripHideFolder();
}
}
filesystem_entry(filesystem_entry& entry) {
if (this != &entry) {
closedir(handle_);
handle_ = entry.handle_;
pathInfo_ = entry.pathInfo_;
}
}
~filesystem_entry() {
release();
}
void release() {
if (handle_ != nullptr)
closedir(handle_);
handle_ = nullptr;
pathInfo_ = nullptr;
}
filesystem_entry& next() {
pathInfo_ = readdir(handle_);
stripHideFolder();
if (pathInfo_ == nullptr) {
release();
}
return *this;
}
bool valid() {
if (handle_ == nullptr)
return false;
return true;
}
path get() {
bool isfolder = (pathInfo_->d_type == DT_DIR);
return path(pathInfo_->d_name, "", isfolder);
}
bool operator==(const filesystem_entry& entry) const {
if (handle_ == entry.handle_) //TODO(binfei):just judgement handle
return true;
return false;
}
bool stripHideFolder() {
while (pathInfo_ != nullptr) {
if (strncmp(pathInfo_->d_name, ".", 1) != 0)
break;
else
pathInfo_ = readdir(handle_);
}
return true;
}
private:
struct dirent* pathInfo_;
DIR* handle_;
};
#else
class filesystem_entry {
public:
filesystem_entry() : handle_(PATH_EOF){
}
explicit filesystem_entry(const ::std::string& file) {
memset(&pathInfo_, 0x0, sizeof(pathInfo_));
//strip "." and ".."
handle_ = _findfirst((file + "/*").c_str(), &pathInfo_);
if (handle_ == PATH_EOF)
return;
_findnext(handle_, &pathInfo_);
if (_findnext(handle_, &pathInfo_)) {
handle_ = PATH_EOF;
memset(&pathInfo_, 0x0, sizeof(pathInfo_));
}
}
filesystem_entry(const filesystem_entry& entry) {
if (this != &entry) {
_findclose(handle_);
handle_ = entry.handle_;
pathInfo_ = entry.pathInfo_;
}
}
~filesystem_entry() {
release();
}
void release() {
_findclose(handle_);
handle_ = PATH_EOF;
}
filesystem_entry& next() {
memset(&pathInfo_, 0x0, sizeof(pathInfo_));
if (_findnext(handle_, &pathInfo_))
release();
return *this;
}
bool valid() {
if (handle_ == PATH_EOF)
return false;
return true;
}
path get() {
bool isfolder = ((pathInfo_.attrib & _A_ARCH)!=_A_ARCH);
return path(pathInfo_.name, "", isfolder);
}
bool operator==(const filesystem_entry& entry) const {
//TODO(binfei):just judgement handle,bug
if (handle_ == entry.handle_)
return true;
return false;
}
private:
struct _finddata_t pathInfo_;
intptr_t handle_;
};
#endif
//-------------------------------------------------------------------------------------------------
directory_iterator::directory_iterator(const ::std::string& file) {
entry_.reset(new filesystem_entry(file));
}
directory_iterator::directory_iterator() {
entry_.reset(new filesystem_entry());
}
directory_iterator::~directory_iterator() {
}
path directory_iterator::operator*() {
return entry_->get();
}
directory_iterator& directory_iterator::operator++() {
entry_->next();
return *this;
}
bool directory_iterator::operator!=(const directory_iterator& other) const {
return !(*other.entry_.get() == *entry_.get());
}
//-------------------------------------------------------------------------------------------------
recursive_directory_iterator::recursive_directory_iterator(const ::std::string& file) {
string folder_parent = file;
string folder_self = file;
while (true) {
shared_ptr<filesystem_entry> entry(new filesystem_entry(folder_self));
entrys_.push_back(make_pair(folder_parent, entry));
path _path = entry->get();
if (!entry->valid() || !_path.isfolder())
break;
folder_self = folder_parent + "/" + _path.name();
folder_parent = folder_self;
}
}
recursive_directory_iterator::recursive_directory_iterator() {
}
recursive_directory_iterator::~recursive_directory_iterator() {
}
path recursive_directory_iterator::operator*() {
shared_ptr<filesystem_entry> entry(entrys_.back().second);
return path(entry->get().name(), entrys_.back().first);
}
recursive_directory_iterator& recursive_directory_iterator::operator++() {
while (true) {
if (entrys_.empty())
break;
shared_ptr<filesystem_entry> entry = entrys_.back().second;
if (!entry->valid()) {
entrys_.pop_back();
break;
}
entry->next();
if (entry->valid()) {
path _path = entry->get();
if (_path.isfolder()) {
recursive_directory_iterator next(entrys_.back().first + "/" + _path.name());
entrys_.insert(entrys_.end(), next.entrys_.begin(), next.entrys_.end());
}
break;
}
entrys_.pop_back();
}
return *this;
}
bool recursive_directory_iterator::operator!=(const recursive_directory_iterator& other) const {
return !(other.entrys_.size() == entrys_.size());//TODO(binfei):just judgement size,bug
}
} //! namespace avatar
} //! namespace utils