Changed general file descriptors to specific for CDU
Dependents: CDUFinal CDU_Mbed_36
Fork of ConfigFile by
ConfigFile.cpp@0:6b4ba48753b9, 2010-09-12 (annotated)
- Committer:
- shintamainjp
- Date:
- Sun Sep 12 07:11:54 2010 +0000
- Revision:
- 0:6b4ba48753b9
- Child:
- 1:f02e081afe42
Who changed what in which revision?
User | Revision | Line number | New 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 | 0:6b4ba48753b9 | 34 | char *ConfigFile::getValue(char *key) { |
shintamainjp | 0:6b4ba48753b9 | 35 | /* |
shintamainjp | 0:6b4ba48753b9 | 36 | * Null check. |
shintamainjp | 0:6b4ba48753b9 | 37 | */ |
shintamainjp | 0:6b4ba48753b9 | 38 | if (key == NULL) { |
shintamainjp | 0:6b4ba48753b9 | 39 | return NULL; |
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 | 0:6b4ba48753b9 | 47 | return NULL; |
shintamainjp | 0:6b4ba48753b9 | 48 | } |
shintamainjp | 0:6b4ba48753b9 | 49 | |
shintamainjp | 0:6b4ba48753b9 | 50 | /* |
shintamainjp | 0:6b4ba48753b9 | 51 | * Return the value. |
shintamainjp | 0:6b4ba48753b9 | 52 | */ |
shintamainjp | 0:6b4ba48753b9 | 53 | return p->value; |
shintamainjp | 0:6b4ba48753b9 | 54 | } |
shintamainjp | 0:6b4ba48753b9 | 55 | |
shintamainjp | 0:6b4ba48753b9 | 56 | bool ConfigFile::setValue(char *key, char *value) { |
shintamainjp | 0:6b4ba48753b9 | 57 | /* |
shintamainjp | 0:6b4ba48753b9 | 58 | * Null check. |
shintamainjp | 0:6b4ba48753b9 | 59 | */ |
shintamainjp | 0:6b4ba48753b9 | 60 | if ((key == NULL) || (value == NULL)) { |
shintamainjp | 0:6b4ba48753b9 | 61 | return false; |
shintamainjp | 0:6b4ba48753b9 | 62 | } |
shintamainjp | 0:6b4ba48753b9 | 63 | |
shintamainjp | 0:6b4ba48753b9 | 64 | /* |
shintamainjp | 0:6b4ba48753b9 | 65 | * Size check. |
shintamainjp | 0:6b4ba48753b9 | 66 | */ |
shintamainjp | 0:6b4ba48753b9 | 67 | if ((MAXLEN_KEY < strlen(key)) || (MAXLEN_VALUE < strlen(value))) { |
shintamainjp | 0:6b4ba48753b9 | 68 | return false; |
shintamainjp | 0:6b4ba48753b9 | 69 | } |
shintamainjp | 0:6b4ba48753b9 | 70 | |
shintamainjp | 0:6b4ba48753b9 | 71 | /* |
shintamainjp | 0:6b4ba48753b9 | 72 | * Search a config_t object from the key. |
shintamainjp | 0:6b4ba48753b9 | 73 | */ |
shintamainjp | 0:6b4ba48753b9 | 74 | config_t *p = search(key); |
shintamainjp | 0:6b4ba48753b9 | 75 | if (p == NULL) { |
shintamainjp | 0:6b4ba48753b9 | 76 | /* |
shintamainjp | 0:6b4ba48753b9 | 77 | * Allocation a memory for a new key. |
shintamainjp | 0:6b4ba48753b9 | 78 | */ |
shintamainjp | 0:6b4ba48753b9 | 79 | char *k = (char *)malloc(sizeof(char) * (strlen(key) + 1)); |
shintamainjp | 0:6b4ba48753b9 | 80 | if (k == NULL) { |
shintamainjp | 0:6b4ba48753b9 | 81 | return false; |
shintamainjp | 0:6b4ba48753b9 | 82 | } |
shintamainjp | 0:6b4ba48753b9 | 83 | strcpy(k, key); |
shintamainjp | 0:6b4ba48753b9 | 84 | |
shintamainjp | 0:6b4ba48753b9 | 85 | /* |
shintamainjp | 0:6b4ba48753b9 | 86 | * Allocation a memory for a new value. |
shintamainjp | 0:6b4ba48753b9 | 87 | */ |
shintamainjp | 0:6b4ba48753b9 | 88 | char *v = (char *)malloc(sizeof(char) * (strlen(value) + 1)); |
shintamainjp | 0:6b4ba48753b9 | 89 | if (v == NULL) { |
shintamainjp | 0:6b4ba48753b9 | 90 | free(k); |
shintamainjp | 0:6b4ba48753b9 | 91 | return false; |
shintamainjp | 0:6b4ba48753b9 | 92 | } |
shintamainjp | 0:6b4ba48753b9 | 93 | strcpy(v, value); |
shintamainjp | 0:6b4ba48753b9 | 94 | |
shintamainjp | 0:6b4ba48753b9 | 95 | /* |
shintamainjp | 0:6b4ba48753b9 | 96 | * Allocation a memory for a new configuration. |
shintamainjp | 0:6b4ba48753b9 | 97 | */ |
shintamainjp | 0:6b4ba48753b9 | 98 | config_t *cfg = (config_t *)malloc(sizeof(config_t) * 1); |
shintamainjp | 0:6b4ba48753b9 | 99 | if (cfg == NULL) { |
shintamainjp | 0:6b4ba48753b9 | 100 | free(k); |
shintamainjp | 0:6b4ba48753b9 | 101 | free(v); |
shintamainjp | 0:6b4ba48753b9 | 102 | return false; |
shintamainjp | 0:6b4ba48753b9 | 103 | } |
shintamainjp | 0:6b4ba48753b9 | 104 | cfg->key = k; |
shintamainjp | 0:6b4ba48753b9 | 105 | cfg->value = v; |
shintamainjp | 0:6b4ba48753b9 | 106 | |
shintamainjp | 0:6b4ba48753b9 | 107 | /* |
shintamainjp | 0:6b4ba48753b9 | 108 | * Add the new configuration. |
shintamainjp | 0:6b4ba48753b9 | 109 | */ |
shintamainjp | 0:6b4ba48753b9 | 110 | if (!add(cfg)) { |
shintamainjp | 0:6b4ba48753b9 | 111 | free(k); |
shintamainjp | 0:6b4ba48753b9 | 112 | free(v); |
shintamainjp | 0:6b4ba48753b9 | 113 | free(cfg); |
shintamainjp | 0:6b4ba48753b9 | 114 | return false; |
shintamainjp | 0:6b4ba48753b9 | 115 | } |
shintamainjp | 0:6b4ba48753b9 | 116 | |
shintamainjp | 0:6b4ba48753b9 | 117 | return true; |
shintamainjp | 0:6b4ba48753b9 | 118 | } else { |
shintamainjp | 0:6b4ba48753b9 | 119 | /* |
shintamainjp | 0:6b4ba48753b9 | 120 | * The value is same. |
shintamainjp | 0:6b4ba48753b9 | 121 | */ |
shintamainjp | 0:6b4ba48753b9 | 122 | if (strcmp(value, p->value) == 0) { |
shintamainjp | 0:6b4ba48753b9 | 123 | return true; |
shintamainjp | 0:6b4ba48753b9 | 124 | } |
shintamainjp | 0:6b4ba48753b9 | 125 | |
shintamainjp | 0:6b4ba48753b9 | 126 | /* |
shintamainjp | 0:6b4ba48753b9 | 127 | * Free a memory for the value. |
shintamainjp | 0:6b4ba48753b9 | 128 | */ |
shintamainjp | 0:6b4ba48753b9 | 129 | free(p->value); |
shintamainjp | 0:6b4ba48753b9 | 130 | p->value = NULL; |
shintamainjp | 0:6b4ba48753b9 | 131 | |
shintamainjp | 0:6b4ba48753b9 | 132 | /* |
shintamainjp | 0:6b4ba48753b9 | 133 | * Allocation memory for the new value. |
shintamainjp | 0:6b4ba48753b9 | 134 | */ |
shintamainjp | 0:6b4ba48753b9 | 135 | char *v = (char *)malloc(sizeof(char) * (strlen(value) + 1)); |
shintamainjp | 0:6b4ba48753b9 | 136 | if (v == NULL) { |
shintamainjp | 0:6b4ba48753b9 | 137 | return false; |
shintamainjp | 0:6b4ba48753b9 | 138 | } |
shintamainjp | 0:6b4ba48753b9 | 139 | |
shintamainjp | 0:6b4ba48753b9 | 140 | /* |
shintamainjp | 0:6b4ba48753b9 | 141 | * Store it. |
shintamainjp | 0:6b4ba48753b9 | 142 | */ |
shintamainjp | 0:6b4ba48753b9 | 143 | strcpy(v, value); |
shintamainjp | 0:6b4ba48753b9 | 144 | p->value = v; |
shintamainjp | 0:6b4ba48753b9 | 145 | |
shintamainjp | 0:6b4ba48753b9 | 146 | return true; |
shintamainjp | 0:6b4ba48753b9 | 147 | } |
shintamainjp | 0:6b4ba48753b9 | 148 | } |
shintamainjp | 0:6b4ba48753b9 | 149 | |
shintamainjp | 0:6b4ba48753b9 | 150 | bool ConfigFile::remove(char *key) { |
shintamainjp | 0:6b4ba48753b9 | 151 | if (key == NULL) { |
shintamainjp | 0:6b4ba48753b9 | 152 | return false; |
shintamainjp | 0:6b4ba48753b9 | 153 | } |
shintamainjp | 0:6b4ba48753b9 | 154 | for (int i = 0; i < MAXCONFIG; i++) { |
shintamainjp | 0:6b4ba48753b9 | 155 | config_t *cfg = configlist[i]; |
shintamainjp | 0:6b4ba48753b9 | 156 | if (cfg != NULL) { |
shintamainjp | 0:6b4ba48753b9 | 157 | if (strcmp(cfg->key, key) == 0) { |
shintamainjp | 0:6b4ba48753b9 | 158 | free(cfg->key); |
shintamainjp | 0:6b4ba48753b9 | 159 | free(cfg->value); |
shintamainjp | 0:6b4ba48753b9 | 160 | free(cfg); |
shintamainjp | 0:6b4ba48753b9 | 161 | configlist[i] = NULL; |
shintamainjp | 0:6b4ba48753b9 | 162 | return true; |
shintamainjp | 0:6b4ba48753b9 | 163 | } |
shintamainjp | 0:6b4ba48753b9 | 164 | } |
shintamainjp | 0:6b4ba48753b9 | 165 | } |
shintamainjp | 0:6b4ba48753b9 | 166 | return false; |
shintamainjp | 0:6b4ba48753b9 | 167 | } |
shintamainjp | 0:6b4ba48753b9 | 168 | |
shintamainjp | 0:6b4ba48753b9 | 169 | bool ConfigFile::removeAll(void) { |
shintamainjp | 0:6b4ba48753b9 | 170 | for (int i = 0; i < MAXCONFIG; i++) { |
shintamainjp | 0:6b4ba48753b9 | 171 | config_t *p = configlist[i]; |
shintamainjp | 0:6b4ba48753b9 | 172 | if (p != NULL) { |
shintamainjp | 0:6b4ba48753b9 | 173 | free(p->key); |
shintamainjp | 0:6b4ba48753b9 | 174 | free(p->value); |
shintamainjp | 0:6b4ba48753b9 | 175 | } |
shintamainjp | 0:6b4ba48753b9 | 176 | free(p); |
shintamainjp | 0:6b4ba48753b9 | 177 | configlist[i] = NULL; |
shintamainjp | 0:6b4ba48753b9 | 178 | } |
shintamainjp | 0:6b4ba48753b9 | 179 | return true; |
shintamainjp | 0:6b4ba48753b9 | 180 | } |
shintamainjp | 0:6b4ba48753b9 | 181 | |
shintamainjp | 0:6b4ba48753b9 | 182 | bool ConfigFile::read(char *file) { |
shintamainjp | 0:6b4ba48753b9 | 183 | /* |
shintamainjp | 0:6b4ba48753b9 | 184 | * Open the target file. |
shintamainjp | 0:6b4ba48753b9 | 185 | */ |
shintamainjp | 0:6b4ba48753b9 | 186 | FILE *fp = fopen(file, "r"); |
shintamainjp | 0:6b4ba48753b9 | 187 | if (fp == NULL) { |
shintamainjp | 0:6b4ba48753b9 | 188 | return false; |
shintamainjp | 0:6b4ba48753b9 | 189 | } |
shintamainjp | 0:6b4ba48753b9 | 190 | |
shintamainjp | 0:6b4ba48753b9 | 191 | /* |
shintamainjp | 0:6b4ba48753b9 | 192 | * Remove all configuration. |
shintamainjp | 0:6b4ba48753b9 | 193 | */ |
shintamainjp | 0:6b4ba48753b9 | 194 | removeAll(); |
shintamainjp | 0:6b4ba48753b9 | 195 | |
shintamainjp | 0:6b4ba48753b9 | 196 | /* |
shintamainjp | 0:6b4ba48753b9 | 197 | * Read from a file. |
shintamainjp | 0:6b4ba48753b9 | 198 | */ |
shintamainjp | 0:6b4ba48753b9 | 199 | char buf[MAXLEN_KEY + 8 + MAXLEN_VALUE]; |
shintamainjp | 0:6b4ba48753b9 | 200 | while (fgets(buf, sizeof(buf), fp) != NULL) { |
shintamainjp | 0:6b4ba48753b9 | 201 | /* |
shintamainjp | 0:6b4ba48753b9 | 202 | * Ignore a comment. |
shintamainjp | 0:6b4ba48753b9 | 203 | */ |
shintamainjp | 0:6b4ba48753b9 | 204 | if (buf[0] == '#') { |
shintamainjp | 0:6b4ba48753b9 | 205 | continue; |
shintamainjp | 0:6b4ba48753b9 | 206 | } |
shintamainjp | 0:6b4ba48753b9 | 207 | |
shintamainjp | 0:6b4ba48753b9 | 208 | /* |
shintamainjp | 0:6b4ba48753b9 | 209 | * Trim a return. |
shintamainjp | 0:6b4ba48753b9 | 210 | */ |
shintamainjp | 0:6b4ba48753b9 | 211 | const size_t len = strlen(buf); |
shintamainjp | 0:6b4ba48753b9 | 212 | for (int i = 0; i < len; i++) { |
shintamainjp | 0:6b4ba48753b9 | 213 | if ((buf[i] == '\r') || (buf[i] == '\n')) { |
shintamainjp | 0:6b4ba48753b9 | 214 | buf[i] = '\0'; |
shintamainjp | 0:6b4ba48753b9 | 215 | } |
shintamainjp | 0:6b4ba48753b9 | 216 | } |
shintamainjp | 0:6b4ba48753b9 | 217 | |
shintamainjp | 0:6b4ba48753b9 | 218 | /* |
shintamainjp | 0:6b4ba48753b9 | 219 | * Separate key and value. |
shintamainjp | 0:6b4ba48753b9 | 220 | */ |
shintamainjp | 0:6b4ba48753b9 | 221 | char k[MAXLEN_KEY]; |
shintamainjp | 0:6b4ba48753b9 | 222 | char v[MAXLEN_VALUE]; |
shintamainjp | 0:6b4ba48753b9 | 223 | char *sp = strchr(buf, SEPARATOR); |
shintamainjp | 0:6b4ba48753b9 | 224 | if (sp != NULL) { |
shintamainjp | 0:6b4ba48753b9 | 225 | strcpy(v, sp + 1); |
shintamainjp | 0:6b4ba48753b9 | 226 | *sp = '\0'; |
shintamainjp | 0:6b4ba48753b9 | 227 | strcpy(k, buf); |
shintamainjp | 0:6b4ba48753b9 | 228 | setValue(k, v); |
shintamainjp | 0:6b4ba48753b9 | 229 | } |
shintamainjp | 0:6b4ba48753b9 | 230 | } |
shintamainjp | 0:6b4ba48753b9 | 231 | fclose(fp); |
shintamainjp | 0:6b4ba48753b9 | 232 | return true; |
shintamainjp | 0:6b4ba48753b9 | 233 | } |
shintamainjp | 0:6b4ba48753b9 | 234 | |
shintamainjp | 0:6b4ba48753b9 | 235 | bool ConfigFile::write(char *file) { |
shintamainjp | 0:6b4ba48753b9 | 236 | FILE *fp = fopen(file, "w"); |
shintamainjp | 0:6b4ba48753b9 | 237 | if (fp == NULL) { |
shintamainjp | 0:6b4ba48753b9 | 238 | return false; |
shintamainjp | 0:6b4ba48753b9 | 239 | } |
shintamainjp | 0:6b4ba48753b9 | 240 | |
shintamainjp | 0:6b4ba48753b9 | 241 | static const char *NEWLINE_UNIX = "\n"; |
shintamainjp | 0:6b4ba48753b9 | 242 | static const char *NEWLINE_DOS = "\r\n"; |
shintamainjp | 0:6b4ba48753b9 | 243 | static const char *NEWLINE_MAC = "\r"; |
shintamainjp | 0:6b4ba48753b9 | 244 | for (int i = 0; i < MAXCONFIG; i++) { |
shintamainjp | 0:6b4ba48753b9 | 245 | config_t *cfg = configlist[i]; |
shintamainjp | 0:6b4ba48753b9 | 246 | if (cfg != NULL) { |
shintamainjp | 0:6b4ba48753b9 | 247 | fprintf(fp, "%s=%s%s", cfg->key, cfg->value, NEWLINE_UNIX); |
shintamainjp | 0:6b4ba48753b9 | 248 | } |
shintamainjp | 0:6b4ba48753b9 | 249 | } |
shintamainjp | 0:6b4ba48753b9 | 250 | fclose(fp); |
shintamainjp | 0:6b4ba48753b9 | 251 | return true; |
shintamainjp | 0:6b4ba48753b9 | 252 | } |
shintamainjp | 0:6b4ba48753b9 | 253 | |
shintamainjp | 0:6b4ba48753b9 | 254 | ConfigFile::config_t *ConfigFile::search(char *key) { |
shintamainjp | 0:6b4ba48753b9 | 255 | if (key == NULL) { |
shintamainjp | 0:6b4ba48753b9 | 256 | return NULL; |
shintamainjp | 0:6b4ba48753b9 | 257 | } |
shintamainjp | 0:6b4ba48753b9 | 258 | for (int i = 0; i < MAXCONFIG; i++) { |
shintamainjp | 0:6b4ba48753b9 | 259 | if (configlist[i] != NULL) { |
shintamainjp | 0:6b4ba48753b9 | 260 | if (strcmp(configlist[i]->key, key) == 0) { |
shintamainjp | 0:6b4ba48753b9 | 261 | return configlist[i]; |
shintamainjp | 0:6b4ba48753b9 | 262 | } |
shintamainjp | 0:6b4ba48753b9 | 263 | } |
shintamainjp | 0:6b4ba48753b9 | 264 | } |
shintamainjp | 0:6b4ba48753b9 | 265 | return NULL; |
shintamainjp | 0:6b4ba48753b9 | 266 | } |
shintamainjp | 0:6b4ba48753b9 | 267 | |
shintamainjp | 0:6b4ba48753b9 | 268 | bool ConfigFile::add(config_t *cfg) { |
shintamainjp | 0:6b4ba48753b9 | 269 | for (int i = 0; i < MAXCONFIG; i++) { |
shintamainjp | 0:6b4ba48753b9 | 270 | if (configlist[i] == NULL) { |
shintamainjp | 0:6b4ba48753b9 | 271 | configlist[i] = cfg; |
shintamainjp | 0:6b4ba48753b9 | 272 | return true; |
shintamainjp | 0:6b4ba48753b9 | 273 | } |
shintamainjp | 0:6b4ba48753b9 | 274 | } |
shintamainjp | 0:6b4ba48753b9 | 275 | return false; |
shintamainjp | 0:6b4ba48753b9 | 276 | } |