5static const char TEST_INI_01[] =
"; This is a sample .ini config file\n"
13 "path = \"/usr/local/bin\"\n"
14 "URL = \"http://www.example.com/~username\"\n"
17static const char TEST_INI_02[] =
"[FreeRDS]\n"
18 "prefix=\"/usr/local\"\n"
22 "datarootdir=\"share\"\n"
23 "localstatedir=\"var\"\n"
24 "sysconfdir=\"etc\"\n"
27static const char TEST_INI_03[] =
"[FreeRDS]\n"
28 "prefix=\"/usr/local\"\n"
30 "# some illegal string\n"
33 "invalid key-value pair\n"
34 "datarootdir=\"share\"\n"
35 "localstatedir=\"var\"\n"
36 "sysconfdir=\"etc\"\n"
39static const char TEST_INI_04[] =
"[foobar]\r\n"
42 "prefix=\"someprefix\"\r\n";
45static BOOL test04(
void)
51 char** sectionNames =
nullptr;
52 wIniFile* ini = IniFile_New();
56 if (IniFile_ReadBuffer(ini, TEST_INI_04) < 0)
59 sectionNames = IniFile_GetSectionNames(ini, &nSections);
60 if (!sectionNames && (nSections > 0))
66 for (
size_t i = 0; i < nSections; i++)
69 char** keyNames = IniFile_GetSectionKeyNames(ini, sectionNames[i], &nKeys);
70 printf(
"[%s]\n", sectionNames[i]);
71 if ((!keyNames && (nKeys > 0)) || (nKeys != 1))
73 free((
void*)keyNames);
76 for (
size_t j = 0; j < nKeys; j++)
78 const char* sValue = IniFile_GetKeyValueString(ini, sectionNames[i], keyNames[j]);
79 printf(
"%s = %s\n", keyNames[j], sValue);
81 free((
void*)keyNames);
84 const char* sValue = IniFile_GetKeyValueString(ini,
"foobar",
"prefix");
86 if (strcmp(sValue,
"someprefix") != 0)
88 printf(
"IniFile_GetKeyValueString failure\n");
94 free((
void*)sectionNames);
99int TestIni(
int argc,
char* argv[])
103 size_t nSections = 0;
105 wIniFile* ini =
nullptr;
106 const char* sValue =
nullptr;
107 char** keyNames =
nullptr;
108 char** sectionNames =
nullptr;
121 if (IniFile_ReadBuffer(ini, TEST_INI_01) < 0)
124 free((
void*)sectionNames);
125 sectionNames = IniFile_GetSectionNames(ini, &nSections);
126 if (!sectionNames && (nSections > 0))
129 for (
size_t i = 0; i < nSections; i++)
131 free((
void*)keyNames);
132 keyNames = IniFile_GetSectionKeyNames(ini, sectionNames[i], &nKeys);
133 printf(
"[%s]\n", sectionNames[i]);
134 if (!keyNames && (nKeys > 0))
136 for (
size_t j = 0; j < nKeys; j++)
138 sValue = IniFile_GetKeyValueString(ini, sectionNames[i], keyNames[j]);
139 printf(
"%s = %s\n", keyNames[j], sValue);
143 iValue = IniFile_GetKeyValueInt(ini,
"first_section",
"one");
147 printf(
"IniFile_GetKeyValueInt failure\n");
151 iValue = IniFile_GetKeyValueInt(ini,
"first_section",
"five");
155 printf(
"IniFile_GetKeyValueInt failure\n");
159 sValue = IniFile_GetKeyValueString(ini,
"first_section",
"animal");
161 if (strcmp(sValue,
"BIRD") != 0)
163 printf(
"IniFile_GetKeyValueString failure\n");
167 sValue = IniFile_GetKeyValueString(ini,
"second_section",
"path");
169 if (strcmp(sValue,
"/usr/local/bin") != 0)
171 printf(
"IniFile_GetKeyValueString failure\n");
175 sValue = IniFile_GetKeyValueString(ini,
"second_section",
"URL");
177 if (strcmp(sValue,
"http://www.example.com/~username") != 0)
179 printf(
"IniFile_GetKeyValueString failure\n");
188 if (IniFile_ReadBuffer(ini, TEST_INI_02) < 0)
190 free((
void*)sectionNames);
191 sectionNames = IniFile_GetSectionNames(ini, &nSections);
192 if (!sectionNames && (nSections > 0))
195 for (
size_t i = 0; i < nSections; i++)
197 free((
void*)keyNames);
198 keyNames = IniFile_GetSectionKeyNames(ini, sectionNames[i], &nKeys);
199 printf(
"[%s]\n", sectionNames[i]);
201 if (!keyNames && (nKeys > 0))
203 for (
size_t j = 0; j < nKeys; j++)
205 sValue = IniFile_GetKeyValueString(ini, sectionNames[i], keyNames[j]);
206 printf(
"%s = %s\n", keyNames[j], sValue);
214 if (IniFile_ReadBuffer(ini, TEST_INI_03) != -1)
219 free((
void*)keyNames);
220 free((
void*)sectionNames);