]> git.ipfire.org Git - pakfire.git/commitdiff
path: Move function to replace the file extension
authorMichael Tremer <michael.tremer@ipfire.org>
Sat, 26 Oct 2024 12:21:18 +0000 (12:21 +0000)
committerMichael Tremer <michael.tremer@ipfire.org>
Sat, 26 Oct 2024 12:21:18 +0000 (12:21 +0000)
Signed-off-by: Michael Tremer <michael.tremer@ipfire.org>
src/libpakfire/include/pakfire/path.h
src/libpakfire/include/pakfire/util.h
src/libpakfire/path.c
src/libpakfire/util.c

index 0e2693adfade3377d96c2ce867c27536be718791..a8d1339ace798082cf57c8c4d3d5c0a7a34605bc 100644 (file)
@@ -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 */
index befce026f4e8a5592d1a8cb3373ad7dc6db41fc6..16c33bb7fc0c8ef0da9f1eacd164b18ed41d0ca3 100644 (file)
@@ -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);
index 7331bb24559445911728066f280aff2ffda88f53..5e820aa8a784a022f22b410b1b7eea91ef6484e3 100644 (file)
@@ -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);
+}
index c6791e35501a12d9086ada3adef8a4f84ca9d7e8..0c709d731a1c01731638c458ab267d8c90158de3 100644 (file)
@@ -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;