project_files/frontlib/iniparser/iniparser.c
branchflibqtfrontend
changeset 8100 0e6fadf81a2c
parent 7175 038e3415100a
equal deleted inserted replaced
8097:59a8feebec2c 8100:0e6fadf81a2c
     7 */
     7 */
     8 /*--------------------------------------------------------------------------*/
     8 /*--------------------------------------------------------------------------*/
     9 /*---------------------------- Includes ------------------------------------*/
     9 /*---------------------------- Includes ------------------------------------*/
    10 #include <ctype.h>
    10 #include <ctype.h>
    11 #include "iniparser.h"
    11 #include "iniparser.h"
       
    12 #include "physfs.h"
       
    13 #include "physfsfgets.h"
    12 
    14 
    13 /*---------------------------- Defines -------------------------------------*/
    15 /*---------------------------- Defines -------------------------------------*/
    14 #define ASCIILINESZ         (1024)
    16 #define ASCIILINESZ         (1024)
    15 #define INI_INVALID_KEY     ((char*)-1)
    17 #define INI_INVALID_KEY     ((char*)-1)
    16 
    18 
   626   The returned dictionary must be freed using iniparser_freedict().
   628   The returned dictionary must be freed using iniparser_freedict().
   627  */
   629  */
   628 /*--------------------------------------------------------------------------*/
   630 /*--------------------------------------------------------------------------*/
   629 dictionary * iniparser_load(const char * ininame)
   631 dictionary * iniparser_load(const char * ininame)
   630 {
   632 {
   631     FILE * in ;
   633     PHYSFS_File * in ;
   632 
   634 
   633     char line    [ASCIILINESZ+1] ;
   635     char line    [ASCIILINESZ+1] ;
   634     char section [ASCIILINESZ+1] ;
   636     char section [ASCIILINESZ+1] ;
   635     char key     [ASCIILINESZ+1] ;
   637     char key     [ASCIILINESZ+1] ;
   636     char tmp     [ASCIILINESZ+1] ;
   638     char tmp     [ASCIILINESZ+1] ;
   641     int  lineno=0 ;
   643     int  lineno=0 ;
   642     int  errs=0;
   644     int  errs=0;
   643 
   645 
   644     dictionary * dict ;
   646     dictionary * dict ;
   645 
   647 
   646     if ((in=fopen(ininame, "r"))==NULL) {
   648     if ((in = PHYSFS_openRead(ininame))==NULL) {
   647         fprintf(stderr, "iniparser: cannot open %s\n", ininame);
   649         fprintf(stderr, "iniparser: cannot open %s\n", ininame);
   648         return NULL ;
   650         return NULL ;
   649     }
   651     }
   650 
   652 
   651     dict = dictionary_new(0) ;
   653     dict = dictionary_new(0) ;
   652     if (!dict) {
   654     if (!dict) {
   653         fclose(in);
   655         PHYSFS_close(in);
   654         return NULL ;
   656         return NULL ;
   655     }
   657     }
   656 
   658 
   657     memset(line,    0, ASCIILINESZ);
   659     memset(line,    0, ASCIILINESZ);
   658     memset(section, 0, ASCIILINESZ);
   660     memset(section, 0, ASCIILINESZ);
   659     memset(key,     0, ASCIILINESZ);
   661     memset(key,     0, ASCIILINESZ);
   660     memset(val,     0, ASCIILINESZ);
   662     memset(val,     0, ASCIILINESZ);
   661     last=0 ;
   663     last=0 ;
   662 
   664 
   663     while (fgets(line+last, ASCIILINESZ-last, in)!=NULL) {
   665     while (PHYSFS_fgets(line+last, ASCIILINESZ-last, in)!=NULL) {
   664         lineno++ ;
   666         lineno++ ;
   665         len = (int)strlen(line)-1;
   667         len = (int)strlen(line)-1;
   666         if (len==0)
   668         if (len==0)
   667             continue;
   669             continue;
   668         /* Safety check against buffer overflows */
   670         /* Safety check against buffer overflows */
   670             fprintf(stderr,
   672             fprintf(stderr,
   671                     "iniparser: input line too long in %s (%d)\n",
   673                     "iniparser: input line too long in %s (%d)\n",
   672                     ininame,
   674                     ininame,
   673                     lineno);
   675                     lineno);
   674             dictionary_del(dict);
   676             dictionary_del(dict);
   675             fclose(in);
   677             PHYSFS_close(in);
   676             return NULL ;
   678             return NULL ;
   677         }
   679         }
   678         /* Get rid of \n and spaces at end of line */
   680         /* Get rid of \n and spaces at end of line */
   679         while ((len>=0) &&
   681         while ((len>=0) &&
   680                 ((line[len]=='\n') || (isspace(line[len])))) {
   682                 ((line[len]=='\n') || (isspace(line[len])))) {
   723     }
   725     }
   724     if (errs) {
   726     if (errs) {
   725         dictionary_del(dict);
   727         dictionary_del(dict);
   726         dict = NULL ;
   728         dict = NULL ;
   727     }
   729     }
   728     fclose(in);
   730     PHYSFS_close(in);
   729     return dict ;
   731     return dict ;
   730 }
   732 }
   731 
   733 
   732 /*-------------------------------------------------------------------------*/
   734 /*-------------------------------------------------------------------------*/
   733 /**
   735 /**