From 09b443b2ca25fde88ce523d83fd6f5cd6367a6d4 Mon Sep 17 00:00:00 2001 From: Julian Hurst Date: Wed, 28 Aug 2019 18:27:34 +0200 Subject: [PATCH 1/2] Add support for loading string/custom streams --- src/iniparser.c | 47 +++++++++++++++++++++++++++++++++++------------ src/iniparser.h | 2 ++ 2 files changed, 37 insertions(+), 12 deletions(-) diff --git a/src/iniparser.c b/src/iniparser.c index f1d1658..989f882 100644 --- a/src/iniparser.c +++ b/src/iniparser.c @@ -700,21 +700,19 @@ static line_status iniparser_line( /*-------------------------------------------------------------------------*/ /** @brief Parse an ini file and return an allocated dictionary object - @param ininame Name of the ini file to read. + @param in File to read. + @param ininame Name of the ini file to read (only used for nicer error messages) @return Pointer to newly allocated dictionary This is the parser for ini files. This function is called, providing - the name of the file to be read. It returns a dictionary object that - should not be accessed directly, but through accessor functions - instead. + the file to be read. It returns a dictionary object that should not + be accessed directly, but through accessor functions instead. The returned dictionary must be freed using iniparser_freedict(). */ /*--------------------------------------------------------------------------*/ -dictionary * iniparser_load(const char * ininame) +dictionary * iniparser_load_file(FILE * in, const char * ininame) { - FILE * in ; - char line [ASCIILINESZ+1] ; char section [ASCIILINESZ+1] ; char key [ASCIILINESZ+1] ; @@ -729,11 +727,6 @@ dictionary * iniparser_load(const char * ininame) dictionary * dict ; - if ((in=fopen(ininame, "r"))==NULL) { - iniparser_error_callback("iniparser: cannot open %s\n", ininame); - return NULL ; - } - dict = dictionary_new(0) ; if (!dict) { fclose(in); @@ -819,6 +812,36 @@ dictionary * iniparser_load(const char * ininame) return dict ; } +/*-------------------------------------------------------------------------*/ +/** + @brief Parse an ini file and return an allocated dictionary object + @param ininame Name of the ini file to read. + @return Pointer to newly allocated dictionary + + This is the parser for ini files. This function is called, providing + the name of the file to be read. It returns a dictionary object that + should not be accessed directly, but through accessor functions + instead. + + The returned dictionary must be freed using iniparser_freedict(). + */ +/*--------------------------------------------------------------------------*/ +dictionary * iniparser_load(const char * ininame) +{ + FILE * in ; + dictionary * dict ; + + if ((in=fopen(ininame, "r"))==NULL) { + iniparser_error_callback("iniparser: cannot open %s\n", ininame); + return NULL ; + } + + dict = iniparser_load_file(in, ininame); + + return dict ; +} + + /*-------------------------------------------------------------------------*/ /** @brief Free all memory associated to an ini dictionary diff --git a/src/iniparser.h b/src/iniparser.h index 37ff7b7..014e086 100644 --- a/src/iniparser.h +++ b/src/iniparser.h @@ -338,6 +338,8 @@ int iniparser_find_entry(const dictionary * ini, const char * entry) ; /*--------------------------------------------------------------------------*/ dictionary * iniparser_load(const char * ininame); +dictionary * iniparser_load_file(FILE * in, const char * ininame); + /*-------------------------------------------------------------------------*/ /** @brief Free all memory associated to an ini dictionary From e98be48e9b5d9cca0eae1adac0bc4bb48dc0a44f Mon Sep 17 00:00:00 2001 From: Julian Hurst Date: Wed, 28 Aug 2019 18:45:56 +0200 Subject: [PATCH 2/2] Add function description to header --- src/iniparser.h | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/src/iniparser.h b/src/iniparser.h index 014e086..b02b3f5 100644 --- a/src/iniparser.h +++ b/src/iniparser.h @@ -338,6 +338,20 @@ int iniparser_find_entry(const dictionary * ini, const char * entry) ; /*--------------------------------------------------------------------------*/ dictionary * iniparser_load(const char * ininame); +/*-------------------------------------------------------------------------*/ +/** + @brief Parse an ini file and return an allocated dictionary object + @param ininame File to read. + @param ininame Name of the ini file to read (only used for nicer error messages) + @return Pointer to newly allocated dictionary + + This is the parser for ini files. This function is called, providing + the file to be read. It returns a dictionary object that should not + be accessed directly, but through accessor functions instead. + + The returned dictionary must be freed using iniparser_freedict(). + */ +/*--------------------------------------------------------------------------*/ dictionary * iniparser_load_file(FILE * in, const char * ininame); /*-------------------------------------------------------------------------*/