A simple .ini file interface.

Dependents:   Smart-WiFly-WebServer SignalGenerator WattEye X10Svr

Committer:
WiredHome
Date:
Sun Mar 16 22:23:11 2014 +0000
Revision:
4:70042853d43b
Parent:
3:64fcaf06b012
Child:
5:bfeb0882bd82
use an OS wait if the mbed_rtos is in use (#ifdef RTOS_H).

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