Skip to content

Commit

Permalink
Fix broken iniparser_dump(section)_ini()
Browse files Browse the repository at this point in the history
- Fix iniparser_find_entry()
- Return (null) in NULL case(section / key with NULL value)
  • Loading branch information
dofuuz committed Aug 23, 2022
1 parent 986d7a5 commit e86fdfa
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 7 deletions.
13 changes: 8 additions & 5 deletions src/iniparser.c
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ typedef enum _line_status_ {
LINE_VALUE
} line_status ;

static const char * INI_NULL_STRING = "(null)";

/*-------------------------------------------------------------------------*/
/**
@brief Convert a string to lowercase.
Expand Down Expand Up @@ -420,8 +422,9 @@ const char * iniparser_getstring(const dictionary * d, const char * key, const c
return def ;

lc_key = strlwc(key, tmp_str, sizeof(tmp_str));
sval = dictionary_get(d, lc_key, def);
return sval ? sval : def ;
sval = dictionary_get(d, lc_key, INI_INVALID_KEY);
if (sval==INI_INVALID_KEY) return def ;
return sval ? sval : INI_NULL_STRING ;
}

/*-------------------------------------------------------------------------*/
Expand Down Expand Up @@ -456,7 +459,7 @@ long int iniparser_getlongint(const dictionary * d, const char * key, long int n
const char * str ;

str = iniparser_getstring(d, key, INI_INVALID_KEY);
if (str==INI_INVALID_KEY) return notfound ;
if (str==INI_INVALID_KEY || str==INI_NULL_STRING) return notfound ;
return strtol(str, NULL, 0);
}

Expand Down Expand Up @@ -511,7 +514,7 @@ double iniparser_getdouble(const dictionary * d, const char * key, double notfou
const char * str ;

str = iniparser_getstring(d, key, INI_INVALID_KEY);
if (str==INI_INVALID_KEY) return notfound ;
if (str==INI_INVALID_KEY || str==INI_NULL_STRING) return notfound ;
return atof(str);
}

Expand Down Expand Up @@ -553,7 +556,7 @@ int iniparser_getboolean(const dictionary * d, const char * key, int notfound)
const char * c ;

c = iniparser_getstring(d, key, INI_INVALID_KEY);
if (c==INI_INVALID_KEY) return notfound ;
if (c==INI_INVALID_KEY || c==INI_NULL_STRING) return notfound ;
if (c[0]=='y' || c[0]=='Y' || c[0]=='1' || c[0]=='t' || c[0]=='T') {
ret = 1 ;
} else if (c[0]=='n' || c[0]=='N' || c[0]=='0' || c[0]=='f' || c[0]=='F') {
Expand Down
4 changes: 2 additions & 2 deletions test/test_iniparser.c
Original file line number Diff line number Diff line change
Expand Up @@ -645,13 +645,13 @@ void Test_dictionary_wrapper(CuTest *tc)
CuAssertStrEquals(tc, "value", iniparser_getstring(dic, "section:key", NULL));
/* reset the key's value*/
CuAssertIntEquals(tc, 0, iniparser_set(dic, "section:key", NULL));
CuAssertStrEquals(tc, "dummy", iniparser_getstring(dic, "section:key", "dummy"));
CuAssertStrEquals(tc, "(null)", iniparser_getstring(dic, "section:key", "dummy"));
CuAssertIntEquals(tc, 0, iniparser_set(dic, "section:key", "value"));
CuAssertStrEquals(tc, "value", iniparser_getstring(dic, "section:key", NULL));

iniparser_unset(dic, "section:key");
CuAssertStrEquals(tc, "dummy", iniparser_getstring(dic, "section:key", "dummy"));
CuAssertStrEquals(tc, "dummy", iniparser_getstring(dic, "section", "dummy"));
CuAssertStrEquals(tc, "(null)", iniparser_getstring(dic, "section", "dummy"));

CuAssertIntEquals(tc, 0, iniparser_set(dic, "section:key", NULL));
CuAssertIntEquals(tc, 0, iniparser_set(dic, "section:key1", NULL));
Expand Down

0 comments on commit e86fdfa

Please sign in to comment.