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