Mercurial > wgsdk
annotate src/dirtools.c @ 10:42ac054c0231
*: huge refactoring
dirtools now uses wchar (wayyy overdue)
the timer doesn't have a stupid design anymore
we don't use windows.h at all now
...
author | Paper <paper@paper.us.eu.org> |
---|---|
date | Sun, 11 Feb 2024 19:43:31 -0500 |
parents | be4835547dd0 |
children | e6a594f16403 |
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 <stdio.h> |
8 #include <stdlib.h> | |
9 #include <string.h> | |
10 #ifndef WIN32_LEAN_AND_MEAN | |
7 | 11 # define WIN32_LEAN_AND_MEAN |
0 | 12 #endif |
13 #include <windows.h> | |
14 #include "dirtools.h" | |
15 | |
10 | 16 /* these are aids and should be converted to wchar */ |
17 | |
18 static int dirtools_directory_exists(LPCWSTR restrict path) { | |
19 DWORD attrib = GetFileAttributesW(path); | |
3
8df8af626dca
dirtools: sys/stat.h->windows.h
Paper <37962225+mrpapersonic@users.noreply.github.com>
parents:
1
diff
changeset
|
20 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
|
21 } |
8df8af626dca
dirtools: sys/stat.h->windows.h
Paper <37962225+mrpapersonic@users.noreply.github.com>
parents:
1
diff
changeset
|
22 |
10 | 23 int dirtools_create_directory(LPCWSTR restrict path) { |
24 size_t len = wcslen(path); | |
25 WCHAR tmp[len + 1]; | |
26 memset(tmp, '\0', (len + 1) * sizeof(WCHAR)); | |
0 | 27 |
10 | 28 for (size_t i = 0; i < len; i++) { |
29 if (path[i] != L'\\') | |
30 continue; | |
31 | |
32 wcsncpy(tmp, path, i); | |
33 if (dirtools_directory_exists(tmp)) | |
34 continue; | |
35 | |
36 if (!CreateDirectoryW(tmp, NULL)) | |
37 if (GetLastError() == ERROR_PATH_NOT_FOUND) | |
38 return 1; | |
0 | 39 } |
40 | |
10 | 41 if (!dirtools_directory_exists(path) && !CreateDirectoryW(path, NULL)) |
42 if (GetLastError() == ERROR_PATH_NOT_FOUND) | |
43 return 1; | |
1 | 44 |
0 | 45 return 0; |
46 } | |
47 | |
10 | 48 LPWSTR dirtools_concat_paths(LPCWSTR restrict a, LPCWSTR restrict b) { |
49 if (a[0] == L'\0' || b[0] == L'\0') | |
0 | 50 return NULL; |
10 | 51 |
52 const size_t a_len = wcslen(a); | |
53 const size_t b_len = wcslen(b); | |
54 const int add_backslash = (a[a_len] != L'\\' && b[0] != L'\\'); | |
55 | |
56 LPWSTR out = calloc(a_len + b_len + 1 + add_backslash, sizeof(WCHAR)); | |
0 | 57 if (out == NULL) |
58 return out; | |
10 | 59 |
60 wcscpy(out, a); | |
61 if (add_backslash) | |
62 wcscat(out, L"\\"); | |
63 | |
64 wcscat(out, b); | |
0 | 65 return out; |
66 } |