commit ecc1012ae7052f64626852f17b5664d48dc6e5be Author: Unbewohnte Date: Wed Mar 22 17:33:35 2023 +0300 Initial commit diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..9aae9c5 --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +test +libpath.so \ No newline at end of file diff --git a/LICENSE b/LICENSE new file mode 100644 index 0000000..7e6b19d --- /dev/null +++ b/LICENSE @@ -0,0 +1,9 @@ +The MIT License (MIT) + +Copyright © 2023 Kasyanov Nikolay Alexeyevich (Unbewohnte) + +Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the “Software”), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..f0f5036 --- /dev/null +++ b/Makefile @@ -0,0 +1,8 @@ +lib: + gcc -Wall -Werror -shared -fPIC libpath.c -o libpath.so + +clean: + rm -f test libpath.so + +test: clean + gcc libpath.c test.c -static -Wall -O2 -o test && ./test \ No newline at end of file diff --git a/README.md b/README.md new file mode 100644 index 0000000..806eba2 --- /dev/null +++ b/README.md @@ -0,0 +1,41 @@ +# libpath + +### A minimal (file)path library + +### Features + +- Joining multiple path parts +- Cleaning path of duplicate separators +- Retrieval of path's parent + + +### Example +```c +// Compiling for *nix system (therefore the separator is '/') +#include +#include +#include +#include "libpath.h" + +int main() { + char* path = path_join(4, "//here", "there", "/over/there_as_well", "file.txt"); + printf("Joined path: %s\n", path); + + char* parent = path_parent(path); + printf("Parent: %s\n", parent); + + path_free(path); + + char* parent_of_parent = path_parent(parent); + printf("Parent of parent: %s\n", parent_of_parent); + + path_free(parent); + path_free(parent_of_parent); + + return EXIT_SUCCESS; +} +``` + +### License + +MIT \ No newline at end of file diff --git a/libpath.c b/libpath.c new file mode 100644 index 0000000..855a642 --- /dev/null +++ b/libpath.c @@ -0,0 +1,177 @@ +/* +The MIT License (MIT) + +Copyright © 2023 Kasyanov Nikolay Alexeyevich (Unbewohnte) + +Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the “Software”), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. +*/ + +#include +#include +#include +#include + +// File path separator. '/' by default and '\\' for Windows and DOS +const char PATH_SEPARATOR = +#if defined(__WIN32__) || defined(_WIN32) || defined(WIN32) || defined(__DOS__) || defined(__SYMBIAN32__) + '\\'; +#else + '/'; +#endif + +/* +Clears allocated path from duplicate separators. +Reallocates memory if needed. +*/ +void path_clean(char* path) { + size_t path_len; + size_t i; + + if (!path) { + return; + } + + path_len = strlen(path); + if (path_len <= 1) { + return; + } + + // remove trailing separator if present + if (path[path_len-1] == PATH_SEPARATOR) { + path[path_len-1] = '\0'; + path_len--; + } + + if (path_len <= 1) { + return; + } + + // get rid of duplicate separators + i = 1; + while (i < path_len) { + if (path[i-1] == PATH_SEPARATOR && path[i] == PATH_SEPARATOR) { + // move to the left + for (size_t j = i; j < path_len-1; j++) { + path[j] = path[j + 1]; + } + path[path_len-1] = '\0'; + path_len--; + i--; + } + i++; + } + + path = realloc(path, sizeof(char) * path_len); +} + +/* +Clears allocated path from duplicate separators +and returns allocated clean path. +Be sure to path_free() after use. +*/ +char* path_clean_copy(const char* path) { + char* clean_path = NULL; + size_t path_len; + + if (!path) { + return NULL; + } + path_len = strlen(path); + + clean_path = malloc(sizeof(char) * (path_len + 1)); + clean_path = memcpy(clean_path, path, sizeof(char) * (path_len + 1)); + path_clean(clean_path); + + return clean_path; +} + +/* +Joins path parts together and allocates the resulting path. +Takes counter for the amount of path parts; path parts must be of char* type. +Calls path_clean on the appended parts. +Be sure to path_free() after use. +*/ +char* path_join(unsigned int count, ...) { + char* resulting_path = NULL; + const char* part = NULL; + unsigned long part_len = 0; + unsigned long current_len = 0; + va_list list; + unsigned int i = 0; + + va_start(list, count); + while (i < count) { + part = va_arg(list, const char*); + part_len = strlen(part); + resulting_path = realloc(resulting_path, current_len + part_len + 1); + for (unsigned long i = current_len; i < current_len+part_len; i++) { + resulting_path[i] = part[i-current_len]; + } + resulting_path[current_len+part_len] = PATH_SEPARATOR; + current_len += part_len + 1; + i++; + } + resulting_path[current_len] = '\0'; + va_end(list); + + path_clean(resulting_path); + + return resulting_path; +} + +// Frees path and sets pointer to NULL +void path_free(char* path) { + free(path); + path = NULL; +} + +/* +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) { + if (!path) { + return NULL; + } + + char* base_path = NULL; + char* clean_path = NULL; + size_t path_len; + size_t last_separator_index = 0; + + clean_path = path_clean_copy(path); + if (!clean_path) { + return NULL; + } + path_len = strlen(clean_path); + + if (path_len <= 1) { + return NULL; + } + + // find the last separator index in clean path + for (size_t i = 0; i < path_len; i++) { + if (clean_path[i] == PATH_SEPARATOR) { + last_separator_index = i; + } + } + + // allocate memory to hold up to last separator bytes + base_path = malloc(sizeof(char) * last_separator_index); + if (!base_path) { + return NULL; + } + + // copy over + memcpy(base_path, clean_path, sizeof(char) * last_separator_index); + base_path[last_separator_index] = '\0'; + + // free temporary cleaned path + path_free(clean_path); + + return base_path; +} \ No newline at end of file diff --git a/libpath.h b/libpath.h new file mode 100644 index 0000000..02dc943 --- /dev/null +++ b/libpath.h @@ -0,0 +1,41 @@ +/* +The MIT License (MIT) + +Copyright © 2023 Kasyanov Nikolay Alexeyevich (Unbewohnte) + +Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the “Software”), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. +*/ + +/* +Clears allocated path from duplicate separators. +Reallocates memory if needed. +*/ +void path_clean(char* path); + +/* +Clears allocated path from duplicate separators +and returns allocated clean path. +Be sure to path_free() after use. +*/ +char* path_clean_copy(const char* path); + +/* +Joins path parts together and allocates the resulting path. +Takes counter for the amount of path parts; path parts must be of char* type. +Calls path_clean on the appended parts. +Be sure to path_free() after use. +*/ +char* path_join(unsigned int count, ...); + +// Frees path and sets pointer to NULL +void path_free(char* path); + +/* +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); \ No newline at end of file diff --git a/test.c b/test.c new file mode 100644 index 0000000..76b55e8 --- /dev/null +++ b/test.c @@ -0,0 +1,34 @@ +/* +The MIT License (MIT) + +Copyright © 2023 Kasyanov Nikolay Alexeyevich (Unbewohnte) + +Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the “Software”), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. +*/ + +#include +#include +#include +#include "libpath.h" + +int main() { + char* path = path_join(4, "//here", "there", "/over/there_as_well", "file.txt"); + printf("Joined path: %s\n", path); + + char* parent = path_parent(path); + printf("Parent: %s\n", parent); + + path_free(path); + + char* parent_of_parent = path_parent(parent); + printf("Parent of parent: %s\n", parent_of_parent); + + path_free(parent); + path_free(parent_of_parent); + + return EXIT_SUCCESS; +} \ No newline at end of file