aboutsummaryrefslogtreecommitdiff
path: root/src/ini.c
diff options
context:
space:
mode:
authorJoseph Hunkeler <jhunkeler@gmail.com>2024-08-05 11:59:45 -0400
committerJoseph Hunkeler <jhunkeler@gmail.com>2024-08-05 11:59:45 -0400
commit1524075828995e14fef035ff5884134997c7479a (patch)
tree0ece450ec17839a26d5c564b753e291cf774aa40 /src/ini.c
parent202e69c8951a38187489c66e994dd593755d62cb (diff)
downloadstasis-1524075828995e14fef035ff5884134997c7479a.tar.gz
Add handler for space-delimited lists
* This needs attention, however. The INI writer has no way to know a list with spaces is a list; this happens in the value conversion functions. * Add type_hint member to INIData structure. At some point support with be added for all INIVAL_TYPE_* defines. Right now it's only used with arrays. * Zero out line buffer in ini_open after each iteration * Do not strip raw INI data. Let the conversion functions handle it * Add spaces to key value pairs in rendered INI output.
Diffstat (limited to 'src/ini.c')
-rw-r--r--src/ini.c24
1 files changed, 19 insertions, 5 deletions
diff --git a/src/ini.c b/src/ini.c
index 6ce54ac..9f3b8f7 100644
--- a/src/ini.c
+++ b/src/ini.c
@@ -292,13 +292,18 @@ int ini_write(struct INIFILE *ini, FILE **stream, unsigned mode) {
char *render = NULL;
if (mode == INI_WRITE_PRESERVE) {
render = tpl_render(parts[p]);
+ replace_text(render, "\n", "\n ", 0);
} else {
render = parts[p];
}
if (p == 0) {
sprintf(outvalue, "%s" LINE_SEP, render);
} else {
- sprintf(outvalue + strlen(outvalue), " %s" LINE_SEP, render);
+ if (!isspace(render[0])) {
+ sprintf(outvalue + strlen(outvalue), " %s" LINE_SEP, render);
+ } else {
+ sprintf(outvalue + strlen(outvalue), "%s" LINE_SEP, render);
+ }
}
if (mode == INI_WRITE_PRESERVE) {
guard_free(render);
@@ -307,9 +312,9 @@ int ini_write(struct INIFILE *ini, FILE **stream, unsigned mode) {
GENERIC_ARRAY_FREE(parts);
strip(outvalue);
strcat(outvalue, LINE_SEP);
- fprintf(*stream, "%s=%s%s", ini->section[x]->data[y]->key, parts_total > 1 ? LINE_SEP " " : "", outvalue);
+ fprintf(*stream, "%s = %s%s", ini->section[x]->data[y]->key, ini->section[x]->data[y]->type_hint || parts_total > 1 ? LINE_SEP " " : "", outvalue);
} else {
- fprintf(*stream, "%s=%s", 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, LINE_SEP);
@@ -352,6 +357,13 @@ void ini_free(struct INIFILE **ini) {
guard_free((*ini));
}
+static void ini_data_set_hint(struct INIFILE **ini, char *section_name, char *key, int hint) {
+ struct INIData *data = ini_data_get(*ini, section_name, key);
+ if (data) {
+ data->type_hint = hint;
+ }
+}
+
struct INIFILE *ini_open(const char *filename) {
FILE *fp;
char line[STASIS_BUFSIZ] = {0};
@@ -440,8 +452,10 @@ struct INIFILE *ini_open(const char *filename) {
ini_section_create(&ini, section_name);
// Record the name of the section. This is used until another section is found.
+ memset(current_section, 0, sizeof(current_section));
strcpy(current_section, section_name);
guard_free(section_name);
+ memset(line, 0, sizeof(line));
continue;
}
@@ -473,6 +487,7 @@ struct INIFILE *ini_open(const char *filename) {
}
if (isempty(value)) {
//printf("%s is probably long raw data\n", key);
+ ini_data_set_hint(&ini, current_section, key, INIVAL_TYPE_STR_ARRAY);
multiline_data = 1;
no_data = 1;
} else {
@@ -484,15 +499,14 @@ struct INIFILE *ini_open(const char *filename) {
strcpy(key, key_last);
strcpy(value, line);
}
+ memset(line, 0, sizeof(line));
// Store key value pair in section's data array
if (strlen(key)) {
lstrip(key);
strip(key);
unquote(value);
- lstrip(value);
if (!multiline_data) {
- strip(value);
reading_value = 0;
ini_data_append(&ini, current_section, key, value);
continue;