Mercurial > wgsdk
annotate src/dirtools.c @ 12:dd427b7cc459 default tip
json: replace with nxjson library
more lightweight, reduces the binary size by about 40 kb
author | Paper <paper@paper.us.eu.org> |
---|---|
date | Fri, 15 Mar 2024 20:46:18 -0400 |
parents | e6a594f16403 |
children |
rev | line source |
---|---|
7 | 1 /** |
2 * dirtools.c: | |
3 * | |
4 * Useful tools for manipulating directory names, | |
5 * or pretty much anything involving directories. | |
6 **/ | |
0 | 7 #include "dirtools.h" |
8 | |
11 | 9 #include <windef.h> |
10 #include <fileapi.h> | |
11 #include <errhandlingapi.h> | |
12 #include <winerror.h> | |
10 | 13 |
14 static int dirtools_directory_exists(LPCWSTR restrict path) { | |
15 DWORD attrib = GetFileAttributesW(path); | |
3
8df8af626dca
dirtools: sys/stat.h->windows.h
Paper <37962225+mrpapersonic@users.noreply.github.com>
parents:
1
diff
changeset
|
16 return (attrib != INVALID_FILE_ATTRIBUTES && (attrib & FILE_ATTRIBUTE_DIRECTORY)); |
8df8af626dca
dirtools: sys/stat.h->windows.h
Paper <37962225+mrpapersonic@users.noreply.github.com>
parents:
1
diff
changeset
|
17 } |
8df8af626dca
dirtools: sys/stat.h->windows.h
Paper <37962225+mrpapersonic@users.noreply.github.com>
parents:
1
diff
changeset
|
18 |
10 | 19 int dirtools_create_directory(LPCWSTR restrict path) { |
20 size_t len = wcslen(path); | |
21 WCHAR tmp[len + 1]; | |
22 memset(tmp, '\0', (len + 1) * sizeof(WCHAR)); | |
0 | 23 |
10 | 24 for (size_t i = 0; i < len; i++) { |
25 if (path[i] != L'\\') | |
26 continue; | |
27 | |
28 wcsncpy(tmp, path, i); | |
29 if (dirtools_directory_exists(tmp)) | |
30 continue; | |
31 | |
32 if (!CreateDirectoryW(tmp, NULL)) | |
33 if (GetLastError() == ERROR_PATH_NOT_FOUND) | |
34 return 1; | |
0 | 35 } |
36 | |
10 | 37 if (!dirtools_directory_exists(path) && !CreateDirectoryW(path, NULL)) |
38 if (GetLastError() == ERROR_PATH_NOT_FOUND) | |
39 return 1; | |
1 | 40 |
0 | 41 return 0; |
42 } | |
43 | |
10 | 44 LPWSTR dirtools_concat_paths(LPCWSTR restrict a, LPCWSTR restrict b) { |
45 if (a[0] == L'\0' || b[0] == L'\0') | |
0 | 46 return NULL; |
10 | 47 |
48 const size_t a_len = wcslen(a); | |
49 const size_t b_len = wcslen(b); | |
50 const int add_backslash = (a[a_len] != L'\\' && b[0] != L'\\'); | |
51 | |
52 LPWSTR out = calloc(a_len + b_len + 1 + add_backslash, sizeof(WCHAR)); | |
0 | 53 if (out == NULL) |
54 return out; | |
10 | 55 |
56 wcscpy(out, a); | |
57 if (add_backslash) | |
58 wcscat(out, L"\\"); | |
59 | |
60 wcscat(out, b); | |
0 | 61 return out; |
62 } |