-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.h
143 lines (131 loc) · 3.6 KB
/
utils.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
#pragma once
#include <sstream>
#include <sys/stat.h>
#include <vector>
#include <sys/types.h>
#ifdef _WIN32
#include <direct.h>
#include <stdio.h>
#include <io.h>
#include <windows.h>
#endif
#if defined(__linux__) || defined(__MINGW32__) || defined(__APPLE__)
#include <dirent.h>
#include <unistd.h>
#include <cstring>
#endif
namespace utils{
/**
* Check whether directory exists
* @param path directory to be checked.
* @return true if directory exists, false otherwise.
*/
static inline bool dirExists(std::string path){
struct stat st;
int ret = stat(path.c_str(), &st);
return ret == 0 && st.st_mode & S_IFDIR;
}
/**
* list all filename in a directory
* @param path directory path.
* @param ret all files name in directory.
* @return files number.
*/
#if defined(_WIN32) && !defined(__MINGW32__)
static inline int scanDir(std::string path, std::vector<std::string> &ret){
std::string extendPath;
if(path[path.size() - 1] == '/'){
extendPath = path + "*";
}
else{
extendPath = path + "/*";
}
WIN32_FIND_DATAA fd;
HANDLE h = FindFirstFileA(extendPath.c_str(), &fd);
if(h == INVALID_HANDLE_VALUE){
return 0;
}
while(true){
std::string ss(fd.cFileName);
if(ss[0] != '.'){
ret.push_back(ss);
}
if(FindNextFile(h, &fd) ==false){
break;
}
}
FindClose(h);
return ret.size();
}
#endif
#if defined(__linux__) || defined(__MINGW32__) || defined(__APPLE__)
static inline int scanDir(std::string path, std::vector<std::string> &ret){
DIR *dir;
struct dirent *rent;
dir = opendir(path.c_str());
char s[100];
while((rent = readdir(dir))){
strcpy(s,rent->d_name);
if (s[0] != '.'){
ret.push_back(s);
}
}
closedir(dir);
return ret.size();
}
#endif
/**
* Create directory
* @param path directory to be created.
* @return 0 if directory is created successfully, -1 otherwise.
*/
static inline int _mkdir(const char *path){
#ifdef _WIN32
return ::_mkdir(path);
#else
return ::mkdir(path, 0775);
#endif
}
/**
* Create directory recursively
* @param path directory to be created.
* @return 0 if directory is created successfully, -1 otherwise.
*/
static inline int mkdir(const char *path){
std::string currentPath = "";
std::string dirName;
std::stringstream ss(path);
while (std::getline(ss, dirName, '/')){
currentPath += dirName;
if (!dirExists(currentPath) && _mkdir(currentPath.c_str()) != 0){
return -1;
}
currentPath += "/";
}
return 0;
}
/**
* Delete a empty directory
* @param path directory to be deleted.
* @return 0 if delete successfully, -1 otherwise.
*/
static inline int rmdir(const char *path){
#ifdef _WIN32
return ::_rmdir(path);
#else
return ::rmdir(path);
#endif
}
/**
* Delete a file
* @param path file to be deleted.
* @return 0 if delete successfully, -1 otherwise.
*/
static inline int rmfile(const char *path){
#ifdef _WIN32
return ::_unlink(path);
#else
return ::unlink(path);
#endif
}
}