@@ -462,10 +462,10 @@ static bool config_file_parse_line(config_file_t *conf,
462462 /* Check whether entire line is a comment */
463463 if (comment )
464464 {
465+ config_file_t sub_conf ;
465466 char real_path [PATH_MAX_LENGTH ];
466467 char * path = NULL ;
467468 char * include_line = NULL ;
468- config_file_t * sub_conf = NULL ;
469469
470470 /* Starting a line with an 'include' directive
471471 * appends a sub-config file
@@ -501,12 +501,22 @@ static bool config_file_parse_line(config_file_t *conf,
501501 config_file_add_sub_conf (conf , path ,
502502 real_path , sizeof (real_path ), cb );
503503
504- sub_conf = config_file_new_alloc ( );
504+ config_file_initialize ( & sub_conf );
505505
506- if (config_file_load_internal (sub_conf , real_path ,
507- conf -> include_depth + 1 , cb ) == 0 )
508- config_file_add_child_list (conf , sub_conf ); /* Pilfer internal list. */
509- config_file_free (sub_conf );
506+ switch (config_file_load_internal (& sub_conf , real_path ,
507+ conf -> include_depth + 1 , cb ))
508+ {
509+ case 0 :
510+ /* Pilfer internal list. */
511+ config_file_add_child_list (conf , & sub_conf );
512+ /* fall-through to deinitialize */
513+ case -1 :
514+ config_file_deinitialize (& sub_conf );
515+ break ;
516+ case 1 :
517+ default :
518+ break ;
519+ }
510520
511521 free (path );
512522 return true;
@@ -560,12 +570,66 @@ static bool config_file_parse_line(config_file_t *conf,
560570 return true;
561571}
562572
563- void config_file_free (config_file_t * conf )
573+ static int config_file_from_string_internal (
574+ struct config_file * conf ,
575+ char * from_string ,
576+ const char * path )
577+ {
578+ char * lines = from_string ;
579+ char * save_ptr = NULL ;
580+ char * line = NULL ;
581+
582+ if (!string_is_empty (path ))
583+ conf -> path = strdup (path );
584+ if (string_is_empty (lines ))
585+ return 0 ;
586+
587+ /* Get first line of config file */
588+ line = strtok_r (lines , "\n" , & save_ptr );
589+
590+ while (line )
591+ {
592+ struct config_entry_list * list = (struct config_entry_list * )
593+ malloc (sizeof (* list ));
594+
595+ if (!list )
596+ return -1 ;
597+
598+ list -> readonly = false;
599+ list -> key = NULL ;
600+ list -> value = NULL ;
601+ list -> next = NULL ;
602+
603+ /* Parse current line */
604+ if (
605+ !string_is_empty (line )
606+ && config_file_parse_line (conf , list , line , NULL ))
607+ {
608+ if (conf -> entries )
609+ conf -> tail -> next = list ;
610+ else
611+ conf -> entries = list ;
612+
613+ conf -> tail = list ;
614+ }
615+
616+ if (list != conf -> tail )
617+ free (list );
618+
619+ /* Get next line of config file */
620+ line = strtok_r (NULL , "\n" , & save_ptr );
621+ }
622+
623+ return 0 ;
624+ }
625+
626+
627+ bool config_file_deinitialize (config_file_t * conf )
564628{
565629 struct config_include_list * inc_tmp = NULL ;
566630 struct config_entry_list * tmp = NULL ;
567631 if (!conf )
568- return ;
632+ return false ;
569633
570634 tmp = conf -> entries ;
571635 while (tmp )
@@ -600,6 +664,13 @@ void config_file_free(config_file_t *conf)
600664
601665 if (conf -> path )
602666 free (conf -> path );
667+ return true;
668+ }
669+
670+ void config_file_free (config_file_t * conf )
671+ {
672+ if (!config_file_deinitialize (conf ))
673+ return ;
603674 free (conf );
604675}
605676
@@ -623,58 +694,15 @@ bool config_append_file(config_file_t *conf, const char *path)
623694config_file_t * config_file_new_from_string (char * from_string ,
624695 const char * path )
625696{
626- char * lines = from_string ;
627- char * save_ptr = NULL ;
628- char * line = NULL ;
629697 struct config_file * conf = config_file_new_alloc ();
630698
631699 if (!conf )
632700 return NULL ;
633-
634- if (!string_is_empty (path ))
635- conf -> path = strdup (path );
636- if (string_is_empty (lines ))
637- return conf ;
638-
639- /* Get first line of config file */
640- line = strtok_r (lines , "\n" , & save_ptr );
641-
642- while (line )
701+ if (config_file_from_string_internal (conf , from_string , path ) == -1 )
643702 {
644- struct config_entry_list * list = (struct config_entry_list * )
645- malloc (sizeof (* list ));
646-
647- if (!list )
648- {
649- config_file_free (conf );
650- return NULL ;
651- }
652-
653- list -> readonly = false;
654- list -> key = NULL ;
655- list -> value = NULL ;
656- list -> next = NULL ;
657-
658- /* Parse current line */
659- if (
660- !string_is_empty (line )
661- && config_file_parse_line (conf , list , line , NULL ))
662- {
663- if (conf -> entries )
664- conf -> tail -> next = list ;
665- else
666- conf -> entries = list ;
667-
668- conf -> tail = list ;
669- }
670-
671- if (list != conf -> tail )
672- free (list );
673-
674- /* Get next line of config file */
675- line = strtok_r (NULL , "\n" , & save_ptr );
703+ config_file_free (conf );
704+ return NULL ;
676705 }
677-
678706 return conf ;
679707}
680708
@@ -693,10 +721,12 @@ config_file_t *config_file_new_from_path_to_string(const char *path)
693721 * modified by config_file_new_from_string() */
694722 if (length >= 0 )
695723 conf = config_file_new_from_string ((char * )ret_buf , path );
724+
696725 if ((void * )ret_buf )
697726 free ((void * )ret_buf );
698727 }
699728 }
729+
700730 return conf ;
701731}
702732
@@ -741,11 +771,10 @@ config_file_t *config_file_new(const char *path)
741771 return conf ;
742772}
743773
744- config_file_t * config_file_new_alloc ( void )
774+ void config_file_initialize ( struct config_file * conf )
745775{
746- struct config_file * conf = (struct config_file * )malloc (sizeof (* conf ));
747776 if (!conf )
748- return NULL ;
777+ return ;
749778
750779 conf -> path = NULL ;
751780 conf -> entries = NULL ;
@@ -755,7 +784,14 @@ config_file_t *config_file_new_alloc(void)
755784 conf -> include_depth = 0 ;
756785 conf -> guaranteed_no_duplicates = false;
757786 conf -> modified = false;
787+ }
758788
789+ config_file_t * config_file_new_alloc (void )
790+ {
791+ struct config_file * conf = (struct config_file * )malloc (sizeof (* conf ));
792+ if (!conf )
793+ return NULL ;
794+ config_file_initialize (conf );
759795 return conf ;
760796}
761797
@@ -1085,9 +1121,7 @@ void config_set_path(config_file_t *conf, const char *entry, const char *val)
10851121
10861122void config_set_double (config_file_t * conf , const char * key , double val )
10871123{
1088- char buf [128 ];
1089-
1090- buf [0 ] = '\0' ;
1124+ char buf [320 ];
10911125#ifdef __cplusplus
10921126 snprintf (buf , sizeof (buf ), "%f" , (float )val );
10931127#elif defined(__STDC_VERSION__ ) && __STDC_VERSION__ >=199901L
@@ -1100,54 +1134,42 @@ void config_set_double(config_file_t *conf, const char *key, double val)
11001134
11011135void config_set_float (config_file_t * conf , const char * key , float val )
11021136{
1103- char buf [128 ];
1104-
1105- buf [0 ] = '\0' ;
1137+ char buf [64 ];
11061138 snprintf (buf , sizeof (buf ), "%f" , val );
11071139 config_set_string (conf , key , buf );
11081140}
11091141
11101142void config_set_int (config_file_t * conf , const char * key , int val )
11111143{
1112- char buf [128 ];
1113-
1114- buf [0 ] = '\0' ;
1144+ char buf [16 ];
11151145 snprintf (buf , sizeof (buf ), "%d" , val );
11161146 config_set_string (conf , key , buf );
11171147}
11181148
11191149void config_set_uint (config_file_t * conf , const char * key , unsigned int val )
11201150{
1121- char buf [128 ];
1122-
1123- buf [0 ] = '\0' ;
1151+ char buf [16 ];
11241152 snprintf (buf , sizeof (buf ), "%u" , val );
11251153 config_set_string (conf , key , buf );
11261154}
11271155
11281156void config_set_hex (config_file_t * conf , const char * key , unsigned val )
11291157{
1130- char buf [128 ];
1131-
1132- buf [0 ] = '\0' ;
1158+ char buf [16 ];
11331159 snprintf (buf , sizeof (buf ), "%x" , val );
11341160 config_set_string (conf , key , buf );
11351161}
11361162
11371163void config_set_uint64 (config_file_t * conf , const char * key , uint64_t val )
11381164{
1139- char buf [128 ];
1140-
1141- buf [0 ] = '\0' ;
1165+ char buf [32 ];
11421166 snprintf (buf , sizeof (buf ), "%" PRIu64 , val );
11431167 config_set_string (conf , key , buf );
11441168}
11451169
11461170void config_set_char (config_file_t * conf , const char * key , char val )
11471171{
11481172 char buf [2 ];
1149-
1150- buf [0 ] = '\0' ;
11511173 snprintf (buf , sizeof (buf ), "%c" , val );
11521174 config_set_string (conf , key , buf );
11531175}
@@ -1307,10 +1329,11 @@ bool config_get_entry_list_next(struct config_file_entry *entry)
13071329
13081330bool config_file_exists (const char * path )
13091331{
1310- config_file_t * config = config_file_new (path );
1311- if (!config )
1332+ config_file_t conf ;
1333+ config_file_initialize (& conf );
1334+ if (config_file_load_internal (& conf , path , 0 , NULL ) == 1 )
13121335 return false;
13131336
1314- config_file_free ( config );
1337+ config_file_deinitialize ( & conf );
13151338 return true;
13161339}
0 commit comments