diff options
author | 3gg <3gg@shellblade.net> | 2024-06-15 11:42:48 -0700 |
---|---|---|
committer | 3gg <3gg@shellblade.net> | 2024-06-15 11:42:48 -0700 |
commit | 04e3ded4c28c0b559620609daaae7b939d776b61 (patch) | |
tree | 55efea02bee351ac01ef764a04105ee9cee69259 /filesystem/src/filesystem.c | |
parent | 993424547df0d253d546dbe7adee9b2448294b08 (diff) |
Add path.
Diffstat (limited to 'filesystem/src/filesystem.c')
-rw-r--r-- | filesystem/src/filesystem.c | 40 |
1 files changed, 1 insertions, 39 deletions
diff --git a/filesystem/src/filesystem.c b/filesystem/src/filesystem.c index f6bb693..b228e85 100644 --- a/filesystem/src/filesystem.c +++ b/filesystem/src/filesystem.c | |||
@@ -1,6 +1,7 @@ | |||
1 | #include <filesystem.h> | 1 | #include <filesystem.h> |
2 | 2 | ||
3 | #include <assert.h> | 3 | #include <assert.h> |
4 | #include <stdio.h> | ||
4 | #include <stdlib.h> | 5 | #include <stdlib.h> |
5 | #include <string.h> | 6 | #include <string.h> |
6 | 7 | ||
@@ -54,42 +55,3 @@ cleanup: | |||
54 | } | 55 | } |
55 | return 0; | 56 | return 0; |
56 | } | 57 | } |
57 | |||
58 | bool make_relative_path( | ||
59 | const char* filepath, const char* path, char* relative, | ||
60 | size_t relative_length) { | ||
61 | assert(filepath); | ||
62 | assert(path); | ||
63 | assert(relative); | ||
64 | |||
65 | const size_t filepath_len = strlen(filepath); | ||
66 | const size_t path_len = strlen(path); | ||
67 | assert(filepath_len < relative_length); | ||
68 | assert(path_len < relative_length); | ||
69 | |||
70 | // Handle empty filepath. | ||
71 | if (filepath_len == 0) { | ||
72 | memcpy(relative, path, path_len); | ||
73 | return true; | ||
74 | } | ||
75 | |||
76 | // Search for the last / in the file path to get its parent directory. | ||
77 | assert(filepath_len > 0); | ||
78 | size_t tm_dir_len = 0; | ||
79 | for (tm_dir_len = strlen(filepath) - 1; tm_dir_len > 0; --tm_dir_len) { | ||
80 | if (filepath[tm_dir_len] == '/') { | ||
81 | break; | ||
82 | } | ||
83 | } | ||
84 | tm_dir_len++; // Preserve the backslash. | ||
85 | |||
86 | // Copy the file path where the parent dir ends. | ||
87 | // Make sure there is enough space in the output. | ||
88 | if ((tm_dir_len + path_len + 1) >= relative_length) { | ||
89 | return false; | ||
90 | } | ||
91 | memcpy(relative, filepath, tm_dir_len); | ||
92 | memcpy(&relative[tm_dir_len], path, path_len); | ||
93 | |||
94 | return true; | ||
95 | } | ||