A simple .ini file interface.
Dependents: Smart-WiFly-WebServer SignalGenerator WattEye X10Svr
IniManager.cpp@1:1e2ee9bbee40, 2013-09-01 (annotated)
- Committer:
- WiredHome
- Date:
- Sun Sep 01 19:52:53 2013 +0000
- Revision:
- 1:1e2ee9bbee40
- Parent:
- 0:ae5bf432c249
- Child:
- 2:c63a794c1fee
A simple INI file manager. Has some level of recovery in the event power is lost, but could be better.
Who changed what in which revision?
User | Revision | Line number | New 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 | 0:ae5bf432c249 | 110 | // helps recover if the system crashed before it could swap in the new file |
WiredHome | 1:1e2ee9bbee40 | 111 | INFO("*** repairing\r\n"); |
WiredHome | 0:ae5bf432c249 | 112 | fclose(repair); |
WiredHome | 0:ae5bf432c249 | 113 | int i; |
WiredHome | 0:ae5bf432c249 | 114 | i = remove(bakFile); // remove an old .bak |
WiredHome | 1:1e2ee9bbee40 | 115 | INFO("remove(%s) returned %d\r\n", bakFile, i); |
WiredHome | 0:ae5bf432c249 | 116 | i = Rename(iniFile, bakFile); // move the existing .ini to .bak |
WiredHome | 1:1e2ee9bbee40 | 117 | INFO("rename(%s,%s) returned %d\r\n", iniFile, bakFile, i); |
WiredHome | 0:ae5bf432c249 | 118 | i = Rename(newFile, iniFile); // move the new .new to .ini |
WiredHome | 1:1e2ee9bbee40 | 119 | INFO("rename(%s,%s) returned %d\r\n", newFile, iniFile, i); |
WiredHome | 0:ae5bf432c249 | 120 | } |
WiredHome | 0:ae5bf432c249 | 121 | } |
WiredHome | 0:ae5bf432c249 | 122 | free(newFile); |
WiredHome | 0:ae5bf432c249 | 123 | free(bakFile); |
WiredHome | 0:ae5bf432c249 | 124 | return true; |
WiredHome | 0:ae5bf432c249 | 125 | } |
WiredHome | 0:ae5bf432c249 | 126 | |
WiredHome | 0:ae5bf432c249 | 127 | // Create the new version as .new |
WiredHome | 0:ae5bf432c249 | 128 | // once complete, if something actually changed, then rename the .ini to .bak and rename the .new to .ini |
WiredHome | 0:ae5bf432c249 | 129 | // once complete, if nothing actually changed, then delete the .new |
WiredHome | 0:ae5bf432c249 | 130 | // |
WiredHome | 0:ae5bf432c249 | 131 | bool INI::WriteString(const char * section, const char * key, char * value) |
WiredHome | 0:ae5bf432c249 | 132 | { |
WiredHome | 0:ae5bf432c249 | 133 | bool found = false; |
WiredHome | 0:ae5bf432c249 | 134 | bool fileChanged = false; |
WiredHome | 0:ae5bf432c249 | 135 | |
WiredHome | 0:ae5bf432c249 | 136 | if (!iniFile || (value != NULL && strlen(value) > INTERNAL_BUF_SIZE)) |
WiredHome | 0:ae5bf432c249 | 137 | return found; |
WiredHome | 0:ae5bf432c249 | 138 | |
WiredHome | 0:ae5bf432c249 | 139 | char * newFile = (char *)malloc(strlen(iniFile)+1); |
WiredHome | 0:ae5bf432c249 | 140 | char * bakFile = (char *)malloc(strlen(iniFile)+1); |
WiredHome | 0:ae5bf432c249 | 141 | if (!newFile) |
WiredHome | 0:ae5bf432c249 | 142 | return found; // no memory |
WiredHome | 0:ae5bf432c249 | 143 | if (!bakFile) { |
WiredHome | 0:ae5bf432c249 | 144 | free(newFile); |
WiredHome | 0:ae5bf432c249 | 145 | return found; |
WiredHome | 0:ae5bf432c249 | 146 | } |
WiredHome | 0:ae5bf432c249 | 147 | strcpy(bakFile, iniFile); |
WiredHome | 0:ae5bf432c249 | 148 | strcpy(newFile, iniFile); |
WiredHome | 0:ae5bf432c249 | 149 | strcpy(bakFile + strlen(bakFile) - 4, ".bak"); |
WiredHome | 0:ae5bf432c249 | 150 | strcpy(newFile + strlen(newFile) - 4, ".new"); |
WiredHome | 0:ae5bf432c249 | 151 | |
WiredHome | 0:ae5bf432c249 | 152 | CrashRecover(); |
WiredHome | 0:ae5bf432c249 | 153 | |
WiredHome | 1:1e2ee9bbee40 | 154 | INFO("Opening [%s] and [%s]\r\n", iniFile, newFile); |
WiredHome | 0:ae5bf432c249 | 155 | FILE * fi = fopen(iniFile, "rt"); |
WiredHome | 0:ae5bf432c249 | 156 | FILE * fo = fopen(newFile, "wt"); |
WiredHome | 0:ae5bf432c249 | 157 | if (fo) { |
WiredHome | 0:ae5bf432c249 | 158 | char buf[INTERNAL_BUF_SIZE]; |
WiredHome | 0:ae5bf432c249 | 159 | bool inSection = (section == NULL) ? true : false; |
WiredHome | 0:ae5bf432c249 | 160 | |
WiredHome | 0:ae5bf432c249 | 161 | if (fi) { |
WiredHome | 0:ae5bf432c249 | 162 | while(fgets(buf, sizeof(buf), fi)) { |
WiredHome | 0:ae5bf432c249 | 163 | // if not inSection, copy across |
WiredHome | 0:ae5bf432c249 | 164 | // if inSection and not key, copy across |
WiredHome | 0:ae5bf432c249 | 165 | // if InSection and key, write new value (or skip if value is null) |
WiredHome | 0:ae5bf432c249 | 166 | int x = strlen(buf) - 1; // remove trailing \r\n combinations |
WiredHome | 0:ae5bf432c249 | 167 | while (x >= 0 && buf[x] < ' ') |
WiredHome | 0:ae5bf432c249 | 168 | buf[x--] = '\0'; |
WiredHome | 0:ae5bf432c249 | 169 | if (inSection && buf[0] != '[') { |
WiredHome | 0:ae5bf432c249 | 170 | char * eq = strchr(buf, '='); |
WiredHome | 0:ae5bf432c249 | 171 | if (eq) { |
WiredHome | 0:ae5bf432c249 | 172 | *eq++ = '\0'; |
WiredHome | 0:ae5bf432c249 | 173 | if (strcmp(buf,key) == 0) { |
WiredHome | 0:ae5bf432c249 | 174 | if (value != NULL && strcmp(eq, value) != 0) { |
WiredHome | 0:ae5bf432c249 | 175 | // replace the old record |
WiredHome | 0:ae5bf432c249 | 176 | if (value != NULL) { |
WiredHome | 0:ae5bf432c249 | 177 | fprintf(fo, "%s=%s\n", key, value); |
WiredHome | 0:ae5bf432c249 | 178 | printf("write: %s=%s\r\n", key, value); |
WiredHome | 0:ae5bf432c249 | 179 | } |
WiredHome | 0:ae5bf432c249 | 180 | } |
WiredHome | 0:ae5bf432c249 | 181 | fileChanged = true; |
WiredHome | 0:ae5bf432c249 | 182 | inSection = false; |
WiredHome | 0:ae5bf432c249 | 183 | found = true; |
WiredHome | 0:ae5bf432c249 | 184 | } else { |
WiredHome | 0:ae5bf432c249 | 185 | // write old record |
WiredHome | 0:ae5bf432c249 | 186 | fprintf(fo, "%s=%s\n", buf, eq); |
WiredHome | 1:1e2ee9bbee40 | 187 | INFO("write: %s=%s\r\n", buf, eq); |
WiredHome | 0:ae5bf432c249 | 188 | } |
WiredHome | 0:ae5bf432c249 | 189 | } else { |
WiredHome | 0:ae5bf432c249 | 190 | // what to do with unknown record(s)? |
WiredHome | 0:ae5bf432c249 | 191 | // fprintf(fo, "%s\n", buf); // eliminate them |
WiredHome | 0:ae5bf432c249 | 192 | } |
WiredHome | 0:ae5bf432c249 | 193 | } else { |
WiredHome | 0:ae5bf432c249 | 194 | if (buf[0] == '[') { |
WiredHome | 0:ae5bf432c249 | 195 | char * br = strchr(buf, ']'); |
WiredHome | 0:ae5bf432c249 | 196 | if (inSection) { // found next section while in good section |
WiredHome | 0:ae5bf432c249 | 197 | // Append new record to desired section |
WiredHome | 0:ae5bf432c249 | 198 | if (value != NULL) { |
WiredHome | 0:ae5bf432c249 | 199 | fprintf(fo, "%s=%s\r\n", key, value); |
WiredHome | 1:1e2ee9bbee40 | 200 | INFO("write: %s=%s\r\n", key, value); |
WiredHome | 0:ae5bf432c249 | 201 | fileChanged = true; |
WiredHome | 0:ae5bf432c249 | 202 | } |
WiredHome | 0:ae5bf432c249 | 203 | found = true; |
WiredHome | 0:ae5bf432c249 | 204 | } |
WiredHome | 0:ae5bf432c249 | 205 | inSection = false; |
WiredHome | 0:ae5bf432c249 | 206 | // write old record |
WiredHome | 0:ae5bf432c249 | 207 | fprintf(fo, "%s\r\n", buf); |
WiredHome | 1:1e2ee9bbee40 | 208 | INFO("write: %s\r\n", buf); |
WiredHome | 0:ae5bf432c249 | 209 | if (br) { |
WiredHome | 0:ae5bf432c249 | 210 | *br = '\0'; |
WiredHome | 0:ae5bf432c249 | 211 | if (strcmp(buf+1, section) == 0) |
WiredHome | 0:ae5bf432c249 | 212 | inSection = true; |
WiredHome | 0:ae5bf432c249 | 213 | } |
WiredHome | 0:ae5bf432c249 | 214 | } else { |
WiredHome | 0:ae5bf432c249 | 215 | // copy unaltered records across |
WiredHome | 0:ae5bf432c249 | 216 | if (buf[0]) { |
WiredHome | 0:ae5bf432c249 | 217 | fprintf(fo, "%s\r\n", buf); |
WiredHome | 1:1e2ee9bbee40 | 218 | INFO("write: %s\r\n", buf); |
WiredHome | 0:ae5bf432c249 | 219 | } |
WiredHome | 0:ae5bf432c249 | 220 | } |
WiredHome | 0:ae5bf432c249 | 221 | } |
WiredHome | 0:ae5bf432c249 | 222 | } |
WiredHome | 1:1e2ee9bbee40 | 223 | INFO("close %s\r\n", iniFile); |
WiredHome | 0:ae5bf432c249 | 224 | fclose(fi); |
WiredHome | 0:ae5bf432c249 | 225 | } |
WiredHome | 0:ae5bf432c249 | 226 | if (!found) { |
WiredHome | 0:ae5bf432c249 | 227 | // No old file, just create it now |
WiredHome | 0:ae5bf432c249 | 228 | if (value != NULL) { |
WiredHome | 0:ae5bf432c249 | 229 | if (!inSection) { |
WiredHome | 0:ae5bf432c249 | 230 | fprintf(fo, "[%s]\r\n", section); |
WiredHome | 1:1e2ee9bbee40 | 231 | INFO("write: [%s]\r\n", section); |
WiredHome | 0:ae5bf432c249 | 232 | } |
WiredHome | 0:ae5bf432c249 | 233 | fprintf(fo, "%s=%s\r\n", key, value); |
WiredHome | 1:1e2ee9bbee40 | 234 | INFO("write: %s=%s\r\n", key, value); |
WiredHome | 0:ae5bf432c249 | 235 | fileChanged = true; |
WiredHome | 0:ae5bf432c249 | 236 | } |
WiredHome | 0:ae5bf432c249 | 237 | found = true; |
WiredHome | 0:ae5bf432c249 | 238 | } |
WiredHome | 1:1e2ee9bbee40 | 239 | INFO("close %s\r\n", newFile); |
WiredHome | 0:ae5bf432c249 | 240 | fclose(fo); |
WiredHome | 0:ae5bf432c249 | 241 | } |
WiredHome | 0:ae5bf432c249 | 242 | if (fileChanged) { |
WiredHome | 1:1e2ee9bbee40 | 243 | INFO("remove bak, rename ini to bak, rename new to ini\r\n"); |
WiredHome | 0:ae5bf432c249 | 244 | remove(bakFile); // remove an old .bak |
WiredHome | 0:ae5bf432c249 | 245 | Rename(iniFile, bakFile); // move the existing .ini to .bak |
WiredHome | 0:ae5bf432c249 | 246 | Rename(newFile, iniFile); // move the new .new to .ini |
WiredHome | 0:ae5bf432c249 | 247 | wait(1); |
WiredHome | 0:ae5bf432c249 | 248 | } |
WiredHome | 0:ae5bf432c249 | 249 | free(newFile); |
WiredHome | 0:ae5bf432c249 | 250 | free(bakFile); |
WiredHome | 0:ae5bf432c249 | 251 | return found; |
WiredHome | 0:ae5bf432c249 | 252 | } |
WiredHome | 0:ae5bf432c249 | 253 | |
WiredHome | 0:ae5bf432c249 | 254 | |
WiredHome | 0:ae5bf432c249 | 255 | //*********************************************************** |
WiredHome | 0:ae5bf432c249 | 256 | // Private version that also works with local file system |
WiredHome | 0:ae5bf432c249 | 257 | // Returns -1 = error; 0 = success |
WiredHome | 0:ae5bf432c249 | 258 | //*********************************************************** |
WiredHome | 0:ae5bf432c249 | 259 | int INI::Rename(const char *oldfname, const char *newfname) |
WiredHome | 0:ae5bf432c249 | 260 | { |
WiredHome | 0:ae5bf432c249 | 261 | int retval = 0; |
WiredHome | 0:ae5bf432c249 | 262 | int ch; |
WiredHome | 0:ae5bf432c249 | 263 | |
WiredHome | 0:ae5bf432c249 | 264 | FILE *fpold = fopen(oldfname, "r"); // src file |
WiredHome | 0:ae5bf432c249 | 265 | FILE *fpnew = fopen(newfname, "w"); // dest file |
WiredHome | 0:ae5bf432c249 | 266 | |
WiredHome | 0:ae5bf432c249 | 267 | while (1) { // Copy src to dest |
WiredHome | 0:ae5bf432c249 | 268 | ch = fgetc(fpold); // until src EOF read. |
WiredHome | 0:ae5bf432c249 | 269 | if (ch == EOF) break; |
WiredHome | 0:ae5bf432c249 | 270 | fputc(ch, fpnew); |
WiredHome | 0:ae5bf432c249 | 271 | } |
WiredHome | 0:ae5bf432c249 | 272 | |
WiredHome | 0:ae5bf432c249 | 273 | fclose(fpnew); |
WiredHome | 0:ae5bf432c249 | 274 | fclose(fpold); |
WiredHome | 0:ae5bf432c249 | 275 | |
WiredHome | 0:ae5bf432c249 | 276 | fpnew = fopen(newfname, "r"); // Reopen dest to insure |
WiredHome | 0:ae5bf432c249 | 277 | if(fpnew == NULL) { // that it was created. |
WiredHome | 0:ae5bf432c249 | 278 | retval = (-1); // Return Error. |
WiredHome | 0:ae5bf432c249 | 279 | } else { |
WiredHome | 0:ae5bf432c249 | 280 | fclose(fpnew); |
WiredHome | 0:ae5bf432c249 | 281 | remove(oldfname); // Remove original file. |
WiredHome | 0:ae5bf432c249 | 282 | retval = (0); // Return Success. |
WiredHome | 0:ae5bf432c249 | 283 | } |
WiredHome | 0:ae5bf432c249 | 284 | return (retval); |
WiredHome | 0:ae5bf432c249 | 285 | } |
WiredHome | 0:ae5bf432c249 | 286 | |
WiredHome | 0:ae5bf432c249 | 287 | //*********************************************************** |
WiredHome | 0:ae5bf432c249 | 288 | // Private version that also works with local file system |
WiredHome | 0:ae5bf432c249 | 289 | // Returns -1 = error; 0 = success |
WiredHome | 0:ae5bf432c249 | 290 | //*********************************************************** |
WiredHome | 0:ae5bf432c249 | 291 | int INI::Copy(const char *src, const char *dst) |
WiredHome | 0:ae5bf432c249 | 292 | { |
WiredHome | 0:ae5bf432c249 | 293 | int retval = 0; |
WiredHome | 0:ae5bf432c249 | 294 | int ch; |
WiredHome | 0:ae5bf432c249 | 295 | |
WiredHome | 0:ae5bf432c249 | 296 | FILE *fpsrc = fopen(src, "r"); // src file |
WiredHome | 0:ae5bf432c249 | 297 | FILE *fpdst = fopen(dst, "w"); // dest file |
WiredHome | 0:ae5bf432c249 | 298 | |
WiredHome | 0:ae5bf432c249 | 299 | while (1) { // Copy src to dest |
WiredHome | 0:ae5bf432c249 | 300 | ch = fgetc(fpsrc); // until src EOF read. |
WiredHome | 0:ae5bf432c249 | 301 | if (ch == EOF) break; |
WiredHome | 0:ae5bf432c249 | 302 | fputc(ch, fpdst); |
WiredHome | 0:ae5bf432c249 | 303 | } |
WiredHome | 0:ae5bf432c249 | 304 | fclose(fpsrc); |
WiredHome | 0:ae5bf432c249 | 305 | fclose(fpdst); |
WiredHome | 0:ae5bf432c249 | 306 | |
WiredHome | 0:ae5bf432c249 | 307 | fpdst = fopen(dst, "r"); // Reopen dest to insure |
WiredHome | 0:ae5bf432c249 | 308 | if(fpdst == NULL) { // that it was created. |
WiredHome | 0:ae5bf432c249 | 309 | retval = (-1); // Return error. |
WiredHome | 0:ae5bf432c249 | 310 | } else { |
WiredHome | 0:ae5bf432c249 | 311 | fclose(fpdst); |
WiredHome | 0:ae5bf432c249 | 312 | retval = (0); // Return success. |
WiredHome | 0:ae5bf432c249 | 313 | } |
WiredHome | 0:ae5bf432c249 | 314 | return (retval); |
WiredHome | 0:ae5bf432c249 | 315 | } |
WiredHome | 0:ae5bf432c249 | 316 | |
WiredHome | 0:ae5bf432c249 | 317 | |
WiredHome | 0:ae5bf432c249 | 318 | #if 0 |
WiredHome | 0:ae5bf432c249 | 319 | // Test code for basic regression testing |
WiredHome | 0:ae5bf432c249 | 320 | // |
WiredHome | 0:ae5bf432c249 | 321 | #include <stdio.h> |
WiredHome | 0:ae5bf432c249 | 322 | #include <assert.h> |
WiredHome | 0:ae5bf432c249 | 323 | #include <string.h> |
WiredHome | 0:ae5bf432c249 | 324 | |
WiredHome | 0:ae5bf432c249 | 325 | #include "INI.h" |
WiredHome | 0:ae5bf432c249 | 326 | |
WiredHome | 0:ae5bf432c249 | 327 | #define TESTFILE "test.ini" |
WiredHome | 0:ae5bf432c249 | 328 | |
WiredHome | 0:ae5bf432c249 | 329 | int main(int argc, char * argv[]) |
WiredHome | 0:ae5bf432c249 | 330 | { |
WiredHome | 0:ae5bf432c249 | 331 | FILE * fp; |
WiredHome | 0:ae5bf432c249 | 332 | char buffer[100]; |
WiredHome | 0:ae5bf432c249 | 333 | INI ini(TESTFILE); |
WiredHome | 0:ae5bf432c249 | 334 | |
WiredHome | 0:ae5bf432c249 | 335 | // Start testing |
WiredHome | 0:ae5bf432c249 | 336 | _unlink(TESTFILE); |
WiredHome | 0:ae5bf432c249 | 337 | assert(ini.ReadString("Section 1", "Name 1", buffer, sizeof(buffer)) == false); |
WiredHome | 0:ae5bf432c249 | 338 | |
WiredHome | 0:ae5bf432c249 | 339 | fp = fopen(TESTFILE, "wt"); |
WiredHome | 0:ae5bf432c249 | 340 | assert(fp); |
WiredHome | 0:ae5bf432c249 | 341 | fprintf(fp, "[Section 1]\n"); |
WiredHome | 0:ae5bf432c249 | 342 | fprintf(fp, "Name 1=Value 1\n"); |
WiredHome | 0:ae5bf432c249 | 343 | fprintf(fp, "Name 2=Value 2\n"); |
WiredHome | 0:ae5bf432c249 | 344 | fprintf(fp, "\n"); |
WiredHome | 0:ae5bf432c249 | 345 | fprintf(fp, "[Section 2]\n"); |
WiredHome | 0:ae5bf432c249 | 346 | fprintf(fp, "Name 1=Value 2\n"); |
WiredHome | 0:ae5bf432c249 | 347 | fprintf(fp, "Name 2=Value 2\n"); |
WiredHome | 0:ae5bf432c249 | 348 | fprintf(fp, "Name 3=Value 3\n"); |
WiredHome | 0:ae5bf432c249 | 349 | fprintf(fp, "\n"); |
WiredHome | 0:ae5bf432c249 | 350 | fclose(fp); |
WiredHome | 0:ae5bf432c249 | 351 | |
WiredHome | 0:ae5bf432c249 | 352 | assert(ini.ReadString("Section 2", "Name 2", buffer, sizeof(buffer)) == true); |
WiredHome | 0:ae5bf432c249 | 353 | assert(strcmp("Value 2", buffer) == 0); |
WiredHome | 0:ae5bf432c249 | 354 | |
WiredHome | 0:ae5bf432c249 | 355 | assert(ini.ReadString("Section 3", "Name", buffer, sizeof(buffer)) == false); |
WiredHome | 0:ae5bf432c249 | 356 | assert(ini.ReadString("Section 1", "Name 3", buffer, sizeof(buffer)) == false); |
WiredHome | 0:ae5bf432c249 | 357 | |
WiredHome | 0:ae5bf432c249 | 358 | assert(ini.WriteString("Section 1", "Name 4", "Value 4") == true); |
WiredHome | 0:ae5bf432c249 | 359 | assert(ini.ReadString("Section 1", "Name 2", buffer, sizeof(buffer)) == true); |
WiredHome | 0:ae5bf432c249 | 360 | assert(ini.ReadString("Section 1", "Name 3", buffer, sizeof(buffer)) == false); |
WiredHome | 0:ae5bf432c249 | 361 | assert(ini.ReadString("Section 1", "Name 4", buffer, sizeof(buffer)) == true); |
WiredHome | 0:ae5bf432c249 | 362 | assert(strcmp("Value 4", buffer) == 0); |
WiredHome | 0:ae5bf432c249 | 363 | |
WiredHome | 0:ae5bf432c249 | 364 | assert(ini.WriteString("Section 1", "Name 4", NULL) == true); |
WiredHome | 0:ae5bf432c249 | 365 | assert(ini.ReadString("Section 1", "Name 4", buffer, sizeof(buffer)) == false); |
WiredHome | 0:ae5bf432c249 | 366 | |
WiredHome | 0:ae5bf432c249 | 367 | return 0; |
WiredHome | 0:ae5bf432c249 | 368 | } |
WiredHome | 0:ae5bf432c249 | 369 | #endif |