A simple .ini file interface.

Dependents:   Smart-WiFly-WebServer SignalGenerator WattEye X10Svr

Committer:
WiredHome
Date:
Thu May 22 16:09:43 2014 +0000
Revision:
7:60f5dc3467ff
Parent:
5:bfeb0882bd82
Child:
8:f128b10dfab1
Documentation updates.

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