diff options
-rw-r--r-- | include/ini.h | 13 | ||||
-rw-r--r-- | src/ini.c | 18 |
2 files changed, 31 insertions, 0 deletions
diff --git a/include/ini.h b/include/ini.h index af81895..a444665 100644 --- a/include/ini.h +++ b/include/ini.h @@ -7,6 +7,8 @@ #define INI_WRITE_RAW 0 ///< Dump INI data. Contents are not modified. #define INI_WRITE_PRESERVE 1 ///< Dump INI data. Template strings are +#define INI_SETVAL_APPEND 0 +#define INI_SETVAL_REPLACE 1 ///< expanded to preserve runtime state. #define INIVAL_TYPE_INT 1 ///< Integer @@ -88,6 +90,17 @@ struct INIFILE { struct INIFILE *ini_open(const char *filename); /** + * Assign value to a section key + * @param ini + * @param type INI_SETVAL_APPEND or INI_SETVAL_REPLACE + * @param section_name + * @param key + * @param value + * @return + */ +int ini_setval(struct INIFILE **ini, unsigned type, char *section_name, char *key, char *value); + +/** * Retrieve all data records in an INI section * * `example.ini` @@ -188,6 +188,24 @@ int ini_data_append(struct INIFILE **ini, char *section_name, char *key, char *v return 0; } +int ini_setval(struct INIFILE **ini, unsigned type, char *section_name, char *key, char *value) { + struct INISection *section = ini_section_search(ini, INI_SEARCH_EXACT, section_name); + if (section == NULL) { + return 1; + } + if (ini_has_key(*ini, section_name, key)) { + if (!type) { + ini_data_append(ini, section_name, key, value); + } else { + struct INIData *data = ini_data_get(*ini, section_name, key); + if (data) { + guard_free(data->value); + data->value = strdup(value); + } + } + } +} + int ini_section_create(struct INIFILE **ini, char *key) { struct INISection **tmp = realloc((*ini)->section, ((*ini)->section_count + 1) * sizeof(**(*ini)->section)); if (!tmp) { |