Browse Source

Added path_to_native_separators

master
parent
commit
a1b1415dda
  1. 21
      libpath.c
  2. 8
      libpath.h
  3. 7
      test.c

21
libpath.c

@ -176,3 +176,24 @@ char* path_parent(const char* path) {
return base_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);
}

8
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. Be sure to path_free() on the base as well after use.
*/ */
char* path_parent(const char* path); 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);

7
test.c

@ -30,5 +30,12 @@ int main() {
path_free(&parent); path_free(&parent);
path_free(&parent_of_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; return EXIT_SUCCESS;
} }
Loading…
Cancel
Save