Changed general file descriptors to specific for CDU

Dependents:   CDUFinal CDU_Mbed_36

Fork of ConfigFile by Shinichiro Nakamura

Committer:
shintamainjp
Date:
Sun Sep 12 07:35:25 2010 +0000
Revision:
2:d8febae84a45
Parent:
1:f02e081afe42
Child:
3:7250e339328c

        

Who changed what in which revision?

UserRevisionLine numberNew contents of line
shintamainjp 0:6b4ba48753b9 1 #include "ConfigFile.h"
shintamainjp 0:6b4ba48753b9 2
shintamainjp 2:d8febae84a45 3 #define NEWLINE_UNIX "\n"
shintamainjp 2:d8febae84a45 4 #define NEWLINE_DOS "\r\n"
shintamainjp 2:d8febae84a45 5 #define NEWLINE_MAC "\r"
shintamainjp 2:d8febae84a45 6
shintamainjp 0:6b4ba48753b9 7 ConfigFile::ConfigFile() {
shintamainjp 0:6b4ba48753b9 8 /*
shintamainjp 0:6b4ba48753b9 9 * Allocation for a config_t list.
shintamainjp 0:6b4ba48753b9 10 */
shintamainjp 0:6b4ba48753b9 11 configlist = (config_t **)malloc(sizeof(config_t *) * MAXCONFIG);
shintamainjp 0:6b4ba48753b9 12 for (int i = 0; i < MAXCONFIG; i++) {
shintamainjp 0:6b4ba48753b9 13 configlist[i] = NULL;
shintamainjp 0:6b4ba48753b9 14 }
shintamainjp 0:6b4ba48753b9 15 }
shintamainjp 0:6b4ba48753b9 16
shintamainjp 0:6b4ba48753b9 17 ConfigFile::~ConfigFile() {
shintamainjp 0:6b4ba48753b9 18 /*
shintamainjp 0:6b4ba48753b9 19 * Remove all storage and the contents.
shintamainjp 0:6b4ba48753b9 20 */
shintamainjp 0:6b4ba48753b9 21 for (int i = 0; i < MAXCONFIG; i++) {
shintamainjp 0:6b4ba48753b9 22 config_t *cfg = configlist[i];
shintamainjp 0:6b4ba48753b9 23 if (cfg != NULL) {
shintamainjp 0:6b4ba48753b9 24 free(cfg->key);
shintamainjp 0:6b4ba48753b9 25 free(cfg->value);
shintamainjp 0:6b4ba48753b9 26 free(cfg);
shintamainjp 0:6b4ba48753b9 27 }
shintamainjp 0:6b4ba48753b9 28 configlist[i] = NULL;
shintamainjp 0:6b4ba48753b9 29 }
shintamainjp 0:6b4ba48753b9 30
shintamainjp 0:6b4ba48753b9 31 /*
shintamainjp 0:6b4ba48753b9 32 * Remove cnofig_t list.
shintamainjp 0:6b4ba48753b9 33 */
shintamainjp 0:6b4ba48753b9 34 free(configlist);
shintamainjp 0:6b4ba48753b9 35 configlist = NULL;
shintamainjp 0:6b4ba48753b9 36 }
shintamainjp 0:6b4ba48753b9 37
shintamainjp 1:f02e081afe42 38 bool ConfigFile::getValue(char *key, char *value, size_t siz) {
shintamainjp 0:6b4ba48753b9 39 /*
shintamainjp 0:6b4ba48753b9 40 * Null check.
shintamainjp 0:6b4ba48753b9 41 */
shintamainjp 0:6b4ba48753b9 42 if (key == NULL) {
shintamainjp 1:f02e081afe42 43 return false;
shintamainjp 0:6b4ba48753b9 44 }
shintamainjp 0:6b4ba48753b9 45
shintamainjp 0:6b4ba48753b9 46 /*
shintamainjp 0:6b4ba48753b9 47 * Search a config_t object from the key.
shintamainjp 0:6b4ba48753b9 48 */
shintamainjp 0:6b4ba48753b9 49 config_t *p = search(key);
shintamainjp 0:6b4ba48753b9 50 if (p == NULL) {
shintamainjp 1:f02e081afe42 51 return false;
shintamainjp 1:f02e081afe42 52 }
shintamainjp 2:d8febae84a45 53
shintamainjp 1:f02e081afe42 54 /*
shintamainjp 1:f02e081afe42 55 * Check the storage size.
shintamainjp 1:f02e081afe42 56 */
shintamainjp 1:f02e081afe42 57 if (siz <= strlen(p->value)) {
shintamainjp 1:f02e081afe42 58 return false;
shintamainjp 0:6b4ba48753b9 59 }
shintamainjp 0:6b4ba48753b9 60
shintamainjp 0:6b4ba48753b9 61 /*
shintamainjp 1:f02e081afe42 62 * Copy the value to the storage.
shintamainjp 0:6b4ba48753b9 63 */
shintamainjp 1:f02e081afe42 64 strcpy(value, p->value);
shintamainjp 1:f02e081afe42 65 return true;
shintamainjp 0:6b4ba48753b9 66 }
shintamainjp 0:6b4ba48753b9 67
shintamainjp 0:6b4ba48753b9 68 bool ConfigFile::setValue(char *key, char *value) {
shintamainjp 0:6b4ba48753b9 69 /*
shintamainjp 0:6b4ba48753b9 70 * Null check.
shintamainjp 0:6b4ba48753b9 71 */
shintamainjp 0:6b4ba48753b9 72 if ((key == NULL) || (value == NULL)) {
shintamainjp 0:6b4ba48753b9 73 return false;
shintamainjp 0:6b4ba48753b9 74 }
shintamainjp 0:6b4ba48753b9 75
shintamainjp 0:6b4ba48753b9 76 /*
shintamainjp 0:6b4ba48753b9 77 * Size check.
shintamainjp 0:6b4ba48753b9 78 */
shintamainjp 0:6b4ba48753b9 79 if ((MAXLEN_KEY < strlen(key)) || (MAXLEN_VALUE < strlen(value))) {
shintamainjp 0:6b4ba48753b9 80 return false;
shintamainjp 0:6b4ba48753b9 81 }
shintamainjp 0:6b4ba48753b9 82
shintamainjp 0:6b4ba48753b9 83 /*
shintamainjp 0:6b4ba48753b9 84 * Search a config_t object from the key.
shintamainjp 0:6b4ba48753b9 85 */
shintamainjp 0:6b4ba48753b9 86 config_t *p = search(key);
shintamainjp 0:6b4ba48753b9 87 if (p == NULL) {
shintamainjp 0:6b4ba48753b9 88 /*
shintamainjp 0:6b4ba48753b9 89 * Allocation a memory for a new key.
shintamainjp 0:6b4ba48753b9 90 */
shintamainjp 0:6b4ba48753b9 91 char *k = (char *)malloc(sizeof(char) * (strlen(key) + 1));
shintamainjp 0:6b4ba48753b9 92 if (k == NULL) {
shintamainjp 0:6b4ba48753b9 93 return false;
shintamainjp 0:6b4ba48753b9 94 }
shintamainjp 0:6b4ba48753b9 95 strcpy(k, key);
shintamainjp 0:6b4ba48753b9 96
shintamainjp 0:6b4ba48753b9 97 /*
shintamainjp 0:6b4ba48753b9 98 * Allocation a memory for a new value.
shintamainjp 0:6b4ba48753b9 99 */
shintamainjp 0:6b4ba48753b9 100 char *v = (char *)malloc(sizeof(char) * (strlen(value) + 1));
shintamainjp 0:6b4ba48753b9 101 if (v == NULL) {
shintamainjp 0:6b4ba48753b9 102 free(k);
shintamainjp 0:6b4ba48753b9 103 return false;
shintamainjp 0:6b4ba48753b9 104 }
shintamainjp 0:6b4ba48753b9 105 strcpy(v, value);
shintamainjp 0:6b4ba48753b9 106
shintamainjp 0:6b4ba48753b9 107 /*
shintamainjp 0:6b4ba48753b9 108 * Allocation a memory for a new configuration.
shintamainjp 0:6b4ba48753b9 109 */
shintamainjp 0:6b4ba48753b9 110 config_t *cfg = (config_t *)malloc(sizeof(config_t) * 1);
shintamainjp 0:6b4ba48753b9 111 if (cfg == NULL) {
shintamainjp 0:6b4ba48753b9 112 free(k);
shintamainjp 0:6b4ba48753b9 113 free(v);
shintamainjp 0:6b4ba48753b9 114 return false;
shintamainjp 0:6b4ba48753b9 115 }
shintamainjp 0:6b4ba48753b9 116 cfg->key = k;
shintamainjp 0:6b4ba48753b9 117 cfg->value = v;
shintamainjp 0:6b4ba48753b9 118
shintamainjp 0:6b4ba48753b9 119 /*
shintamainjp 0:6b4ba48753b9 120 * Add the new configuration.
shintamainjp 0:6b4ba48753b9 121 */
shintamainjp 0:6b4ba48753b9 122 if (!add(cfg)) {
shintamainjp 0:6b4ba48753b9 123 free(k);
shintamainjp 0:6b4ba48753b9 124 free(v);
shintamainjp 0:6b4ba48753b9 125 free(cfg);
shintamainjp 0:6b4ba48753b9 126 return false;
shintamainjp 0:6b4ba48753b9 127 }
shintamainjp 0:6b4ba48753b9 128
shintamainjp 0:6b4ba48753b9 129 return true;
shintamainjp 0:6b4ba48753b9 130 } else {
shintamainjp 0:6b4ba48753b9 131 /*
shintamainjp 0:6b4ba48753b9 132 * The value is same.
shintamainjp 0:6b4ba48753b9 133 */
shintamainjp 0:6b4ba48753b9 134 if (strcmp(value, p->value) == 0) {
shintamainjp 0:6b4ba48753b9 135 return true;
shintamainjp 0:6b4ba48753b9 136 }
shintamainjp 0:6b4ba48753b9 137
shintamainjp 0:6b4ba48753b9 138 /*
shintamainjp 0:6b4ba48753b9 139 * Free a memory for the value.
shintamainjp 0:6b4ba48753b9 140 */
shintamainjp 0:6b4ba48753b9 141 free(p->value);
shintamainjp 0:6b4ba48753b9 142 p->value = NULL;
shintamainjp 0:6b4ba48753b9 143
shintamainjp 0:6b4ba48753b9 144 /*
shintamainjp 0:6b4ba48753b9 145 * Allocation memory for the new value.
shintamainjp 0:6b4ba48753b9 146 */
shintamainjp 0:6b4ba48753b9 147 char *v = (char *)malloc(sizeof(char) * (strlen(value) + 1));
shintamainjp 0:6b4ba48753b9 148 if (v == NULL) {
shintamainjp 0:6b4ba48753b9 149 return false;
shintamainjp 0:6b4ba48753b9 150 }
shintamainjp 0:6b4ba48753b9 151
shintamainjp 0:6b4ba48753b9 152 /*
shintamainjp 0:6b4ba48753b9 153 * Store it.
shintamainjp 0:6b4ba48753b9 154 */
shintamainjp 0:6b4ba48753b9 155 strcpy(v, value);
shintamainjp 0:6b4ba48753b9 156 p->value = v;
shintamainjp 0:6b4ba48753b9 157
shintamainjp 0:6b4ba48753b9 158 return true;
shintamainjp 0:6b4ba48753b9 159 }
shintamainjp 0:6b4ba48753b9 160 }
shintamainjp 0:6b4ba48753b9 161
shintamainjp 0:6b4ba48753b9 162 bool ConfigFile::remove(char *key) {
shintamainjp 0:6b4ba48753b9 163 if (key == NULL) {
shintamainjp 0:6b4ba48753b9 164 return false;
shintamainjp 0:6b4ba48753b9 165 }
shintamainjp 0:6b4ba48753b9 166 for (int i = 0; i < MAXCONFIG; i++) {
shintamainjp 0:6b4ba48753b9 167 config_t *cfg = configlist[i];
shintamainjp 0:6b4ba48753b9 168 if (cfg != NULL) {
shintamainjp 0:6b4ba48753b9 169 if (strcmp(cfg->key, key) == 0) {
shintamainjp 0:6b4ba48753b9 170 free(cfg->key);
shintamainjp 0:6b4ba48753b9 171 free(cfg->value);
shintamainjp 0:6b4ba48753b9 172 free(cfg);
shintamainjp 0:6b4ba48753b9 173 configlist[i] = NULL;
shintamainjp 0:6b4ba48753b9 174 return true;
shintamainjp 0:6b4ba48753b9 175 }
shintamainjp 0:6b4ba48753b9 176 }
shintamainjp 0:6b4ba48753b9 177 }
shintamainjp 0:6b4ba48753b9 178 return false;
shintamainjp 0:6b4ba48753b9 179 }
shintamainjp 0:6b4ba48753b9 180
shintamainjp 0:6b4ba48753b9 181 bool ConfigFile::removeAll(void) {
shintamainjp 0:6b4ba48753b9 182 for (int i = 0; i < MAXCONFIG; i++) {
shintamainjp 0:6b4ba48753b9 183 config_t *p = configlist[i];
shintamainjp 0:6b4ba48753b9 184 if (p != NULL) {
shintamainjp 0:6b4ba48753b9 185 free(p->key);
shintamainjp 0:6b4ba48753b9 186 free(p->value);
shintamainjp 0:6b4ba48753b9 187 }
shintamainjp 0:6b4ba48753b9 188 free(p);
shintamainjp 0:6b4ba48753b9 189 configlist[i] = NULL;
shintamainjp 0:6b4ba48753b9 190 }
shintamainjp 0:6b4ba48753b9 191 return true;
shintamainjp 0:6b4ba48753b9 192 }
shintamainjp 0:6b4ba48753b9 193
shintamainjp 0:6b4ba48753b9 194 bool ConfigFile::read(char *file) {
shintamainjp 0:6b4ba48753b9 195 /*
shintamainjp 0:6b4ba48753b9 196 * Open the target file.
shintamainjp 0:6b4ba48753b9 197 */
shintamainjp 0:6b4ba48753b9 198 FILE *fp = fopen(file, "r");
shintamainjp 0:6b4ba48753b9 199 if (fp == NULL) {
shintamainjp 0:6b4ba48753b9 200 return false;
shintamainjp 0:6b4ba48753b9 201 }
shintamainjp 0:6b4ba48753b9 202
shintamainjp 0:6b4ba48753b9 203 /*
shintamainjp 0:6b4ba48753b9 204 * Remove all configuration.
shintamainjp 0:6b4ba48753b9 205 */
shintamainjp 0:6b4ba48753b9 206 removeAll();
shintamainjp 0:6b4ba48753b9 207
shintamainjp 0:6b4ba48753b9 208 /*
shintamainjp 0:6b4ba48753b9 209 * Read from a file.
shintamainjp 0:6b4ba48753b9 210 */
shintamainjp 0:6b4ba48753b9 211 char buf[MAXLEN_KEY + 8 + MAXLEN_VALUE];
shintamainjp 0:6b4ba48753b9 212 while (fgets(buf, sizeof(buf), fp) != NULL) {
shintamainjp 0:6b4ba48753b9 213 /*
shintamainjp 0:6b4ba48753b9 214 * Ignore a comment.
shintamainjp 0:6b4ba48753b9 215 */
shintamainjp 0:6b4ba48753b9 216 if (buf[0] == '#') {
shintamainjp 0:6b4ba48753b9 217 continue;
shintamainjp 0:6b4ba48753b9 218 }
shintamainjp 2:d8febae84a45 219
shintamainjp 0:6b4ba48753b9 220 /*
shintamainjp 0:6b4ba48753b9 221 * Trim a return.
shintamainjp 0:6b4ba48753b9 222 */
shintamainjp 0:6b4ba48753b9 223 const size_t len = strlen(buf);
shintamainjp 0:6b4ba48753b9 224 for (int i = 0; i < len; i++) {
shintamainjp 0:6b4ba48753b9 225 if ((buf[i] == '\r') || (buf[i] == '\n')) {
shintamainjp 0:6b4ba48753b9 226 buf[i] = '\0';
shintamainjp 0:6b4ba48753b9 227 }
shintamainjp 0:6b4ba48753b9 228 }
shintamainjp 0:6b4ba48753b9 229
shintamainjp 0:6b4ba48753b9 230 /*
shintamainjp 0:6b4ba48753b9 231 * Separate key and value.
shintamainjp 2:d8febae84a45 232 */
shintamainjp 0:6b4ba48753b9 233 char k[MAXLEN_KEY];
shintamainjp 0:6b4ba48753b9 234 char v[MAXLEN_VALUE];
shintamainjp 0:6b4ba48753b9 235 char *sp = strchr(buf, SEPARATOR);
shintamainjp 0:6b4ba48753b9 236 if (sp != NULL) {
shintamainjp 0:6b4ba48753b9 237 strcpy(v, sp + 1);
shintamainjp 0:6b4ba48753b9 238 *sp = '\0';
shintamainjp 0:6b4ba48753b9 239 strcpy(k, buf);
shintamainjp 0:6b4ba48753b9 240 setValue(k, v);
shintamainjp 0:6b4ba48753b9 241 }
shintamainjp 0:6b4ba48753b9 242 }
shintamainjp 0:6b4ba48753b9 243 fclose(fp);
shintamainjp 0:6b4ba48753b9 244 return true;
shintamainjp 0:6b4ba48753b9 245 }
shintamainjp 0:6b4ba48753b9 246
shintamainjp 2:d8febae84a45 247 bool ConfigFile::write(char *file, FileFormat ff) {
shintamainjp 2:d8febae84a45 248 /*
shintamainjp 2:d8febae84a45 249 * Open the target file.
shintamainjp 2:d8febae84a45 250 */
shintamainjp 0:6b4ba48753b9 251 FILE *fp = fopen(file, "w");
shintamainjp 0:6b4ba48753b9 252 if (fp == NULL) {
shintamainjp 0:6b4ba48753b9 253 return false;
shintamainjp 0:6b4ba48753b9 254 }
shintamainjp 0:6b4ba48753b9 255
shintamainjp 2:d8febae84a45 256 /*
shintamainjp 2:d8febae84a45 257 * Set a type of new line.
shintamainjp 2:d8febae84a45 258 */
shintamainjp 2:d8febae84a45 259 char *newline = NEWLINE_UNIX;
shintamainjp 2:d8febae84a45 260 switch (ff) {
shintamainjp 2:d8febae84a45 261 case UNIX:
shintamainjp 2:d8febae84a45 262 newline = NEWLINE_UNIX;
shintamainjp 2:d8febae84a45 263 break;
shintamainjp 2:d8febae84a45 264 case MAC:
shintamainjp 2:d8febae84a45 265 newline = NEWLINE_MAC;
shintamainjp 2:d8febae84a45 266 break;
shintamainjp 2:d8febae84a45 267 case DOS:
shintamainjp 2:d8febae84a45 268 newline = NEWLINE_DOS;
shintamainjp 2:d8febae84a45 269 break;
shintamainjp 2:d8febae84a45 270 default:
shintamainjp 2:d8febae84a45 271 newline = NEWLINE_UNIX;
shintamainjp 2:d8febae84a45 272 break;
shintamainjp 2:d8febae84a45 273 }
shintamainjp 2:d8febae84a45 274
shintamainjp 2:d8febae84a45 275 /*
shintamainjp 2:d8febae84a45 276 * Write the data.
shintamainjp 2:d8febae84a45 277 */
shintamainjp 0:6b4ba48753b9 278 for (int i = 0; i < MAXCONFIG; i++) {
shintamainjp 0:6b4ba48753b9 279 config_t *cfg = configlist[i];
shintamainjp 0:6b4ba48753b9 280 if (cfg != NULL) {
shintamainjp 2:d8febae84a45 281 fprintf(fp, "%s=%s%s", cfg->key, cfg->value, newline);
shintamainjp 0:6b4ba48753b9 282 }
shintamainjp 0:6b4ba48753b9 283 }
shintamainjp 0:6b4ba48753b9 284 fclose(fp);
shintamainjp 0:6b4ba48753b9 285 return true;
shintamainjp 0:6b4ba48753b9 286 }
shintamainjp 0:6b4ba48753b9 287
shintamainjp 0:6b4ba48753b9 288 ConfigFile::config_t *ConfigFile::search(char *key) {
shintamainjp 0:6b4ba48753b9 289 if (key == NULL) {
shintamainjp 0:6b4ba48753b9 290 return NULL;
shintamainjp 0:6b4ba48753b9 291 }
shintamainjp 0:6b4ba48753b9 292 for (int i = 0; i < MAXCONFIG; i++) {
shintamainjp 0:6b4ba48753b9 293 if (configlist[i] != NULL) {
shintamainjp 0:6b4ba48753b9 294 if (strcmp(configlist[i]->key, key) == 0) {
shintamainjp 0:6b4ba48753b9 295 return configlist[i];
shintamainjp 0:6b4ba48753b9 296 }
shintamainjp 0:6b4ba48753b9 297 }
shintamainjp 0:6b4ba48753b9 298 }
shintamainjp 0:6b4ba48753b9 299 return NULL;
shintamainjp 0:6b4ba48753b9 300 }
shintamainjp 0:6b4ba48753b9 301
shintamainjp 0:6b4ba48753b9 302 bool ConfigFile::add(config_t *cfg) {
shintamainjp 0:6b4ba48753b9 303 for (int i = 0; i < MAXCONFIG; i++) {
shintamainjp 0:6b4ba48753b9 304 if (configlist[i] == NULL) {
shintamainjp 0:6b4ba48753b9 305 configlist[i] = cfg;
shintamainjp 0:6b4ba48753b9 306 return true;
shintamainjp 0:6b4ba48753b9 307 }
shintamainjp 0:6b4ba48753b9 308 }
shintamainjp 0:6b4ba48753b9 309 return false;
shintamainjp 0:6b4ba48753b9 310 }