From f999712c5b2550366251b74c2ff8e45fc9b07276 Mon Sep 17 00:00:00 2001 From: Sylvain Berfini Date: Wed, 21 Oct 2015 11:58:47 +0200 Subject: [PATCH] Added new functions in lpconfig to dump xml and get a list of sections' names --- coreapi/lpconfig.c | 24 +++++++++++++++++++++++- coreapi/lpconfig.h | 20 ++++++++++++++++++++ tools/lpc2xml_test.c | 16 +++++++++------- 3 files changed, 52 insertions(+), 8 deletions(-) diff --git a/coreapi/lpconfig.c b/coreapi/lpconfig.c index 607a5c763..b4b3dc7f0 100644 --- a/coreapi/lpconfig.c +++ b/coreapi/lpconfig.c @@ -56,7 +56,7 @@ #define lp_new0(type,n) (type*)calloc(sizeof(type),n) #include "lpconfig.h" - +#include "lpc2xml.h" typedef struct _LpItem{ char *key; @@ -877,3 +877,25 @@ err: if(realfilepath) ms_free(realfilepath); return -1; } + +static void get_sections_names_cb(const char *section, void *ctx) { + MSList **list = (MSList**)ctx; + *list = ms_list_append(*list, (void*)ms_strdup(section)); +} + +const MSList* lp_config_get_sections_names(LpConfig *lpconfig) { + MSList *result = NULL; + lp_config_for_each_section(lpconfig, get_sections_names_cb, &result); + return result; +} + +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 diff --git a/coreapi/lpconfig.h b/coreapi/lpconfig.h index b03bfa57f..aa49a4cf9 100644 --- a/coreapi/lpconfig.h +++ b/coreapi/lpconfig.h @@ -200,6 +200,17 @@ LINPHONE_PUBLIC int lp_config_has_section(const LpConfig *lpconfig, const char * **/ LINPHONE_PUBLIC void lp_config_clean_section(LpConfig *lpconfig, const char *section); +/** + * Returns the list of the sections' names in the config. + * @param[in] lpconfig The LpConfig object + * @return \mslist{const char*} + * + * This list is unmodifiable. The ->data field of the MSList points to a const char*. + * + * @ingroup misc +**/ +LINPHONE_PUBLIC const MSList * lp_config_get_sections_names(LpConfig *lpconfig); + /** * Call a function for each section present in the configuration. * @@ -295,6 +306,15 @@ LINPHONE_PUBLIC int lp_config_read_relative_file(const LpConfig *lpconfig, const **/ LINPHONE_PUBLIC bool_t lp_config_relative_file_exists(const LpConfig *lpconfig, const char *filename); +/** + * 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); + #ifdef __cplusplus } #endif diff --git a/tools/lpc2xml_test.c b/tools/lpc2xml_test.c index 03ac571cf..b52ecbb51 100644 --- a/tools/lpc2xml_test.c +++ b/tools/lpc2xml_test.c @@ -42,28 +42,30 @@ void cb_function(void *ctx, lpc2xml_log_level level, const char *msg, va_list li } void show_usage(int argc, char *argv[]) { - fprintf(stderr, "usage %s convert \n", - argv[0]); + fprintf(stderr, "usage:\n%s convert \n%s dump \n", argv[0], argv[0]); } int main(int argc, char *argv[]) { lpc2xml_context *ctx; LpConfig *lpc; - if(argc != 4) { + if(argc > 4 || argc < 3) { show_usage(argc, argv); return -1; } - ctx = lpc2xml_context_new(cb_function, NULL); lpc = lp_config_new(argv[2]); - lpc2xml_set_lpc(ctx, lpc); - if(strcmp("convert", argv[1]) == 0) { + if(strcmp("convert", argv[1]) == 0 && argc == 4) { + ctx = lpc2xml_context_new(cb_function, NULL); lpc2xml_convert_file(ctx, argv[3]); + lpc2xml_set_lpc(ctx, lpc); + lpc2xml_context_destroy(ctx); + } else if (strcmp("dump", argv[1]) == 0 && argc == 3) { + char *dump = lp_config_dump_as_xml(lpc); + fprintf(stdout, "%s", dump); } else { show_usage(argc, argv); } lp_config_destroy(lpc); - lpc2xml_context_destroy(ctx); return 0; }