summaryrefslogtreecommitdiff
path: root/conf.c
diff options
context:
space:
mode:
authorGravatar Alexey Yakovenko <wakeroid@gmail.com>2009-10-11 11:43:50 +0200
committerGravatar Alexey Yakovenko <wakeroid@gmail.com>2009-10-11 11:43:50 +0200
commit9425bbc1716081cff72c73744a4fc82251b9db41 (patch)
tree0588a4d97196009229e946bbe262b0ab5828da31 /conf.c
parent22838720bbe1fc3c9fd46f34aa91474c7d6005a3 (diff)
added configuration autosaving at exit
Diffstat (limited to 'conf.c')
-rw-r--r--conf.c46
1 files changed, 45 insertions, 1 deletions
diff --git a/conf.c b/conf.c
index 70ceeae5..f45eabd5 100644
--- a/conf.c
+++ b/conf.c
@@ -22,6 +22,7 @@
#include "conf.h"
static DB_conf_item_t *conf_items;
+static int changed = 0;
int
conf_load (void) {
@@ -34,6 +35,7 @@ conf_load (void) {
return -1;
}
int line = 0;
+ DB_conf_item_t *tail = NULL;
while (fgets (str, 1024, fp) != NULL) {
line++;
if (str[0] == '#' || str[0] <= 0x20) {
@@ -63,19 +65,50 @@ conf_load (void) {
p++;
}
*p = 0;
- conf_set_str (str, value);
+ // new items are appended, to preserve order
+ DB_conf_item_t *it = malloc (sizeof (DB_conf_item_t));
+ memset (it, 0, sizeof (DB_conf_item_t));
+ it->key = strdup (str);
+ it->value = strdup (value);
+ if (!tail) {
+ conf_items = it;
+ }
+ else {
+ tail->next = it;
+ }
+ tail = it;
}
fclose (fp);
+ changed = 0;
return 0;
}
int
conf_save (void) {
+ extern char dbconfdir[1024]; // $HOME/.config/deadbeef
+ char str[1024];
+ snprintf (str, 1024, "%s/config", dbconfdir);
+ FILE *fp = fopen (str, "w+t");
+ if (!fp) {
+ fprintf (stderr, "failed to open config file for writing\n");
+ return -1;
+ }
+ for (DB_conf_item_t *it = conf_items; it; it = it->next) {
+ fprintf (fp, "%s %s\n", it->key, it->value);
+ }
+ fclose (fp);
return 0;
}
void
conf_free (void) {
+ DB_conf_item_t *next = NULL;
+ for (DB_conf_item_t *it = conf_items; it; it = next) {
+ next = it->next;
+ free (it->key);
+ free (it->value);
+ free (it);
+ }
}
const char *
@@ -113,6 +146,7 @@ conf_find (const char *group, DB_conf_item_t *prev) {
void
conf_set_str (const char *key, const char *val) {
+ changed = 1;
for (DB_conf_item_t *it = conf_items; it; it = it->next) {
if (!strcasecmp (key, it->key)) {
free (it->value);
@@ -141,3 +175,13 @@ conf_set_float (const char *key, float val) {
snprintf (s, sizeof (s), "%0.7f", val);
conf_set_str (key, s);
}
+
+int
+conf_ischanged (void) {
+ return changed;
+}
+
+void
+conf_setchanged (int c) {
+ changed = c;
+}