A simple .ini file interface.

Dependents:   Smart-WiFly-WebServer SignalGenerator WattEye X10Svr

Committer:
WiredHome
Date:
Sun Sep 04 02:40:40 2016 +0000
Revision:
13:d5957065d066
Parent:
12:6cf929bde139
Child:
15:3fc2b87a234d
Using standard memory manager

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 13:d5957065d066 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 12:6cf929bde139 136 long int INI::ReadLongInt(const char * section, const char * key, long int defaultValue)
WiredHome 12:6cf929bde139 137 {
WiredHome 12:6cf929bde139 138 char localBuf[16];
WiredHome 12:6cf929bde139 139
WiredHome 12:6cf929bde139 140 if (ReadString(section, key, localBuf, sizeof(localBuf))) {
WiredHome 12:6cf929bde139 141 return atol(localBuf);
WiredHome 12:6cf929bde139 142 } else {
WiredHome 12:6cf929bde139 143 return defaultValue;
WiredHome 12:6cf929bde139 144 }
WiredHome 12:6cf929bde139 145 }
WiredHome 12:6cf929bde139 146
WiredHome 8:f128b10dfab1 147 bool INI::CleanUp()
WiredHome 0:ae5bf432c249 148 {
WiredHome 11:738604f18088 149 char * newFile = (char *)swMalloc(strlen(iniFile)+1);
WiredHome 11:738604f18088 150 char * bakFile = (char *)swMalloc(strlen(iniFile)+1);
WiredHome 0:ae5bf432c249 151
WiredHome 0:ae5bf432c249 152 if (newFile && bakFile) {
WiredHome 8:f128b10dfab1 153 INFO("CleanUp");
WiredHome 0:ae5bf432c249 154 strcpy(bakFile, iniFile);
WiredHome 0:ae5bf432c249 155 strcpy(newFile, iniFile);
WiredHome 0:ae5bf432c249 156 strcpy(bakFile + strlen(bakFile) - 4, ".bak");
WiredHome 0:ae5bf432c249 157 strcpy(newFile + strlen(newFile) - 4, ".new");
WiredHome 0:ae5bf432c249 158
WiredHome 8:f128b10dfab1 159 if (Exists(newFile)) {
WiredHome 2:c63a794c1fee 160 int i;
WiredHome 9:4947b8c244e9 161 i = i; // suppress warning about i not used when !DEBUG
WiredHome 0:ae5bf432c249 162 // helps recover if the system crashed before it could swap in the new file
WiredHome 8:f128b10dfab1 163 INFO(" *** found %s, repairing ...", newFile);
WiredHome 0:ae5bf432c249 164 i = remove(bakFile); // remove an old .bak
WiredHome 8:f128b10dfab1 165 INFO(" remove(%s) returned %d", bakFile, i);
WiredHome 0:ae5bf432c249 166 i = Rename(iniFile, bakFile); // move the existing .ini to .bak
WiredHome 8:f128b10dfab1 167 INFO(" rename(%s,%s) returned %d", iniFile, bakFile, i);
WiredHome 0:ae5bf432c249 168 i = Rename(newFile, iniFile); // move the new .new to .ini
WiredHome 8:f128b10dfab1 169 INFO(" rename(%s,%s) returned %d", newFile, iniFile, i);
WiredHome 8:f128b10dfab1 170 } else {
WiredHome 8:f128b10dfab1 171 // nothing to do, move on silently.
WiredHome 0:ae5bf432c249 172 }
WiredHome 0:ae5bf432c249 173 }
WiredHome 11:738604f18088 174 swFree(newFile);
WiredHome 11:738604f18088 175 swFree(bakFile);
WiredHome 0:ae5bf432c249 176 return true;
WiredHome 0:ae5bf432c249 177 }
WiredHome 0:ae5bf432c249 178
WiredHome 0:ae5bf432c249 179 // Create the new version as .new
WiredHome 0:ae5bf432c249 180 // once complete, if something actually changed, then rename the .ini to .bak and rename the .new to .ini
WiredHome 0:ae5bf432c249 181 // once complete, if nothing actually changed, then delete the .new
WiredHome 0:ae5bf432c249 182 //
WiredHome 10:57b93035ad82 183 bool INI::WriteString(const char * section, const char * key, const char * value)
WiredHome 0:ae5bf432c249 184 {
WiredHome 0:ae5bf432c249 185 bool found = false;
WiredHome 0:ae5bf432c249 186 bool fileChanged = false;
WiredHome 0:ae5bf432c249 187
WiredHome 8:f128b10dfab1 188 INFO("WriteString(%s,%s,%s)", section, key, value);
WiredHome 0:ae5bf432c249 189 if (!iniFile || (value != NULL && strlen(value) > INTERNAL_BUF_SIZE))
WiredHome 0:ae5bf432c249 190 return found;
WiredHome 0:ae5bf432c249 191
WiredHome 11:738604f18088 192 char * newFile = (char *)swMalloc(strlen(iniFile)+1);
WiredHome 11:738604f18088 193 char * bakFile = (char *)swMalloc(strlen(iniFile)+1);
WiredHome 0:ae5bf432c249 194 if (!newFile)
WiredHome 0:ae5bf432c249 195 return found; // no memory
WiredHome 0:ae5bf432c249 196 if (!bakFile) {
WiredHome 11:738604f18088 197 swFree(newFile);
WiredHome 0:ae5bf432c249 198 return found;
WiredHome 0:ae5bf432c249 199 }
WiredHome 0:ae5bf432c249 200 strcpy(bakFile, iniFile);
WiredHome 0:ae5bf432c249 201 strcpy(newFile, iniFile);
WiredHome 0:ae5bf432c249 202 strcpy(bakFile + strlen(bakFile) - 4, ".bak");
WiredHome 0:ae5bf432c249 203 strcpy(newFile + strlen(newFile) - 4, ".new");
WiredHome 0:ae5bf432c249 204
WiredHome 8:f128b10dfab1 205 CleanUp();
WiredHome 0:ae5bf432c249 206
WiredHome 8:f128b10dfab1 207 INFO(" Opening [%s] and [%s]", iniFile, newFile);
WiredHome 0:ae5bf432c249 208 FILE * fi = fopen(iniFile, "rt");
WiredHome 0:ae5bf432c249 209 FILE * fo = fopen(newFile, "wt");
WiredHome 0:ae5bf432c249 210 if (fo) {
WiredHome 0:ae5bf432c249 211 char buf[INTERNAL_BUF_SIZE];
WiredHome 0:ae5bf432c249 212 bool inSection = (section == NULL) ? true : false;
WiredHome 0:ae5bf432c249 213
WiredHome 0:ae5bf432c249 214 if (fi) {
WiredHome 8:f128b10dfab1 215 INFO(" %s opened for reading", iniFile);
WiredHome 0:ae5bf432c249 216 while(fgets(buf, sizeof(buf), fi)) {
WiredHome 0:ae5bf432c249 217 // if not inSection, copy across
WiredHome 0:ae5bf432c249 218 // if inSection and not key, copy across
WiredHome 0:ae5bf432c249 219 // if InSection and key, write new value (or skip if value is null)
WiredHome 0:ae5bf432c249 220 int x = strlen(buf) - 1; // remove trailing \r\n combinations
WiredHome 0:ae5bf432c249 221 while (x >= 0 && buf[x] < ' ')
WiredHome 0:ae5bf432c249 222 buf[x--] = '\0';
WiredHome 8:f128b10dfab1 223
WiredHome 0:ae5bf432c249 224 if (inSection && buf[0] != '[') {
WiredHome 0:ae5bf432c249 225 char * eq = strchr(buf, '=');
WiredHome 0:ae5bf432c249 226 if (eq) {
WiredHome 0:ae5bf432c249 227 *eq++ = '\0';
WiredHome 0:ae5bf432c249 228 if (strcmp(buf,key) == 0) {
WiredHome 0:ae5bf432c249 229 if (value != NULL && strcmp(eq, value) != 0) {
WiredHome 0:ae5bf432c249 230 // replace the old record
WiredHome 0:ae5bf432c249 231 if (value != NULL) {
WiredHome 0:ae5bf432c249 232 fprintf(fo, "%s=%s\n", key, value);
WiredHome 0:ae5bf432c249 233 printf("write: %s=%s\r\n", key, value);
WiredHome 8:f128b10dfab1 234 INFO(" write: %s=%s", key, value);
WiredHome 0:ae5bf432c249 235 }
WiredHome 0:ae5bf432c249 236 }
WiredHome 0:ae5bf432c249 237 fileChanged = true;
WiredHome 0:ae5bf432c249 238 inSection = false;
WiredHome 0:ae5bf432c249 239 found = true;
WiredHome 0:ae5bf432c249 240 } else {
WiredHome 0:ae5bf432c249 241 // write old record
WiredHome 0:ae5bf432c249 242 fprintf(fo, "%s=%s\n", buf, eq);
WiredHome 8:f128b10dfab1 243 INFO(" write: %s=%s", buf, eq);
WiredHome 0:ae5bf432c249 244 }
WiredHome 0:ae5bf432c249 245 } else {
WiredHome 0:ae5bf432c249 246 // what to do with unknown record(s)?
WiredHome 0:ae5bf432c249 247 // fprintf(fo, "%s\n", buf); // eliminate them
WiredHome 0:ae5bf432c249 248 }
WiredHome 0:ae5bf432c249 249 } else {
WiredHome 0:ae5bf432c249 250 if (buf[0] == '[') {
WiredHome 0:ae5bf432c249 251 char * br = strchr(buf, ']');
WiredHome 0:ae5bf432c249 252 if (inSection) { // found next section while in good section
WiredHome 0:ae5bf432c249 253 // Append new record to desired section
WiredHome 0:ae5bf432c249 254 if (value != NULL) {
WiredHome 0:ae5bf432c249 255 fprintf(fo, "%s=%s\r\n", key, value);
WiredHome 8:f128b10dfab1 256 INFO(" write: %s=%s", key, value);
WiredHome 0:ae5bf432c249 257 fileChanged = true;
WiredHome 0:ae5bf432c249 258 }
WiredHome 0:ae5bf432c249 259 found = true;
WiredHome 0:ae5bf432c249 260 }
WiredHome 0:ae5bf432c249 261 inSection = false;
WiredHome 0:ae5bf432c249 262 // write old record
WiredHome 0:ae5bf432c249 263 fprintf(fo, "%s\r\n", buf);
WiredHome 8:f128b10dfab1 264 INFO(" write: %s", buf);
WiredHome 0:ae5bf432c249 265 if (br) {
WiredHome 0:ae5bf432c249 266 *br = '\0';
WiredHome 0:ae5bf432c249 267 if (strcmp(buf+1, section) == 0)
WiredHome 0:ae5bf432c249 268 inSection = true;
WiredHome 0:ae5bf432c249 269 }
WiredHome 0:ae5bf432c249 270 } else {
WiredHome 0:ae5bf432c249 271 // copy unaltered records across
WiredHome 0:ae5bf432c249 272 if (buf[0]) {
WiredHome 0:ae5bf432c249 273 fprintf(fo, "%s\r\n", buf);
WiredHome 8:f128b10dfab1 274 INFO(" write: %s", buf);
WiredHome 0:ae5bf432c249 275 }
WiredHome 0:ae5bf432c249 276 }
WiredHome 0:ae5bf432c249 277 }
WiredHome 0:ae5bf432c249 278 }
WiredHome 8:f128b10dfab1 279 INFO("close %s", iniFile);
WiredHome 0:ae5bf432c249 280 fclose(fi);
WiredHome 8:f128b10dfab1 281 } else {
WiredHome 8:f128b10dfab1 282 INFO(" %s did not previously exist.", iniFile);
WiredHome 0:ae5bf432c249 283 }
WiredHome 0:ae5bf432c249 284 if (!found) {
WiredHome 0:ae5bf432c249 285 // No old file, just create it now
WiredHome 0:ae5bf432c249 286 if (value != NULL) {
WiredHome 0:ae5bf432c249 287 if (!inSection) {
WiredHome 0:ae5bf432c249 288 fprintf(fo, "[%s]\r\n", section);
WiredHome 8:f128b10dfab1 289 INFO(" write: [%s]", section);
WiredHome 0:ae5bf432c249 290 }
WiredHome 0:ae5bf432c249 291 fprintf(fo, "%s=%s\r\n", key, value);
WiredHome 8:f128b10dfab1 292 INFO(" write: %s=%s", key, value);
WiredHome 0:ae5bf432c249 293 fileChanged = true;
WiredHome 0:ae5bf432c249 294 }
WiredHome 0:ae5bf432c249 295 found = true;
WiredHome 0:ae5bf432c249 296 }
WiredHome 8:f128b10dfab1 297 INFO(" close %s", newFile);
WiredHome 0:ae5bf432c249 298 fclose(fo);
WiredHome 8:f128b10dfab1 299 } else {
WiredHome 8:f128b10dfab1 300 ERR("*** Failed to open %s", newFile);
WiredHome 0:ae5bf432c249 301 }
WiredHome 0:ae5bf432c249 302 if (fileChanged) {
WiredHome 8:f128b10dfab1 303 INFO(" File changed: remove bak, rename ini to bak, rename new to ini");
WiredHome 0:ae5bf432c249 304 remove(bakFile); // remove an old .bak
WiredHome 8:f128b10dfab1 305 INFO(" a");
WiredHome 0:ae5bf432c249 306 Rename(iniFile, bakFile); // move the existing .ini to .bak
WiredHome 8:f128b10dfab1 307 INFO(" b");
WiredHome 0:ae5bf432c249 308 Rename(newFile, iniFile); // move the new .new to .ini
WiredHome 8:f128b10dfab1 309 INFO(" c");
WiredHome 4:70042853d43b 310 #ifdef RTOS_H
WiredHome 4:70042853d43b 311 Thread::wait(1000);
WiredHome 4:70042853d43b 312 #else
WiredHome 0:ae5bf432c249 313 wait(1);
WiredHome 4:70042853d43b 314 #endif
WiredHome 8:f128b10dfab1 315 INFO(" d");
WiredHome 0:ae5bf432c249 316 }
WiredHome 11:738604f18088 317 swFree(newFile);
WiredHome 11:738604f18088 318 swFree(bakFile);
WiredHome 0:ae5bf432c249 319 return found;
WiredHome 0:ae5bf432c249 320 }
WiredHome 0:ae5bf432c249 321
WiredHome 0:ae5bf432c249 322
WiredHome 0:ae5bf432c249 323 //***********************************************************
WiredHome 0:ae5bf432c249 324 // Private version that also works with local file system
WiredHome 8:f128b10dfab1 325 // by copying one file to the other.
WiredHome 0:ae5bf432c249 326 // Returns -1 = error; 0 = success
WiredHome 0:ae5bf432c249 327 //***********************************************************
WiredHome 0:ae5bf432c249 328 int INI::Rename(const char *oldfname, const char *newfname)
WiredHome 0:ae5bf432c249 329 {
WiredHome 0:ae5bf432c249 330 int retval = 0;
WiredHome 0:ae5bf432c249 331
WiredHome 8:f128b10dfab1 332 INFO("Rename(%s,%s)", oldfname, newfname);
WiredHome 8:f128b10dfab1 333 if (Copy(oldfname, newfname) == 0) {
WiredHome 8:f128b10dfab1 334 remove(oldfname);
WiredHome 8:f128b10dfab1 335 retval = 0;
WiredHome 0:ae5bf432c249 336 } else {
WiredHome 8:f128b10dfab1 337 retval = -1;
WiredHome 0:ae5bf432c249 338 }
WiredHome 0:ae5bf432c249 339 return (retval);
WiredHome 0:ae5bf432c249 340 }
WiredHome 0:ae5bf432c249 341
WiredHome 0:ae5bf432c249 342 //***********************************************************
WiredHome 0:ae5bf432c249 343 // Private version that also works with local file system
WiredHome 0:ae5bf432c249 344 // Returns -1 = error; 0 = success
WiredHome 0:ae5bf432c249 345 //***********************************************************
WiredHome 0:ae5bf432c249 346 int INI::Copy(const char *src, const char *dst)
WiredHome 0:ae5bf432c249 347 {
WiredHome 0:ae5bf432c249 348 int retval = 0;
WiredHome 0:ae5bf432c249 349 int ch;
WiredHome 0:ae5bf432c249 350
WiredHome 8:f128b10dfab1 351 INFO("Copy(%s,%s)", src, dst);
WiredHome 0:ae5bf432c249 352 FILE *fpsrc = fopen(src, "r"); // src file
WiredHome 0:ae5bf432c249 353 FILE *fpdst = fopen(dst, "w"); // dest file
WiredHome 0:ae5bf432c249 354
WiredHome 8:f128b10dfab1 355 if (fpsrc) {
WiredHome 8:f128b10dfab1 356 INFO(" c1a");
WiredHome 8:f128b10dfab1 357 if (fpdst) {
WiredHome 8:f128b10dfab1 358 INFO(" c1b");
WiredHome 8:f128b10dfab1 359 while (1) { // Copy src to dest
WiredHome 8:f128b10dfab1 360 ch = fgetc(fpsrc); // until src EOF read.
WiredHome 8:f128b10dfab1 361 if (ch == EOF) break;
WiredHome 8:f128b10dfab1 362 fputc(ch, fpdst);
WiredHome 8:f128b10dfab1 363 }
WiredHome 8:f128b10dfab1 364 INFO(" c2");
WiredHome 8:f128b10dfab1 365 fclose(fpsrc);
WiredHome 8:f128b10dfab1 366 fclose(fpdst);
WiredHome 8:f128b10dfab1 367 }
WiredHome 0:ae5bf432c249 368 }
WiredHome 8:f128b10dfab1 369 INFO(" c3");
WiredHome 0:ae5bf432c249 370
WiredHome 8:f128b10dfab1 371 if (Exists(dst)) {
WiredHome 8:f128b10dfab1 372 retval = 0;
WiredHome 0:ae5bf432c249 373 } else {
WiredHome 8:f128b10dfab1 374 retval = -1;
WiredHome 0:ae5bf432c249 375 }
WiredHome 8:f128b10dfab1 376 INFO(" c4");
WiredHome 0:ae5bf432c249 377 return (retval);
WiredHome 0:ae5bf432c249 378 }
WiredHome 0:ae5bf432c249 379
WiredHome 0:ae5bf432c249 380
WiredHome 0:ae5bf432c249 381 #if 0
WiredHome 0:ae5bf432c249 382 // Test code for basic regression testing
WiredHome 0:ae5bf432c249 383 //
WiredHome 0:ae5bf432c249 384 #include <stdio.h>
WiredHome 0:ae5bf432c249 385 #include <assert.h>
WiredHome 0:ae5bf432c249 386 #include <string.h>
WiredHome 0:ae5bf432c249 387
WiredHome 0:ae5bf432c249 388 #include "INI.h"
WiredHome 0:ae5bf432c249 389
WiredHome 0:ae5bf432c249 390 #define TESTFILE "test.ini"
WiredHome 0:ae5bf432c249 391
WiredHome 0:ae5bf432c249 392 int main(int argc, char * argv[])
WiredHome 0:ae5bf432c249 393 {
WiredHome 0:ae5bf432c249 394 FILE * fp;
WiredHome 0:ae5bf432c249 395 char buffer[100];
WiredHome 0:ae5bf432c249 396 INI ini(TESTFILE);
WiredHome 0:ae5bf432c249 397
WiredHome 0:ae5bf432c249 398 // Start testing
WiredHome 0:ae5bf432c249 399 _unlink(TESTFILE);
WiredHome 0:ae5bf432c249 400 assert(ini.ReadString("Section 1", "Name 1", buffer, sizeof(buffer)) == false);
WiredHome 0:ae5bf432c249 401
WiredHome 0:ae5bf432c249 402 fp = fopen(TESTFILE, "wt");
WiredHome 0:ae5bf432c249 403 assert(fp);
WiredHome 0:ae5bf432c249 404 fprintf(fp, "[Section 1]\n");
WiredHome 0:ae5bf432c249 405 fprintf(fp, "Name 1=Value 1\n");
WiredHome 0:ae5bf432c249 406 fprintf(fp, "Name 2=Value 2\n");
WiredHome 0:ae5bf432c249 407 fprintf(fp, "\n");
WiredHome 0:ae5bf432c249 408 fprintf(fp, "[Section 2]\n");
WiredHome 0:ae5bf432c249 409 fprintf(fp, "Name 1=Value 2\n");
WiredHome 0:ae5bf432c249 410 fprintf(fp, "Name 2=Value 2\n");
WiredHome 0:ae5bf432c249 411 fprintf(fp, "Name 3=Value 3\n");
WiredHome 0:ae5bf432c249 412 fprintf(fp, "\n");
WiredHome 0:ae5bf432c249 413 fclose(fp);
WiredHome 0:ae5bf432c249 414
WiredHome 0:ae5bf432c249 415 assert(ini.ReadString("Section 2", "Name 2", buffer, sizeof(buffer)) == true);
WiredHome 0:ae5bf432c249 416 assert(strcmp("Value 2", buffer) == 0);
WiredHome 0:ae5bf432c249 417
WiredHome 0:ae5bf432c249 418 assert(ini.ReadString("Section 3", "Name", buffer, sizeof(buffer)) == false);
WiredHome 0:ae5bf432c249 419 assert(ini.ReadString("Section 1", "Name 3", buffer, sizeof(buffer)) == false);
WiredHome 0:ae5bf432c249 420
WiredHome 0:ae5bf432c249 421 assert(ini.WriteString("Section 1", "Name 4", "Value 4") == true);
WiredHome 0:ae5bf432c249 422 assert(ini.ReadString("Section 1", "Name 2", buffer, sizeof(buffer)) == true);
WiredHome 0:ae5bf432c249 423 assert(ini.ReadString("Section 1", "Name 3", buffer, sizeof(buffer)) == false);
WiredHome 0:ae5bf432c249 424 assert(ini.ReadString("Section 1", "Name 4", buffer, sizeof(buffer)) == true);
WiredHome 0:ae5bf432c249 425 assert(strcmp("Value 4", buffer) == 0);
WiredHome 0:ae5bf432c249 426
WiredHome 0:ae5bf432c249 427 assert(ini.WriteString("Section 1", "Name 4", NULL) == true);
WiredHome 0:ae5bf432c249 428 assert(ini.ReadString("Section 1", "Name 4", buffer, sizeof(buffer)) == false);
WiredHome 0:ae5bf432c249 429
WiredHome 0:ae5bf432c249 430 return 0;
WiredHome 0:ae5bf432c249 431 }
WiredHome 0:ae5bf432c249 432 #endif
WiredHome 12:6cf929bde139 433
WiredHome 12:6cf929bde139 434