#############################################################################*/
#include <errno.h>
+#include <limits.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
struct nw_config {
int nrefs;
+ // The path to the configuration file
+ char path[PATH_MAX];
+
STAILQ_HEAD(entries, nw_config_entry) entries;
};
free(config);
}
-int nw_config_create(struct nw_config** config) {
+int nw_config_create(struct nw_config** config, const char* path) {
+ int r;
+
struct nw_config* c = calloc(1, sizeof(*c));
if (!c)
return 1;
// Initialise entries
STAILQ_INIT(&c->entries);
+ // Store the path
+ if (path) {
+ r = nw_string_set(c->path, path);
+ if (r)
+ goto ERROR;
+
+ // Try to read the configuration from path
+ r = nw_config_read(c);
+ if (r)
+ goto ERROR;
+ }
+
*config = c;
return 0;
+
+ERROR:
+ nw_config_free(c);
+
+ return r;
}
struct nw_config* nw_config_ref(struct nw_config* config) {
return NULL;
}
+const char* nw_config_path(struct nw_config* config) {
+ if (*config->path)
+ return config->path;
+
+ return NULL;
+}
+
int nw_config_flush(struct nw_config* config) {
struct nw_config_entry* entry = NULL;
return 0;
}
-int nw_config_readf(struct nw_config* config, FILE* f) {
+static int nw_config_readf(struct nw_config* config, FILE* f) {
// XXX TODO
return 0;
}
-int nw_config_read(struct nw_config* config, const char* path) {
+int nw_config_read(struct nw_config* config) {
FILE* f = NULL;
int r;
+ // We cannot read if path is not set
+ if (!*config->path) {
+ errno = ENOTSUP;
+ return 1;
+ }
+
// Open the file
- f = fopen(path, "r");
+ f = fopen(config->path, "r");
if (!f) {
- ERROR("Could not read configuration file %s: %m\n", path);
+ // Silently ignore if the file does not exist
+ if (errno == ENOENT)
+ return 0;
+
+ ERROR("Could not read configuration file %s: %m\n", config->path);
r = 1;
goto ERROR;
}
return r;
}
-int nw_config_writef(struct nw_config* config, FILE* f) {
+static int nw_config_writef(struct nw_config* config, FILE* f) {
struct nw_config_entry* entry = NULL;
int r;
return 0;
}
-int nw_config_write(struct nw_config* config, const char* path) {
+int nw_config_write(struct nw_config* config) {
int r;
- FILE* f = fopen(path, "w");
+ // We cannot write if path is not set
+ if (!*config->path) {
+ errno = ENOTSUP;
+ return 1;
+ }
+
+ FILE* f = fopen(config->path, "w");
if (!f) {
- ERROR("Failed to open %s for writing: %m\n", path);
+ ERROR("Failed to open %s for writing: %m\n", config->path);
r = 1;
goto ERROR;
}
struct nw_config;
-int nw_config_create(struct nw_config** config);
+int nw_config_create(struct nw_config** config, const char* path);
struct nw_config* nw_config_ref(struct nw_config* config);
struct nw_config* nw_config_unref(struct nw_config* config);
-int nw_config_flush(struct nw_config* config);
+const char* nw_config_path(struct nw_config* config);
-int nw_config_readf(struct nw_config* config, FILE* f);
-int nw_config_read(struct nw_config* config, const char* path);
+int nw_config_flush(struct nw_config* config);
-int nw_config_writef(struct nw_config* config, FILE* f);
-int nw_config_write(struct nw_config* config, const char* path);
+int nw_config_read(struct nw_config* config);
+int nw_config_write(struct nw_config* config);
int nw_config_del(struct nw_config* config, const char* key);