A simple .ini file interface.

Dependents:   Smart-WiFly-WebServer SignalGenerator WattEye X10Svr

Revision:
16:82e0f8747b95
Parent:
12:6cf929bde139
Child:
17:01c0ee144433
--- a/IniManager.cpp	Mon Apr 11 02:16:18 2016 +0000
+++ b/IniManager.cpp	Thu Apr 14 03:12:22 2016 +0000
@@ -180,27 +180,37 @@
 // once complete, if something actually changed, then rename the .ini to .bak and rename the .new to .ini
 // once complete, if nothing actually changed, then delete the .new
 //
-bool INI::WriteString(const char * section, const char * key, const char * value)
+bool INI::WriteString(const char * section, const char * key, const char * value, int len)
 {
     bool found = false;
     bool fileChanged = false;
 
+    if (len == -1)
+        len = strlen(value);
     INFO("WriteString(%s,%s,%s)", section, key, value);
     if (!iniFile || (value != NULL && strlen(value) > INTERNAL_BUF_SIZE))
         return found;
 
     char * newFile = (char *)swMalloc(strlen(iniFile)+1);
-    char * bakFile = (char *)swMalloc(strlen(iniFile)+1);
     if (!newFile)
         return found;       // no memory
+    char * bakFile = (char *)swMalloc(strlen(iniFile)+1);
     if (!bakFile) {
         swFree(newFile);
         return found;
     }
+    char * valBuf = (char *)swMalloc(len+1);
+    if (!valBuf) {
+        swFree(bakFile);
+        swFree(newFile);
+    }
+
     strcpy(bakFile, iniFile);
     strcpy(newFile, iniFile);
     strcpy(bakFile + strlen(bakFile) - 4, ".bak");
     strcpy(newFile + strlen(newFile) - 4, ".new");
+    strncpy(valBuf, value, len);
+    valBuf[len] = '\0';
 
     CleanUp();
 
@@ -226,12 +236,12 @@
                     if (eq) {
                         *eq++ = '\0';
                         if (strcmp(buf,key) == 0) {
-                            if (value != NULL && strcmp(eq, value) != 0) {
+                            if (valBuf != NULL && strcmp(eq, valBuf) != 0) {
                                 // replace the old record
-                                if (value != NULL) {
-                                    fprintf(fo, "%s=%s\n", key, value);
-                                    printf("write: %s=%s\r\n", key, value);
-                                    INFO("  write: %s=%s", key, value);
+                                if (valBuf != NULL) {
+                                    fprintf(fo, "%s=%s\n", key, valBuf);
+                                    printf("write: %s=%s\r\n", key, valBuf);
+                                    INFO("  write: %s=%s", key, valBuf);
                                 }
                             }
                             fileChanged = true;
@@ -251,9 +261,9 @@
                         char * br = strchr(buf, ']');
                         if (inSection) { // found next section while in good section
                             // Append new record to desired section
-                            if (value != NULL) {
-                                fprintf(fo, "%s=%s\r\n", key, value);
-                                INFO("  write: %s=%s", key, value);
+                            if (valBuf != NULL) {
+                                fprintf(fo, "%s=%s\r\n", key, valBuf);
+                                INFO("  write: %s=%s", key, valBuf);
                                 fileChanged = true;
                             }
                             found = true;
@@ -283,13 +293,13 @@
         }
         if (!found) {
             // No old file, just create it now
-            if (value != NULL) {
+            if (valBuf != NULL) {
                 if (!inSection) {
                     fprintf(fo, "[%s]\r\n", section);
                     INFO("  write: [%s]", section);
                 }
-                fprintf(fo, "%s=%s\r\n", key, value);
-                INFO("  write: %s=%s", key, value);
+                fprintf(fo, "%s=%s\r\n", key, valBuf);
+                INFO("  write: %s=%s", key, valBuf);
                 fileChanged = true;
             }
             found = true;
@@ -314,6 +324,7 @@
         #endif
         INFO("  d");
     }
+    swFree(valBuf);
     swFree(newFile);
     swFree(bakFile);
     return found;
@@ -432,3 +443,4 @@
 #endif
 
 
+