@@ -247,6 +247,26 @@ void iniparser_dump(const dictionary * d, FILE * f)
247
247
return ;
248
248
}
249
249
250
+ static void escape_value (char * escaped , char * value ) {
251
+ char c ;
252
+ int v = 0 ;
253
+ int e = 0 ;
254
+
255
+ if (!escaped || !value )
256
+ return ;
257
+
258
+ while ((c = value [v ]) != '\0' ) {
259
+ if (c == '\\' || c == '"' ) {
260
+ escaped [e ] = '\\' ;
261
+ e ++ ;
262
+ }
263
+ escaped [e ] = c ;
264
+ v ++ ;
265
+ e ++ ;
266
+ }
267
+ escaped [e ] = '\0' ;
268
+ }
269
+
250
270
/*-------------------------------------------------------------------------*/
251
271
/**
252
272
@brief Save a dictionary to a loadable ini file
@@ -263,6 +283,7 @@ void iniparser_dump_ini(const dictionary * d, FILE * f)
263
283
size_t i ;
264
284
size_t nsec ;
265
285
const char * secname ;
286
+ char escaped [ASCIILINESZ + 1 ] = "" ;
266
287
267
288
if (d == NULL || f == NULL ) return ;
268
289
@@ -272,7 +293,8 @@ void iniparser_dump_ini(const dictionary * d, FILE * f)
272
293
for (i = 0 ; i < d -> size ; i ++ ) {
273
294
if (d -> key [i ]== NULL )
274
295
continue ;
275
- fprintf (f , "%s = %s\n" , d -> key [i ], d -> val [i ]);
296
+ escape_value (escaped , d -> val [i ]);
297
+ fprintf (f , "%s = \"%s\"\n" , d -> key [i ], escaped );
276
298
}
277
299
return ;
278
300
}
@@ -301,6 +323,7 @@ void iniparser_dumpsection_ini(const dictionary * d, const char * s, FILE * f)
301
323
size_t j ;
302
324
char keym [ASCIILINESZ + 1 ];
303
325
int seclen ;
326
+ char escaped [ASCIILINESZ + 1 ] = "" ;
304
327
305
328
if (d == NULL || f == NULL ) return ;
306
329
if (! iniparser_find_entry (d , s )) return ;
@@ -312,10 +335,8 @@ void iniparser_dumpsection_ini(const dictionary * d, const char * s, FILE * f)
312
335
if (d -> key [j ]== NULL )
313
336
continue ;
314
337
if (!strncmp (d -> key [j ], keym , seclen + 1 )) {
315
- fprintf (f ,
316
- "%-30s = %s\n" ,
317
- d -> key [j ]+ seclen + 1 ,
318
- d -> val [j ] ? d -> val [j ] : "" );
338
+ escape_value (escaped , d -> val [j ]);
339
+ fprintf (f , "%-30s = \"%s\"\n" , d -> key [j ]+ seclen + 1 , escaped );
319
340
}
320
341
}
321
342
fprintf (f , "\n" );
@@ -642,6 +663,44 @@ void iniparser_unset(dictionary * ini, const char * entry)
642
663
dictionary_unset (ini , strlwc (entry , tmp_str , sizeof (tmp_str )));
643
664
}
644
665
666
+ static void parse_quoted_value (char * value , char quote ) {
667
+ char c ;
668
+ char * quoted ;
669
+ int q = 0 , v = 0 ;
670
+ int esc = 0 ;
671
+
672
+ if (!value )
673
+ return ;
674
+
675
+ quoted = xstrdup (value );
676
+
677
+ if (!quoted ) {
678
+ iniparser_error_callback ("iniparser: memory allocation failure\n" );
679
+ goto end_of_value ;
680
+ }
681
+
682
+ while ((c = quoted [q ]) != '\0' ) {
683
+ if (!esc ) {
684
+ if (c == '\\' ) {
685
+ esc = 1 ;
686
+ q ++ ;
687
+ continue ;
688
+ }
689
+
690
+ if (c == quote ) {
691
+ goto end_of_value ;
692
+ }
693
+ }
694
+ esc = 0 ;
695
+ value [v ] = c ;
696
+ v ++ ;
697
+ q ++ ;
698
+ }
699
+ end_of_value :
700
+ value [v ] = '\0' ;
701
+ free (quoted );
702
+ }
703
+
645
704
/*-------------------------------------------------------------------------*/
646
705
/**
647
706
@brief Load a single line from an INI file
@@ -661,6 +720,7 @@ static line_status iniparser_line(
661
720
line_status sta ;
662
721
char * line = NULL ;
663
722
size_t len ;
723
+ int d_quote ;
664
724
665
725
line = xstrdup (input_line );
666
726
len = strstrip (line );
@@ -678,11 +738,15 @@ static line_status iniparser_line(
678
738
strstrip (section );
679
739
strlwc (section , section , len );
680
740
sta = LINE_SECTION ;
681
- } else if (sscanf (line , "%[^=] = \"%[^\" ]\"" , key , value ) == 2
682
- || sscanf (line , "%[^=] = '%[^\' ]'" , key , value ) == 2 ) {
741
+ } else if (( d_quote = sscanf (line , "%[^=] = \"%[^\n ]\"" , key , value ) ) == 2
742
+ || sscanf (line , "%[^=] = '%[^\n ]'" , key , value ) == 2 ) {
683
743
/* Usual key=value with quotes, with or without comments */
684
744
strstrip (key );
685
745
strlwc (key , key , len );
746
+ if (d_quote == 2 )
747
+ parse_quoted_value (value , '"' );
748
+ else
749
+ parse_quoted_value (value , '\'' );
686
750
/* Don't strip spaces from values surrounded with quotes */
687
751
sta = LINE_VALUE ;
688
752
} else if (sscanf (line , "%[^=] = %[^;#]" , key , value ) == 2 ) {
0 commit comments