18
18
#include "config.h"
19
19
#endif
20
20
21
+ #include "php.h" /* Headers are un utter mess so this needs to be before */
21
22
#include "inifile.h"
22
23
23
24
#include <stdlib.h>
30
31
#include "zend_alloc.h"
31
32
#include "php_streams.h"
32
33
#include "spprintf.h"
33
- #include "php.h"
34
34
35
35
/* ret = -1 means that database was opened for read-only
36
36
* ret = 0 success
@@ -150,8 +150,7 @@ static char *etrim(const char *str)
150
150
}
151
151
/* }}} */
152
152
153
- /* {{{ inifile_findkey */
154
- static int inifile_read (inifile * dba , line_type * ln ) {
153
+ static bool inifile_read (inifile * dba , line_type * ln ) {
155
154
char * fline ;
156
155
char * pos ;
157
156
@@ -170,7 +169,7 @@ static int inifile_read(inifile *dba, line_type *ln) {
170
169
ln -> key .name = estrdup ("" );
171
170
ln -> pos = php_stream_tell (dba -> fp );
172
171
efree (fline );
173
- return 1 ;
172
+ return true ;
174
173
} else {
175
174
efree (fline );
176
175
continue ;
@@ -190,7 +189,7 @@ static int inifile_read(inifile *dba, line_type *ln) {
190
189
ln -> val .value = etrim (pos + 1 );
191
190
ln -> pos = php_stream_tell (dba -> fp );
192
191
efree (fline );
193
- return 1 ;
192
+ return true ;
194
193
} else {
195
194
/* simply ignore lines without '='
196
195
* those should be comments
@@ -202,9 +201,8 @@ static int inifile_read(inifile *dba, line_type *ln) {
202
201
}
203
202
}
204
203
inifile_line_free (ln );
205
- return 0 ;
204
+ return false ;
206
205
}
207
- /* }}} */
208
206
209
207
enum inifile_key_cmp_status {
210
208
EQUAL ,
@@ -306,60 +304,50 @@ static bool inifile_truncate(inifile *dba, size_t size)
306
304
return true;
307
305
}
308
306
309
- /* {{{ inifile_find_group
310
- * if found pos_grp_start points to "[group_name]"
311
- */
312
- static bool inifile_find_group (inifile * dba , const key_type * key , size_t * pos_grp_start )
307
+ /* Return position where which points to "[group_name]" */
308
+ static size_t inifile_find_group (inifile * dba , const key_type * key )
313
309
{
314
- bool found_group = false;
315
-
310
+ /* Rewind to the beginning of the file and free currently hold lines */
316
311
php_stream_flush (dba -> fp );
317
312
php_stream_seek (dba -> fp , 0 , SEEK_SET );
318
313
inifile_line_free (& dba -> curr );
319
314
inifile_line_free (& dba -> next );
320
315
321
- if (key -> group && strlen (key -> group )) {
322
- line_type ln = {{NULL ,NULL },{NULL },0 };
316
+ /* If key is not in a group return the position of the start of the file */
317
+ if (!key -> group || strlen (key -> group ) == 0 ) {
318
+ return 0 ;
319
+ }
323
320
324
- while (inifile_read (dba , & ln )) {
325
- if (inifile_key_cmp (& ln .key , key ) != DIFFERENT ) {
326
- found_group = true;
327
- break ;
328
- }
329
- * pos_grp_start = php_stream_tell (dba -> fp );
321
+ size_t group_start_position = 0 ;
322
+ line_type ln = {{NULL ,NULL },{NULL },0 };
323
+ while (inifile_read (dba , & ln )) {
324
+ if (inifile_key_cmp (& ln .key , key ) != DIFFERENT ) {
325
+ break ;
330
326
}
331
- inifile_line_free (& ln );
332
- } else {
333
- * pos_grp_start = 0 ;
334
- found_group = true;
335
- }
336
- if (!found_group ) {
337
- * pos_grp_start = php_stream_tell (dba -> fp );
327
+ group_start_position = php_stream_tell (dba -> fp );
338
328
}
339
- return found_group ;
329
+ inifile_line_free (& ln );
330
+ return group_start_position ;
340
331
}
341
- /* }}} */
342
332
343
- /* {{{ inifile_next_group
344
- * only valid after a call to inifile_find_group
333
+ /* Must only be called after inifile_find_group()
334
+ *
345
335
* if any next group is found pos_grp_start points to "[group_name]" or whitespace before that
346
336
*/
347
- static bool inifile_next_group (inifile * dba , const key_type * key , size_t * pos_grp_start )
337
+ static size_t inifile_next_group (inifile * dba , const key_type * key )
348
338
{
349
- bool has_next_group = false;
350
339
line_type ln = {{NULL ,NULL },{NULL },0 };
351
340
352
- * pos_grp_start = php_stream_tell (dba -> fp );
341
+ size_t start_position = php_stream_tell (dba -> fp );
353
342
ln .key .group = estrdup (key -> group );
354
343
while (inifile_read (dba , & ln )) {
355
344
if (inifile_key_cmp (& ln .key , key ) == DIFFERENT ) {
356
- has_next_group = true;
357
345
break ;
358
346
}
359
- * pos_grp_start = php_stream_tell (dba -> fp );
347
+ start_position = php_stream_tell (dba -> fp );
360
348
}
361
349
inifile_line_free (& ln );
362
- return has_next_group ;
350
+ return start_position ;
363
351
}
364
352
/* }}} */
365
353
@@ -369,7 +357,7 @@ static bool inifile_copy_to(inifile *dba, size_t pos_start, size_t pos_end, inif
369
357
370
358
if (pos_start == pos_end ) {
371
359
* ini_copy = NULL ;
372
- return false ;
360
+ return true ;
373
361
}
374
362
if ((fp = php_stream_temp_create (0 , 64 * 1024 )) == NULL ) {
375
363
php_error_docref (NULL , E_WARNING , "Could not create temporary stream" );
@@ -438,10 +426,9 @@ static int inifile_filter(inifile *dba, inifile *from, const key_type *key, bool
438
426
}
439
427
/* }}} */
440
428
441
- /* {{{ inifile_delete_replace_append */
429
+ /* If value == NULL we are deleting the entry, if is_append is true then we are inserting a new entry */
442
430
static int inifile_delete_replace_append (inifile * dba , const key_type * key , const val_type * value , bool is_append , bool * found )
443
431
{
444
- size_t pos_grp_start = 0 , pos_grp_next ;
445
432
inifile * ini_tmp = NULL ;
446
433
php_stream * fp_tmp = NULL ;
447
434
int ret = FAILURE ;
@@ -457,11 +444,12 @@ static int inifile_delete_replace_append(inifile *dba, const key_type *key, cons
457
444
* 8) Append temporary stream
458
445
*/
459
446
460
- assert (!is_append || (key -> name && value )); /* missuse */
447
+ /* Check missuse of API */
448
+ assert (!is_append || (key -> name && value ));
461
449
462
450
/* 1 - 3 */
463
- inifile_find_group (dba , key , & pos_grp_start );
464
- inifile_next_group (dba , key , & pos_grp_next );
451
+ size_t pos_grp_start = inifile_find_group (dba , key );
452
+ size_t pos_grp_next = inifile_next_group (dba , key );
465
453
if (!is_append && !inifile_copy_to (dba , pos_grp_start , pos_grp_next , & ini_tmp )) {
466
454
goto end ;
467
455
}
0 commit comments