Skip to content

Commit

Permalink
Merge pull request #159 from ndevilla/sections-with-brackets
Browse files Browse the repository at this point in the history
Sections with brackets
  • Loading branch information
lmoellendorf authored Apr 21, 2024
2 parents e2bae69 + f8bcd90 commit c9d9fd5
Show file tree
Hide file tree
Showing 2 changed files with 162 additions and 2 deletions.
7 changes: 5 additions & 2 deletions src/iniparser.c
Original file line number Diff line number Diff line change
Expand Up @@ -733,8 +733,11 @@ static line_status iniparser_line(
/* Comment line */
sta = LINE_COMMENT ;
} else if (line[0]=='[' && line[len-1]==']') {
/* Section name */
sscanf(line, "[%[^]]", section);
/* Section name without opening square bracket */
sscanf(line, "[%s", section);
/* Section name without closing square bracket */
section[len] = '\0';
len--;
strstrip(section);
strlwc(section, section, len);
sta = LINE_SECTION ;
Expand Down
157 changes: 157 additions & 0 deletions test/test_iniparser.c
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,10 @@
#define GRUEZI_INI_PATH "ressources/gruezi.ini"
#define UTF8_INI_PATH "ressources/utf8.ini"
#define TMP_INI_PATH "ressources/tmp.ini"
#define MISFORMED_INI_SEC0 "[12345"
#define MISFORMED_INI_SEC1 "12345]"
#define MISFORMED_INI_SEC2 "123]45"
#define MISFORMED_INI_ATTR "1111"
#define QUOTES_INI_PATH "ressources/quotes.ini"
#define QUOTES_INI_SEC "quotes"
#define QUOTES_INI_ATTR0 "string0"
Expand Down Expand Up @@ -1049,6 +1053,159 @@ static void create_empty_ini_file(const char *filename)
fclose(ini);
}

void Test_iniparser_misformed(CuTest *tc)
{
dictionary *dic;
FILE *ini;
int ret;

create_empty_ini_file(TMP_INI_PATH);
dic = iniparser_load(TMP_INI_PATH);

if (!dic) {
fprintf(stderr, "cannot parse file: %s\n", TMP_INI_PATH);
return;
}

ret = iniparser_set(dic, MISFORMED_INI_SEC0, NULL);

if (ret < 0) {
fprintf(stderr, "cannot set section %s in: %s\n", MISFORMED_INI_SEC0,
TMP_INI_PATH);
goto del_dic;
}

iniparser_set(dic, MISFORMED_INI_SEC0 ":" MISFORMED_INI_ATTR, "2222");
/* test dictionary */
CuAssertIntEquals(tc, 2222, iniparser_getint(dic,
MISFORMED_INI_SEC0 ":" MISFORMED_INI_ATTR, -1));
ini = fopen(TMP_INI_PATH, "w+");

if (!ini) {
fprintf(stderr, "iniparser: cannot open %s\n", TMP_INI_PATH);
goto del_dic;
}

iniparser_dump_ini(dic, ini);
fclose(ini);
dictionary_del(dic);
/* check if section has been written as expected */
dic = iniparser_load(TMP_INI_PATH);

if (!dic) {
fprintf(stderr, "cannot parse file: %s\n", TMP_INI_PATH);
goto rm_ini;
}

CuAssertIntEquals(tc, 2222, iniparser_getint(dic,
MISFORMED_INI_SEC0 ":" MISFORMED_INI_ATTR, -1));
dictionary_del(dic);
ret = remove(TMP_INI_PATH);

if (ret) {
fprintf(stderr, "cannot remove file: %s\n", TMP_INI_PATH);
return;
}

create_empty_ini_file(TMP_INI_PATH);
dic = iniparser_load(TMP_INI_PATH);

if (!dic) {
fprintf(stderr, "cannot parse file: %s\n", TMP_INI_PATH);
return;
}

ret = iniparser_set(dic, MISFORMED_INI_SEC1, NULL);

if (ret < 0) {
fprintf(stderr, "cannot set section %s in: %s\n", MISFORMED_INI_SEC1,
TMP_INI_PATH);
goto del_dic;
}

iniparser_set(dic, MISFORMED_INI_SEC1 ":" MISFORMED_INI_ATTR, "2222");
/* test dictionary */
CuAssertIntEquals(tc, 2222, iniparser_getint(dic,
MISFORMED_INI_SEC1 ":" MISFORMED_INI_ATTR, -1));
ini = fopen(TMP_INI_PATH, "w+");

if (!ini) {
fprintf(stderr, "iniparser: cannot open %s\n", TMP_INI_PATH);
goto del_dic;
}

iniparser_dump_ini(dic, ini);
fclose(ini);
dictionary_del(dic);
/* check if section has been written as expected */
dic = iniparser_load(TMP_INI_PATH);

if (!dic) {
fprintf(stderr, "cannot parse file: %s\n", TMP_INI_PATH);
return;
}

CuAssertIntEquals(tc, 2222, iniparser_getint(dic,
MISFORMED_INI_SEC1 ":" MISFORMED_INI_ATTR, -1));
dictionary_del(dic);
ret = remove(TMP_INI_PATH);

if (ret) {
fprintf(stderr, "cannot remove file: %s\n", TMP_INI_PATH);
return;
}

create_empty_ini_file(TMP_INI_PATH);
dic = iniparser_load(TMP_INI_PATH);

if (!dic) {
fprintf(stderr, "cannot parse file: %s\n", TMP_INI_PATH);
return;
}

ret = iniparser_set(dic, MISFORMED_INI_SEC2, NULL);

if (ret < 0) {
fprintf(stderr, "cannot set section %s in: %s\n", MISFORMED_INI_SEC2,
TMP_INI_PATH);
goto del_dic;
}

iniparser_set(dic, MISFORMED_INI_SEC2 ":" MISFORMED_INI_ATTR, "2222");
/* test dictionary */
CuAssertIntEquals(tc, 2222, iniparser_getint(dic,
MISFORMED_INI_SEC2 ":" MISFORMED_INI_ATTR, -1));
ini = fopen(TMP_INI_PATH, "w+");

if (!ini) {
fprintf(stderr, "iniparser: cannot open %s\n", TMP_INI_PATH);
goto del_dic;
}

iniparser_dump_ini(dic, ini);
fclose(ini);
dictionary_del(dic);
/* check if section has been written as expected */
dic = iniparser_load(TMP_INI_PATH);

if (!dic) {
fprintf(stderr, "cannot parse file: %s\n", TMP_INI_PATH);
return;
}

CuAssertIntEquals(tc, 2222, iniparser_getint(dic,
MISFORMED_INI_SEC2 ":" MISFORMED_INI_ATTR, -1));
del_dic:
dictionary_del(dic);
rm_ini:
ret = remove(TMP_INI_PATH);

if (ret) {
fprintf(stderr, "cannot remove file: %s\n", TMP_INI_PATH);
return;
}
}

void Test_iniparser_quotes(CuTest *tc)
{
dictionary *dic;
Expand Down

0 comments on commit c9d9fd5

Please sign in to comment.