ConfigFile

Dependents:   ConfigFile_TestProgram StarBoardOrangeExpansion1 StarBoardOrangeExpansion2 Drive2ChoroQ ... more

Files at this revision

API Documentation at this revision

Comitter:
shintamainjp
Date:
Sun Sep 12 07:11:54 2010 +0000
Child:
1:f02e081afe42
Commit message:

Changed in this revision

ConfigFile.cpp Show annotated file Show diff for this revision Revisions of this file
ConfigFile.h Show annotated file Show diff for this revision Revisions of this file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/ConfigFile.cpp	Sun Sep 12 07:11:54 2010 +0000
@@ -0,0 +1,276 @@
+#include "ConfigFile.h"
+
+ConfigFile::ConfigFile() {
+    /*
+     * Allocation for a config_t list.
+     */
+    configlist = (config_t **)malloc(sizeof(config_t *) * MAXCONFIG);
+    for (int i = 0; i < MAXCONFIG; i++) {
+        configlist[i] = NULL;
+    }
+}
+
+ConfigFile::~ConfigFile() {
+    /*
+     * Remove all storage and the contents.
+     */
+    for (int i = 0; i < MAXCONFIG; i++) {
+        config_t *cfg = configlist[i];
+        if (cfg != NULL) {
+            free(cfg->key);
+            free(cfg->value);
+            free(cfg);
+        }
+        configlist[i] = NULL;
+    }
+
+    /*
+     * Remove cnofig_t list.
+     */
+    free(configlist);
+    configlist = NULL;
+}
+
+char *ConfigFile::getValue(char *key) {
+    /*
+     * Null check.
+     */
+    if (key == NULL) {
+        return NULL;
+    }
+
+    /*
+     * Search a config_t object from the key.
+     */
+    config_t *p = search(key);
+    if (p == NULL) {
+        return NULL;
+    }
+
+    /*
+     * Return the value.
+     */
+    return p->value;
+}
+
+bool ConfigFile::setValue(char *key, char *value) {
+    /*
+     * Null check.
+     */
+    if ((key == NULL) || (value == NULL)) {
+        return false;
+    }
+
+    /*
+     * Size check.
+     */
+    if ((MAXLEN_KEY < strlen(key)) || (MAXLEN_VALUE < strlen(value))) {
+        return false;
+    }
+
+    /*
+     * Search a config_t object from the key.
+     */
+    config_t *p = search(key);
+    if (p == NULL) {
+        /*
+         * Allocation a memory for a new key.
+         */
+        char *k = (char *)malloc(sizeof(char) * (strlen(key) + 1));
+        if (k == NULL) {
+            return false;
+        }
+        strcpy(k, key);
+
+        /*
+         * Allocation a memory for a new value.
+         */
+        char *v = (char *)malloc(sizeof(char) * (strlen(value) + 1));
+        if (v == NULL) {
+            free(k);
+            return false;
+        }
+        strcpy(v, value);
+
+        /*
+         * Allocation a memory for a new configuration.
+         */
+        config_t *cfg = (config_t *)malloc(sizeof(config_t) * 1);
+        if (cfg == NULL) {
+            free(k);
+            free(v);
+            return false;
+        }
+        cfg->key = k;
+        cfg->value = v;
+
+        /*
+         * Add the new configuration.
+         */
+        if (!add(cfg)) {
+            free(k);
+            free(v);
+            free(cfg);
+            return false;
+        }
+
+        return true;
+    } else {
+        /*
+         * The value is same.
+         */
+        if (strcmp(value, p->value) == 0) {
+            return true;
+        }
+
+        /*
+         * Free a memory for the value.
+         */
+        free(p->value);
+        p->value = NULL;
+
+        /*
+         * Allocation memory for the new value.
+         */
+        char *v = (char *)malloc(sizeof(char) * (strlen(value) + 1));
+        if (v == NULL) {
+            return false;
+        }
+
+        /*
+         * Store it.
+         */
+        strcpy(v, value);
+        p->value = v;
+
+        return true;
+    }
+}
+
+bool ConfigFile::remove(char *key) {
+    if (key == NULL) {
+        return false;
+    }
+    for (int i = 0; i < MAXCONFIG; i++) {
+        config_t *cfg = configlist[i];
+        if (cfg != NULL) {
+            if (strcmp(cfg->key, key) == 0) {
+                free(cfg->key);
+                free(cfg->value);
+                free(cfg);
+                configlist[i] = NULL;
+                return true;
+            }
+        }
+    }
+    return false;
+}
+
+bool ConfigFile::removeAll(void) {
+    for (int i = 0; i < MAXCONFIG; i++) {
+        config_t *p = configlist[i];
+        if (p != NULL) {
+            free(p->key);
+            free(p->value);
+        }
+        free(p);
+        configlist[i] = NULL;
+    }
+    return true;
+}
+
+bool ConfigFile::read(char *file) {
+    /*
+     * Open the target file.
+     */
+    FILE *fp = fopen(file, "r");
+    if (fp == NULL) {
+        return false;
+    }
+
+    /*
+     * Remove all configuration.
+     */
+    removeAll();
+
+    /*
+     * Read from a file.
+     */
+    char buf[MAXLEN_KEY + 8 + MAXLEN_VALUE];
+    while (fgets(buf, sizeof(buf), fp) != NULL) {
+        /*
+         * Ignore a comment.
+         */
+        if (buf[0] == '#') {
+            continue;
+        }
+        
+        /*
+         * Trim a return.
+         */
+        const size_t len = strlen(buf);
+        for (int i = 0; i < len; i++) {
+            if ((buf[i] == '\r') || (buf[i] == '\n')) {
+                buf[i] = '\0';
+            }
+        }
+
+        /*
+         * Separate key and value.
+         */        
+        char k[MAXLEN_KEY];
+        char v[MAXLEN_VALUE];
+        char *sp = strchr(buf, SEPARATOR);
+        if (sp != NULL) {
+            strcpy(v, sp + 1);
+            *sp = '\0';
+            strcpy(k, buf);
+            setValue(k, v);
+        }
+    }
+    fclose(fp);
+    return true;
+}
+
+bool ConfigFile::write(char *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";
+    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);
+        }
+    }
+    fclose(fp);
+    return true;
+}
+
+ConfigFile::config_t *ConfigFile::search(char *key) {
+    if (key == NULL) {
+        return NULL;
+    }
+    for (int i = 0; i < MAXCONFIG; i++) {
+        if (configlist[i] != NULL) {
+            if (strcmp(configlist[i]->key, key) == 0) {
+                return configlist[i];
+            }
+        }
+    }
+    return NULL;
+}
+
+bool ConfigFile::add(config_t *cfg) {
+    for (int i = 0; i < MAXCONFIG; i++) {
+        if (configlist[i] == NULL) {
+            configlist[i] = cfg;
+            return true;
+        }
+    }
+    return false;
+}
\ No newline at end of file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/ConfigFile.h	Sun Sep 12 07:11:54 2010 +0000
@@ -0,0 +1,104 @@
+#include "mbed.h"
+
+#ifndef _CONFIG_FILE_H_
+#define _CONFIG_FILE_H_
+
+/**
+ * Configuration File class.
+ */
+class ConfigFile {
+public:
+
+    /**
+     * Create a configuration file class.
+     */
+    ConfigFile();
+    
+    /**
+     * Destroy a configuration file class.
+     */
+    ~ConfigFile();
+    
+    /**
+     * Get a value for a key.
+     *
+     * @param key A target key name.
+     *
+     * @return A value or NULL.
+     */
+    char *getValue(char *key);
+    
+    /**
+     * Set a set of a key and value.
+     *
+     * @param key A key.
+     * @param value A value.
+     *
+     * @return True if it succeed.
+     */
+    bool setValue(char *key, char *value);
+    
+    /**
+     * Remove a config.
+     *
+     * @param key A key.
+     *
+     * @return True if it succeed.
+     */
+    bool remove(char *key);
+    
+    /**
+     * Remove all config.
+     *
+     * @return True if it succeed.
+     */
+    bool removeAll(void);
+    
+    /**
+     * Read from the target file.
+     *
+     * @param file A target file name.
+     */
+    bool read(char *file);
+    
+    /**
+     * Write from the target file.
+     *
+     * @param file A target file name.
+     */
+    bool write(char *file);
+    
+    /**
+     * Output for debugging.
+     *
+     * @deprecated Please do not use this method.
+     */
+    void debout(void) {
+        printf("===========================================================================\n");
+        for (int i = 0; i < MAXCONFIG; i++) {
+            config_t *cfg = configlist[i];
+            printf("[%03d]:", i);
+            if (cfg == NULL) {
+                printf("NULL\n");
+            } else {
+                printf("'%s'='%s'\n", cfg->key, cfg->value);
+            }
+        }
+        printf("===========================================================================\n");
+    }
+private:
+    typedef struct {
+        char *key;
+        char *value;
+    } config_t;
+    config_t **configlist;
+    static const int MAXCONFIG = 64;
+    static const int MAXLEN_KEY = 64;
+    static const int MAXLEN_VALUE = 128;
+    static const char SEPARATOR = '=';
+
+    config_t *search(char *key);
+    bool add(config_t *cfg);
+};
+
+#endif