Skip to content

Commit cbd3ad2

Browse files
authored
Merge pull request #158 from ndevilla/JulianHurst-master
Support for loading string/custom streams
2 parents 24b5d4b + 4a97a81 commit cbd3ad2

File tree

2 files changed

+51
-12
lines changed

2 files changed

+51
-12
lines changed

src/iniparser.c

+35-12
Original file line numberDiff line numberDiff line change
@@ -722,21 +722,19 @@ static line_status iniparser_line(
722722
/*-------------------------------------------------------------------------*/
723723
/**
724724
@brief Parse an ini file and return an allocated dictionary object
725-
@param ininame Name of the ini file to read.
725+
@param in File to read.
726+
@param ininame Name of the ini file to read (only used for nicer error messages)
726727
@return Pointer to newly allocated dictionary
727728
728729
This is the parser for ini files. This function is called, providing
729-
the name of the file to be read. It returns a dictionary object that
730-
should not be accessed directly, but through accessor functions
731-
instead.
730+
the file to be read. It returns a dictionary object that should not
731+
be accessed directly, but through accessor functions instead.
732732
733733
The returned dictionary must be freed using iniparser_freedict().
734734
*/
735735
/*--------------------------------------------------------------------------*/
736-
dictionary * iniparser_load(const char * ininame)
736+
dictionary * iniparser_load_file(FILE * in, const char * ininame)
737737
{
738-
FILE * in ;
739-
740738
char line [ASCIILINESZ+1] ;
741739
char section [ASCIILINESZ+1] ;
742740
char key [ASCIILINESZ+1] ;
@@ -751,11 +749,6 @@ dictionary * iniparser_load(const char * ininame)
751749

752750
dictionary * dict ;
753751

754-
if ((in=fopen(ininame, "r"))==NULL) {
755-
iniparser_error_callback("iniparser: cannot open %s\n", ininame);
756-
return NULL ;
757-
}
758-
759752
dict = dictionary_new(0) ;
760753
if (!dict) {
761754
fclose(in);
@@ -841,6 +834,36 @@ dictionary * iniparser_load(const char * ininame)
841834
return dict ;
842835
}
843836

837+
/*-------------------------------------------------------------------------*/
838+
/**
839+
@brief Parse an ini file and return an allocated dictionary object
840+
@param ininame Name of the ini file to read.
841+
@return Pointer to newly allocated dictionary
842+
843+
This is the parser for ini files. This function is called, providing
844+
the name of the file to be read. It returns a dictionary object that
845+
should not be accessed directly, but through accessor functions
846+
instead.
847+
848+
The returned dictionary must be freed using iniparser_freedict().
849+
*/
850+
/*--------------------------------------------------------------------------*/
851+
dictionary * iniparser_load(const char * ininame)
852+
{
853+
FILE * in ;
854+
dictionary * dict ;
855+
856+
if ((in=fopen(ininame, "r"))==NULL) {
857+
iniparser_error_callback("iniparser: cannot open %s\n", ininame);
858+
return NULL ;
859+
}
860+
861+
dict = iniparser_load_file(in, ininame);
862+
863+
return dict ;
864+
}
865+
866+
844867
/*-------------------------------------------------------------------------*/
845868
/**
846869
@brief Free all memory associated to an ini dictionary

src/iniparser.h

+16
Original file line numberDiff line numberDiff line change
@@ -387,6 +387,22 @@ int iniparser_find_entry(const dictionary * ini, const char * entry) ;
387387
/*--------------------------------------------------------------------------*/
388388
dictionary * iniparser_load(const char * ininame);
389389

390+
/*-------------------------------------------------------------------------*/
391+
/**
392+
@brief Parse an ini file and return an allocated dictionary object
393+
@param ininame File to read.
394+
@param ininame Name of the ini file to read (only used for nicer error messages)
395+
@return Pointer to newly allocated dictionary
396+
397+
This is the parser for ini files. This function is called, providing
398+
the file to be read. It returns a dictionary object that should not
399+
be accessed directly, but through accessor functions instead.
400+
401+
The returned dictionary must be freed using iniparser_freedict().
402+
*/
403+
/*--------------------------------------------------------------------------*/
404+
dictionary * iniparser_load_file(FILE * in, const char * ininame);
405+
390406
/*-------------------------------------------------------------------------*/
391407
/**
392408
@brief Free all memory associated to an ini dictionary

0 commit comments

Comments
 (0)