annotate src/dirtools.c @ 4:59bf702b2b21

*: stylistic changes
author Paper <mrpapersonic@gmail.com>
date Sun, 14 Aug 2022 13:17:32 -0400
parents 8df8af626dca
children be4835547dd0
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
d91dfd53b8b4 Initial commit
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
1 #include <stdio.h>
d91dfd53b8b4 Initial commit
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
2 #include <stdlib.h>
d91dfd53b8b4 Initial commit
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
3 #include <string.h>
d91dfd53b8b4 Initial commit
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
4 #ifndef WIN32_LEAN_AND_MEAN
d91dfd53b8b4 Initial commit
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
5 # define WIN32_LEAN_AND_MEAN
d91dfd53b8b4 Initial commit
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
6 #endif
d91dfd53b8b4 Initial commit
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
7 #include <windows.h>
d91dfd53b8b4 Initial commit
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
8 #include "dirtools.h"
d91dfd53b8b4 Initial commit
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
9
3
8df8af626dca dirtools: sys/stat.h->windows.h
Paper <37962225+mrpapersonic@users.noreply.github.com>
parents: 1
diff changeset
10 int dirtools_directory_exists(char* path) {
8df8af626dca dirtools: sys/stat.h->windows.h
Paper <37962225+mrpapersonic@users.noreply.github.com>
parents: 1
diff changeset
11 DWORD attrib = GetFileAttributesA(path);
8df8af626dca dirtools: sys/stat.h->windows.h
Paper <37962225+mrpapersonic@users.noreply.github.com>
parents: 1
diff changeset
12 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
13 }
8df8af626dca dirtools: sys/stat.h->windows.h
Paper <37962225+mrpapersonic@users.noreply.github.com>
parents: 1
diff changeset
14
0
d91dfd53b8b4 Initial commit
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
15 int dirtools_create_directory(char* path) {
4
59bf702b2b21 *: stylistic changes
Paper <mrpapersonic@gmail.com>
parents: 3
diff changeset
16 char* alltoks = calloc(strlen(path)+2, sizeof(char)), *tok;
0
d91dfd53b8b4 Initial commit
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
17
d91dfd53b8b4 Initial commit
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
18 for (tok = strtok(path, "\\"); tok != NULL; tok = strtok(NULL, "\\")) {
d91dfd53b8b4 Initial commit
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
19 strcat(alltoks, tok);
4
59bf702b2b21 *: stylistic changes
Paper <mrpapersonic@gmail.com>
parents: 3
diff changeset
20 strcat(alltoks, "\\");
3
8df8af626dca dirtools: sys/stat.h->windows.h
Paper <37962225+mrpapersonic@users.noreply.github.com>
parents: 1
diff changeset
21 if (dirtools_directory_exists(path)) {
1
7abb5d8b20ea Initial commit: part 2
Paper <mrpapersonic@gmail.com>
parents: 0
diff changeset
22 if (!CreateDirectoryA(alltoks, NULL)) {
7abb5d8b20ea Initial commit: part 2
Paper <mrpapersonic@gmail.com>
parents: 0
diff changeset
23 if (GetLastError() == ERROR_PATH_NOT_FOUND) {
7abb5d8b20ea Initial commit: part 2
Paper <mrpapersonic@gmail.com>
parents: 0
diff changeset
24 /* ERROR_PATH_NOT_FOUND should NOT happen here */
7abb5d8b20ea Initial commit: part 2
Paper <mrpapersonic@gmail.com>
parents: 0
diff changeset
25 return 1;
7abb5d8b20ea Initial commit: part 2
Paper <mrpapersonic@gmail.com>
parents: 0
diff changeset
26 }
7abb5d8b20ea Initial commit: part 2
Paper <mrpapersonic@gmail.com>
parents: 0
diff changeset
27 }
0
d91dfd53b8b4 Initial commit
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
28 }
d91dfd53b8b4 Initial commit
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
29 }
d91dfd53b8b4 Initial commit
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
30
1
7abb5d8b20ea Initial commit: part 2
Paper <mrpapersonic@gmail.com>
parents: 0
diff changeset
31 free(alltoks);
7abb5d8b20ea Initial commit: part 2
Paper <mrpapersonic@gmail.com>
parents: 0
diff changeset
32
0
d91dfd53b8b4 Initial commit
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
33 return 0;
d91dfd53b8b4 Initial commit
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
34 }
d91dfd53b8b4 Initial commit
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
35
d91dfd53b8b4 Initial commit
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
36 char* dirtools_concat_paths(char* a, char* b) {
d91dfd53b8b4 Initial commit
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
37 if (a[0] == '\0' || b[0] == '\0')
d91dfd53b8b4 Initial commit
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
38 return NULL;
d91dfd53b8b4 Initial commit
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
39 char* out = calloc((strlen(a) + strlen(b) + 2), sizeof(char));
d91dfd53b8b4 Initial commit
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
40 if (out == NULL)
d91dfd53b8b4 Initial commit
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
41 return out;
d91dfd53b8b4 Initial commit
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
42 strcpy(out, a);
d91dfd53b8b4 Initial commit
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
43 if (a[strlen(a)] != '\\' && b[0] != '\\')
d91dfd53b8b4 Initial commit
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
44 strcat(out, "\\");
d91dfd53b8b4 Initial commit
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
45 strcat(out, b);
d91dfd53b8b4 Initial commit
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
46 return out;
d91dfd53b8b4 Initial commit
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
47 }