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] = "";
}
// 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';
}
// 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);
*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);
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);
# #
#############################################################################*/
+#include <ctype.h>
#include <errno.h>
#include <stdarg.h>
#include <stdio.h>
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)