This repository has been archived by the owner on Aug 29, 2023. It is now read-only.
forked from facebookarchive/flint
-
Notifications
You must be signed in to change notification settings - Fork 21
/
Copy pathPolyfill.cpp
209 lines (184 loc) · 4.11 KB
/
Polyfill.cpp
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
#include "Polyfill.hpp"
#include <sys/types.h>
#include <sys/stat.h>
#include <fstream>
#include <sstream>
#include <algorithm>
// Conditional includes for folder traversal
#ifdef _WIN32
#include <windows.h>
#else
#include <dirent.h>
#endif
namespace flint {
/**
* Checks if a given path is a file or directory
*
* @param path
* The path to test
* @return
* Returns a flag representing what the path was
*/
FSType fsObjectExists(const string &path) {
struct stat info;
if (stat(path.c_str(), &info) != 0) {
// Cannot Access
return FSType::NO_ACCESS;
}
else if (info.st_mode & S_IFDIR) {
// Is a Directory
return FSType::IS_DIR;
}
else if (info.st_mode & S_IFREG) {
// Is a File
return FSType::IS_FILE;
}
return FSType::NO_ACCESS;
};
/**
* Checks if a given path contains a .nolint file
*
* @param path
* The path to test
* @return
* Returns a bool of whether a .nolint file was found or not
*/
bool fsContainsNoLint(const string &path) {
string fileName = path + FS_SEP + ".nolint";
return (fsObjectExists(fileName) == FSType::IS_FILE);
};
/**
* Parses a directory and returns a list of it's contents
*
* @param path
* The path to search
* @param dirs
* A vector to fill with objects
* @return
* Returns true if any valid objects were found
*/
bool fsGetDirContents(const string &path, vector<string> &dirs) {
dirs.clear();
#ifdef _WIN32
//
// windows.h Implementation of directory traversal for Windows systems
//
HANDLE dir;
WIN32_FIND_DATA fileData;
if ((dir = FindFirstFile((path + FS_SEP + "*").c_str(), &fileData)) == INVALID_HANDLE_VALUE) {
return false; /* No files found */
}
do {
const string fsObj = fileData.cFileName;
if (FS_ISNOT_LINK(fsObj) && FS_ISNOT_GIT(fsObj)) {
const string fileName = path + FS_SEP + fsObj;
dirs.push_back(move(fileName));
}
} while (FindNextFile(dir, &fileData));
FindClose(dir);
#else
//
// dirent.h Implementation of directory traversal for POSIX systems
//
DIR *pDIR;
struct dirent *entry;
if ((pDIR = opendir(path.c_str()))) {
while ((entry = readdir(pDIR))) {
const string fsObj = entry->d_name;
if (FS_ISNOT_LINK(fsObj) && FS_ISNOT_GIT(fsObj)) {
const string fileName = path + FS_SEP + fsObj;
dirs.push_back(move(fileName));
}
}
closedir(pDIR);
}
stable_sort(dirs.begin(), dirs.end());
#endif
return !dirs.empty();
};
/**
* Attempts to load a file into a std::string
*
* @param path
* The file to load
* @param file
* The string to load into
* @return
* Returns a bool of whether the load was successful
*/
bool getFileContents(const string &path, string &file) {
ifstream in(path);
if (in) {
stringstream buffer;
buffer << in.rdbuf();
file = buffer.str();
return true;
}
return false;
};
/**
* Tests if a given string starts with a prefix
*
* @param str
* The string to search
* @param prefix
* The prefix to search for
* @return
* Returns true if str ends with an instance of prefix
*/
template <class T>
bool startsWith(const T &str, const T &prefix) {
return equal(begin(prefix), end(prefix), begin(str));
};
/**
* Tests if a given string starts with a C-string prefix
*
* @param str_iter
* The string position to start search
* @param prefix
* The prefix (C-string) to search for
* @return
* Returns true if str ends with an instance of prefix
*/
bool startsWith(string::const_iterator str_iter, const char *prefix) {
while (*prefix != '\0' && *prefix == *str_iter) {
++prefix;
++str_iter;
}
return *prefix == '\0';
};
/**
* Escapes a C++ std::string
*
* @param input
* The string to sanitize
* @return
* Returns a string with no escape characters
*/
string escapeString(const string &input) {
string output;
output.reserve(input.length());
for (char c : input) {
switch (c) {
case '\n':
output += "\\n";
break;
case '\t':
output += "\\t";
break;
case '\r':
output += "\\r";
break;
case '\\':
output += "\\\\";
break;
case '"':
output += "\\\"";
break;
default:
output += c;
}
}
return output;
};
};