]> git.ipfire.org Git - pakfire.git/commitdiff
config: Move the strip functions into the string library
authorMichael Tremer <michael.tremer@ipfire.org>
Tue, 26 Sep 2023 17:48:56 +0000 (17:48 +0000)
committerMichael Tremer <michael.tremer@ipfire.org>
Tue, 26 Sep 2023 17:58:01 +0000 (17:58 +0000)
Signed-off-by: Michael Tremer <michael.tremer@ipfire.org>
src/libpakfire/config.c
src/libpakfire/include/pakfire/string.h
src/libpakfire/string.c

index cffa34c40e4223b2fd9cdbbe5d7fd17bdfd31d20..4141b186d75e090dbea57d3f4140b3eb5da76a93 100644 (file)
@@ -421,47 +421,6 @@ int pakfire_config_has_section(struct pakfire_config* config, const char* name)
        return 0;
 }
 
-static ssize_t lstrip(char* s) {
-       if (!s)
-               return 0;
-
-       size_t l = strlen(s);
-       if (!l)
-               return 0;
-
-       // Remove leading space
-       while (isspace(s[0]))
-               memmove(s, s + 1, l--);
-
-       return l;
-}
-
-static ssize_t rstrip(char* s) {
-       if (!s)
-               return 0;
-
-       size_t l = strlen(s);
-
-       // Remove trailing space
-       while (l > 0 && isspace(s[l - 1]))
-               s[l-- - 1] = '\0';
-
-       return l;
-}
-
-static ssize_t strip(char* s) {
-       ssize_t l;
-
-       // Strip everything on the left side
-       l = lstrip(s);
-
-       // Strip everything on the right side
-       if (l)
-               l = rstrip(s);
-
-       return l;
-}
-
 int pakfire_config_read(struct pakfire_config* config, FILE* f) {
        char section[SECTION_MAX_LENGTH] = "";
        char key[KEY_MAX_LENGTH] = "";
@@ -487,12 +446,15 @@ int pakfire_config_read(struct pakfire_config* config, FILE* f) {
                }
 
                // Remove trailing space
-               length = rstrip(line);
+               pakfire_string_rstrip(line);
 
                // Skip empty lines
                if (*line == '\0')
                        continue;
 
+               // Update length after rstrip()
+               length = strlen(line);
+
                // Is this a new section?
                if (line[0] == '[' && line[length - 1] == ']') {
                        line[length - 1] = '\0';
@@ -516,7 +478,7 @@ int pakfire_config_read(struct pakfire_config* config, FILE* f) {
                        }
 
                        // Strip any leading whitespace
-                       lstrip(line);
+                       pakfire_string_lstrip(line);
 
                        // Append the value to the existing one
                        r = pakfire_config_append(config, section, key, line);
@@ -538,8 +500,8 @@ int pakfire_config_read(struct pakfire_config* config, FILE* f) {
                *value++ = '\0';
 
                // Remove trailing space
-               strip(line);
-               strip(value);
+               pakfire_string_strip(line);
+               pakfire_string_strip(value);
 
                // Update the key
                pakfire_string_set(key, line);
index b650895d61b3ceaf5be6cadc416033493a9936df..54091248944a66bcc51b9b0c8a1acdc701f5bf42 100644 (file)
@@ -46,6 +46,11 @@ int pakfire_string_startswith(const char* s, const char* prefix);
 int pakfire_string_endswith(const char* s, const char* suffix);
 int pakfire_string_matches(const char* s, const char* pattern);
 
+// Strip
+void pakfire_string_lstrip(char* s);
+void pakfire_string_rstrip(char* s);
+void pakfire_string_strip(char* s);
+
 int pakfire_string_partition(const char* s, const char* delim, char** s1, char** s2);
 char* pakfire_string_replace(const char* s, const char* pattern, const char* repl);
 char** pakfire_string_split(const char* s, char delim);
index 3edebef5c3a39177ef7982a4b0a3d29029df3d1d..f4991c8ed9d47aff68cb16474ddea83244544658 100644 (file)
@@ -18,6 +18,7 @@
 #                                                                             #
 #############################################################################*/
 
+#include <ctype.h>
 #include <errno.h>
 #include <stdarg.h>
 #include <stdio.h>
@@ -85,6 +86,38 @@ int pakfire_string_endswith(const char* s, const char* suffix) {
        return !strcmp(s + strlen(s) - strlen(suffix), suffix);
 }
 
+void pakfire_string_lstrip(char* s) {
+       if (!s)
+               return;
+
+       size_t l = strlen(s);
+       if (!l)
+               return;
+
+       // Remove leading space
+       while (isspace(s[0]))
+               memmove(s, s + 1, l--);
+}
+
+void pakfire_string_rstrip(char* s) {
+       if (!s)
+               return;
+
+       size_t l = strlen(s);
+
+       // Remove trailing space
+       while (l > 0 && isspace(s[l - 1]))
+               s[l-- - 1] = '\0';
+}
+
+void pakfire_string_strip(char* s) {
+       // Strip everything on the left side
+       pakfire_string_lstrip(s);
+
+       // Strip everything on the right side
+       pakfire_string_rstrip(s);
+}
+
 int pakfire_string_matches(const char* s, const char* pattern) {
        // Validate input
        if (!s || !pattern)