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:22:00 2010 +0000
Revision:
1:f02e081afe42
Parent:
0:6b4ba48753b9
Child:
2:d8febae84a45

        

Who changed what in which revision?

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