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
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 "dirtools.h"
d91dfd53b8b4 Initial commit
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
8
11
e6a594f16403 *: huge refactor
Paper <paper@paper.us.eu.org>
parents: 10
diff changeset
9 #include <windef.h>
e6a594f16403 *: huge refactor
Paper <paper@paper.us.eu.org>
parents: 10
diff changeset
10 #include <fileapi.h>
e6a594f16403 *: huge refactor
Paper <paper@paper.us.eu.org>
parents: 10
diff changeset
11 #include <errhandlingapi.h>
e6a594f16403 *: huge refactor
Paper <paper@paper.us.eu.org>
parents: 10
diff changeset
12 #include <winerror.h>
10
42ac054c0231 *: huge refactoring
Paper <paper@paper.us.eu.org>
parents: 7
diff changeset
13
42ac054c0231 *: huge refactoring
Paper <paper@paper.us.eu.org>
parents: 7
diff changeset
14 static int dirtools_directory_exists(LPCWSTR restrict path) {
42ac054c0231 *: huge refactoring
Paper <paper@paper.us.eu.org>
parents: 7
diff changeset
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
42ac054c0231 *: huge refactoring
Paper <paper@paper.us.eu.org>
parents: 7
diff changeset
19 int dirtools_create_directory(LPCWSTR restrict path) {
42ac054c0231 *: huge refactoring
Paper <paper@paper.us.eu.org>
parents: 7
diff changeset
20 size_t len = wcslen(path);
42ac054c0231 *: huge refactoring
Paper <paper@paper.us.eu.org>
parents: 7
diff changeset
21 WCHAR tmp[len + 1];
42ac054c0231 *: huge refactoring
Paper <paper@paper.us.eu.org>
parents: 7
diff changeset
22 memset(tmp, '\0', (len + 1) * sizeof(WCHAR));
0
d91dfd53b8b4 Initial commit
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
23
10
42ac054c0231 *: huge refactoring
Paper <paper@paper.us.eu.org>
parents: 7
diff changeset
24 for (size_t i = 0; i < len; i++) {
42ac054c0231 *: huge refactoring
Paper <paper@paper.us.eu.org>
parents: 7
diff changeset
25 if (path[i] != L'\\')
42ac054c0231 *: huge refactoring
Paper <paper@paper.us.eu.org>
parents: 7
diff changeset
26 continue;
42ac054c0231 *: huge refactoring
Paper <paper@paper.us.eu.org>
parents: 7
diff changeset
27
42ac054c0231 *: huge refactoring
Paper <paper@paper.us.eu.org>
parents: 7
diff changeset
28 wcsncpy(tmp, path, i);
42ac054c0231 *: huge refactoring
Paper <paper@paper.us.eu.org>
parents: 7
diff changeset
29 if (dirtools_directory_exists(tmp))
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 if (!CreateDirectoryW(tmp, NULL))
42ac054c0231 *: huge refactoring
Paper <paper@paper.us.eu.org>
parents: 7
diff changeset
33 if (GetLastError() == ERROR_PATH_NOT_FOUND)
42ac054c0231 *: huge refactoring
Paper <paper@paper.us.eu.org>
parents: 7
diff changeset
34 return 1;
0
d91dfd53b8b4 Initial commit
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
35 }
d91dfd53b8b4 Initial commit
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
36
10
42ac054c0231 *: huge refactoring
Paper <paper@paper.us.eu.org>
parents: 7
diff changeset
37 if (!dirtools_directory_exists(path) && !CreateDirectoryW(path, NULL))
42ac054c0231 *: huge refactoring
Paper <paper@paper.us.eu.org>
parents: 7
diff changeset
38 if (GetLastError() == ERROR_PATH_NOT_FOUND)
42ac054c0231 *: huge refactoring
Paper <paper@paper.us.eu.org>
parents: 7
diff changeset
39 return 1;
1
7abb5d8b20ea Initial commit: part 2
Paper <mrpapersonic@gmail.com>
parents: 0
diff changeset
40
0
d91dfd53b8b4 Initial commit
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
41 return 0;
d91dfd53b8b4 Initial commit
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
42 }
d91dfd53b8b4 Initial commit
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
43
10
42ac054c0231 *: huge refactoring
Paper <paper@paper.us.eu.org>
parents: 7
diff changeset
44 LPWSTR dirtools_concat_paths(LPCWSTR restrict a, LPCWSTR restrict b) {
42ac054c0231 *: huge refactoring
Paper <paper@paper.us.eu.org>
parents: 7
diff changeset
45 if (a[0] == L'\0' || b[0] == L'\0')
0
d91dfd53b8b4 Initial commit
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
46 return NULL;
10
42ac054c0231 *: huge refactoring
Paper <paper@paper.us.eu.org>
parents: 7
diff changeset
47
42ac054c0231 *: huge refactoring
Paper <paper@paper.us.eu.org>
parents: 7
diff changeset
48 const size_t a_len = wcslen(a);
42ac054c0231 *: huge refactoring
Paper <paper@paper.us.eu.org>
parents: 7
diff changeset
49 const size_t b_len = wcslen(b);
42ac054c0231 *: huge refactoring
Paper <paper@paper.us.eu.org>
parents: 7
diff changeset
50 const int add_backslash = (a[a_len] != L'\\' && b[0] != L'\\');
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 LPWSTR out = calloc(a_len + b_len + 1 + add_backslash, sizeof(WCHAR));
0
d91dfd53b8b4 Initial commit
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
53 if (out == NULL)
d91dfd53b8b4 Initial commit
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
54 return out;
10
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 wcscpy(out, a);
42ac054c0231 *: huge refactoring
Paper <paper@paper.us.eu.org>
parents: 7
diff changeset
57 if (add_backslash)
42ac054c0231 *: huge refactoring
Paper <paper@paper.us.eu.org>
parents: 7
diff changeset
58 wcscat(out, L"\\");
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 wcscat(out, b);
0
d91dfd53b8b4 Initial commit
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
61 return out;
d91dfd53b8b4 Initial commit
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
62 }