A simple .ini file interface.

Dependents:   Smart-WiFly-WebServer SignalGenerator WattEye X10Svr

Files at this revision

API Documentation at this revision

Comitter:
WiredHome
Date:
Thu Apr 14 03:12:22 2016 +0000
Parent:
12:6cf929bde139
Child:
17:01c0ee144433
Commit message:
Added optional length parameter to a string write.

Changed in this revision

IniManager.cpp Show annotated file Show diff for this revision Revisions of this file
IniManager.h Show annotated file Show diff for this revision Revisions of this file
--- 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
 
 
+
--- a/IniManager.h	Mon Apr 11 02:16:18 2016 +0000
+++ b/IniManager.h	Thu Apr 14 03:12:22 2016 +0000
@@ -104,10 +104,12 @@
     * @param[in] key is the name of the key to search.
     * @param[in] buffer is the caller provided buffer containing the string to write. If
     *       buffer is NULL, then any existing entry is removed.
+    * @param[in] len is the number of characters to write, if specified. If not specified,
+    *       the length of the buffer defines the length to write.
     *
     * @return true if the write was successful; false otherwise.
     */
-    bool WriteString(const char * section, const char * key, const char * buffer);
+    bool WriteString(const char * section, const char * key, const char * buffer, int len = -1);
 
 private:
     char * iniFile;