A simple .ini file interface.
Dependents: Smart-WiFly-WebServer SignalGenerator WattEye X10Svr
Revision 1:1e2ee9bbee40, committed 2013-09-01
- Comitter:
- WiredHome
- Date:
- Sun Sep 01 19:52:53 2013 +0000
- Parent:
- 0:ae5bf432c249
- Child:
- 2:c63a794c1fee
- Commit message:
- A simple INI file manager. Has some level of recovery in the event power is lost, but could be better.
Changed in this revision
| IniManager.cpp | Show annotated file Show diff for this revision Revisions of this file |
--- a/IniManager.cpp Mon Aug 12 22:57:54 2013 +0000
+++ b/IniManager.cpp Sun Sep 01 19:52:53 2013 +0000
@@ -10,6 +10,20 @@
#include "IniManager.h"
+//#define DEBUG //Debug is disabled by default
+
+#if (defined(DEBUG) && !defined(TARGET_LPC11U24))
+#define DBG(x, ...) std::printf("[DBG INI%4d] "x"\r\n", __LINE__, ##__VA_ARGS__);
+#define WARN(x, ...) std::printf("[WRN INI%4d] "x"\r\n", __LINE__, ##__VA_ARGS__);
+#define ERR(x, ...) std::printf("[ERR INI%4d] "x"\r\n", __LINE__, ##__VA_ARGS__);
+#define INFO(x, ...) std::printf("[INF INI%4d] "x"\r\n", __LINE__, ##__VA_ARGS__);
+#else
+#define DBG(x, ...)
+#define WARN(x, ...)
+#define ERR(x, ...)
+#define INFO(x, ...)
+#endif
+
INI::INI(const char * file)
: iniFile(0)
{
@@ -18,12 +32,6 @@
if (iniFile)
strcpy(iniFile, file);
}
- FILE * fo = fopen("/local/test.txt", "wt");
- if (fo) {
- printf("Writing to /local/test.txt\r\n");
- fprintf(fo, "This is three - %d\r\n", 3);
- fclose(fo);
- }
}
@@ -40,7 +48,7 @@
if (!iniFile)
return found;
CrashRecover();
- printf("ReadString from %s\r\n", iniFile);
+ INFO("ReadString from %s\r\n", iniFile);
FILE * fp = fopen(iniFile,"rt");
if (fp) {
char buf[INTERNAL_BUF_SIZE];
@@ -50,7 +58,7 @@
int x = strlen(buf) - 1; // remove trailing \r\n combinations
while (x >= 0 && buf[x] < ' ')
buf[x--] = '\0';
- printf("read in [%s]\r\n", buf);
+ INFO("read in [%s]\r\n", buf);
if (inSection && buf[0] != '[') {
char * eq = strchr(buf, '=');
if (eq) {
@@ -79,7 +87,7 @@
if (!found && defaultString != NULL && *defaultString) {
strncpy(buffer, defaultString, bufferSize);
buffer[bufferSize-1] = '\0';
- printf("sub %s.\r\n", buffer);
+ INFO("sub %s.\r\n", buffer);
found = true;
}
return found;
@@ -91,7 +99,7 @@
char * bakFile = (char *)malloc(strlen(iniFile)+1);
if (newFile && bakFile) {
- printf("*** CrashRecover\r\n");
+ WARN("*** CrashRecover\r\n");
strcpy(bakFile, iniFile);
strcpy(newFile, iniFile);
strcpy(bakFile + strlen(bakFile) - 4, ".bak");
@@ -100,15 +108,15 @@
FILE * repair = fopen(newFile, "rt");
if (repair) {
// helps recover if the system crashed before it could swap in the new file
- printf("*** repairing\r\n");
+ INFO("*** repairing\r\n");
fclose(repair);
int i;
i = remove(bakFile); // remove an old .bak
- printf("remove(%s) returned %d\r\n", bakFile, i);
+ INFO("remove(%s) returned %d\r\n", bakFile, i);
i = Rename(iniFile, bakFile); // move the existing .ini to .bak
- printf("rename(%s,%s) returned %d\r\n", iniFile, bakFile, i);
+ INFO("rename(%s,%s) returned %d\r\n", iniFile, bakFile, i);
i = Rename(newFile, iniFile); // move the new .new to .ini
- printf("rename(%s,%s) returned %d\r\n", newFile, iniFile, i);
+ INFO("rename(%s,%s) returned %d\r\n", newFile, iniFile, i);
}
}
free(newFile);
@@ -143,7 +151,7 @@
CrashRecover();
- printf("Opening [%s] and [%s]\r\n", iniFile, newFile);
+ INFO("Opening [%s] and [%s]\r\n", iniFile, newFile);
FILE * fi = fopen(iniFile, "rt");
FILE * fo = fopen(newFile, "wt");
if (fo) {
@@ -176,7 +184,7 @@
} else {
// write old record
fprintf(fo, "%s=%s\n", buf, eq);
- printf("write: %s=%s\r\n", buf, eq);
+ INFO("write: %s=%s\r\n", buf, eq);
}
} else {
// what to do with unknown record(s)?
@@ -189,7 +197,7 @@
// Append new record to desired section
if (value != NULL) {
fprintf(fo, "%s=%s\r\n", key, value);
- printf("write: %s=%s\r\n", key, value);
+ INFO("write: %s=%s\r\n", key, value);
fileChanged = true;
}
found = true;
@@ -197,7 +205,7 @@
inSection = false;
// write old record
fprintf(fo, "%s\r\n", buf);
- printf("write: %s\r\n", buf);
+ INFO("write: %s\r\n", buf);
if (br) {
*br = '\0';
if (strcmp(buf+1, section) == 0)
@@ -207,12 +215,12 @@
// copy unaltered records across
if (buf[0]) {
fprintf(fo, "%s\r\n", buf);
- printf("write: %s\r\n", buf);
+ INFO("write: %s\r\n", buf);
}
}
}
}
- printf("close %s\r\n", iniFile);
+ INFO("close %s\r\n", iniFile);
fclose(fi);
}
if (!found) {
@@ -220,19 +228,19 @@
if (value != NULL) {
if (!inSection) {
fprintf(fo, "[%s]\r\n", section);
- printf("write: [%s]\r\n", section);
+ INFO("write: [%s]\r\n", section);
}
fprintf(fo, "%s=%s\r\n", key, value);
- printf("write: %s=%s\r\n", key, value);
+ INFO("write: %s=%s\r\n", key, value);
fileChanged = true;
}
found = true;
}
- printf("close %s\r\n", newFile);
+ INFO("close %s\r\n", newFile);
fclose(fo);
}
if (fileChanged) {
- printf("remove bak, rename ini to bak, rename new to ini\r\n");
+ INFO("remove bak, rename ini to bak, rename new to ini\r\n");
remove(bakFile); // remove an old .bak
Rename(iniFile, bakFile); // move the existing .ini to .bak
Rename(newFile, iniFile); // move the new .new to .ini