Skip to content

Commit 72f2224

Browse files
committed
Fixup and refactoring
1 parent 4a2bf56 commit 72f2224

File tree

1 file changed

+32
-44
lines changed

1 file changed

+32
-44
lines changed

ext/dba/libinifile/inifile.c

+32-44
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
#include "config.h"
1919
#endif
2020

21+
#include "php.h" /* Headers are un utter mess so this needs to be before */
2122
#include "inifile.h"
2223

2324
#include <stdlib.h>
@@ -30,7 +31,6 @@
3031
#include "zend_alloc.h"
3132
#include "php_streams.h"
3233
#include "spprintf.h"
33-
#include "php.h"
3434

3535
/* ret = -1 means that database was opened for read-only
3636
* ret = 0 success
@@ -150,8 +150,7 @@ static char *etrim(const char *str)
150150
}
151151
/* }}} */
152152

153-
/* {{{ inifile_findkey */
154-
static int inifile_read(inifile *dba, line_type *ln) {
153+
static bool inifile_read(inifile *dba, line_type *ln) {
155154
char *fline;
156155
char *pos;
157156

@@ -170,7 +169,7 @@ static int inifile_read(inifile *dba, line_type *ln) {
170169
ln->key.name = estrdup("");
171170
ln->pos = php_stream_tell(dba->fp);
172171
efree(fline);
173-
return 1;
172+
return true;
174173
} else {
175174
efree(fline);
176175
continue;
@@ -190,7 +189,7 @@ static int inifile_read(inifile *dba, line_type *ln) {
190189
ln->val.value = etrim(pos+1);
191190
ln->pos = php_stream_tell(dba->fp);
192191
efree(fline);
193-
return 1;
192+
return true;
194193
} else {
195194
/* simply ignore lines without '='
196195
* those should be comments
@@ -202,9 +201,8 @@ static int inifile_read(inifile *dba, line_type *ln) {
202201
}
203202
}
204203
inifile_line_free(ln);
205-
return 0;
204+
return false;
206205
}
207-
/* }}} */
208206

209207
enum inifile_key_cmp_status {
210208
EQUAL,
@@ -306,60 +304,50 @@ static bool inifile_truncate(inifile *dba, size_t size)
306304
return true;
307305
}
308306

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)
313309
{
314-
bool found_group = false;
315-
310+
/* Rewind to the beginning of the file and free currently hold lines */
316311
php_stream_flush(dba->fp);
317312
php_stream_seek(dba->fp, 0, SEEK_SET);
318313
inifile_line_free(&dba->curr);
319314
inifile_line_free(&dba->next);
320315

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+
}
323320

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;
330326
}
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);
338328
}
339-
return found_group;
329+
inifile_line_free(&ln);
330+
return group_start_position;
340331
}
341-
/* }}} */
342332

343-
/* {{{ inifile_next_group
344-
* only valid after a call to inifile_find_group
333+
/* Must only be called after inifile_find_group()
334+
*
345335
* if any next group is found pos_grp_start points to "[group_name]" or whitespace before that
346336
*/
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)
348338
{
349-
bool has_next_group = false;
350339
line_type ln = {{NULL,NULL},{NULL},0};
351340

352-
*pos_grp_start = php_stream_tell(dba->fp);
341+
size_t start_position = php_stream_tell(dba->fp);
353342
ln.key.group = estrdup(key->group);
354343
while (inifile_read(dba, &ln)) {
355344
if (inifile_key_cmp(&ln.key, key) == DIFFERENT) {
356-
has_next_group = true;
357345
break;
358346
}
359-
*pos_grp_start = php_stream_tell(dba->fp);
347+
start_position = php_stream_tell(dba->fp);
360348
}
361349
inifile_line_free(&ln);
362-
return has_next_group;
350+
return start_position;
363351
}
364352
/* }}} */
365353

@@ -369,7 +357,7 @@ static bool inifile_copy_to(inifile *dba, size_t pos_start, size_t pos_end, inif
369357

370358
if (pos_start == pos_end) {
371359
*ini_copy = NULL;
372-
return false;
360+
return true;
373361
}
374362
if ((fp = php_stream_temp_create(0, 64 * 1024)) == NULL) {
375363
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
438426
}
439427
/* }}} */
440428

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 */
442430
static int inifile_delete_replace_append(inifile *dba, const key_type *key, const val_type *value, bool is_append, bool *found)
443431
{
444-
size_t pos_grp_start = 0, pos_grp_next;
445432
inifile *ini_tmp = NULL;
446433
php_stream *fp_tmp = NULL;
447434
int ret = FAILURE;
@@ -457,11 +444,12 @@ static int inifile_delete_replace_append(inifile *dba, const key_type *key, cons
457444
* 8) Append temporary stream
458445
*/
459446

460-
assert(!is_append || (key->name && value)); /* missuse */
447+
/* Check missuse of API */
448+
assert(!is_append || (key->name && value));
461449

462450
/* 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);
465453
if (!is_append && !inifile_copy_to(dba, pos_grp_start, pos_grp_next, &ini_tmp)) {
466454
goto end;
467455
}

0 commit comments

Comments
 (0)