diff options
| author | Joseph Hunkeler <jhunkeler@gmail.com> | 2024-03-14 15:40:12 -0400 | 
|---|---|---|
| committer | Joseph Hunkeler <jhunkeler@gmail.com> | 2024-03-14 15:40:12 -0400 | 
| commit | 5f50ddc00a48bb14122164a6c3b514ba8b09a321 (patch) | |
| tree | 2cf2f2e4f3454590491050438119a83d69e461b5 | |
| parent | 914ddc68511be45c8de9fa7f7e4e468fed05b88d (diff) | |
| download | stasis-5f50ddc00a48bb14122164a6c3b514ba8b09a321.tar.gz | |
Replace ini_show with ini_write
* One can still display the ini configuration by using ini_show(iniptr, stdout);
| -rw-r--r-- | include/ini.h | 6 | ||||
| -rw-r--r-- | src/ini.c | 27 | 
2 files changed, 27 insertions, 6 deletions
| diff --git a/include/ini.h b/include/ini.h index 1391555..41692b6 100644 --- a/include/ini.h +++ b/include/ini.h @@ -168,10 +168,12 @@ struct INIData *ini_getall(struct INIFILE *ini, char *section_name);  int ini_getval(struct INIFILE *ini, char *section_name, char *key, int type, union INIVal *result);  /** - * Print INIFILE sections and data + * Write INIFILE sections and data to a file stream   * @param ini pointer to INIFILE + * @param file pointer to address of file stream + * @return 0 on success, -1 on error   */ -void ini_show(struct INIFILE *ini); +int ini_write(struct INIFILE *ini, FILE **stream);  /**   * Free memory allocated by ini_open() @@ -191,14 +191,33 @@ int ini_section_create(struct INIFILE **ini, char *key) {      return 0;  } -void ini_show(struct INIFILE *ini) { +int ini_write(struct INIFILE *ini, FILE **stream) { +    if (!*stream) { +        return -1; +    }      for (size_t x = 0; x < ini->section_count; x++) { -        printf("[%s]\n", ini->section[x]->key); +        fprintf(*stream, "[%s]\n", ini->section[x]->key);          for (size_t y = 0; y < ini->section[x]->data_count; y++) { -            printf("%s='%s'\n", ini->section[x]->data[y]->key, ini->section[x]->data[y]->value); +            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); +                for (size_t p = 0; parts && parts[p] != NULL; p++) { +                    if (p == 0) { +                        sprintf(outvalue, "%s\n", parts[p]); +                    } else { +                        sprintf(outvalue + strlen(outvalue), "    %s\n", parts[p]); +                    } +                } +                split_free(parts); +                fprintf(*stream, "%s=%s\n", ini->section[x]->data[y]->key, outvalue); +            } else { +                fprintf(*stream, "%s=%s\n", ini->section[x]->data[y]->key, ini->section[x]->data[y]->value); +            }          } -        printf("\n"); +        fprintf(*stream, "\n");      } +    return 0;  }  char *unquote(char *s) { | 
