diff options
Diffstat (limited to '')
-rw-r--r-- | cfg.c | 57 | ||||
-rw-r--r-- | cfg.h | 3 |
2 files changed, 58 insertions, 2 deletions
@@ -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; @@ -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); |