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