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
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
7
be4835547dd0 clean up code, convert git files to hg, etc.
Paper
parents: 4
diff changeset
1 /**
be4835547dd0 clean up code, convert git files to hg, etc.
Paper
parents: 4
diff changeset
2 * dirtools.c:
be4835547dd0 clean up code, convert git files to hg, etc.
Paper
parents: 4
diff changeset
3 *
be4835547dd0 clean up code, convert git files to hg, etc.
Paper
parents: 4
diff changeset
4 * Useful tools for manipulating directory names,
be4835547dd0 clean up code, convert git files to hg, etc.
Paper
parents: 4
diff changeset
5 * or pretty much anything involving directories.
be4835547dd0 clean up code, convert git files to hg, etc.
Paper
parents: 4
diff changeset
6 **/
0
d91dfd53b8b4 Initial commit
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
7 #include <stdio.h>
d91dfd53b8b4 Initial commit
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
8 #include <stdlib.h>
d91dfd53b8b4 Initial commit
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
9 #include <string.h>
d91dfd53b8b4 Initial commit
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
10 #ifndef WIN32_LEAN_AND_MEAN
7
be4835547dd0 clean up code, convert git files to hg, etc.
Paper
parents: 4
diff changeset
11 # define WIN32_LEAN_AND_MEAN
0
d91dfd53b8b4 Initial commit
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
12 #endif
d91dfd53b8b4 Initial commit
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
13 #include <windows.h>
d91dfd53b8b4 Initial commit
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
14 #include "dirtools.h"
d91dfd53b8b4 Initial commit
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
15
10
42ac054c0231 *: huge refactoring
Paper <paper@paper.us.eu.org>
parents: 7
diff changeset
16 /* these are aids and should be converted to wchar */
42ac054c0231 *: huge refactoring
Paper <paper@paper.us.eu.org>
parents: 7
diff changeset
17
42ac054c0231 *: huge refactoring
Paper <paper@paper.us.eu.org>
parents: 7
diff changeset
18 static int dirtools_directory_exists(LPCWSTR restrict path) {
42ac054c0231 *: huge refactoring
Paper <paper@paper.us.eu.org>
parents: 7
diff changeset
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
42ac054c0231 *: huge refactoring
Paper <paper@paper.us.eu.org>
parents: 7
diff changeset
23 int dirtools_create_directory(LPCWSTR restrict path) {
42ac054c0231 *: huge refactoring
Paper <paper@paper.us.eu.org>
parents: 7
diff changeset
24 size_t len = wcslen(path);
42ac054c0231 *: huge refactoring
Paper <paper@paper.us.eu.org>
parents: 7
diff changeset
25 WCHAR tmp[len + 1];
42ac054c0231 *: huge refactoring
Paper <paper@paper.us.eu.org>
parents: 7
diff changeset
26 memset(tmp, '\0', (len + 1) * sizeof(WCHAR));
0
d91dfd53b8b4 Initial commit
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
27
10
42ac054c0231 *: huge refactoring
Paper <paper@paper.us.eu.org>
parents: 7
diff changeset
28 for (size_t i = 0; i < len; i++) {
42ac054c0231 *: huge refactoring
Paper <paper@paper.us.eu.org>
parents: 7
diff changeset
29 if (path[i] != L'\\')
42ac054c0231 *: huge refactoring
Paper <paper@paper.us.eu.org>
parents: 7
diff changeset
30 continue;
42ac054c0231 *: huge refactoring
Paper <paper@paper.us.eu.org>
parents: 7
diff changeset
31
42ac054c0231 *: huge refactoring
Paper <paper@paper.us.eu.org>
parents: 7
diff changeset
32 wcsncpy(tmp, path, i);
42ac054c0231 *: huge refactoring
Paper <paper@paper.us.eu.org>
parents: 7
diff changeset
33 if (dirtools_directory_exists(tmp))
42ac054c0231 *: huge refactoring
Paper <paper@paper.us.eu.org>
parents: 7
diff changeset
34 continue;
42ac054c0231 *: huge refactoring
Paper <paper@paper.us.eu.org>
parents: 7
diff changeset
35
42ac054c0231 *: huge refactoring
Paper <paper@paper.us.eu.org>
parents: 7
diff changeset
36 if (!CreateDirectoryW(tmp, NULL))
42ac054c0231 *: huge refactoring
Paper <paper@paper.us.eu.org>
parents: 7
diff changeset
37 if (GetLastError() == ERROR_PATH_NOT_FOUND)
42ac054c0231 *: huge refactoring
Paper <paper@paper.us.eu.org>
parents: 7
diff changeset
38 return 1;
0
d91dfd53b8b4 Initial commit
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
39 }
d91dfd53b8b4 Initial commit
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
40
10
42ac054c0231 *: huge refactoring
Paper <paper@paper.us.eu.org>
parents: 7
diff changeset
41 if (!dirtools_directory_exists(path) && !CreateDirectoryW(path, NULL))
42ac054c0231 *: huge refactoring
Paper <paper@paper.us.eu.org>
parents: 7
diff changeset
42 if (GetLastError() == ERROR_PATH_NOT_FOUND)
42ac054c0231 *: huge refactoring
Paper <paper@paper.us.eu.org>
parents: 7
diff changeset
43 return 1;
1
7abb5d8b20ea Initial commit: part 2
Paper <mrpapersonic@gmail.com>
parents: 0
diff changeset
44
0
d91dfd53b8b4 Initial commit
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
45 return 0;
d91dfd53b8b4 Initial commit
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
46 }
d91dfd53b8b4 Initial commit
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
47
10
42ac054c0231 *: huge refactoring
Paper <paper@paper.us.eu.org>
parents: 7
diff changeset
48 LPWSTR dirtools_concat_paths(LPCWSTR restrict a, LPCWSTR restrict b) {
42ac054c0231 *: huge refactoring
Paper <paper@paper.us.eu.org>
parents: 7
diff changeset
49 if (a[0] == L'\0' || b[0] == L'\0')
0
d91dfd53b8b4 Initial commit
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
50 return NULL;
10
42ac054c0231 *: huge refactoring
Paper <paper@paper.us.eu.org>
parents: 7
diff changeset
51
42ac054c0231 *: huge refactoring
Paper <paper@paper.us.eu.org>
parents: 7
diff changeset
52 const size_t a_len = wcslen(a);
42ac054c0231 *: huge refactoring
Paper <paper@paper.us.eu.org>
parents: 7
diff changeset
53 const size_t b_len = wcslen(b);
42ac054c0231 *: huge refactoring
Paper <paper@paper.us.eu.org>
parents: 7
diff changeset
54 const int add_backslash = (a[a_len] != L'\\' && b[0] != L'\\');
42ac054c0231 *: huge refactoring
Paper <paper@paper.us.eu.org>
parents: 7
diff changeset
55
42ac054c0231 *: huge refactoring
Paper <paper@paper.us.eu.org>
parents: 7
diff changeset
56 LPWSTR out = calloc(a_len + b_len + 1 + add_backslash, sizeof(WCHAR));
0
d91dfd53b8b4 Initial commit
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
57 if (out == NULL)
d91dfd53b8b4 Initial commit
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
58 return out;
10
42ac054c0231 *: huge refactoring
Paper <paper@paper.us.eu.org>
parents: 7
diff changeset
59
42ac054c0231 *: huge refactoring
Paper <paper@paper.us.eu.org>
parents: 7
diff changeset
60 wcscpy(out, a);
42ac054c0231 *: huge refactoring
Paper <paper@paper.us.eu.org>
parents: 7
diff changeset
61 if (add_backslash)
42ac054c0231 *: huge refactoring
Paper <paper@paper.us.eu.org>
parents: 7
diff changeset
62 wcscat(out, L"\\");
42ac054c0231 *: huge refactoring
Paper <paper@paper.us.eu.org>
parents: 7
diff changeset
63
42ac054c0231 *: huge refactoring
Paper <paper@paper.us.eu.org>
parents: 7
diff changeset
64 wcscat(out, b);
0
d91dfd53b8b4 Initial commit
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
65 return out;
d91dfd53b8b4 Initial commit
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
66 }