diff options
author | A404M <ahmadmahmoudiprogrammer@gmail.com> | 2025-04-24 02:30:09 +0330 |
---|---|---|
committer | A404M <ahmadmahmoudiprogrammer@gmail.com> | 2025-04-24 02:30:09 +0330 |
commit | 07e5fc400493093cd03493ef469b9b97fa1fe87a (patch) | |
tree | cd9aff588630fab8bfb98e30f0f1c72080a2946f /src/utils/file.c | |
parent | f91fc3bac670a8c775c7ea7b8ba8789dba9559ad (diff) |
add @import
Diffstat (limited to 'src/utils/file.c')
-rw-r--r-- | src/utils/file.c | 43 |
1 files changed, 43 insertions, 0 deletions
diff --git a/src/utils/file.c b/src/utils/file.c index 714dc2c..340875c 100644 --- a/src/utils/file.c +++ b/src/utils/file.c @@ -80,3 +80,46 @@ size_t getFileIndex(const char *filePath) { return fileCodes_length; } + +char *joinToPathOf(const char *original, const char *file) { + size_t result_size = 0; + for (size_t i = 0; original[i] != '\0'; ++i) { + if (original[i] == '/') { + result_size = i; + } + } + char *result = + a404m_malloc((result_size + 1 + strlen(file) + 1) * sizeof(*result)); + + strncpy(result, original, result_size); + result[result_size++] = '/'; + + bool startOfDirName = true; + for (size_t i = 0; file[i] != '\0'; ++i) { + if (startOfDirName && file[i + 1] == '.') { + if (file[i + 2] == '/') { + i += 2; + continue; + } else if (file[i + 2] == '.' && file[i + 3] == '/') { + bool found = false; + for (size_t j = i - 2; j != -1ULL; --j) { + if (result[j] == '/') { + result_size = j; + found = true; + break; + } + } + if (!found) { + result_size = 0; + } + continue; + } + } + result[result_size++] = file[i]; + } + + result[result_size++] = '\0'; + result = a404m_realloc(result, result_size); + + return result; +} |