Committer:
shintamainjp
Date:
Thu Nov 04 14:33:43 2010 +0000
Revision:
0:e9a647f18c71
Initial version.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
shintamainjp 0:e9a647f18c71 1 /**
shintamainjp 0:e9a647f18c71 2 * =============================================================================
shintamainjp 0:e9a647f18c71 3 * Application configuration for 'Expansion Board One' example no.2
shintamainjp 0:e9a647f18c71 4 * http://mbed.org/users/shintamainjp/notebook/starboard_expbrd-one_ex2_en/
shintamainjp 0:e9a647f18c71 5 * =============================================================================
shintamainjp 0:e9a647f18c71 6 * Copyright (c) 2010 Shinichiro Nakamura (CuBeatSystems)
shintamainjp 0:e9a647f18c71 7 *
shintamainjp 0:e9a647f18c71 8 * Permission is hereby granted, free of charge, to any person obtaining a copy
shintamainjp 0:e9a647f18c71 9 * of this software and associated documentation files (the "Software"), to deal
shintamainjp 0:e9a647f18c71 10 * in the Software without restriction, including without limitation the rights
shintamainjp 0:e9a647f18c71 11 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
shintamainjp 0:e9a647f18c71 12 * copies of the Software, and to permit persons to whom the Software is
shintamainjp 0:e9a647f18c71 13 * furnished to do so, subject to the following conditions:
shintamainjp 0:e9a647f18c71 14 *
shintamainjp 0:e9a647f18c71 15 * The above copyright notice and this permission notice shall be included in
shintamainjp 0:e9a647f18c71 16 * all copies or substantial portions of the Software.
shintamainjp 0:e9a647f18c71 17 *
shintamainjp 0:e9a647f18c71 18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
shintamainjp 0:e9a647f18c71 19 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
shintamainjp 0:e9a647f18c71 20 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
shintamainjp 0:e9a647f18c71 21 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
shintamainjp 0:e9a647f18c71 22 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
shintamainjp 0:e9a647f18c71 23 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
shintamainjp 0:e9a647f18c71 24 * THE SOFTWARE.
shintamainjp 0:e9a647f18c71 25 * =============================================================================
shintamainjp 0:e9a647f18c71 26 */
shintamainjp 0:e9a647f18c71 27
shintamainjp 0:e9a647f18c71 28 #include "appconf.h"
shintamainjp 0:e9a647f18c71 29 #include "ConfigFile.h"
shintamainjp 0:e9a647f18c71 30
shintamainjp 0:e9a647f18c71 31 #define KEY_PACHUBE_APIKEY "PACHUBE_APIKEY"
shintamainjp 0:e9a647f18c71 32 #define KEY_PACHUBE_FEEDID "PACHUBE_FEEDID"
shintamainjp 0:e9a647f18c71 33 #define KEY_PACHUBE_INTERVAL "PACHUBE_INTERVAL"
shintamainjp 0:e9a647f18c71 34
shintamainjp 0:e9a647f18c71 35 /**
shintamainjp 0:e9a647f18c71 36 * Initialize configuration.
shintamainjp 0:e9a647f18c71 37 *
shintamainjp 0:e9a647f18c71 38 * @param p A pointer to a application config.
shintamainjp 0:e9a647f18c71 39 */
shintamainjp 0:e9a647f18c71 40 void appconf_init(appconf_t *p) {
shintamainjp 0:e9a647f18c71 41 memset(p->apikey, 0, sizeof(p->apikey));
shintamainjp 0:e9a647f18c71 42 memset(p->feedid, 0, sizeof(p->feedid));
shintamainjp 0:e9a647f18c71 43 p->interval = 0;
shintamainjp 0:e9a647f18c71 44 }
shintamainjp 0:e9a647f18c71 45
shintamainjp 0:e9a647f18c71 46 /**
shintamainjp 0:e9a647f18c71 47 * Read configuration.
shintamainjp 0:e9a647f18c71 48 *
shintamainjp 0:e9a647f18c71 49 * @param filename Filename.
shintamainjp 0:e9a647f18c71 50 * @param p A pointer to a application config.
shintamainjp 0:e9a647f18c71 51 * @return Return zero if it succeed.
shintamainjp 0:e9a647f18c71 52 */
shintamainjp 0:e9a647f18c71 53 int appconf_read(char *filename, appconf_t *p) {
shintamainjp 0:e9a647f18c71 54 ConfigFile cf;
shintamainjp 0:e9a647f18c71 55 if (!cf.read(filename)) {
shintamainjp 0:e9a647f18c71 56 return -1;
shintamainjp 0:e9a647f18c71 57 }
shintamainjp 0:e9a647f18c71 58 if (!cf.getValue(KEY_PACHUBE_APIKEY, p->apikey, sizeof(p->apikey))) {
shintamainjp 0:e9a647f18c71 59 return -2;
shintamainjp 0:e9a647f18c71 60 }
shintamainjp 0:e9a647f18c71 61 if (!cf.getValue(KEY_PACHUBE_FEEDID, p->feedid, sizeof(p->feedid))) {
shintamainjp 0:e9a647f18c71 62 return -3;
shintamainjp 0:e9a647f18c71 63 }
shintamainjp 0:e9a647f18c71 64 char buf[64];
shintamainjp 0:e9a647f18c71 65 if (!cf.getValue(KEY_PACHUBE_INTERVAL, buf, sizeof(buf))) {
shintamainjp 0:e9a647f18c71 66 return -4;
shintamainjp 0:e9a647f18c71 67 } else {
shintamainjp 0:e9a647f18c71 68 if (sscanf(buf, "%d", &(p->interval)) != 1) {
shintamainjp 0:e9a647f18c71 69 return -5;
shintamainjp 0:e9a647f18c71 70 }
shintamainjp 0:e9a647f18c71 71 }
shintamainjp 0:e9a647f18c71 72 return 0;
shintamainjp 0:e9a647f18c71 73 }
shintamainjp 0:e9a647f18c71 74
shintamainjp 0:e9a647f18c71 75 /**
shintamainjp 0:e9a647f18c71 76 * Write configuration.
shintamainjp 0:e9a647f18c71 77 *
shintamainjp 0:e9a647f18c71 78 * @param filename Filename.
shintamainjp 0:e9a647f18c71 79 * @param p A pointer to a application config.
shintamainjp 0:e9a647f18c71 80 * @return Return zero if it succeed.
shintamainjp 0:e9a647f18c71 81 */
shintamainjp 0:e9a647f18c71 82 int appconf_write(char *filename, appconf_t *p) {
shintamainjp 0:e9a647f18c71 83 ConfigFile cf;
shintamainjp 0:e9a647f18c71 84 if (!cf.setValue(KEY_PACHUBE_APIKEY, p->apikey)) {
shintamainjp 0:e9a647f18c71 85 return -1;
shintamainjp 0:e9a647f18c71 86 }
shintamainjp 0:e9a647f18c71 87 if (!cf.setValue(KEY_PACHUBE_FEEDID, p->feedid)) {
shintamainjp 0:e9a647f18c71 88 return -2;
shintamainjp 0:e9a647f18c71 89 }
shintamainjp 0:e9a647f18c71 90 char buf[64];
shintamainjp 0:e9a647f18c71 91 snprintf(buf, sizeof(buf) - 1, "%d", p->interval);
shintamainjp 0:e9a647f18c71 92 if (!cf.setValue(KEY_PACHUBE_INTERVAL, buf)) {
shintamainjp 0:e9a647f18c71 93 return -3;
shintamainjp 0:e9a647f18c71 94 }
shintamainjp 0:e9a647f18c71 95 if (!cf.write(filename)) {
shintamainjp 0:e9a647f18c71 96 return -4;
shintamainjp 0:e9a647f18c71 97 }
shintamainjp 0:e9a647f18c71 98 return 0;
shintamainjp 0:e9a647f18c71 99 }