forked from philogb/v8-gl
-
Notifications
You must be signed in to change notification settings - Fork 3
/
utils.cpp
66 lines (57 loc) · 1.98 KB
/
utils.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
#include "utils.h"
#include <string>
#include <string.h>
namespace V8GLUtils {
char separator = '/';
char* root_path;
void setRootPath(char* program_path, char* jsfile_path) {
//Set the root_path for opening shader files with
//relative paths
//take path from executable
char* pch = strrchr(program_path, V8GLUtils::separator);
int last_index = pch ? (pch - program_path +1) : 2;
char* tmp_exec_path = new char[last_index +1];
strncpy(tmp_exec_path, pch ? program_path : "./", last_index);
tmp_exec_path[last_index] = '\0';
//take relative path from javascript file
char* p1ch = strrchr(jsfile_path, V8GLUtils::separator);
int last_index1 = p1ch ? (p1ch - jsfile_path +1) : 2;
char* tmp_js_path = new char[last_index1 +1];
strncpy(tmp_js_path, p1ch ? jsfile_path : "./", last_index1);
tmp_js_path[last_index1] = '\0';
V8GLUtils::root_path = new char[last_index + last_index1 +1];
strcpy(V8GLUtils::root_path, tmp_exec_path);
strcat(V8GLUtils::root_path, tmp_js_path);
delete[] tmp_exec_path;
delete[] tmp_js_path;
}
char *pushRootPath(char *new_path) {
char *old_path = root_path;
char *pch = strrchr(new_path, V8GLUtils::separator);
int last_index = pch ? (pch - new_path + 1) : 2;
root_path = new char[last_index + 1];
strncpy(root_path, pch ? new_path : "./", last_index);
root_path[last_index] = '\0';
return old_path;
}
void popRootPath(char *old_path) {
delete[] root_path;
root_path = old_path;
}
char* getRootPath(void) {
return V8GLUtils::root_path;
}
char* getRealPath(char* filepath_str) {
//read the file source
char* filename = NULL;
if(filepath_str[0] != V8GLUtils::separator) {
filename = new char[strlen(V8GLUtils::root_path) + strlen(filepath_str) +1];
strcpy(filename, V8GLUtils::root_path);
strcat(filename, filepath_str);
} else {
filename = new char[strlen(filepath_str) +1];
strcpy(filename, filepath_str);
}
return filename;
}
};