comparison 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
comparison
equal deleted inserted replaced
9:07f0e2f43204 10:42ac054c0231
11 # define WIN32_LEAN_AND_MEAN 11 # define WIN32_LEAN_AND_MEAN
12 #endif 12 #endif
13 #include <windows.h> 13 #include <windows.h>
14 #include "dirtools.h" 14 #include "dirtools.h"
15 15
16 int dirtools_directory_exists(char* path) { 16 /* these are aids and should be converted to wchar */
17 DWORD attrib = GetFileAttributesA(path); 17
18 static int dirtools_directory_exists(LPCWSTR restrict path) {
19 DWORD attrib = GetFileAttributesW(path);
18 return (attrib != INVALID_FILE_ATTRIBUTES && (attrib & FILE_ATTRIBUTE_DIRECTORY)); 20 return (attrib != INVALID_FILE_ATTRIBUTES && (attrib & FILE_ATTRIBUTE_DIRECTORY));
19 } 21 }
20 22
21 int dirtools_create_directory(char* path) { 23 int dirtools_create_directory(LPCWSTR restrict path) {
22 char* alltoks = calloc(strlen(path)+2, sizeof(char)), *tok; 24 size_t len = wcslen(path);
25 WCHAR tmp[len + 1];
26 memset(tmp, '\0', (len + 1) * sizeof(WCHAR));
23 27
24 for (tok = strtok(path, "\\"); tok != NULL; tok = strtok(NULL, "\\")) { 28 for (size_t i = 0; i < len; i++) {
25 strcat(alltoks, tok); 29 if (path[i] != L'\\')
26 strcat(alltoks, "\\"); 30 continue;
27 if (dirtools_directory_exists(path)) { 31
28 if (!CreateDirectoryA(alltoks, NULL)) { 32 wcsncpy(tmp, path, i);
29 if (GetLastError() == ERROR_PATH_NOT_FOUND) { 33 if (dirtools_directory_exists(tmp))
30 /* ERROR_PATH_NOT_FOUND should NOT happen here */ 34 continue;
31 return 1; 35
32 } 36 if (!CreateDirectoryW(tmp, NULL))
33 } 37 if (GetLastError() == ERROR_PATH_NOT_FOUND)
34 } 38 return 1;
35 } 39 }
36 40
37 free(alltoks); 41 if (!dirtools_directory_exists(path) && !CreateDirectoryW(path, NULL))
42 if (GetLastError() == ERROR_PATH_NOT_FOUND)
43 return 1;
38 44
39 return 0; 45 return 0;
40 } 46 }
41 47
42 char* dirtools_concat_paths(char* a, char* b) { 48 LPWSTR dirtools_concat_paths(LPCWSTR restrict a, LPCWSTR restrict b) {
43 if (a[0] == '\0' || b[0] == '\0') 49 if (a[0] == L'\0' || b[0] == L'\0')
44 return NULL; 50 return NULL;
45 char* out = calloc((strlen(a) + strlen(b) + 2), sizeof(char)); 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));
46 if (out == NULL) 57 if (out == NULL)
47 return out; 58 return out;
48 strcpy(out, a); 59
49 if (a[strlen(a)] != '\\' && b[0] != '\\') 60 wcscpy(out, a);
50 strcat(out, "\\"); 61 if (add_backslash)
51 strcat(out, b); 62 wcscat(out, L"\\");
63
64 wcscat(out, b);
52 return out; 65 return out;
53 } 66 }