-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathutil.cpp
More file actions
129 lines (92 loc) · 1.88 KB
/
Copy pathutil.cpp
File metadata and controls
129 lines (92 loc) · 1.88 KB
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
// string util
#include "util.h"
#include <ctype.h>
#include <stdio.h>
#include <string.h>
#include <stdarg.h>
#ifdef _WIN32
#include <direct.h>
#define CORRECT_PATH_SEPARATOR '\\'
#define INCORRECT_PATH_SEPARATOR '/'
#else // POSIX
#define CORRECT_PATH_SEPARATOR '/'
#define INCORRECT_PATH_SEPARATOR '\\'
#include <sys/stat.h>
#include <errno.h>
#endif
void FixPathSlashes(char* pathbuff)
{
while (*pathbuff)
{
if (*pathbuff == INCORRECT_PATH_SEPARATOR) // make unix-style path
*pathbuff = CORRECT_PATH_SEPARATOR;
pathbuff++;
}
}
bool mkdirRecursive(const char* path, bool includeDotPath)
{
char temp[1024];
char folder[265];
char *end, *curend;
strcpy(temp, path);
FixPathSlashes(temp);
end = temp;
do
{
int result;
// skip any separators in the beginning
while (*end == CORRECT_PATH_SEPARATOR)
end++;
// get next string part
curend = (char*)strchr(end, CORRECT_PATH_SEPARATOR);
if (curend)
end = curend;
else
end = temp + strlen(temp);
strncpy(folder, temp, end - temp);
folder[end - temp] = 0;
// stop on file extension if needed
if (!includeDotPath && strchr(folder, '.'))
break;
result = _mkdir(folder);
if (result > 0 && result != EEXIST)
return false;
} while (curend);
return true;
}
char* varargs(const char* fmt, ...)
{
va_list argptr;
static int index = 0;
static char string[4][4096];
char* buf = string[index];
index = (index + 1) & 3;
memset(buf, 0, 4096);
va_start(argptr, fmt);
vsnprintf(buf, 4096, fmt, argptr);
va_end(argptr);
return buf;
}
int xstrsplitws(char* str, char **pointer_array)
{
char c = *str;
int num_indices = 0;
bool bAdd = true;
while(c != '\0')
{
c = *str;
if(bAdd)
{
pointer_array[num_indices] = str;
num_indices++;
bAdd = false;
}
if( isspace(c) )
{
bAdd = true;
*str = '\0'; // make null-string
}
str++;
}
return num_indices;
}