ConfigFile

Dependents:   ConfigFile_TestProgram StarBoardOrangeExpansion1 StarBoardOrangeExpansion2 Drive2ChoroQ ... more

Revision:
2:d8febae84a45
Parent:
1:f02e081afe42
Child:
3:7250e339328c
--- a/ConfigFile.cpp	Sun Sep 12 07:22:00 2010 +0000
+++ b/ConfigFile.cpp	Sun Sep 12 07:35:25 2010 +0000
@@ -1,5 +1,9 @@
 #include "ConfigFile.h"
 
+#define NEWLINE_UNIX "\n"
+#define NEWLINE_DOS "\r\n"
+#define NEWLINE_MAC "\r"
+
 ConfigFile::ConfigFile() {
     /*
      * Allocation for a config_t list.
@@ -46,7 +50,7 @@
     if (p == NULL) {
         return false;
     }
-    
+
     /*
      * Check the storage size.
      */
@@ -212,7 +216,7 @@
         if (buf[0] == '#') {
             continue;
         }
-        
+
         /*
          * Trim a return.
          */
@@ -225,7 +229,7 @@
 
         /*
          * Separate key and value.
-         */        
+         */
         char k[MAXLEN_KEY];
         char v[MAXLEN_VALUE];
         char *sp = strchr(buf, SEPARATOR);
@@ -240,19 +244,41 @@
     return true;
 }
 
-bool ConfigFile::write(char *file) {
+bool ConfigFile::write(char *file, FileFormat ff) {
+    /*
+     * Open the target file.
+     */
     FILE *fp = fopen(file, "w");
     if (fp == NULL) {
         return false;
     }
 
-    static const char *NEWLINE_UNIX = "\n";
-    static const char *NEWLINE_DOS = "\r\n";
-    static const char *NEWLINE_MAC = "\r";
+    /*
+     * Set a type of new line.
+     */
+    char *newline = NEWLINE_UNIX;
+    switch (ff) {
+        case UNIX:
+            newline = NEWLINE_UNIX;
+            break;
+        case MAC:
+            newline = NEWLINE_MAC;
+            break;
+        case DOS:
+            newline = NEWLINE_DOS;
+            break;
+        default:
+            newline = NEWLINE_UNIX;
+            break;
+    }
+
+    /*
+     * Write the data.
+     */
     for (int i = 0; i < MAXCONFIG; i++) {
         config_t *cfg = configlist[i];
         if (cfg != NULL) {
-            fprintf(fp, "%s=%s%s", cfg->key, cfg->value, NEWLINE_UNIX);
+            fprintf(fp, "%s=%s%s", cfg->key, cfg->value, newline);
         }
     }
     fclose(fp);