2014-03-25 02:20:34 +04:00
|
|
|
|
2014-03-25 06:41:30 +04:00
|
|
|
#include <winpr/crt.h>
|
2014-03-25 02:20:34 +04:00
|
|
|
#include <winpr/ini.h>
|
|
|
|
|
2021-07-28 16:18:03 +03:00
|
|
|
static const char TEST_INI_01[] = "; This is a sample .ini config file\n"
|
|
|
|
"\n"
|
|
|
|
"[first_section]\n"
|
|
|
|
"one = 1\n"
|
|
|
|
"five = 5\n"
|
|
|
|
"animal = BIRD\n"
|
|
|
|
"\n"
|
|
|
|
"[second_section]\n"
|
|
|
|
"path = \"/usr/local/bin\"\n"
|
|
|
|
"URL = \"http://www.example.com/~username\"\n"
|
|
|
|
"\n";
|
2019-11-06 17:24:51 +03:00
|
|
|
|
2021-07-28 16:18:03 +03:00
|
|
|
static const char TEST_INI_02[] = "[FreeRDS]\n"
|
|
|
|
"prefix=\"/usr/local\"\n"
|
|
|
|
"bindir=\"bin\"\n"
|
|
|
|
"sbindir=\"sbin\"\n"
|
|
|
|
"libdir=\"lib\"\n"
|
|
|
|
"datarootdir=\"share\"\n"
|
|
|
|
"localstatedir=\"var\"\n"
|
|
|
|
"sysconfdir=\"etc\"\n"
|
|
|
|
"\n";
|
2019-11-06 17:24:51 +03:00
|
|
|
|
2021-07-28 16:18:03 +03:00
|
|
|
static const char TEST_INI_03[] = "[FreeRDS]\n"
|
|
|
|
"prefix=\"/usr/local\"\n"
|
|
|
|
"bindir=\"bin\"\n"
|
|
|
|
"# some illegal string\n"
|
|
|
|
"sbindir=\"sbin\"\n"
|
|
|
|
"libdir=\"lib\"\n"
|
|
|
|
"invalid key-value pair\n"
|
|
|
|
"datarootdir=\"share\"\n"
|
|
|
|
"localstatedir=\"var\"\n"
|
|
|
|
"sysconfdir=\"etc\"\n"
|
|
|
|
"\n";
|
2014-03-25 08:32:05 +04:00
|
|
|
|
2014-03-25 02:20:34 +04:00
|
|
|
int TestIni(int argc, char* argv[])
|
|
|
|
{
|
2020-05-15 15:49:52 +03:00
|
|
|
int rc = -1;
|
2014-03-25 06:41:30 +04:00
|
|
|
int i, j;
|
|
|
|
int nKeys;
|
|
|
|
int nSections;
|
|
|
|
UINT32 iValue;
|
2020-05-15 15:49:52 +03:00
|
|
|
wIniFile* ini = NULL;
|
2014-10-03 02:45:53 +04:00
|
|
|
const char* sValue;
|
2020-05-15 15:49:52 +03:00
|
|
|
char** keyNames = NULL;
|
|
|
|
char** sectionNames = NULL;
|
2021-07-29 11:18:52 +03:00
|
|
|
|
|
|
|
WINPR_UNUSED(argc);
|
|
|
|
WINPR_UNUSED(argv);
|
|
|
|
|
2014-03-25 08:32:05 +04:00
|
|
|
/* First Sample */
|
2014-03-25 02:20:34 +04:00
|
|
|
ini = IniFile_New();
|
2020-05-15 15:49:52 +03:00
|
|
|
if (!ini)
|
|
|
|
goto fail;
|
|
|
|
|
|
|
|
if (IniFile_ReadBuffer(ini, TEST_INI_01) < 0)
|
|
|
|
goto fail;
|
|
|
|
|
|
|
|
free(sectionNames);
|
2014-03-25 06:41:30 +04:00
|
|
|
sectionNames = IniFile_GetSectionNames(ini, &nSections);
|
2020-05-15 15:49:52 +03:00
|
|
|
if (!sectionNames && (nSections > 0))
|
|
|
|
goto fail;
|
2019-04-08 10:26:46 +03:00
|
|
|
|
2014-03-25 06:41:30 +04:00
|
|
|
for (i = 0; i < nSections; i++)
|
|
|
|
{
|
2020-05-15 15:49:52 +03:00
|
|
|
free(keyNames);
|
2014-03-25 06:41:30 +04:00
|
|
|
keyNames = IniFile_GetSectionKeyNames(ini, sectionNames[i], &nKeys);
|
|
|
|
printf("[%s]\n", sectionNames[i]);
|
2020-05-15 15:49:52 +03:00
|
|
|
if (!keyNames && (nKeys > 0))
|
|
|
|
goto fail;
|
2014-03-25 06:41:30 +04:00
|
|
|
for (j = 0; j < nKeys; j++)
|
|
|
|
{
|
|
|
|
sValue = IniFile_GetKeyValueString(ini, sectionNames[i], keyNames[j]);
|
|
|
|
printf("%s = %s\n", keyNames[j], sValue);
|
|
|
|
}
|
|
|
|
}
|
2014-03-25 02:20:34 +04:00
|
|
|
|
2014-03-25 06:41:30 +04:00
|
|
|
iValue = IniFile_GetKeyValueInt(ini, "first_section", "one");
|
2019-04-08 10:26:46 +03:00
|
|
|
|
2014-03-25 06:41:30 +04:00
|
|
|
if (iValue != 1)
|
|
|
|
{
|
|
|
|
printf("IniFile_GetKeyValueInt failure\n");
|
2020-05-15 15:49:52 +03:00
|
|
|
goto fail;
|
2014-03-25 06:41:30 +04:00
|
|
|
}
|
2019-04-08 10:26:46 +03:00
|
|
|
|
2014-03-25 06:41:30 +04:00
|
|
|
iValue = IniFile_GetKeyValueInt(ini, "first_section", "five");
|
2019-04-08 10:26:46 +03:00
|
|
|
|
2014-03-25 06:41:30 +04:00
|
|
|
if (iValue != 5)
|
|
|
|
{
|
|
|
|
printf("IniFile_GetKeyValueInt failure\n");
|
2020-05-15 15:49:52 +03:00
|
|
|
goto fail;
|
2014-03-25 06:41:30 +04:00
|
|
|
}
|
2019-04-08 10:26:46 +03:00
|
|
|
|
2014-03-25 06:41:30 +04:00
|
|
|
sValue = IniFile_GetKeyValueString(ini, "first_section", "animal");
|
2019-04-08 10:26:46 +03:00
|
|
|
|
2014-03-25 06:41:30 +04:00
|
|
|
if (strcmp(sValue, "BIRD") != 0)
|
|
|
|
{
|
|
|
|
printf("IniFile_GetKeyValueString failure\n");
|
2020-05-15 15:49:52 +03:00
|
|
|
goto fail;
|
2014-03-25 06:41:30 +04:00
|
|
|
}
|
2019-04-08 10:26:46 +03:00
|
|
|
|
2014-03-25 06:41:30 +04:00
|
|
|
sValue = IniFile_GetKeyValueString(ini, "second_section", "path");
|
2019-04-08 10:26:46 +03:00
|
|
|
|
2014-03-25 06:41:30 +04:00
|
|
|
if (strcmp(sValue, "/usr/local/bin") != 0)
|
|
|
|
{
|
|
|
|
printf("IniFile_GetKeyValueString failure\n");
|
2020-05-15 15:49:52 +03:00
|
|
|
goto fail;
|
2014-03-25 06:41:30 +04:00
|
|
|
}
|
2019-04-08 10:26:46 +03:00
|
|
|
|
2014-03-25 06:41:30 +04:00
|
|
|
sValue = IniFile_GetKeyValueString(ini, "second_section", "URL");
|
2019-04-08 10:26:46 +03:00
|
|
|
|
2014-03-25 06:41:30 +04:00
|
|
|
if (strcmp(sValue, "http://www.example.com/~username") != 0)
|
|
|
|
{
|
|
|
|
printf("IniFile_GetKeyValueString failure\n");
|
2020-05-15 15:49:52 +03:00
|
|
|
goto fail;
|
2014-03-25 06:41:30 +04:00
|
|
|
}
|
2019-04-08 10:26:46 +03:00
|
|
|
|
2014-03-25 02:20:34 +04:00
|
|
|
IniFile_Free(ini);
|
2014-03-25 08:32:05 +04:00
|
|
|
/* Second Sample */
|
|
|
|
ini = IniFile_New();
|
2020-05-15 15:49:52 +03:00
|
|
|
if (!ini)
|
|
|
|
goto fail;
|
|
|
|
if (IniFile_ReadBuffer(ini, TEST_INI_02) < 0)
|
|
|
|
goto fail;
|
|
|
|
free(sectionNames);
|
2014-03-25 08:32:05 +04:00
|
|
|
sectionNames = IniFile_GetSectionNames(ini, &nSections);
|
2020-05-15 15:49:52 +03:00
|
|
|
if (!sectionNames && (nSections > 0))
|
|
|
|
goto fail;
|
2019-04-08 10:26:46 +03:00
|
|
|
|
2014-03-25 08:32:05 +04:00
|
|
|
for (i = 0; i < nSections; i++)
|
|
|
|
{
|
2020-05-15 15:49:52 +03:00
|
|
|
free(keyNames);
|
2014-03-25 08:32:05 +04:00
|
|
|
keyNames = IniFile_GetSectionKeyNames(ini, sectionNames[i], &nKeys);
|
|
|
|
printf("[%s]\n", sectionNames[i]);
|
2019-04-08 10:26:46 +03:00
|
|
|
|
2020-05-15 15:49:52 +03:00
|
|
|
if (!keyNames && (nKeys > 0))
|
|
|
|
goto fail;
|
2014-03-25 08:32:05 +04:00
|
|
|
for (j = 0; j < nKeys; j++)
|
|
|
|
{
|
|
|
|
sValue = IniFile_GetKeyValueString(ini, sectionNames[i], keyNames[j]);
|
|
|
|
printf("%s = %s\n", keyNames[j], sValue);
|
|
|
|
}
|
|
|
|
}
|
2019-04-08 10:26:46 +03:00
|
|
|
|
2014-03-25 08:32:05 +04:00
|
|
|
IniFile_Free(ini);
|
2019-04-08 10:26:46 +03:00
|
|
|
/* Third sample - invalid input */
|
|
|
|
ini = IniFile_New();
|
|
|
|
|
|
|
|
if (IniFile_ReadBuffer(ini, TEST_INI_03) != -1)
|
2020-05-15 15:49:52 +03:00
|
|
|
goto fail;
|
2019-04-08 10:26:46 +03:00
|
|
|
|
2020-05-15 15:49:52 +03:00
|
|
|
rc = 0;
|
|
|
|
fail:
|
|
|
|
free(keyNames);
|
|
|
|
free(sectionNames);
|
2019-04-08 10:26:46 +03:00
|
|
|
IniFile_Free(ini);
|
2021-11-29 16:29:07 +03:00
|
|
|
return rc;
|
2014-03-25 02:20:34 +04:00
|
|
|
}
|