Mercurial > wgsdk
annotate src/dirtools.c @ 9:07f0e2f43204
*: add the restrict keyword when necessary
also fixed some typos in the README
author | Paper |
---|---|
date | Fri, 16 Dec 2022 21:55:37 -0500 |
parents | be4835547dd0 |
children | 42ac054c0231 |
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 | |
3
8df8af626dca
dirtools: sys/stat.h->windows.h
Paper <37962225+mrpapersonic@users.noreply.github.com>
parents:
1
diff
changeset
|
16 int dirtools_directory_exists(char* path) { |
8df8af626dca
dirtools: sys/stat.h->windows.h
Paper <37962225+mrpapersonic@users.noreply.github.com>
parents:
1
diff
changeset
|
17 DWORD attrib = GetFileAttributesA(path); |
8df8af626dca
dirtools: sys/stat.h->windows.h
Paper <37962225+mrpapersonic@users.noreply.github.com>
parents:
1
diff
changeset
|
18 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
|
19 } |
8df8af626dca
dirtools: sys/stat.h->windows.h
Paper <37962225+mrpapersonic@users.noreply.github.com>
parents:
1
diff
changeset
|
20 |
0 | 21 int dirtools_create_directory(char* path) { |
4 | 22 char* alltoks = calloc(strlen(path)+2, sizeof(char)), *tok; |
0 | 23 |
24 for (tok = strtok(path, "\\"); tok != NULL; tok = strtok(NULL, "\\")) { | |
25 strcat(alltoks, tok); | |
4 | 26 strcat(alltoks, "\\"); |
3
8df8af626dca
dirtools: sys/stat.h->windows.h
Paper <37962225+mrpapersonic@users.noreply.github.com>
parents:
1
diff
changeset
|
27 if (dirtools_directory_exists(path)) { |
1 | 28 if (!CreateDirectoryA(alltoks, NULL)) { |
29 if (GetLastError() == ERROR_PATH_NOT_FOUND) { | |
30 /* ERROR_PATH_NOT_FOUND should NOT happen here */ | |
31 return 1; | |
32 } | |
33 } | |
0 | 34 } |
35 } | |
36 | |
1 | 37 free(alltoks); |
38 | |
0 | 39 return 0; |
40 } | |
41 | |
42 char* dirtools_concat_paths(char* a, char* b) { | |
43 if (a[0] == '\0' || b[0] == '\0') | |
44 return NULL; | |
45 char* out = calloc((strlen(a) + strlen(b) + 2), sizeof(char)); | |
46 if (out == NULL) | |
47 return out; | |
48 strcpy(out, a); | |
49 if (a[strlen(a)] != '\\' && b[0] != '\\') | |
50 strcat(out, "\\"); | |
51 strcat(out, b); | |
52 return out; | |
53 } |