A simple .ini file interface.

Dependents:   Smart-WiFly-WebServer SignalGenerator WattEye X10Svr

Committer:
WiredHome
Date:
Wed Mar 26 11:20:18 2014 +0000
Revision:
5:bfeb0882bd82
Parent:
4:70042853d43b
Child:
7:60f5dc3467ff
Update to permit deferring the filespec to later, which also permits changing the filespec w/o having to destroy and reconstruct the object.

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