From: Michael Tremer Date: Sat, 26 Oct 2024 12:21:18 +0000 (+0000) Subject: path: Move function to replace the file extension X-Git-Tag: 0.9.30~842 X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=19f687bd9afd708e8e503e59ab71e165c366e41a;p=pakfire.git path: Move function to replace the file extension Signed-off-by: Michael Tremer --- diff --git a/src/libpakfire/include/pakfire/path.h b/src/libpakfire/include/pakfire/path.h index 0e2693adf..a8d1339ac 100644 --- a/src/libpakfire/include/pakfire/path.h +++ b/src/libpakfire/include/pakfire/path.h @@ -55,4 +55,8 @@ int __pakfire_path_absolute(char* buffer, const size_t length, const char* s); int pakfire_path_match(const char* p, const char* s); +#define pakfire_path_replace_extension(path, extension) \ + __pakfire_path_replace_extension(path, sizeof(path), extension) +int __pakfire_path_replace_extension(char* path, const size_t length, const char* extension); + #endif /* PAKFIRE_PATH_H */ diff --git a/src/libpakfire/include/pakfire/util.h b/src/libpakfire/include/pakfire/util.h index befce026f..16c33bb7f 100644 --- a/src/libpakfire/include/pakfire/util.h +++ b/src/libpakfire/include/pakfire/util.h @@ -52,12 +52,6 @@ static inline void* pakfire_realloc(void* p, size_t size) { int pakfire_path_exists(const char* path); time_t pakfire_path_age(const char* path); -int pakfire_path_strip_extension(char* path); - -#define pakfire_path_replace_extension(path, extension) \ - __pakfire_path_replace_extension(path, sizeof(path), extension) -int __pakfire_path_replace_extension(char* path, const size_t length, const char* extension); - char* pakfire_remove_trailing_newline(char* str); int pakfire_read_file_into_buffer(FILE* f, char** buffer, size_t* len); diff --git a/src/libpakfire/path.c b/src/libpakfire/path.c index 7331bb245..5e820aa8a 100644 --- a/src/libpakfire/path.c +++ b/src/libpakfire/path.c @@ -633,3 +633,37 @@ int pakfire_path_match(const char* p, const char* s) { // We reached the end of the string and all characters matched return 1; } + +static int pakfire_path_strip_extension(char* path) { + if (!path) + return -EINVAL; + + // Find the extension + char* ext = strrchr(path, '.'); + + // If . could not be found, we return an error. + if (!ext) + return 1; + + // Otherwise, we will terminate the string + *ext = '\0'; + return 0; +} + +int __pakfire_path_replace_extension(char* path, const size_t length, const char* extension) { + char buffer[PATH_MAX]; + int r; + + // Copy path to buffer + r = pakfire_string_set(buffer, path); + if (r < 0) + return r; + + // Strip any old extension + r = pakfire_path_strip_extension(buffer); + if (r) + return r; + + // Compose the new string + return __pakfire_string_format(path, length, "%s.%s", buffer, extension); +} diff --git a/src/libpakfire/util.c b/src/libpakfire/util.c index c6791e355..0c709d731 100644 --- a/src/libpakfire/util.c +++ b/src/libpakfire/util.c @@ -98,37 +98,6 @@ time_t pakfire_path_age(const char* path) { return -1; } -int pakfire_path_strip_extension(char* path) { - char* ext = strrchr(path, '.'); - - // If . could not be found, we return an error. - if (!ext) - return 1; - - // Otherwise, we will terminate the string - *ext = '\0'; - - return 0; -} - -int __pakfire_path_replace_extension(char* path, const size_t length, const char* extension) { - char buffer[PATH_MAX]; - int r; - - // Copy path to buffer - r = pakfire_string_set(buffer, path); - if (r) - return r; - - // Strip any old extension - r = pakfire_path_strip_extension(buffer); - if (r) - return r; - - // Compose the new string - return __pakfire_string_format(path, length, "%s.%s", buffer, extension); -} - int pakfire_file_write(struct pakfire* pakfire, const char* path, uid_t owner, gid_t group, mode_t mode, const char* format, ...) { va_list args;