From a1b1415dda481ac4faef356f52af008abb97940b Mon Sep 17 00:00:00 2001 From: Unbewohnte Date: Tue, 11 Apr 2023 17:37:48 +0300 Subject: [PATCH] Added path_to_native_separators --- libpath.c | 21 +++++++++++++++++++++ libpath.h | 8 ++++++++ test.c | 7 +++++++ 3 files changed, 36 insertions(+) diff --git a/libpath.c b/libpath.c index a4cb2a9..2a710cc 100644 --- a/libpath.c +++ b/libpath.c @@ -176,3 +176,24 @@ char* path_parent(const char* path) { return base_path; } +/* +Swaps Windows' path separators (\) in given path with +POSIX path separators (/) if compiled for POSIX systems +and the other way around. Calls path_clean on the resulting +path +*/ +void path_to_native_separators(char* path) { + if (!path) { + return; + } + + size_t path_len = strlen(path); + + for (size_t i = 0; i < path_len; i++) { + if (path[i] == '\\' || path[i] == '/') { + path[i] = PATH_SEPARATOR; + } + } + + path_clean(path); +} \ No newline at end of file diff --git a/libpath.h b/libpath.h index 2bc5adc..2fff3c5 100644 --- a/libpath.h +++ b/libpath.h @@ -39,3 +39,11 @@ Creates a new path pointing to the parent. Be sure to path_free() on the base as well after use. */ char* path_parent(const char* path); + +/* +Swaps Windows' path separators (\) in given path with +POSIX path separators (/) if compiled for POSIX systems +and the other way around. Calls path_clean on the resulting +path +*/ +void path_to_native_separators(char* path); \ No newline at end of file diff --git a/test.c b/test.c index 887df01..de64366 100644 --- a/test.c +++ b/test.c @@ -30,5 +30,12 @@ int main() { path_free(&parent); path_free(&parent_of_parent); + char* windows_path = path_join(2, "C:\\Documents\\", "file.txt"); + printf("Windows path: %s\n", windows_path); + path_to_native_separators(windows_path); + printf("Windows path with native separators: %s\n", windows_path); + + path_free(&windows_path); + return EXIT_SUCCESS; } \ No newline at end of file