Changed general file descriptors to specific for CDU

Dependents:   CDUFinal CDU_Mbed_36

Fork of ConfigFile by Shinichiro Nakamura

Committer:
shintamainjp
Date:
Wed Sep 15 13:49:15 2010 +0000
Revision:
6:f6ceafabe9f8
Parent:
5:56d544b8e5c6
Child:
7:b0224b2c20fd

        

Who changed what in which revision?

UserRevisionLine numberNew contents of line
shintamainjp 3:7250e339328c 1 /**
shintamainjp 3:7250e339328c 2 * Configuration file interface class (Version 0.0.1)
shintamainjp 3:7250e339328c 3 *
shintamainjp 3:7250e339328c 4 * Copyright (C) 2010 Shinichiro Nakamura (CuBeatSystems)
shintamainjp 3:7250e339328c 5 * http://shinta.main.jp/
shintamainjp 3:7250e339328c 6 */
shintamainjp 0:6b4ba48753b9 7 #include "ConfigFile.h"
shintamainjp 0:6b4ba48753b9 8
shintamainjp 2:d8febae84a45 9 #define NEWLINE_UNIX "\n"
shintamainjp 2:d8febae84a45 10 #define NEWLINE_DOS "\r\n"
shintamainjp 2:d8febae84a45 11 #define NEWLINE_MAC "\r"
shintamainjp 2:d8febae84a45 12
shintamainjp 6:f6ceafabe9f8 13 /**
shintamainjp 6:f6ceafabe9f8 14 * Create a configuration file class.
shintamainjp 6:f6ceafabe9f8 15 */
shintamainjp 0:6b4ba48753b9 16 ConfigFile::ConfigFile() {
shintamainjp 0:6b4ba48753b9 17 /*
shintamainjp 0:6b4ba48753b9 18 * Allocation for a config_t list.
shintamainjp 0:6b4ba48753b9 19 */
shintamainjp 0:6b4ba48753b9 20 configlist = (config_t **)malloc(sizeof(config_t *) * MAXCONFIG);
shintamainjp 0:6b4ba48753b9 21 for (int i = 0; i < MAXCONFIG; i++) {
shintamainjp 0:6b4ba48753b9 22 configlist[i] = NULL;
shintamainjp 0:6b4ba48753b9 23 }
shintamainjp 0:6b4ba48753b9 24 }
shintamainjp 0:6b4ba48753b9 25
shintamainjp 6:f6ceafabe9f8 26 /**
shintamainjp 6:f6ceafabe9f8 27 * Destroy a configuration file class.
shintamainjp 6:f6ceafabe9f8 28 */
shintamainjp 0:6b4ba48753b9 29 ConfigFile::~ConfigFile() {
shintamainjp 0:6b4ba48753b9 30 /*
shintamainjp 0:6b4ba48753b9 31 * Remove all storage and the contents.
shintamainjp 0:6b4ba48753b9 32 */
shintamainjp 0:6b4ba48753b9 33 for (int i = 0; i < MAXCONFIG; i++) {
shintamainjp 0:6b4ba48753b9 34 config_t *cfg = configlist[i];
shintamainjp 0:6b4ba48753b9 35 if (cfg != NULL) {
shintamainjp 0:6b4ba48753b9 36 free(cfg->key);
shintamainjp 0:6b4ba48753b9 37 free(cfg->value);
shintamainjp 0:6b4ba48753b9 38 free(cfg);
shintamainjp 0:6b4ba48753b9 39 }
shintamainjp 0:6b4ba48753b9 40 configlist[i] = NULL;
shintamainjp 0:6b4ba48753b9 41 }
shintamainjp 0:6b4ba48753b9 42
shintamainjp 0:6b4ba48753b9 43 /*
shintamainjp 0:6b4ba48753b9 44 * Remove cnofig_t list.
shintamainjp 0:6b4ba48753b9 45 */
shintamainjp 0:6b4ba48753b9 46 free(configlist);
shintamainjp 0:6b4ba48753b9 47 configlist = NULL;
shintamainjp 0:6b4ba48753b9 48 }
shintamainjp 0:6b4ba48753b9 49
shintamainjp 6:f6ceafabe9f8 50 /**
shintamainjp 6:f6ceafabe9f8 51 * Get a value for a key.
shintamainjp 6:f6ceafabe9f8 52 *
shintamainjp 6:f6ceafabe9f8 53 * @param key A target key name.
shintamainjp 6:f6ceafabe9f8 54 * @param value A pointer to a value storage.
shintamainjp 6:f6ceafabe9f8 55 * @param siz A size of a value storage.
shintamainjp 6:f6ceafabe9f8 56 * @return A value or NULL.
shintamainjp 6:f6ceafabe9f8 57 */
shintamainjp 1:f02e081afe42 58 bool ConfigFile::getValue(char *key, char *value, size_t siz) {
shintamainjp 0:6b4ba48753b9 59 /*
shintamainjp 0:6b4ba48753b9 60 * Null check.
shintamainjp 0:6b4ba48753b9 61 */
shintamainjp 0:6b4ba48753b9 62 if (key == NULL) {
shintamainjp 1:f02e081afe42 63 return false;
shintamainjp 0:6b4ba48753b9 64 }
shintamainjp 0:6b4ba48753b9 65
shintamainjp 0:6b4ba48753b9 66 /*
shintamainjp 0:6b4ba48753b9 67 * Search a config_t object from the key.
shintamainjp 0:6b4ba48753b9 68 */
shintamainjp 0:6b4ba48753b9 69 config_t *p = search(key);
shintamainjp 0:6b4ba48753b9 70 if (p == NULL) {
shintamainjp 1:f02e081afe42 71 return false;
shintamainjp 1:f02e081afe42 72 }
shintamainjp 2:d8febae84a45 73
shintamainjp 1:f02e081afe42 74 /*
shintamainjp 1:f02e081afe42 75 * Check the storage size.
shintamainjp 1:f02e081afe42 76 */
shintamainjp 1:f02e081afe42 77 if (siz <= strlen(p->value)) {
shintamainjp 1:f02e081afe42 78 return false;
shintamainjp 0:6b4ba48753b9 79 }
shintamainjp 0:6b4ba48753b9 80
shintamainjp 0:6b4ba48753b9 81 /*
shintamainjp 1:f02e081afe42 82 * Copy the value to the storage.
shintamainjp 0:6b4ba48753b9 83 */
shintamainjp 1:f02e081afe42 84 strcpy(value, p->value);
shintamainjp 1:f02e081afe42 85 return true;
shintamainjp 0:6b4ba48753b9 86 }
shintamainjp 0:6b4ba48753b9 87
shintamainjp 6:f6ceafabe9f8 88 /**
shintamainjp 6:f6ceafabe9f8 89 * Set a set of a key and value.
shintamainjp 6:f6ceafabe9f8 90 *
shintamainjp 6:f6ceafabe9f8 91 * @param key A key.
shintamainjp 6:f6ceafabe9f8 92 * @param value A value.
shintamainjp 6:f6ceafabe9f8 93 *
shintamainjp 6:f6ceafabe9f8 94 * @return True if it succeed.
shintamainjp 6:f6ceafabe9f8 95 */
shintamainjp 0:6b4ba48753b9 96 bool ConfigFile::setValue(char *key, char *value) {
shintamainjp 0:6b4ba48753b9 97 /*
shintamainjp 0:6b4ba48753b9 98 * Null check.
shintamainjp 0:6b4ba48753b9 99 */
shintamainjp 0:6b4ba48753b9 100 if ((key == NULL) || (value == NULL)) {
shintamainjp 0:6b4ba48753b9 101 return false;
shintamainjp 0:6b4ba48753b9 102 }
shintamainjp 0:6b4ba48753b9 103
shintamainjp 0:6b4ba48753b9 104 /*
shintamainjp 0:6b4ba48753b9 105 * Size check.
shintamainjp 0:6b4ba48753b9 106 */
shintamainjp 0:6b4ba48753b9 107 if ((MAXLEN_KEY < strlen(key)) || (MAXLEN_VALUE < strlen(value))) {
shintamainjp 0:6b4ba48753b9 108 return false;
shintamainjp 0:6b4ba48753b9 109 }
shintamainjp 0:6b4ba48753b9 110
shintamainjp 0:6b4ba48753b9 111 /*
shintamainjp 0:6b4ba48753b9 112 * Search a config_t object from the key.
shintamainjp 0:6b4ba48753b9 113 */
shintamainjp 0:6b4ba48753b9 114 config_t *p = search(key);
shintamainjp 0:6b4ba48753b9 115 if (p == NULL) {
shintamainjp 0:6b4ba48753b9 116 /*
shintamainjp 0:6b4ba48753b9 117 * Allocation a memory for a new key.
shintamainjp 0:6b4ba48753b9 118 */
shintamainjp 0:6b4ba48753b9 119 char *k = (char *)malloc(sizeof(char) * (strlen(key) + 1));
shintamainjp 0:6b4ba48753b9 120 if (k == NULL) {
shintamainjp 0:6b4ba48753b9 121 return false;
shintamainjp 0:6b4ba48753b9 122 }
shintamainjp 0:6b4ba48753b9 123 strcpy(k, key);
shintamainjp 0:6b4ba48753b9 124
shintamainjp 0:6b4ba48753b9 125 /*
shintamainjp 0:6b4ba48753b9 126 * Allocation a memory for a new value.
shintamainjp 0:6b4ba48753b9 127 */
shintamainjp 0:6b4ba48753b9 128 char *v = (char *)malloc(sizeof(char) * (strlen(value) + 1));
shintamainjp 0:6b4ba48753b9 129 if (v == NULL) {
shintamainjp 0:6b4ba48753b9 130 free(k);
shintamainjp 0:6b4ba48753b9 131 return false;
shintamainjp 0:6b4ba48753b9 132 }
shintamainjp 0:6b4ba48753b9 133 strcpy(v, value);
shintamainjp 0:6b4ba48753b9 134
shintamainjp 0:6b4ba48753b9 135 /*
shintamainjp 0:6b4ba48753b9 136 * Allocation a memory for a new configuration.
shintamainjp 0:6b4ba48753b9 137 */
shintamainjp 0:6b4ba48753b9 138 config_t *cfg = (config_t *)malloc(sizeof(config_t) * 1);
shintamainjp 0:6b4ba48753b9 139 if (cfg == NULL) {
shintamainjp 0:6b4ba48753b9 140 free(k);
shintamainjp 0:6b4ba48753b9 141 free(v);
shintamainjp 0:6b4ba48753b9 142 return false;
shintamainjp 0:6b4ba48753b9 143 }
shintamainjp 0:6b4ba48753b9 144 cfg->key = k;
shintamainjp 0:6b4ba48753b9 145 cfg->value = v;
shintamainjp 0:6b4ba48753b9 146
shintamainjp 0:6b4ba48753b9 147 /*
shintamainjp 0:6b4ba48753b9 148 * Add the new configuration.
shintamainjp 0:6b4ba48753b9 149 */
shintamainjp 0:6b4ba48753b9 150 if (!add(cfg)) {
shintamainjp 0:6b4ba48753b9 151 free(k);
shintamainjp 0:6b4ba48753b9 152 free(v);
shintamainjp 0:6b4ba48753b9 153 free(cfg);
shintamainjp 0:6b4ba48753b9 154 return false;
shintamainjp 0:6b4ba48753b9 155 }
shintamainjp 0:6b4ba48753b9 156
shintamainjp 0:6b4ba48753b9 157 return true;
shintamainjp 0:6b4ba48753b9 158 } else {
shintamainjp 0:6b4ba48753b9 159 /*
shintamainjp 0:6b4ba48753b9 160 * The value is same.
shintamainjp 0:6b4ba48753b9 161 */
shintamainjp 0:6b4ba48753b9 162 if (strcmp(value, p->value) == 0) {
shintamainjp 0:6b4ba48753b9 163 return true;
shintamainjp 0:6b4ba48753b9 164 }
shintamainjp 0:6b4ba48753b9 165
shintamainjp 0:6b4ba48753b9 166 /*
shintamainjp 0:6b4ba48753b9 167 * Free a memory for the value.
shintamainjp 0:6b4ba48753b9 168 */
shintamainjp 0:6b4ba48753b9 169 free(p->value);
shintamainjp 0:6b4ba48753b9 170 p->value = NULL;
shintamainjp 0:6b4ba48753b9 171
shintamainjp 0:6b4ba48753b9 172 /*
shintamainjp 0:6b4ba48753b9 173 * Allocation memory for the new value.
shintamainjp 0:6b4ba48753b9 174 */
shintamainjp 0:6b4ba48753b9 175 char *v = (char *)malloc(sizeof(char) * (strlen(value) + 1));
shintamainjp 0:6b4ba48753b9 176 if (v == NULL) {
shintamainjp 0:6b4ba48753b9 177 return false;
shintamainjp 0:6b4ba48753b9 178 }
shintamainjp 0:6b4ba48753b9 179
shintamainjp 0:6b4ba48753b9 180 /*
shintamainjp 0:6b4ba48753b9 181 * Store it.
shintamainjp 0:6b4ba48753b9 182 */
shintamainjp 0:6b4ba48753b9 183 strcpy(v, value);
shintamainjp 0:6b4ba48753b9 184 p->value = v;
shintamainjp 0:6b4ba48753b9 185
shintamainjp 0:6b4ba48753b9 186 return true;
shintamainjp 0:6b4ba48753b9 187 }
shintamainjp 0:6b4ba48753b9 188 }
shintamainjp 0:6b4ba48753b9 189
shintamainjp 6:f6ceafabe9f8 190 /**
shintamainjp 6:f6ceafabe9f8 191 * Remove a config.
shintamainjp 6:f6ceafabe9f8 192 *
shintamainjp 6:f6ceafabe9f8 193 * @param key A key.
shintamainjp 6:f6ceafabe9f8 194 *
shintamainjp 6:f6ceafabe9f8 195 * @return True if it succeed.
shintamainjp 6:f6ceafabe9f8 196 */
shintamainjp 0:6b4ba48753b9 197 bool ConfigFile::remove(char *key) {
shintamainjp 0:6b4ba48753b9 198 if (key == NULL) {
shintamainjp 0:6b4ba48753b9 199 return false;
shintamainjp 0:6b4ba48753b9 200 }
shintamainjp 0:6b4ba48753b9 201 for (int i = 0; i < MAXCONFIG; i++) {
shintamainjp 0:6b4ba48753b9 202 config_t *cfg = configlist[i];
shintamainjp 0:6b4ba48753b9 203 if (cfg != NULL) {
shintamainjp 0:6b4ba48753b9 204 if (strcmp(cfg->key, key) == 0) {
shintamainjp 0:6b4ba48753b9 205 free(cfg->key);
shintamainjp 0:6b4ba48753b9 206 free(cfg->value);
shintamainjp 0:6b4ba48753b9 207 free(cfg);
shintamainjp 0:6b4ba48753b9 208 configlist[i] = NULL;
shintamainjp 0:6b4ba48753b9 209 return true;
shintamainjp 0:6b4ba48753b9 210 }
shintamainjp 0:6b4ba48753b9 211 }
shintamainjp 0:6b4ba48753b9 212 }
shintamainjp 0:6b4ba48753b9 213 return false;
shintamainjp 0:6b4ba48753b9 214 }
shintamainjp 0:6b4ba48753b9 215
shintamainjp 6:f6ceafabe9f8 216 /**
shintamainjp 6:f6ceafabe9f8 217 * Remove all config.
shintamainjp 6:f6ceafabe9f8 218 *
shintamainjp 6:f6ceafabe9f8 219 * @return True if it succeed.
shintamainjp 6:f6ceafabe9f8 220 */
shintamainjp 0:6b4ba48753b9 221 bool ConfigFile::removeAll(void) {
shintamainjp 0:6b4ba48753b9 222 for (int i = 0; i < MAXCONFIG; i++) {
shintamainjp 0:6b4ba48753b9 223 config_t *p = configlist[i];
shintamainjp 0:6b4ba48753b9 224 if (p != NULL) {
shintamainjp 0:6b4ba48753b9 225 free(p->key);
shintamainjp 0:6b4ba48753b9 226 free(p->value);
shintamainjp 0:6b4ba48753b9 227 }
shintamainjp 0:6b4ba48753b9 228 free(p);
shintamainjp 0:6b4ba48753b9 229 configlist[i] = NULL;
shintamainjp 0:6b4ba48753b9 230 }
shintamainjp 0:6b4ba48753b9 231 return true;
shintamainjp 0:6b4ba48753b9 232 }
shintamainjp 0:6b4ba48753b9 233
shintamainjp 6:f6ceafabe9f8 234 /**
shintamainjp 6:f6ceafabe9f8 235 * Get a number of configuration sets.
shintamainjp 6:f6ceafabe9f8 236 *
shintamainjp 6:f6ceafabe9f8 237 * @return number of configuration sets.
shintamainjp 6:f6ceafabe9f8 238 */
shintamainjp 5:56d544b8e5c6 239 int ConfigFile::getCount() {
shintamainjp 5:56d544b8e5c6 240 int cnt = 0;
shintamainjp 5:56d544b8e5c6 241 for (int i = 0; i < MAXCONFIG; i++) {
shintamainjp 5:56d544b8e5c6 242 config_t *p = configlist[i];
shintamainjp 5:56d544b8e5c6 243 if (p != NULL) {
shintamainjp 5:56d544b8e5c6 244 cnt++;
shintamainjp 5:56d544b8e5c6 245 }
shintamainjp 5:56d544b8e5c6 246 }
shintamainjp 5:56d544b8e5c6 247 return cnt;
shintamainjp 5:56d544b8e5c6 248 }
shintamainjp 5:56d544b8e5c6 249
shintamainjp 6:f6ceafabe9f8 250 /**
shintamainjp 6:f6ceafabe9f8 251 * Get a key and a value.
shintamainjp 6:f6ceafabe9f8 252 *
shintamainjp 6:f6ceafabe9f8 253 * @param index Index number of this list.
shintamainjp 6:f6ceafabe9f8 254 * @param key A pointer to a buffer for key.
shintamainjp 6:f6ceafabe9f8 255 * @param keybufsiz A size of the key buffer.
shintamainjp 6:f6ceafabe9f8 256 * @param value A pointer to a buffer for value.
shintamainjp 6:f6ceafabe9f8 257 * @param valuebufsiz A size of the value buffer.
shintamainjp 6:f6ceafabe9f8 258 *
shintamainjp 6:f6ceafabe9f8 259 * @return true if it succeed.
shintamainjp 6:f6ceafabe9f8 260 */
shintamainjp 5:56d544b8e5c6 261 bool ConfigFile::getKeyAndValue(int index, char *key, size_t keybufsiz, char *value, size_t valuebufsiz) {
shintamainjp 5:56d544b8e5c6 262 int cnt = 0;
shintamainjp 5:56d544b8e5c6 263 for (int i = 0; i < MAXCONFIG; i++) {
shintamainjp 5:56d544b8e5c6 264 config_t *p = configlist[i];
shintamainjp 5:56d544b8e5c6 265 if (p != NULL) {
shintamainjp 5:56d544b8e5c6 266 if (cnt == index) {
shintamainjp 5:56d544b8e5c6 267 if ((strlen(p->key) < keybufsiz) && (strlen(p->value) < valuebufsiz)) {
shintamainjp 5:56d544b8e5c6 268 strcpy(key, p->key);
shintamainjp 5:56d544b8e5c6 269 strcpy(value, p->value);
shintamainjp 5:56d544b8e5c6 270 return true;
shintamainjp 5:56d544b8e5c6 271 }
shintamainjp 5:56d544b8e5c6 272 return false;
shintamainjp 5:56d544b8e5c6 273 }
shintamainjp 5:56d544b8e5c6 274 cnt++;
shintamainjp 5:56d544b8e5c6 275 }
shintamainjp 5:56d544b8e5c6 276 }
shintamainjp 5:56d544b8e5c6 277 return false;
shintamainjp 5:56d544b8e5c6 278 }
shintamainjp 5:56d544b8e5c6 279
shintamainjp 6:f6ceafabe9f8 280 /**
shintamainjp 6:f6ceafabe9f8 281 * Read from the target file.
shintamainjp 6:f6ceafabe9f8 282 *
shintamainjp 6:f6ceafabe9f8 283 * @param file A target file name.
shintamainjp 6:f6ceafabe9f8 284 */
shintamainjp 0:6b4ba48753b9 285 bool ConfigFile::read(char *file) {
shintamainjp 0:6b4ba48753b9 286 /*
shintamainjp 0:6b4ba48753b9 287 * Open the target file.
shintamainjp 0:6b4ba48753b9 288 */
shintamainjp 0:6b4ba48753b9 289 FILE *fp = fopen(file, "r");
shintamainjp 0:6b4ba48753b9 290 if (fp == NULL) {
shintamainjp 0:6b4ba48753b9 291 return false;
shintamainjp 0:6b4ba48753b9 292 }
shintamainjp 0:6b4ba48753b9 293
shintamainjp 0:6b4ba48753b9 294 /*
shintamainjp 0:6b4ba48753b9 295 * Remove all configuration.
shintamainjp 0:6b4ba48753b9 296 */
shintamainjp 0:6b4ba48753b9 297 removeAll();
shintamainjp 0:6b4ba48753b9 298
shintamainjp 0:6b4ba48753b9 299 /*
shintamainjp 0:6b4ba48753b9 300 * Read from a file.
shintamainjp 0:6b4ba48753b9 301 */
shintamainjp 0:6b4ba48753b9 302 char buf[MAXLEN_KEY + 8 + MAXLEN_VALUE];
shintamainjp 0:6b4ba48753b9 303 while (fgets(buf, sizeof(buf), fp) != NULL) {
shintamainjp 0:6b4ba48753b9 304 /*
shintamainjp 0:6b4ba48753b9 305 * Ignore a comment.
shintamainjp 0:6b4ba48753b9 306 */
shintamainjp 0:6b4ba48753b9 307 if (buf[0] == '#') {
shintamainjp 0:6b4ba48753b9 308 continue;
shintamainjp 0:6b4ba48753b9 309 }
shintamainjp 2:d8febae84a45 310
shintamainjp 0:6b4ba48753b9 311 /*
shintamainjp 0:6b4ba48753b9 312 * Trim a return.
shintamainjp 0:6b4ba48753b9 313 */
shintamainjp 0:6b4ba48753b9 314 const size_t len = strlen(buf);
shintamainjp 0:6b4ba48753b9 315 for (int i = 0; i < len; i++) {
shintamainjp 0:6b4ba48753b9 316 if ((buf[i] == '\r') || (buf[i] == '\n')) {
shintamainjp 0:6b4ba48753b9 317 buf[i] = '\0';
shintamainjp 0:6b4ba48753b9 318 }
shintamainjp 0:6b4ba48753b9 319 }
shintamainjp 0:6b4ba48753b9 320
shintamainjp 0:6b4ba48753b9 321 /*
shintamainjp 0:6b4ba48753b9 322 * Separate key and value.
shintamainjp 2:d8febae84a45 323 */
shintamainjp 0:6b4ba48753b9 324 char k[MAXLEN_KEY];
shintamainjp 0:6b4ba48753b9 325 char v[MAXLEN_VALUE];
shintamainjp 0:6b4ba48753b9 326 char *sp = strchr(buf, SEPARATOR);
shintamainjp 0:6b4ba48753b9 327 if (sp != NULL) {
shintamainjp 0:6b4ba48753b9 328 strcpy(v, sp + 1);
shintamainjp 0:6b4ba48753b9 329 *sp = '\0';
shintamainjp 0:6b4ba48753b9 330 strcpy(k, buf);
shintamainjp 0:6b4ba48753b9 331 setValue(k, v);
shintamainjp 0:6b4ba48753b9 332 }
shintamainjp 0:6b4ba48753b9 333 }
shintamainjp 0:6b4ba48753b9 334 fclose(fp);
shintamainjp 0:6b4ba48753b9 335 return true;
shintamainjp 0:6b4ba48753b9 336 }
shintamainjp 0:6b4ba48753b9 337
shintamainjp 6:f6ceafabe9f8 338 /**
shintamainjp 6:f6ceafabe9f8 339 * Write from the target file.
shintamainjp 6:f6ceafabe9f8 340 *
shintamainjp 6:f6ceafabe9f8 341 * @param file A pointer to a file name.
shintamainjp 6:f6ceafabe9f8 342 * @param header A pointer to a header.
shintamainjp 6:f6ceafabe9f8 343 * @param ff File format.
shintamainjp 6:f6ceafabe9f8 344 */
shintamainjp 4:940510a29b44 345 bool ConfigFile::write(char *file, char *header, FileFormat ff) {
shintamainjp 2:d8febae84a45 346 /*
shintamainjp 2:d8febae84a45 347 * Open the target file.
shintamainjp 2:d8febae84a45 348 */
shintamainjp 0:6b4ba48753b9 349 FILE *fp = fopen(file, "w");
shintamainjp 0:6b4ba48753b9 350 if (fp == NULL) {
shintamainjp 0:6b4ba48753b9 351 return false;
shintamainjp 0:6b4ba48753b9 352 }
shintamainjp 0:6b4ba48753b9 353
shintamainjp 2:d8febae84a45 354 /*
shintamainjp 2:d8febae84a45 355 * Set a type of new line.
shintamainjp 2:d8febae84a45 356 */
shintamainjp 2:d8febae84a45 357 char *newline = NEWLINE_UNIX;
shintamainjp 2:d8febae84a45 358 switch (ff) {
shintamainjp 2:d8febae84a45 359 case UNIX:
shintamainjp 2:d8febae84a45 360 newline = NEWLINE_UNIX;
shintamainjp 2:d8febae84a45 361 break;
shintamainjp 2:d8febae84a45 362 case MAC:
shintamainjp 2:d8febae84a45 363 newline = NEWLINE_MAC;
shintamainjp 2:d8febae84a45 364 break;
shintamainjp 2:d8febae84a45 365 case DOS:
shintamainjp 2:d8febae84a45 366 newline = NEWLINE_DOS;
shintamainjp 2:d8febae84a45 367 break;
shintamainjp 2:d8febae84a45 368 default:
shintamainjp 2:d8febae84a45 369 newline = NEWLINE_UNIX;
shintamainjp 2:d8febae84a45 370 break;
shintamainjp 2:d8febae84a45 371 }
shintamainjp 2:d8febae84a45 372
shintamainjp 2:d8febae84a45 373 /*
shintamainjp 4:940510a29b44 374 * Write the header.
shintamainjp 4:940510a29b44 375 */
shintamainjp 4:940510a29b44 376 if (header != NULL) {
shintamainjp 4:940510a29b44 377 fprintf(fp, "%s%s", header, newline);
shintamainjp 4:940510a29b44 378 }
shintamainjp 4:940510a29b44 379
shintamainjp 4:940510a29b44 380 /*
shintamainjp 2:d8febae84a45 381 * Write the data.
shintamainjp 2:d8febae84a45 382 */
shintamainjp 0:6b4ba48753b9 383 for (int i = 0; i < MAXCONFIG; i++) {
shintamainjp 0:6b4ba48753b9 384 config_t *cfg = configlist[i];
shintamainjp 0:6b4ba48753b9 385 if (cfg != NULL) {
shintamainjp 2:d8febae84a45 386 fprintf(fp, "%s=%s%s", cfg->key, cfg->value, newline);
shintamainjp 0:6b4ba48753b9 387 }
shintamainjp 0:6b4ba48753b9 388 }
shintamainjp 0:6b4ba48753b9 389 fclose(fp);
shintamainjp 0:6b4ba48753b9 390 return true;
shintamainjp 0:6b4ba48753b9 391 }
shintamainjp 0:6b4ba48753b9 392
shintamainjp 0:6b4ba48753b9 393 ConfigFile::config_t *ConfigFile::search(char *key) {
shintamainjp 0:6b4ba48753b9 394 if (key == NULL) {
shintamainjp 0:6b4ba48753b9 395 return NULL;
shintamainjp 0:6b4ba48753b9 396 }
shintamainjp 0:6b4ba48753b9 397 for (int i = 0; i < MAXCONFIG; i++) {
shintamainjp 0:6b4ba48753b9 398 if (configlist[i] != NULL) {
shintamainjp 0:6b4ba48753b9 399 if (strcmp(configlist[i]->key, key) == 0) {
shintamainjp 0:6b4ba48753b9 400 return configlist[i];
shintamainjp 0:6b4ba48753b9 401 }
shintamainjp 0:6b4ba48753b9 402 }
shintamainjp 0:6b4ba48753b9 403 }
shintamainjp 0:6b4ba48753b9 404 return NULL;
shintamainjp 0:6b4ba48753b9 405 }
shintamainjp 0:6b4ba48753b9 406
shintamainjp 0:6b4ba48753b9 407 bool ConfigFile::add(config_t *cfg) {
shintamainjp 0:6b4ba48753b9 408 for (int i = 0; i < MAXCONFIG; i++) {
shintamainjp 0:6b4ba48753b9 409 if (configlist[i] == NULL) {
shintamainjp 0:6b4ba48753b9 410 configlist[i] = cfg;
shintamainjp 0:6b4ba48753b9 411 return true;
shintamainjp 0:6b4ba48753b9 412 }
shintamainjp 0:6b4ba48753b9 413 }
shintamainjp 0:6b4ba48753b9 414 return false;
shintamainjp 0:6b4ba48753b9 415 }