FreeRDP
TestIni.c
1 
2 #include <winpr/crt.h>
3 #include <winpr/ini.h>
4 
5 static const char TEST_INI_01[] = "; This is a sample .ini config file\n"
6  "\n"
7  "[first_section]\n"
8  "one = 1\n"
9  "five = 5\n"
10  "animal = BIRD\n"
11  "\n"
12  "[second_section]\n"
13  "path = \"/usr/local/bin\"\n"
14  "URL = \"http://www.example.com/~username\"\n"
15  "\n";
16 
17 static const char TEST_INI_02[] = "[FreeRDS]\n"
18  "prefix=\"/usr/local\"\n"
19  "bindir=\"bin\"\n"
20  "sbindir=\"sbin\"\n"
21  "libdir=\"lib\"\n"
22  "datarootdir=\"share\"\n"
23  "localstatedir=\"var\"\n"
24  "sysconfdir=\"etc\"\n"
25  "\n";
26 
27 static const char TEST_INI_03[] = "[FreeRDS]\n"
28  "prefix=\"/usr/local\"\n"
29  "bindir=\"bin\"\n"
30  "# some illegal string\n"
31  "sbindir=\"sbin\"\n"
32  "libdir=\"lib\"\n"
33  "invalid key-value pair\n"
34  "datarootdir=\"share\"\n"
35  "localstatedir=\"var\"\n"
36  "sysconfdir=\"etc\"\n"
37  "\n";
38 
39 int TestIni(int argc, char* argv[])
40 {
41  int rc = -1;
42  size_t nKeys = 0;
43  size_t nSections = 0;
44  UINT32 iValue = 0;
45  wIniFile* ini = NULL;
46  const char* sValue = NULL;
47  char** keyNames = NULL;
48  char** sectionNames = NULL;
49 
50  WINPR_UNUSED(argc);
51  WINPR_UNUSED(argv);
52 
53  /* First Sample */
54  ini = IniFile_New();
55  if (!ini)
56  goto fail;
57 
58  if (IniFile_ReadBuffer(ini, TEST_INI_01) < 0)
59  goto fail;
60 
61  free(sectionNames);
62  sectionNames = IniFile_GetSectionNames(ini, &nSections);
63  if (!sectionNames && (nSections > 0))
64  goto fail;
65 
66  for (size_t i = 0; i < nSections; i++)
67  {
68  free(keyNames);
69  keyNames = IniFile_GetSectionKeyNames(ini, sectionNames[i], &nKeys);
70  printf("[%s]\n", sectionNames[i]);
71  if (!keyNames && (nKeys > 0))
72  goto fail;
73  for (size_t j = 0; j < nKeys; j++)
74  {
75  sValue = IniFile_GetKeyValueString(ini, sectionNames[i], keyNames[j]);
76  printf("%s = %s\n", keyNames[j], sValue);
77  }
78  }
79 
80  iValue = IniFile_GetKeyValueInt(ini, "first_section", "one");
81 
82  if (iValue != 1)
83  {
84  printf("IniFile_GetKeyValueInt failure\n");
85  goto fail;
86  }
87 
88  iValue = IniFile_GetKeyValueInt(ini, "first_section", "five");
89 
90  if (iValue != 5)
91  {
92  printf("IniFile_GetKeyValueInt failure\n");
93  goto fail;
94  }
95 
96  sValue = IniFile_GetKeyValueString(ini, "first_section", "animal");
97 
98  if (strcmp(sValue, "BIRD") != 0)
99  {
100  printf("IniFile_GetKeyValueString failure\n");
101  goto fail;
102  }
103 
104  sValue = IniFile_GetKeyValueString(ini, "second_section", "path");
105 
106  if (strcmp(sValue, "/usr/local/bin") != 0)
107  {
108  printf("IniFile_GetKeyValueString failure\n");
109  goto fail;
110  }
111 
112  sValue = IniFile_GetKeyValueString(ini, "second_section", "URL");
113 
114  if (strcmp(sValue, "http://www.example.com/~username") != 0)
115  {
116  printf("IniFile_GetKeyValueString failure\n");
117  goto fail;
118  }
119 
120  IniFile_Free(ini);
121  /* Second Sample */
122  ini = IniFile_New();
123  if (!ini)
124  goto fail;
125  if (IniFile_ReadBuffer(ini, TEST_INI_02) < 0)
126  goto fail;
127  free(sectionNames);
128  sectionNames = IniFile_GetSectionNames(ini, &nSections);
129  if (!sectionNames && (nSections > 0))
130  goto fail;
131 
132  for (size_t i = 0; i < nSections; i++)
133  {
134  free(keyNames);
135  keyNames = IniFile_GetSectionKeyNames(ini, sectionNames[i], &nKeys);
136  printf("[%s]\n", sectionNames[i]);
137 
138  if (!keyNames && (nKeys > 0))
139  goto fail;
140  for (size_t j = 0; j < nKeys; j++)
141  {
142  sValue = IniFile_GetKeyValueString(ini, sectionNames[i], keyNames[j]);
143  printf("%s = %s\n", keyNames[j], sValue);
144  }
145  }
146 
147  IniFile_Free(ini);
148  /* Third sample - invalid input */
149  ini = IniFile_New();
150 
151  if (IniFile_ReadBuffer(ini, TEST_INI_03) != -1)
152  goto fail;
153 
154  rc = 0;
155 fail:
156  free(keyNames);
157  free(sectionNames);
158  IniFile_Free(ini);
159  return rc;
160 }