diff options
| author | Joseph Hunkeler <jhunkeler@gmail.com> | 2024-04-02 13:34:00 -0400 | 
|---|---|---|
| committer | Joseph Hunkeler <jhunkeler@gmail.com> | 2024-04-02 18:55:46 -0400 | 
| commit | cfdf0e333d240526436aff5f886a1da6a0959bce (patch) | |
| tree | a22071401f406723f38bdc4c825245e36bd61b73 /src | |
| parent | 9910a30f0c26b3180d46418abad760f86a253aa2 (diff) | |
| download | stasis-cfdf0e333d240526436aff5f886a1da6a0959bce.tar.gz | |
Changes to ini_write() behavior:
* Caller can choose to dump raw unmodified key/value pairs, or render all values through the template engine
* Fixes spurious line feeds injected into the output stream
* Uses LINE_SEP instead of "\n"
Diffstat (limited to 'src')
| -rw-r--r-- | src/ini.c | 27 | ||||
| -rw-r--r-- | src/utils.c | 2 | 
2 files changed, 21 insertions, 8 deletions
| @@ -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); | 
