-
Notifications
You must be signed in to change notification settings - Fork 65
/
pathUtilities.c
249 lines (211 loc) · 6.4 KB
/
pathUtilities.c
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
#include "pharovm/pathUtilities.h"
#include "pharovm/stringUtilities.h"
#include <string.h>
#include <stdio.h>
#ifdef _WIN32
# include <windows.h>
#elif defined(__unix__) || defined(__MACH__) || defined(__APPLE__)
# include <unistd.h>
# include <dirent.h>
#else
# define getcwd(target, targetSize) strcpy(target, ".") // Assume target size is always greater than one.
#endif
EXPORT(VMErrorCode)
vm_path_get_current_working_dir_into(char *target, size_t targetSize)
{
#ifdef _WIN32
DWORD tempBufferSize = GetCurrentDirectoryW(0, NULL) + 1; // for \0
WCHAR *tempBuffer = (WCHAR*)calloc(tempBufferSize, sizeof(WCHAR));
if(!tempBuffer) {
return VM_ERROR_OUT_OF_MEMORY;
}
GetCurrentDirectoryW(tempBufferSize, tempBuffer);
WideCharToMultiByte(CP_UTF8, 0, tempBuffer, tempBufferSize, target, targetSize, NULL, FALSE);
target[targetSize - 1] = 0; // CHECK ME: Is this really needed?
#else
if(getcwd(target, targetSize) == NULL) {
return VM_ERROR;
}
#endif
return VM_SUCCESS;
}
EXPORT(bool)
vm_path_is_absolute_path(const char *path)
{
#ifdef _WIN32
return path && (path[0] != 0 && path[1] == ':');
#else
/* Assume UNIX style path. */
return path && (*path == '/');
#endif
}
EXPORT(VMErrorCode)
vm_path_make_absolute_into(char *target, size_t targetSize, const char *src)
{
VMErrorCode error;
if (vm_path_is_absolute_path(src))
{
strncpy(target, src, targetSize - 1);
target[targetSize-1] = 0;
}
else
{
error = vm_path_get_current_working_dir_into(target, targetSize);
if(error) {
return error;
}
size_t workDirSize = strlen(target);
#ifdef _WIN32
if(workDirSize > 0 && target[workDirSize - 1] != '\\')
{
vm_string_append_into(target, "\\", targetSize);
}
if (src[0] == '.' && (src[1] == '/' || src[1] == '\\'))
{
vm_string_append_into(target, src + 2, targetSize);
}
else {
vm_string_append_into(target, src, targetSize);
}
#else
if(workDirSize > 0 && target[workDirSize - 1] != '/')
{
vm_string_append_into(target, "/", targetSize);
}
if(src[0] == '.' && src[1] == '/')
{
vm_string_append_into(target, src + 2, targetSize);
}
else
{
vm_string_append_into(target, src, targetSize);
}
#endif
}
return VM_SUCCESS;
}
EXPORT(VMErrorCode)
vm_path_extract_dirname_into(char *target, size_t targetSize, const char *src)
{
size_t copySize;
const char *lastSeparator = strrchr(src, '/');
#ifdef _WIN32
const char *lastSeparator2 = strrchr(src, '\\');
if (!lastSeparator || lastSeparator < lastSeparator2)
lastSeparator = lastSeparator2;
#endif
copySize = targetSize - 1;
if (lastSeparator && lastSeparator - src < copySize)
{
// FIXME: raise an error code, and handle this case.
size_t newCopySize = lastSeparator - src;
if(newCopySize < copySize)
copySize = newCopySize;
}
strncpy(target, src, copySize);
target[copySize] = 0;
return VM_SUCCESS;
}
EXPORT(VMErrorCode)
vm_path_extract_basename_into(char *target, size_t targetSize, const char *src)
{
const char *lastSeparator = strrchr(src, '/');
#ifdef _WIN32
const char *lastSeparator2 = strrchr(src, '\\');
if (!lastSeparator || lastSeparator < lastSeparator2)
lastSeparator = lastSeparator2;
#endif
if (lastSeparator)
{
strncpy(target, lastSeparator, targetSize - 1);
target[targetSize - 1] = 0;
}
else
{
strcpy(target, "");
}
return VM_SUCCESS;
}
#ifdef _WIN32
# define SEPARATOR_STRING "\\"
# define SEPARATOR_CHAR '\\'
#else
# define SEPARATOR_STRING "/"
# define SEPARATOR_CHAR '/'
#endif
EXPORT(VMErrorCode)
vm_path_join_into(char *target, size_t targetSize, const char *first, const char *second)
{
strncpy(target, first, targetSize - 1);
target[targetSize - 1] = 0;
if(first[strlen(first)-1] != SEPARATOR_CHAR) {
vm_string_append_into(target, SEPARATOR_STRING, targetSize);
}
vm_string_append_into(target, second, targetSize);
return VM_SUCCESS;
}
#ifdef _WIN32
EXPORT(size_t)
vm_path_find_files_with_extension_in_folder(const char *searchPath, const char *extension, char *imagePathBuffer, size_t imagePathBufferSize)
{
WIN32_FIND_DATAW findFileData;
HANDLE findHandle;
WCHAR *searchPathW = vm_string_convert_utf8_to_utf16(searchPath);
WCHAR *searchPathPattern = (WCHAR*)calloc(FILENAME_MAX, 2);
lstrcpyW(searchPathPattern, searchPathW);
lstrcatW(searchPathPattern, L"\\*.image");
findHandle = FindFirstFileW(searchPathPattern, &findFileData);
if(findHandle == INVALID_HANDLE_VALUE)
{
free(searchPathW);
return 0;
}
bool hasPreviousOutput = *imagePathBuffer != 0;
int result = 0;
WCHAR *pathConversionBuffer = (WCHAR*)calloc(FILENAME_MAX, 2);
do
{
if(!hasPreviousOutput)
{
lstrcpyW(pathConversionBuffer, searchPathW);
lstrcatW(pathConversionBuffer, L"\\");
lstrcatW(pathConversionBuffer, findFileData.cFileName);
WideCharToMultiByte(CP_UTF8, 0, pathConversionBuffer, -1, imagePathBuffer, imagePathBufferSize, NULL, FALSE);
imagePathBuffer[imagePathBufferSize - 1] = 0; // CHECK ME: Is this really needed?
}
hasPreviousOutput = true;
++result;
} while(FindNextFileW(findHandle, &findFileData));
FindClose(findHandle);
free(pathConversionBuffer);
free(searchPathPattern);
free(searchPathW);
return result;
}
#else
EXPORT(size_t)
vm_path_find_files_with_extension_in_folder(const char *searchPath, const char *extension, char *imagePathBuffer, size_t imagePathBufferSize)
{
struct dirent *entry;
int result = 0;
bool hasPreviousOutput = *imagePathBuffer != 0;
DIR *dir = opendir(searchPath);
if(!dir)
return 0;
while((entry = readdir(dir)) != NULL)
{
char *name = entry->d_name;
char *fileExtension = strrchr(name, '.');
if(!extension)
continue;
if(strcmp(fileExtension, extension) != 0)
continue;
if(!hasPreviousOutput)
snprintf(imagePathBuffer, imagePathBufferSize, "%s/%s", searchPath, name);
++result;
hasPreviousOutput = true;
}
closedir(dir);
return result;
}
#endif