diff options
-rw-r--r-- | include/ini.h | 6 | ||||
-rw-r--r-- | src/ini.c | 27 | ||||
-rw-r--r-- | src/utils.c | 2 |
3 files changed, 26 insertions, 9 deletions
diff --git a/include/ini.h b/include/ini.h index 41692b6..af81895 100644 --- a/include/ini.h +++ b/include/ini.h @@ -5,6 +5,10 @@ #include <stddef.h> #include <stdbool.h> +#define INI_WRITE_RAW 0 ///< Dump INI data. Contents are not modified. +#define INI_WRITE_PRESERVE 1 ///< Dump INI data. Template strings are + ///< expanded to preserve runtime state. + #define INIVAL_TYPE_INT 1 ///< Integer #define INIVAL_TYPE_UINT 2 ///< Unsigned integer #define INIVAL_TYPE_LONG 3 ///< Long integer @@ -173,7 +177,7 @@ int ini_getval(struct INIFILE *ini, char *section_name, char *key, int type, uni * @param file pointer to address of file stream * @return 0 on success, -1 on error */ -int ini_write(struct INIFILE *ini, FILE **stream); +int ini_write(struct INIFILE *ini, FILE **stream, unsigned mode); /** * Free memory allocated by ini_open() @@ -210,31 +210,44 @@ int ini_section_create(struct INIFILE **ini, char *key) { return 0; } -int ini_write(struct INIFILE *ini, FILE **stream) { +int ini_write(struct INIFILE *ini, FILE **stream, unsigned mode) { if (!*stream) { return -1; } for (size_t x = 0; x < ini->section_count; x++) { - fprintf(*stream, "[%s]\n", ini->section[x]->key); + fprintf(*stream, "[%s]" LINE_SEP, ini->section[x]->key); for (size_t y = 0; y < ini->section[x]->data_count; y++) { char outvalue[OMC_BUFSIZ]; memset(outvalue, 0, sizeof(outvalue)); if (ini->section[x]->data[y]->value) { char **parts = split(ini->section[x]->data[y]->value, LINE_SEP, 0); + size_t parts_total = 0; + for (; parts && parts[parts_total] != NULL; parts_total++); for (size_t p = 0; parts && parts[p] != NULL; p++) { + char *render = NULL; + if (mode == INI_WRITE_PRESERVE) { + render = tpl_render(parts[p]); + } else { + render = parts[p]; + } if (p == 0) { - sprintf(outvalue, "%s\n", parts[p]); + sprintf(outvalue, "%s" LINE_SEP, render); } else { - sprintf(outvalue + strlen(outvalue), " %s\n", parts[p]); + sprintf(outvalue + strlen(outvalue), " %s" LINE_SEP, render); + } + if (mode == INI_WRITE_PRESERVE) { + guard_free(render); } } GENERIC_ARRAY_FREE(parts); - fprintf(*stream, "%s=%s\n", ini->section[x]->data[y]->key, outvalue); + strip(outvalue); + strcat(outvalue, LINE_SEP); + fprintf(*stream, "%s=%s%s", ini->section[x]->data[y]->key, parts_total > 1 ? LINE_SEP " " : "", outvalue); } else { - fprintf(*stream, "%s=%s\n", ini->section[x]->data[y]->key, ini->section[x]->data[y]->value); + fprintf(*stream, "%s=%s", ini->section[x]->data[y]->key, ini->section[x]->data[y]->value); } } - fprintf(*stream, "\n"); + fprintf(*stream, LINE_SEP); } return 0; } diff --git a/src/utils.c b/src/utils.c index d45f33a..402b62d 100644 --- a/src/utils.c +++ b/src/utils.c @@ -643,7 +643,7 @@ int fix_tox_conf(const char *filename, char **result) { } } - ini_write(toxini, &fptemp); + ini_write(toxini, &fptemp, INI_WRITE_RAW); fclose(fptemp); strcpy(*result, tempfile); |