From: Michael Tremer Date: Tue, 26 Sep 2023 17:48:56 +0000 (+0000) Subject: config: Move the strip functions into the string library X-Git-Tag: 0.9.30~1631 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=cb231b4003a1042abcd850737f173e9d5118a097;p=pakfire.git config: Move the strip functions into the string library Signed-off-by: Michael Tremer --- diff --git a/src/libpakfire/config.c b/src/libpakfire/config.c index cffa34c40..4141b186d 100644 --- a/src/libpakfire/config.c +++ b/src/libpakfire/config.c @@ -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); diff --git a/src/libpakfire/include/pakfire/string.h b/src/libpakfire/include/pakfire/string.h index b650895d6..540912489 100644 --- a/src/libpakfire/include/pakfire/string.h +++ b/src/libpakfire/include/pakfire/string.h @@ -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); diff --git a/src/libpakfire/string.c b/src/libpakfire/string.c index 3edebef5c..f4991c8ed 100644 --- a/src/libpakfire/string.c +++ b/src/libpakfire/string.c @@ -18,6 +18,7 @@ # # #############################################################################*/ +#include #include #include #include @@ -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)