From 1537a5228a40fb82b0a1d2a39297903ef4e0adb6 Mon Sep 17 00:00:00 2001 From: Gautier Pelloux-Prayer Date: Tue, 5 Apr 2016 10:26:33 +0200 Subject: [PATCH] lpconfig: add lp_config_dump --- coreapi/lpconfig.c | 39 +++++++++++++++++++++++++++++++++------ coreapi/lpconfig.h | 11 ++++++++++- 2 files changed, 43 insertions(+), 7 deletions(-) diff --git a/coreapi/lpconfig.c b/coreapi/lpconfig.c index 83bd411b7..cd036947e 100644 --- a/coreapi/lpconfig.c +++ b/coreapi/lpconfig.c @@ -923,26 +923,53 @@ const char** lp_config_get_sections_names(LpConfig *lpconfig) { const MSList *sections = lpconfig->sections; int ndev; int i; - + ndev = ms_list_size(sections); sections_names = ms_malloc((ndev + 1) * sizeof(const char *)); - + for (i = 0; sections != NULL; sections = sections->next, i++) { LpSection *section = (LpSection *)sections->data; sections_names[i] = ms_strdup(section->name); } - + sections_names[ndev] = NULL; return sections_names; } char* lp_config_dump_as_xml(const LpConfig *lpconfig) { char *buffer; - + lpc2xml_context *ctx = lpc2xml_context_new(NULL, NULL); lpc2xml_set_lpc(ctx, lpconfig); lpc2xml_convert_string(ctx, &buffer); lpc2xml_context_destroy(ctx); - + return buffer; -} \ No newline at end of file +} + +struct _entry_data { + const LpConfig *conf; + const char *section; + char** buffer; +}; + +static void dump_entry(const char *entry, void *data) { + struct _entry_data *d = (struct _entry_data *) data; + const char *value = lp_config_get_string(d->conf, d->section, entry, ""); + *d->buffer = ms_strcat_printf(*d->buffer, "\t%s=%s\n", entry, value); +} + +static void dump_section(const char *section, void *data) { + struct _entry_data *d = (struct _entry_data *) data; + d->section = section; + *d->buffer = ms_strcat_printf(*d->buffer, "[%s]\n", section); + lp_config_for_each_entry(d->conf, section, dump_entry, d); +} + +char* lp_config_dump(const LpConfig *lpconfig) { + char* buffer = NULL; + struct _entry_data d = { lpconfig, NULL, &buffer }; + lp_config_for_each_section(lpconfig, dump_section, &d); + + return buffer; +} diff --git a/coreapi/lpconfig.h b/coreapi/lpconfig.h index 49e2bb50d..c18af988a 100644 --- a/coreapi/lpconfig.h +++ b/coreapi/lpconfig.h @@ -306,11 +306,20 @@ LINPHONE_PUBLIC bool_t lp_config_relative_file_exists(const LpConfig *lpconfig, * Dumps the LpConfig as XML into a buffer * @param[in] lpconfig The LpConfig object * @return The buffer that contains the XML dump - * + * * @ingroup misc **/ LINPHONE_PUBLIC char* lp_config_dump_as_xml(const LpConfig *lpconfig); +/** + * Dumps the LpConfig as INI into a buffer + * @param[in] lpconfig The LpConfig object + * @return The buffer that contains the config dump + * + * @ingroup misc +**/ +LINPHONE_PUBLIC char* lp_config_dump(const LpConfig *lpconfig); + /** * Retrieves the overwrite flag for a config item *