-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfilesystem.h
390 lines (324 loc) · 11.3 KB
/
filesystem.h
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
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
#ifndef JH_PATH_H_
#define JH_PATH_H_
#include<memory>
#include<fstream>
#include<algorithm>
#include<functional>
#include<initializer_list>
#include"3rdparty/path.h"
#include"ezlog.h"
namespace jheaders
{
using Path = TinyPath::path;
inline Path current_path();
inline bool create_directory (const Path& p);
inline bool is_directory (const Path&p);
inline bool is_regular_file (const Path&p);
inline size_t file_size (const Path&p);
inline bool exists (const Path&p);
inline bool copy_file (const std::string &from, const std::string &dst); //successfully copy file or die
inline bool copy_file2dir (const std::string &from, const std::string &dst_dir); //successfully copy file or die
inline std::string get_a_valid_path_or_die (const std::initializer_list<std::string> &paths);
enum class FileType { ALL, FILE, DIR };//type for list in list_d
//for directory do not return . and ..
//return file or dir name in folder p
inline std::vector<std::string> list_dir (const Path& p, FileType type = FileType::FILE);
//return regular file name with extend of suffix ,eg. *.jpg
//use op to check file or dir name
inline std::vector<std::string> list_dir (const Path&p, std::function<bool (const std::string) > op);
//return file name with extension ,extension string eg .jpg .txt .log
//auto convert file extension to lower eg .Jpg -> .jpg
inline std::vector<std::string> list_dir (const Path&p, const std::string &extension);
//return file name with extension in extensions, extension has point
inline std::vector<std::string> list_dir (const Path&p, const std::vector<std::string> &extensions);
inline bool readfile2str (const Path&p, std::string&dst_str); //read file (txt) to a string, string will be cleared before write
inline std::pair<std::shared_ptr<char>, unsigned long long> readfile2mem (const Path&p); //read file and write content to a memory block
/**************** implementtion ****************/
inline Path current_path()
{
return Path::getcwd();
}
inline bool is_directory (const Path&p)
{
return p.is_directory();
}
inline bool is_regular_file (const Path&p)
{
return p.is_file();
}
inline size_t file_size (const Path&p)
{
return p.file_size();
}
inline bool exists (const Path&p)
{
return p.exists();
}
inline bool copy_file (const std::string &from, const std::string &dst)
{
if (!exists (from))
{
EZLOG_ (fatal) << "no such file: " << from;
}
if (exists (dst))
{
EZLOG_ (fatal) << "already exists file: " << dst << "copy file failed.";
}
std::ifstream src_f (from, std::ios::binary);
std::ofstream dst_f (dst, std::ios::binary);
dst_f << src_f.rdbuf();
}
inline bool copy_file2dir (const std::string &from, const std::string &dst_dir)
{
if (!exists (from))
{
EZLOG_ (fatal) << "no such file: " << from;
}
if (!exists (dst_dir) && !create_directory (dst_dir))
{
EZLOG_ (fatal) << "Can not create this dir: " << dst_dir;
}
auto dst_file = Path (dst_dir) / Path (from).filename();
copy_file (from, dst_file.string());
}
inline bool create_directory (const Path& p)
{
#if defined(_WIN32)
return CreateDirectoryW (p.wstr().c_str(), NULL) != 0;
#else
return mkdir (p.str().c_str(), S_IRUSR | S_IWUSR | S_IXUSR) == 0;
#endif
}
//list directory
#if defined(_WIN32)
//for directory do not return . and ..
#include <windows.h>
inline std::vector<std::string> list_dir (const Path& p, FileType type)
{
if (!exists (p))
{
EZLOG (Log_level::ERR) << "no file (dircetoy): " << p.string();
return std::vector<std::string>();
}
WIN32_FIND_DATA ffd;
LARGE_INTEGER filesize;
HANDLE hFind = INVALID_HANDLE_VALUE;
TCHAR szDir[MAX_PATH];
int i = 0;
std::string path_str (p.string());
for (; i < path_str.size(); i++)
{
szDir[i] = TCHAR (path_str[i]);
}
szDir[i++] = (TCHAR) '/';
szDir[i++] = (TCHAR) '*';
szDir[i++] = (TCHAR) 0;
std::vector<std::string> result;
hFind = FindFirstFile (szDir, &ffd);
auto list_type = FILE_ATTRIBUTE_ARCHIVE;
switch (type)
{
case FileType::ALL:
list_type = FILE_ATTRIBUTE_ARCHIVE | FILE_ATTRIBUTE_DIRECTORY;
break;
case FileType::FILE:
list_type = FILE_ATTRIBUTE_ARCHIVE;
break;
case FileType::DIR:
list_type = FILE_ATTRIBUTE_DIRECTORY;
break;
default:
break;
}
do
{
//FILE_ATTRIBUTE_ARCHIVE
//FILE_ATTRIBUTE_DIRECTORY
if (list_type & ffd.dwFileAttributes)
{
std::string str;
for (const auto &x : ffd.cFileName)
{
if (x == 0) { break; }
str += static_cast<char> (x);
}
result.push_back (str);
}
else
{
filesize.LowPart = ffd.nFileSizeLow;
filesize.HighPart = ffd.nFileSizeHigh;
}
}
while (FindNextFile (hFind, &ffd) != 0);
FindClose (hFind);
if (type != FileType::FILE && result.size() >= 2)
{
return std::vector<std::string> (result.begin() + 2, result.end());
}
else
{
return result;
}
}
#else
#include<dirent.h>
//for directory do not return . and ..
inline std::vector<std::string> list_dir (const Path &dir_path, FileType type)
{
std::vector<std::string> files;
if (!exists (dir_path))
{
EZLOG (Log_level::ERR) << "no such file or Path: " << dir_path.string();
return files;
}
DIR *dp = opendir (dir_path.string().c_str());
struct dirent *dirp;
while ( (dirp = readdir (dp)) != NULL)
{
if (std::string (".") == std::string (dirp->d_name) || std::string ("..") == std::string (dirp->d_name))
{
continue;
}
auto full_path = dir_path / Path (std::string (dirp->d_name));
switch (type)
{
case FileType::ALL:
{
files.push_back (dirp->d_name);
}
break;
case FileType::DIR:
{
if (is_directory (full_path))
{
files.push_back (dirp->d_name);
}
}
break;
case FileType::FILE:
{
if (is_regular_file (full_path))
{
files.push_back (dirp->d_name);
}
}
break;
}
}
closedir (dp);
// if (type != FileType::FILE && files.size() >= 2) { return std::vector<std::string> (files.begin() + 2, files.end()); }
return files;
}
#endif
inline std::vector<std::string> list_dir (const Path&p, std::function<bool (const std::string) > op)
{
std::vector<std::string> file_names;
auto all_fles = list_dir (p, FileType::ALL);
for (auto&f : all_fles)
{
if (op (f))
{
file_names.push_back (f);
}
}
return file_names;
}
inline std::vector<std::string> list_dir (const Path&p, const std::vector<std::string> &extensions)
{
std::vector<std::string> file_names;
auto all_fles = list_dir (p, FileType::FILE);
auto to_lower = [] (std::string s)->std::string
{
std::transform (s.begin(), s.end(), s.begin(), [] (unsigned char c) {return std::tolower (c); });
return s;
};
for (auto&f : all_fles)
{
auto lower_extension = to_lower (Path (f).extension());
for (auto &e : extensions)
{
if (e == lower_extension)
{
file_names.push_back (f);
}
}
}
return file_names;
}
inline std::vector<std::string> list_dir (const Path&p, const std::string &extension)
{
return list_dir (p, std::vector<std::string> {extension});
}
inline bool readfile2str (const Path&p, std::string&dst_str)
{
std::ifstream in;
if (!exists (p) || !is_regular_file (p) || ! (in.open (p.string()), in.good()))
{
EZLOG_ (fatal) << "can not open file: " << p.string();
}
std::stringstream buffer;
buffer << in.rdbuf();
dst_str = buffer.str();
if (!in)
{
EZLOG_ (fatal) << "read file failed: " << p.string();
//return{ ptr, unsigned long long (0) };
}
else
{
return true;
}
}
inline std::pair<std::shared_ptr<char>, unsigned long long> readfile2mem (const Path&p)
{
std::ifstream in ;
if (!exists (p) || !is_regular_file (p) || ! (in.open (p.string(), std::ifstream::binary), in.good()))
{
EZLOG_ (fatal) << "can not open file: " << p.string();
}
std::shared_ptr<char> ptr (nullptr, [] (char *data) {delete[] data; });
in.seekg (0, std::ios::end);
unsigned long long file_size = in.tellg();
in.seekg (0, std::ios::beg);
try
{
ptr.reset (new char[file_size]);
}
catch (std::exception &e)
{
EZLOG_ (fatal) << "can not malloc mem: " << file_size << " bytes. msg: " << e.what();
}
in.read (ptr.get(), file_size);
if (!in)
{
EZLOG_ (fatal) << "read file failed: " << p.string();
return{ ptr, (unsigned long long) (0) };
}
else
{
return{ ptr, file_size };
}
}
inline std::ostream& operator<< (std::ostream&out, const std::initializer_list<std::string> &paths)
{
out << std::endl;
for (auto&p : paths)
{
out << p << std::endl;
}
return out;
}
inline std::string get_a_valid_path_or_die (const std::initializer_list<std::string> &paths)
{
for (auto&path : paths)
{
if (exists (path))
{
return path;
}
}
EZLOG_ (fatal) << "no one path in paths is valid: " << paths;
}
}
#endif // !JH_PATH_H_