A simple .ini file interface.

Dependents:   Smart-WiFly-WebServer SignalGenerator WattEye X10Svr

Committer:
WiredHome
Date:
Tue Jan 26 11:51:08 2016 +0000
Revision:
11:738604f18088
Parent:
10:57b93035ad82
Child:
12:6cf929bde139
Hook the private memory manager when configured, and standard malloc/free when not.

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