-
-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathfs.cpp
338 lines (275 loc) · 9.95 KB
/
fs.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
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
#include "vera/ops/fs.h"
#include "vera/ops/string.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <iostream> // cout
#include <fstream> // File
#include <iterator> // std::back_inserter
#include <algorithm> // std::unique
#include <sys/stat.h>
#ifdef _WIN32
#include <errno.h>
#define WIN32_LEAN_AND_MEAN 1
#include <windows.h>
#else
#include "glob.h"
#endif
namespace vera {
bool haveExt(const std::string& _file, const std::string& _ext){
return _file.find( "." + _ext) != std::string::npos;
}
bool urlExists(const std::string& _name) {
struct stat buffer;
return (stat (_name.c_str(), &buffer) == 0);
}
std::string getExt(const std::string& _filename) {
if (_filename.find_last_of(".") != std::string::npos)
return _filename.substr(_filename.find_last_of(".") + 1);
return "";
}
std::string getBaseDir(const std::string& filepath) {
std::string base_dir = "";
if (filepath.find_last_of("/\\") != std::string::npos)
base_dir = filepath.substr(0, filepath.find_last_of("/\\"));
else
base_dir = ".";
#ifdef _WIN32
base_dir += "\\";
#else
base_dir += "/";
#endif
return base_dir;
}
#if defined (_WIN32)
const char* realpath(const char* str, void*)
{
return str;
}
#endif
std::string getAbsPath(const std::string& _path) {
std::string abs_path = realpath(_path.c_str(), NULL);
std::size_t found = abs_path.find_last_of("\\/");
if (found) return abs_path.substr(0, found);
else return "";
}
std::string urlResolve(const std::string& _path, const StringList &_include_folders) {
// .. search on the include path
for ( uint32_t i = 0; i < _include_folders.size(); i++) {
std::string new_path = _include_folders[i] + "/" + _path;
if (urlExists(new_path))
return realpath(new_path.c_str(), NULL);
}
return _path;
}
std::string urlResolve(const std::string& _path, const std::string& _pwd, const StringList &_include_folders) {
std::string url = _pwd +'/'+ _path;
// If the path is not in the same directory
if (urlExists(url))
return realpath(url.c_str(), NULL);
// .. search on the include path
else
return urlResolve(_path, _include_folders);
}
bool extractDependency(const std::string &_line, std::string *_dependency) {
// Search for ocurences of #include or #pargma include (OF)
if (_line.find("#include ") == 0 || _line.find("#pragma include ") == 0) {
unsigned begin = _line.find_first_of("\"");
unsigned end = _line.find_last_of("\"");
if ((end - begin) > 4) {
std::string sub = _line.substr(begin+1, end-begin-1);
std::vector<std::string> subs = split(sub, '.');
if (subs.size() > 1)
if (subs[subs.size() - 1] == "glsl" || subs[subs.size() - 1] == "GLSL") {
(*_dependency) = sub;
return true;
}
}
}
return false;
}
bool alreadyInclude(const std::string &_path, StringList *_dependencies) {
for (unsigned int i = 0; i < _dependencies->size(); i++) {
if ( _path == (*_dependencies)[i]) {
return true;
}
}
return false;
}
std::string loadGlslFrom(const std::string& _path) {
const std::vector<std::string> folders;
StringList deps;
return loadGlslFrom(_path, folders, &deps);
}
std::string loadGlslFrom(const std::string &_path, const StringList& _include_folders, StringList *_dependencies) {
std::string str = "";
loadGlslFrom(_path, &str, _include_folders, _dependencies);
return str;
}
bool loadGlslFrom(const std::string &_path, std::string *_into) {
const std::vector<std::string> folders;
StringList deps;
_into->clear();
return loadGlslFrom(_path, _into, folders, &deps);
}
bool loadGlslFrom(const std::string &_path, std::string *_into, const std::vector<std::string> &_include_folders, StringList *_dependencies) {
std::ifstream file;
file.open(_path.c_str());
// Skip if it's already open
if (!file.is_open())
return false;
// Get absolute home folder
std::string original_path = getAbsPath(_path);
// Get absolute home folder
std::string line;
std::string dependency;
std::string newBuffer;
while (!file.eof()) {
dependency = "";
getline(file, line);
if (extractDependency(line, &dependency)) {
dependency = urlResolve(dependency, original_path, _include_folders);
newBuffer = "";
if (loadGlslFrom(dependency, &newBuffer, _include_folders, _dependencies)) {
if (!alreadyInclude(dependency, _dependencies)) {
// Insert the content of the dependency
(*_into) += "\n" + newBuffer + "\n";
// Add dependency to dependency list
_dependencies->push_back(dependency);
}
}
else
std::cerr << "Error: " << dependency << " not found at " << original_path << std::endl;
}
else
(*_into) += line + "\n";
}
file.close();
return true;
}
std::string resolveGlsl(const std::string& _src, const StringList& _include_folders, StringList *_dependencies) {
std::string str = "";
resolveGlsl(_src, &str, _include_folders, _dependencies);
return str;
}
bool resolveGlsl(const std::string& _src, std::string *_into, const StringList& _include_folders, StringList *_dependencies) {
std::vector<std::string> lines = split(_src, '\n');
std::string dependency = "";
std::string newBuffer;
for (size_t i = 0; i < lines.size(); i++) {
dependency = "";
if (extractDependency(lines[i], &dependency)) {
dependency = urlResolve(dependency, _include_folders);
newBuffer = "";
if (loadGlslFrom(dependency, &newBuffer, _include_folders, _dependencies)) {
if (!alreadyInclude(dependency, _dependencies)) {
// Insert the content of the dependency
(*_into) += "\n" + newBuffer + "\n";
// Add dependency to dependency list
_dependencies->push_back(dependency);
}
}
else
std::cerr << "Error: " << dependency << " not found." << std::endl;
}
else
(*_into) += lines[i] + "\n";
}
return true;
}
std::vector<std::string> glob(const std::string& _pattern) {
std::vector<std::string> files;
#if defined(_WIN32)
int err = 0;
WIN32_FIND_DATAA finddata;
HANDLE hfindfile = FindFirstFileA(_pattern.c_str(), &finddata);
if (hfindfile != INVALID_HANDLE_VALUE) {
do {
files.push_back(std::string(finddata.cFileName));
} while (FindNextFileA(hfindfile, &finddata));
FindClose(hfindfile);
}
#else
std::vector<std::string> folders = split(_pattern, '/', true);
std::string folder = "";
for (size_t i = 0; i < folders.size() - 1; i++)
folder += folders[i] + "/";
glob::glob glob(_pattern.c_str());
while (glob) {
files.push_back( folder + glob.current_match() );
glob.next();
}
#endif
return files;
}
static const std::string base64_chars =
"ABCDEFGHIJKLMNOPQRSTUVWXYZ"
"abcdefghijklmnopqrstuvwxyz"
"0123456789+/";
bool is_base64(unsigned char c) { return (isalnum(c) || (c == '+') || (c == '/')); }
std::string encodeBase64(const unsigned char* _src, size_t _size) {
std::string ret;
int i = 0;
int j = 0;
unsigned char char_array_3[3];
unsigned char char_array_4[4];
while (_size--) {
char_array_3[i++] = *(_src++);
if (i == 3) {
char_array_4[0] = (char_array_3[0] & 0xfc) >> 2;
char_array_4[1] = ((char_array_3[0] & 0x03) << 4) + ((char_array_3[1] & 0xf0) >> 4);
char_array_4[2] = ((char_array_3[1] & 0x0f) << 2) + ((char_array_3[2] & 0xc0) >> 6);
char_array_4[3] = char_array_3[2] & 0x3f;
for(i = 0; (i <4) ; i++)
ret += base64_chars[char_array_4[i]];
i = 0;
}
}
if (i) {
for(j = i; j < 3; j++)
char_array_3[j] = '\0';
char_array_4[0] = (char_array_3[0] & 0xfc) >> 2;
char_array_4[1] = ((char_array_3[0] & 0x03) << 4) + ((char_array_3[1] & 0xf0) >> 4);
char_array_4[2] = ((char_array_3[1] & 0x0f) << 2) + ((char_array_3[2] & 0xc0) >> 6);
char_array_4[3] = char_array_3[2] & 0x3f;
for (j = 0; (j < i + 1); j++)
ret += base64_chars[char_array_4[j]];
while((i++ < 3))
ret += '=';
}
return ret;
}
size_t decodeBase64(const std::string& _src, unsigned char *_to) {
size_t in_len = _src.size();
int i = 0;
int j = 0;
int in_ = 0;
unsigned char char_array_4[4], char_array_3[3];
unsigned char *p = _to;
while (in_len-- && ( _src[in_] != '=') && is_base64(_src[in_])) {
char_array_4[i++] = _src[in_]; in_++;
if (i ==4) {
for (i = 0; i <4; i++)
char_array_4[i] = base64_chars.find(char_array_4[i]);
char_array_3[0] = (char_array_4[0] << 2) + ((char_array_4[1] & 0x30) >> 4);
char_array_3[1] = ((char_array_4[1] & 0xf) << 4) + ((char_array_4[2] & 0x3c) >> 2);
char_array_3[2] = ((char_array_4[2] & 0x3) << 6) + char_array_4[3];
for (i = 0; (i < 3); i++)
*p++ = char_array_3[i] ;
i = 0;
}
}
if (i) {
for (j = i; j <4; j++)
char_array_4[j] = 0;
for (j = 0; j <4; j++)
char_array_4[j] = base64_chars.find(char_array_4[j]);
char_array_3[0] = (char_array_4[0] << 2) + ((char_array_4[1] & 0x30) >> 4);
char_array_3[1] = ((char_array_4[1] & 0xf) << 4) + ((char_array_4[2] & 0x3c) >> 2);
char_array_3[2] = ((char_array_4[2] & 0x3) << 6) + char_array_4[3];
for (j = 0; (j < i - 1); j++)
*p++ = char_array_3[j] ;
}
return (p - _to);
}
}