A simple .ini file interface.

Dependents:   Smart-WiFly-WebServer SignalGenerator WattEye X10Svr

Committer:
WiredHome
Date:
Mon Aug 12 22:57:54 2013 +0000
Revision:
0:ae5bf432c249
Child:
1:1e2ee9bbee40
A simple ini file interface.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
WiredHome 0:ae5bf432c249 1 // Simple INI file manager.
WiredHome 0:ae5bf432c249 2 //
WiredHome 0:ae5bf432c249 3 #ifdef WIN32
WiredHome 0:ae5bf432c249 4 #include "string.h"
WiredHome 0:ae5bf432c249 5 #include "stdlib.h"
WiredHome 0:ae5bf432c249 6 #include "stdio.h"
WiredHome 0:ae5bf432c249 7 #else
WiredHome 0:ae5bf432c249 8 #include "mbed.h"
WiredHome 0:ae5bf432c249 9 #endif
WiredHome 0:ae5bf432c249 10
WiredHome 0:ae5bf432c249 11 #include "IniManager.h"
WiredHome 0:ae5bf432c249 12
WiredHome 0:ae5bf432c249 13 INI::INI(const char * file)
WiredHome 0:ae5bf432c249 14 : iniFile(0)
WiredHome 0:ae5bf432c249 15 {
WiredHome 0:ae5bf432c249 16 if (file) {
WiredHome 0:ae5bf432c249 17 iniFile = (char *)malloc(strlen(file)+1);
WiredHome 0:ae5bf432c249 18 if (iniFile)
WiredHome 0:ae5bf432c249 19 strcpy(iniFile, file);
WiredHome 0:ae5bf432c249 20 }
WiredHome 0:ae5bf432c249 21 FILE * fo = fopen("/local/test.txt", "wt");
WiredHome 0:ae5bf432c249 22 if (fo) {
WiredHome 0:ae5bf432c249 23 printf("Writing to /local/test.txt\r\n");
WiredHome 0:ae5bf432c249 24 fprintf(fo, "This is three - %d\r\n", 3);
WiredHome 0:ae5bf432c249 25 fclose(fo);
WiredHome 0:ae5bf432c249 26 }
WiredHome 0:ae5bf432c249 27 }
WiredHome 0:ae5bf432c249 28
WiredHome 0:ae5bf432c249 29
WiredHome 0:ae5bf432c249 30 INI::~INI(void)
WiredHome 0:ae5bf432c249 31 {
WiredHome 0:ae5bf432c249 32 if (iniFile)
WiredHome 0:ae5bf432c249 33 free(iniFile);
WiredHome 0:ae5bf432c249 34 }
WiredHome 0:ae5bf432c249 35
WiredHome 0:ae5bf432c249 36
WiredHome 0:ae5bf432c249 37 bool INI::ReadString(const char * section, const char * key, char * buffer, size_t bufferSize, const char * defaultString)
WiredHome 0:ae5bf432c249 38 {
WiredHome 0:ae5bf432c249 39 bool found = false;
WiredHome 0:ae5bf432c249 40 if (!iniFile)
WiredHome 0:ae5bf432c249 41 return found;
WiredHome 0:ae5bf432c249 42 CrashRecover();
WiredHome 0:ae5bf432c249 43 printf("ReadString from %s\r\n", iniFile);
WiredHome 0:ae5bf432c249 44 FILE * fp = fopen(iniFile,"rt");
WiredHome 0:ae5bf432c249 45 if (fp) {
WiredHome 0:ae5bf432c249 46 char buf[INTERNAL_BUF_SIZE];
WiredHome 0:ae5bf432c249 47 bool inSection = (section == NULL) ? true : false;
WiredHome 0:ae5bf432c249 48
WiredHome 0:ae5bf432c249 49 while(fgets(buf, sizeof(buf), fp)) {
WiredHome 0:ae5bf432c249 50 int x = strlen(buf) - 1; // remove trailing \r\n combinations
WiredHome 0:ae5bf432c249 51 while (x >= 0 && buf[x] < ' ')
WiredHome 0:ae5bf432c249 52 buf[x--] = '\0';
WiredHome 0:ae5bf432c249 53 printf("read in [%s]\r\n", buf);
WiredHome 0:ae5bf432c249 54 if (inSection && buf[0] != '[') {
WiredHome 0:ae5bf432c249 55 char * eq = strchr(buf, '=');
WiredHome 0:ae5bf432c249 56 if (eq) {
WiredHome 0:ae5bf432c249 57 *eq++ = '\0';
WiredHome 0:ae5bf432c249 58 if ( (strcmp(buf,key) == 0) && (strlen(eq) <= bufferSize) ) {
WiredHome 0:ae5bf432c249 59 strcpy(buffer, eq);
WiredHome 0:ae5bf432c249 60 memset(buf, 0, INTERNAL_BUF_SIZE); // secure the memory space
WiredHome 0:ae5bf432c249 61 found = true;
WiredHome 0:ae5bf432c249 62 break;
WiredHome 0:ae5bf432c249 63 }
WiredHome 0:ae5bf432c249 64 }
WiredHome 0:ae5bf432c249 65 } else {
WiredHome 0:ae5bf432c249 66 if (buf[0] == '[') {
WiredHome 0:ae5bf432c249 67 char * br = strchr(buf, ']');
WiredHome 0:ae5bf432c249 68 inSection = false;
WiredHome 0:ae5bf432c249 69 if (br) {
WiredHome 0:ae5bf432c249 70 *br = '\0';
WiredHome 0:ae5bf432c249 71 if (strcmp(buf+1, section) == 0)
WiredHome 0:ae5bf432c249 72 inSection = true;
WiredHome 0:ae5bf432c249 73 }
WiredHome 0:ae5bf432c249 74 }
WiredHome 0:ae5bf432c249 75 }
WiredHome 0:ae5bf432c249 76 }
WiredHome 0:ae5bf432c249 77 fclose(fp);
WiredHome 0:ae5bf432c249 78 }
WiredHome 0:ae5bf432c249 79 if (!found && defaultString != NULL && *defaultString) {
WiredHome 0:ae5bf432c249 80 strncpy(buffer, defaultString, bufferSize);
WiredHome 0:ae5bf432c249 81 buffer[bufferSize-1] = '\0';
WiredHome 0:ae5bf432c249 82 printf("sub %s.\r\n", buffer);
WiredHome 0:ae5bf432c249 83 found = true;
WiredHome 0:ae5bf432c249 84 }
WiredHome 0:ae5bf432c249 85 return found;
WiredHome 0:ae5bf432c249 86 }
WiredHome 0:ae5bf432c249 87
WiredHome 0:ae5bf432c249 88 bool INI::CrashRecover()
WiredHome 0:ae5bf432c249 89 {
WiredHome 0:ae5bf432c249 90 char * newFile = (char *)malloc(strlen(iniFile)+1);
WiredHome 0:ae5bf432c249 91 char * bakFile = (char *)malloc(strlen(iniFile)+1);
WiredHome 0:ae5bf432c249 92
WiredHome 0:ae5bf432c249 93 if (newFile && bakFile) {
WiredHome 0:ae5bf432c249 94 printf("*** CrashRecover\r\n");
WiredHome 0:ae5bf432c249 95 strcpy(bakFile, iniFile);
WiredHome 0:ae5bf432c249 96 strcpy(newFile, iniFile);
WiredHome 0:ae5bf432c249 97 strcpy(bakFile + strlen(bakFile) - 4, ".bak");
WiredHome 0:ae5bf432c249 98 strcpy(newFile + strlen(newFile) - 4, ".new");
WiredHome 0:ae5bf432c249 99
WiredHome 0:ae5bf432c249 100 FILE * repair = fopen(newFile, "rt");
WiredHome 0:ae5bf432c249 101 if (repair) {
WiredHome 0:ae5bf432c249 102 // helps recover if the system crashed before it could swap in the new file
WiredHome 0:ae5bf432c249 103 printf("*** repairing\r\n");
WiredHome 0:ae5bf432c249 104 fclose(repair);
WiredHome 0:ae5bf432c249 105 int i;
WiredHome 0:ae5bf432c249 106 i = remove(bakFile); // remove an old .bak
WiredHome 0:ae5bf432c249 107 printf("remove(%s) returned %d\r\n", bakFile, i);
WiredHome 0:ae5bf432c249 108 i = Rename(iniFile, bakFile); // move the existing .ini to .bak
WiredHome 0:ae5bf432c249 109 printf("rename(%s,%s) returned %d\r\n", iniFile, bakFile, i);
WiredHome 0:ae5bf432c249 110 i = Rename(newFile, iniFile); // move the new .new to .ini
WiredHome 0:ae5bf432c249 111 printf("rename(%s,%s) returned %d\r\n", newFile, iniFile, i);
WiredHome 0:ae5bf432c249 112 }
WiredHome 0:ae5bf432c249 113 }
WiredHome 0:ae5bf432c249 114 free(newFile);
WiredHome 0:ae5bf432c249 115 free(bakFile);
WiredHome 0:ae5bf432c249 116 return true;
WiredHome 0:ae5bf432c249 117 }
WiredHome 0:ae5bf432c249 118
WiredHome 0:ae5bf432c249 119 // Create the new version as .new
WiredHome 0:ae5bf432c249 120 // once complete, if something actually changed, then rename the .ini to .bak and rename the .new to .ini
WiredHome 0:ae5bf432c249 121 // once complete, if nothing actually changed, then delete the .new
WiredHome 0:ae5bf432c249 122 //
WiredHome 0:ae5bf432c249 123 bool INI::WriteString(const char * section, const char * key, char * value)
WiredHome 0:ae5bf432c249 124 {
WiredHome 0:ae5bf432c249 125 bool found = false;
WiredHome 0:ae5bf432c249 126 bool fileChanged = false;
WiredHome 0:ae5bf432c249 127
WiredHome 0:ae5bf432c249 128 if (!iniFile || (value != NULL && strlen(value) > INTERNAL_BUF_SIZE))
WiredHome 0:ae5bf432c249 129 return found;
WiredHome 0:ae5bf432c249 130
WiredHome 0:ae5bf432c249 131 char * newFile = (char *)malloc(strlen(iniFile)+1);
WiredHome 0:ae5bf432c249 132 char * bakFile = (char *)malloc(strlen(iniFile)+1);
WiredHome 0:ae5bf432c249 133 if (!newFile)
WiredHome 0:ae5bf432c249 134 return found; // no memory
WiredHome 0:ae5bf432c249 135 if (!bakFile) {
WiredHome 0:ae5bf432c249 136 free(newFile);
WiredHome 0:ae5bf432c249 137 return found;
WiredHome 0:ae5bf432c249 138 }
WiredHome 0:ae5bf432c249 139 strcpy(bakFile, iniFile);
WiredHome 0:ae5bf432c249 140 strcpy(newFile, iniFile);
WiredHome 0:ae5bf432c249 141 strcpy(bakFile + strlen(bakFile) - 4, ".bak");
WiredHome 0:ae5bf432c249 142 strcpy(newFile + strlen(newFile) - 4, ".new");
WiredHome 0:ae5bf432c249 143
WiredHome 0:ae5bf432c249 144 CrashRecover();
WiredHome 0:ae5bf432c249 145
WiredHome 0:ae5bf432c249 146 printf("Opening [%s] and [%s]\r\n", iniFile, newFile);
WiredHome 0:ae5bf432c249 147 FILE * fi = fopen(iniFile, "rt");
WiredHome 0:ae5bf432c249 148 FILE * fo = fopen(newFile, "wt");
WiredHome 0:ae5bf432c249 149 if (fo) {
WiredHome 0:ae5bf432c249 150 char buf[INTERNAL_BUF_SIZE];
WiredHome 0:ae5bf432c249 151 bool inSection = (section == NULL) ? true : false;
WiredHome 0:ae5bf432c249 152
WiredHome 0:ae5bf432c249 153 if (fi) {
WiredHome 0:ae5bf432c249 154 while(fgets(buf, sizeof(buf), fi)) {
WiredHome 0:ae5bf432c249 155 // if not inSection, copy across
WiredHome 0:ae5bf432c249 156 // if inSection and not key, copy across
WiredHome 0:ae5bf432c249 157 // if InSection and key, write new value (or skip if value is null)
WiredHome 0:ae5bf432c249 158 int x = strlen(buf) - 1; // remove trailing \r\n combinations
WiredHome 0:ae5bf432c249 159 while (x >= 0 && buf[x] < ' ')
WiredHome 0:ae5bf432c249 160 buf[x--] = '\0';
WiredHome 0:ae5bf432c249 161 if (inSection && buf[0] != '[') {
WiredHome 0:ae5bf432c249 162 char * eq = strchr(buf, '=');
WiredHome 0:ae5bf432c249 163 if (eq) {
WiredHome 0:ae5bf432c249 164 *eq++ = '\0';
WiredHome 0:ae5bf432c249 165 if (strcmp(buf,key) == 0) {
WiredHome 0:ae5bf432c249 166 if (value != NULL && strcmp(eq, value) != 0) {
WiredHome 0:ae5bf432c249 167 // replace the old record
WiredHome 0:ae5bf432c249 168 if (value != NULL) {
WiredHome 0:ae5bf432c249 169 fprintf(fo, "%s=%s\n", key, value);
WiredHome 0:ae5bf432c249 170 printf("write: %s=%s\r\n", key, value);
WiredHome 0:ae5bf432c249 171 }
WiredHome 0:ae5bf432c249 172 }
WiredHome 0:ae5bf432c249 173 fileChanged = true;
WiredHome 0:ae5bf432c249 174 inSection = false;
WiredHome 0:ae5bf432c249 175 found = true;
WiredHome 0:ae5bf432c249 176 } else {
WiredHome 0:ae5bf432c249 177 // write old record
WiredHome 0:ae5bf432c249 178 fprintf(fo, "%s=%s\n", buf, eq);
WiredHome 0:ae5bf432c249 179 printf("write: %s=%s\r\n", buf, eq);
WiredHome 0:ae5bf432c249 180 }
WiredHome 0:ae5bf432c249 181 } else {
WiredHome 0:ae5bf432c249 182 // what to do with unknown record(s)?
WiredHome 0:ae5bf432c249 183 // fprintf(fo, "%s\n", buf); // eliminate them
WiredHome 0:ae5bf432c249 184 }
WiredHome 0:ae5bf432c249 185 } else {
WiredHome 0:ae5bf432c249 186 if (buf[0] == '[') {
WiredHome 0:ae5bf432c249 187 char * br = strchr(buf, ']');
WiredHome 0:ae5bf432c249 188 if (inSection) { // found next section while in good section
WiredHome 0:ae5bf432c249 189 // Append new record to desired section
WiredHome 0:ae5bf432c249 190 if (value != NULL) {
WiredHome 0:ae5bf432c249 191 fprintf(fo, "%s=%s\r\n", key, value);
WiredHome 0:ae5bf432c249 192 printf("write: %s=%s\r\n", key, value);
WiredHome 0:ae5bf432c249 193 fileChanged = true;
WiredHome 0:ae5bf432c249 194 }
WiredHome 0:ae5bf432c249 195 found = true;
WiredHome 0:ae5bf432c249 196 }
WiredHome 0:ae5bf432c249 197 inSection = false;
WiredHome 0:ae5bf432c249 198 // write old record
WiredHome 0:ae5bf432c249 199 fprintf(fo, "%s\r\n", buf);
WiredHome 0:ae5bf432c249 200 printf("write: %s\r\n", buf);
WiredHome 0:ae5bf432c249 201 if (br) {
WiredHome 0:ae5bf432c249 202 *br = '\0';
WiredHome 0:ae5bf432c249 203 if (strcmp(buf+1, section) == 0)
WiredHome 0:ae5bf432c249 204 inSection = true;
WiredHome 0:ae5bf432c249 205 }
WiredHome 0:ae5bf432c249 206 } else {
WiredHome 0:ae5bf432c249 207 // copy unaltered records across
WiredHome 0:ae5bf432c249 208 if (buf[0]) {
WiredHome 0:ae5bf432c249 209 fprintf(fo, "%s\r\n", buf);
WiredHome 0:ae5bf432c249 210 printf("write: %s\r\n", buf);
WiredHome 0:ae5bf432c249 211 }
WiredHome 0:ae5bf432c249 212 }
WiredHome 0:ae5bf432c249 213 }
WiredHome 0:ae5bf432c249 214 }
WiredHome 0:ae5bf432c249 215 printf("close %s\r\n", iniFile);
WiredHome 0:ae5bf432c249 216 fclose(fi);
WiredHome 0:ae5bf432c249 217 }
WiredHome 0:ae5bf432c249 218 if (!found) {
WiredHome 0:ae5bf432c249 219 // No old file, just create it now
WiredHome 0:ae5bf432c249 220 if (value != NULL) {
WiredHome 0:ae5bf432c249 221 if (!inSection) {
WiredHome 0:ae5bf432c249 222 fprintf(fo, "[%s]\r\n", section);
WiredHome 0:ae5bf432c249 223 printf("write: [%s]\r\n", section);
WiredHome 0:ae5bf432c249 224 }
WiredHome 0:ae5bf432c249 225 fprintf(fo, "%s=%s\r\n", key, value);
WiredHome 0:ae5bf432c249 226 printf("write: %s=%s\r\n", key, value);
WiredHome 0:ae5bf432c249 227 fileChanged = true;
WiredHome 0:ae5bf432c249 228 }
WiredHome 0:ae5bf432c249 229 found = true;
WiredHome 0:ae5bf432c249 230 }
WiredHome 0:ae5bf432c249 231 printf("close %s\r\n", newFile);
WiredHome 0:ae5bf432c249 232 fclose(fo);
WiredHome 0:ae5bf432c249 233 }
WiredHome 0:ae5bf432c249 234 if (fileChanged) {
WiredHome 0:ae5bf432c249 235 printf("remove bak, rename ini to bak, rename new to ini\r\n");
WiredHome 0:ae5bf432c249 236 remove(bakFile); // remove an old .bak
WiredHome 0:ae5bf432c249 237 Rename(iniFile, bakFile); // move the existing .ini to .bak
WiredHome 0:ae5bf432c249 238 Rename(newFile, iniFile); // move the new .new to .ini
WiredHome 0:ae5bf432c249 239 wait(1);
WiredHome 0:ae5bf432c249 240 }
WiredHome 0:ae5bf432c249 241 free(newFile);
WiredHome 0:ae5bf432c249 242 free(bakFile);
WiredHome 0:ae5bf432c249 243 return found;
WiredHome 0:ae5bf432c249 244 }
WiredHome 0:ae5bf432c249 245
WiredHome 0:ae5bf432c249 246
WiredHome 0:ae5bf432c249 247 //***********************************************************
WiredHome 0:ae5bf432c249 248 // Private version that also works with local file system
WiredHome 0:ae5bf432c249 249 // Returns -1 = error; 0 = success
WiredHome 0:ae5bf432c249 250 //***********************************************************
WiredHome 0:ae5bf432c249 251 int INI::Rename(const char *oldfname, const char *newfname)
WiredHome 0:ae5bf432c249 252 {
WiredHome 0:ae5bf432c249 253 int retval = 0;
WiredHome 0:ae5bf432c249 254 int ch;
WiredHome 0:ae5bf432c249 255
WiredHome 0:ae5bf432c249 256 FILE *fpold = fopen(oldfname, "r"); // src file
WiredHome 0:ae5bf432c249 257 FILE *fpnew = fopen(newfname, "w"); // dest file
WiredHome 0:ae5bf432c249 258
WiredHome 0:ae5bf432c249 259 while (1) { // Copy src to dest
WiredHome 0:ae5bf432c249 260 ch = fgetc(fpold); // until src EOF read.
WiredHome 0:ae5bf432c249 261 if (ch == EOF) break;
WiredHome 0:ae5bf432c249 262 fputc(ch, fpnew);
WiredHome 0:ae5bf432c249 263 }
WiredHome 0:ae5bf432c249 264
WiredHome 0:ae5bf432c249 265 fclose(fpnew);
WiredHome 0:ae5bf432c249 266 fclose(fpold);
WiredHome 0:ae5bf432c249 267
WiredHome 0:ae5bf432c249 268 fpnew = fopen(newfname, "r"); // Reopen dest to insure
WiredHome 0:ae5bf432c249 269 if(fpnew == NULL) { // that it was created.
WiredHome 0:ae5bf432c249 270 retval = (-1); // Return Error.
WiredHome 0:ae5bf432c249 271 } else {
WiredHome 0:ae5bf432c249 272 fclose(fpnew);
WiredHome 0:ae5bf432c249 273 remove(oldfname); // Remove original file.
WiredHome 0:ae5bf432c249 274 retval = (0); // Return Success.
WiredHome 0:ae5bf432c249 275 }
WiredHome 0:ae5bf432c249 276 return (retval);
WiredHome 0:ae5bf432c249 277 }
WiredHome 0:ae5bf432c249 278
WiredHome 0:ae5bf432c249 279 //***********************************************************
WiredHome 0:ae5bf432c249 280 // Private version that also works with local file system
WiredHome 0:ae5bf432c249 281 // Returns -1 = error; 0 = success
WiredHome 0:ae5bf432c249 282 //***********************************************************
WiredHome 0:ae5bf432c249 283 int INI::Copy(const char *src, const char *dst)
WiredHome 0:ae5bf432c249 284 {
WiredHome 0:ae5bf432c249 285 int retval = 0;
WiredHome 0:ae5bf432c249 286 int ch;
WiredHome 0:ae5bf432c249 287
WiredHome 0:ae5bf432c249 288 FILE *fpsrc = fopen(src, "r"); // src file
WiredHome 0:ae5bf432c249 289 FILE *fpdst = fopen(dst, "w"); // dest file
WiredHome 0:ae5bf432c249 290
WiredHome 0:ae5bf432c249 291 while (1) { // Copy src to dest
WiredHome 0:ae5bf432c249 292 ch = fgetc(fpsrc); // until src EOF read.
WiredHome 0:ae5bf432c249 293 if (ch == EOF) break;
WiredHome 0:ae5bf432c249 294 fputc(ch, fpdst);
WiredHome 0:ae5bf432c249 295 }
WiredHome 0:ae5bf432c249 296 fclose(fpsrc);
WiredHome 0:ae5bf432c249 297 fclose(fpdst);
WiredHome 0:ae5bf432c249 298
WiredHome 0:ae5bf432c249 299 fpdst = fopen(dst, "r"); // Reopen dest to insure
WiredHome 0:ae5bf432c249 300 if(fpdst == NULL) { // that it was created.
WiredHome 0:ae5bf432c249 301 retval = (-1); // Return error.
WiredHome 0:ae5bf432c249 302 } else {
WiredHome 0:ae5bf432c249 303 fclose(fpdst);
WiredHome 0:ae5bf432c249 304 retval = (0); // Return success.
WiredHome 0:ae5bf432c249 305 }
WiredHome 0:ae5bf432c249 306 return (retval);
WiredHome 0:ae5bf432c249 307 }
WiredHome 0:ae5bf432c249 308
WiredHome 0:ae5bf432c249 309
WiredHome 0:ae5bf432c249 310 #if 0
WiredHome 0:ae5bf432c249 311 // Test code for basic regression testing
WiredHome 0:ae5bf432c249 312 //
WiredHome 0:ae5bf432c249 313 #include <stdio.h>
WiredHome 0:ae5bf432c249 314 #include <assert.h>
WiredHome 0:ae5bf432c249 315 #include <string.h>
WiredHome 0:ae5bf432c249 316
WiredHome 0:ae5bf432c249 317 #include "INI.h"
WiredHome 0:ae5bf432c249 318
WiredHome 0:ae5bf432c249 319 #define TESTFILE "test.ini"
WiredHome 0:ae5bf432c249 320
WiredHome 0:ae5bf432c249 321 int main(int argc, char * argv[])
WiredHome 0:ae5bf432c249 322 {
WiredHome 0:ae5bf432c249 323 FILE * fp;
WiredHome 0:ae5bf432c249 324 char buffer[100];
WiredHome 0:ae5bf432c249 325 INI ini(TESTFILE);
WiredHome 0:ae5bf432c249 326
WiredHome 0:ae5bf432c249 327 // Start testing
WiredHome 0:ae5bf432c249 328 _unlink(TESTFILE);
WiredHome 0:ae5bf432c249 329 assert(ini.ReadString("Section 1", "Name 1", buffer, sizeof(buffer)) == false);
WiredHome 0:ae5bf432c249 330
WiredHome 0:ae5bf432c249 331 fp = fopen(TESTFILE, "wt");
WiredHome 0:ae5bf432c249 332 assert(fp);
WiredHome 0:ae5bf432c249 333 fprintf(fp, "[Section 1]\n");
WiredHome 0:ae5bf432c249 334 fprintf(fp, "Name 1=Value 1\n");
WiredHome 0:ae5bf432c249 335 fprintf(fp, "Name 2=Value 2\n");
WiredHome 0:ae5bf432c249 336 fprintf(fp, "\n");
WiredHome 0:ae5bf432c249 337 fprintf(fp, "[Section 2]\n");
WiredHome 0:ae5bf432c249 338 fprintf(fp, "Name 1=Value 2\n");
WiredHome 0:ae5bf432c249 339 fprintf(fp, "Name 2=Value 2\n");
WiredHome 0:ae5bf432c249 340 fprintf(fp, "Name 3=Value 3\n");
WiredHome 0:ae5bf432c249 341 fprintf(fp, "\n");
WiredHome 0:ae5bf432c249 342 fclose(fp);
WiredHome 0:ae5bf432c249 343
WiredHome 0:ae5bf432c249 344 assert(ini.ReadString("Section 2", "Name 2", buffer, sizeof(buffer)) == true);
WiredHome 0:ae5bf432c249 345 assert(strcmp("Value 2", buffer) == 0);
WiredHome 0:ae5bf432c249 346
WiredHome 0:ae5bf432c249 347 assert(ini.ReadString("Section 3", "Name", buffer, sizeof(buffer)) == false);
WiredHome 0:ae5bf432c249 348 assert(ini.ReadString("Section 1", "Name 3", buffer, sizeof(buffer)) == false);
WiredHome 0:ae5bf432c249 349
WiredHome 0:ae5bf432c249 350 assert(ini.WriteString("Section 1", "Name 4", "Value 4") == true);
WiredHome 0:ae5bf432c249 351 assert(ini.ReadString("Section 1", "Name 2", buffer, sizeof(buffer)) == true);
WiredHome 0:ae5bf432c249 352 assert(ini.ReadString("Section 1", "Name 3", buffer, sizeof(buffer)) == false);
WiredHome 0:ae5bf432c249 353 assert(ini.ReadString("Section 1", "Name 4", buffer, sizeof(buffer)) == true);
WiredHome 0:ae5bf432c249 354 assert(strcmp("Value 4", buffer) == 0);
WiredHome 0:ae5bf432c249 355
WiredHome 0:ae5bf432c249 356 assert(ini.WriteString("Section 1", "Name 4", NULL) == true);
WiredHome 0:ae5bf432c249 357 assert(ini.ReadString("Section 1", "Name 4", buffer, sizeof(buffer)) == false);
WiredHome 0:ae5bf432c249 358
WiredHome 0:ae5bf432c249 359 return 0;
WiredHome 0:ae5bf432c249 360 }
WiredHome 0:ae5bf432c249 361 #endif