}
static int nw_config_readf(struct nw_config* config, FILE* f) {
- // XXX TODO
+ char* line = NULL;
+ size_t length = 0;
+ int r;
- return 0;
+ ssize_t bytes_read = 0;
+
+ char* key = NULL;
+ char* val = NULL;
+
+ for (;;) {
+ // Read the next line
+ bytes_read = getline(&line, &length, f);
+ if (bytes_read < 0)
+ break;
+
+ // Key starts at the beginning of the line
+ key = line;
+
+ // Value starts after '='
+ val = strchr(line, '=');
+
+ // Invalid line without a '=' character
+ if (!val)
+ continue;
+
+ // Split the string
+ *val++ = '\0';
+
+ // Strip any whitespace from value
+ r = nw_string_strip(val);
+ if (r)
+ break;
+
+ // Store the setting
+ r = nw_config_set(config, key, val);
+ if (r)
+ break;
+ }
+
+ if (line)
+ free(line);
+
+ return r;
}
int nw_config_read(struct nw_config* config) {
continue;
// Write the entry
- r = fprintf(f, "%s=\"%s\"\n", entry->key, entry->value);
+ r = fprintf(f, "%s=%s\n", entry->key, entry->value);
if (r < 0) {
ERROR("Failed to write configuration: %m\n");
return r;
#ifndef NETWORKD_STRING_H
#define NETWORKD_STRING_H
+#include <ctype.h>
#include <errno.h>
#include <stdarg.h>
#include <stdio.h>
return __nw_string_format(s, length, "%s", value);
}
+static inline int nw_string_lstrip(char* s) {
+ char* p = s;
+
+ // Count any leading spaces
+ while (*p && isspace(*p))
+ p++;
+
+ // Move the string to the beginning of the buffer
+ while (*p)
+ *s++ = *p++;
+
+ // Terminate the string
+ *s = '\0';
+
+ return 0;
+}
+
+static inline int nw_string_rstrip(char* s) {
+ ssize_t l = strlen(s) - 1;
+
+ while (l >= 0 && isspace(s[l]))
+ s[l--] = '\0';
+
+ return 0;
+}
+
+static inline int nw_string_strip(char* s) {
+ int r;
+
+ r = nw_string_lstrip(s);
+ if (r)
+ return r;
+
+ r = nw_string_rstrip(s);
+ if (r)
+ return r;
+
+ return 0;
+}
+
/*
Paths
*/