]> git.webhop.me Git - lcd4linux.git/commitdiff
new function cfg_rename()
authormichael <michael@3ae390bd-cb1e-0410-b409-cd5a39f66f1f>
Thu, 15 Jan 2009 15:15:24 +0000 (15:15 +0000)
committermichael <michael@3ae390bd-cb1e-0410-b409-cd5a39f66f1f>
Thu, 15 Jan 2009 15:15:24 +0000 (15:15 +0000)
git-svn-id: https://ssl.bulix.org/svn/lcd4linux/trunk@967 3ae390bd-cb1e-0410-b409-cd5a39f66f1f

cfg.c
cfg.h

diff --git a/cfg.c b/cfg.c
index d712db9f133ab96c0b2397009f980ab0f82bce81..832b1965f81e00dce7200ba10e96aa83af223602 100644 (file)
--- a/cfg.c
+++ b/cfg.c
@@ -5,7 +5,7 @@
  * config file stuff
  *
  * Copyright (C) 1999, 2000 Michael Reinelt <michael@reinelt.co.at>
- * Copyright (C) 2004 The LCD4Linux Team <lcd4linux-devel@users.sourceforge.net>
+ * Copyright (C) 2004, 2009 The LCD4Linux Team <lcd4linux-devel@users.sourceforge.net>
  *
  * This file is part of LCD4Linux.
  *
@@ -48,6 +48,9 @@
  *   This list was allocated be cfg_list() and must be 
  *   freed by the caller!
  *
+ * cfg_rename (section, old, new)
+ *   changes the key of a existing entry
+ *
  * cfg_get_raw (section, key, defval) 
  *   return the a value for a given key in a given section 
  *   or <defval> if key does not exist. Does NOT evaluate
@@ -314,6 +317,58 @@ char *cfg_list(const char *section)
 }
 
 
+int cfg_rename(const char *section, const char *old, const char *new)
+{
+    char *buffer;
+    ENTRY *old_entry, *new_entry;
+
+    /* prepare old section.key */
+    buffer = malloc(strlen(section) + strlen(old) + 2);
+    *buffer = '\0';
+    if (section != NULL && *section != '\0') {
+       strcpy(buffer, section);
+       strcat(buffer, ".");
+    }
+    strcat(buffer, old);
+
+    /* lookup old entry */
+    old_entry = bsearch(buffer, Config, nConfig, sizeof(ENTRY), c_lookup);
+    free(buffer);
+
+    if (old_entry == NULL) {
+       error("internal error: cfg_rename(%s, %s, %s) failed: entry not found!", section, old, new);
+       return -1;
+    }
+
+    /* prepare new section.key */
+    buffer = malloc(strlen(section) + strlen(new) + 2);
+    *buffer = '\0';
+    if (section != NULL && *section != '\0') {
+       strcpy(buffer, section);
+       strcat(buffer, ".");
+    }
+    strcat(buffer, new);
+
+    /* lookup new entry */
+    new_entry = bsearch(buffer, Config, nConfig, sizeof(ENTRY), c_lookup);
+
+    if (new_entry != NULL) {
+       info("cfg_rename(%s, %s, %s) failed: entry already exists!", section, old, new);
+       free(buffer);
+       return -1;
+    }
+
+    /* replace key */
+    free(old_entry->key);
+    old_entry->key = buffer;
+
+    /* sort table again */
+    qsort(Config, nConfig, sizeof(ENTRY), c_sort);
+
+    return 0;
+}
+
+
 static char *cfg_lookup(const char *section, const char *key)
 {
     int len;
diff --git a/cfg.h b/cfg.h
index 2337439fd58994da9312439e02f0ac131524a96b..2c672edec9ee4bc44e3dbecc48677f2262d756f0 100644 (file)
--- a/cfg.h
+++ b/cfg.h
@@ -4,7 +4,7 @@
  * config file stuff
  *
  * Copyright (C) 1999, 2000 Michael Reinelt <michael@reinelt.co.at>
- * Copyright (C) 2004 The LCD4Linux Team <lcd4linux-devel@users.sourceforge.net>
+ * Copyright (C) 2004, 2009 The LCD4Linux Team <lcd4linux-devel@users.sourceforge.net>
  *
  * This file is part of LCD4Linux.
  *
@@ -31,6 +31,7 @@ int cfg_init(const char *file);
 char *cfg_source(void);
 int cfg_cmd(const char *arg);
 char *cfg_list(const char *section);
+int cfg_rename(const char *section, const char *old, const char *new);
 char *cfg_get_raw(const char *section, const char *key, const char *defval);
 char *cfg_get(const char *section, const char *key, const char *defval);
 int cfg_number(const char *section, const char *key, const int defval, const int min, const int max, int *value);