![](/media/cache/profiles/f095cedd23b99f1696fc8caecbcf257e.jpg.50x50_q85.jpg)
mbed Weather Platform firmware http://mbed.org/users/okini3939/notebook/mbed-weather-platform-firmware/
Dependencies: EthernetNetIf SDHCFileSystem I2CLEDDisp Agentbed NTPClient_NetServices mbed BMP085 HTTPClient ConfigFile I2CLCD
Diff: ConfigFile.cpp
- Revision:
- 0:4265d973a98f
- Child:
- 1:86d4b7431fbe
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/ConfigFile.cpp Fri Dec 10 17:15:15 2010 +0000 @@ -0,0 +1,166 @@ +#include "ConfigFile.h" + +#define BUF_SIZE 100 + +#define strnicmp strncasecmp + +ConfigFile::ConfigFile () { + ipaddr = IpAddr(0, 0, 0, 0); + netmask = IpAddr(255, 255, 255, 0); + gateway = IpAddr(0, 0, 0, 0); + nameserver = IpAddr(0, 0, 0, 0); + interval = 60; + ntpserver[0] = 0; + filetype = 0; + actionscount = 0; + pachube_apikey[0] = 0; + pachube_feedid[0] = 0; + twitter_user[0] = 0; + twitter_pwd[0] = 0; +} + +void ConfigFile::load (char *filename) { + char buf[BUF_SIZE]; + int ip0, ip1, ip2, ip3; + FILE *fp; + + fp = fopen(filename, "r"); + if (fp) { + for (;;) { + if (feof(fp)) break; + + if (! fgets(buf, BUF_SIZE, fp)) break; + + if (strnicmp(buf, "IPADDRESS=DHCP", 14) == 0) { + ipaddr = IpAddr(255, 255, 255, 255); + } else + if (strnicmp(buf, "IPADDRESS=", 10) == 0) { + sscanf(&buf[10], "%d.%d.%d.%d", &ip0, &ip1, &ip2, &ip3); + ipaddr = IpAddr(ip0, ip1, ip2, ip3); + } else + if (strnicmp(buf, "NETMASK=", 8) == 0) { + sscanf(&buf[10], "%d.%d.%d.%d", &ip0, &ip1, &ip2, &ip3); + netmask = IpAddr(ip0, ip1, ip2, ip3); + } else + if (strnicmp(buf, "GATEWAY=", 8) == 0) { + sscanf(&buf[10], "%d.%d.%d.%d", &ip0, &ip1, &ip2, &ip3); + gateway = IpAddr(ip0, ip1, ip2, ip3); + } else + if (strnicmp(buf, "NAMESERVER=", 11) == 0) { + sscanf(&buf[10], "%d.%d.%d.%d", &ip0, &ip1, &ip2, &ip3); + nameserver = IpAddr(ip0, ip1, ip2, ip3); + } else + if (strnicmp(buf, "NTPSERVER=", 10) == 0) { + strncpy(ntpserver, chop(&buf[10]), sizeof(ntpserver)); + } else + if (strnicmp(buf, "INTERVAL=", 9) == 0) { + interval = atoi(&buf[9]); + } else + if (strnicmp(buf, "FILE=CF", 7) == 0) { + filetype = 1; + } else + if (strnicmp(buf, "FILE=USB", 8) == 0) { + filetype = 2; + } else + if (strnicmp(buf, "ACTION=", 7) == 0) { + add(&buf[7]); + } else + if (strnicmp(buf, "PACHUBE_APIKEY=", 15) == 0) { + strncpy(pachube_apikey, chop(&buf[15]), sizeof(pachube_apikey)); + } else + if (strnicmp(buf, "PACHUBE_FEEDID=", 15) == 0) { + strncpy(pachube_feedid, chop(&buf[15]), sizeof(pachube_feedid)); + } else + if (strnicmp(buf, "TWITTER_USER=", 13) == 0) { + strncpy(twitter_user, chop(&buf[13]), sizeof(twitter_user)); + } else + if (strnicmp(buf, "TWITTER_PWD=", 12) == 0) { + strncpy(twitter_pwd, chop(&buf[12]), sizeof(twitter_pwd)); + } + } + fclose(fp); + } +} + +void ConfigFile::add (char *buf) { + int i, len, count; + char c; + char *tmp = NULL; + + actions[actionscount].action = atoi(&buf[0]); + + count = 0; + len = strlen(buf); + for (i = 0; i < len; i ++) { + c = buf[i]; + if (c == ' ' || c == '\t' || c == '\n' || c == '\r') { + buf[i] = 0; + if (count) { addsub(&actions[actionscount].exps[count - 1], tmp); } + if (count >= CF_ACTION_EXPS || c == '\n') { break; } + tmp = &buf[i + 1]; + count ++; + } + } + + actions[actionscount].count = count; + actionscount ++; +} + +void ConfigFile::addsub (struct tExpression *exp, char *buf) { + + exp->key = buf[0]; + + switch (buf[1]) { + case '=': + if (buf[2] == '=') { + exp->expression = EXP_EQ; + exp->value = atof(&buf[3]); + } + break; + + case '!': + if (buf[2] == '=') { + exp->expression = EXP_NE; + exp->value = atof(&buf[3]); + } + break; + + case '<': + if (buf[2] == '=') { + exp->expression = EXP_LE; + exp->value = atof(&buf[3]); + } else { + exp->expression = EXP_LT; + exp->value = atof(&buf[2]); + } + break; + + case '>': + if (buf[2] == '=') { + exp->expression = EXP_GE; + exp->value = atof(&buf[3]); + } else { + exp->expression = EXP_GT; + exp->value = atof(&buf[2]); + } + break; + + default: + exp->expression = EXP_NULL; + break; + } +} + +char* ConfigFile::chop (char *s) { + int i, len; + + len = strlen(s); + for (i = len - 1; i >= 0; i --) { + if (s[i] == '\n' || s[i] == '\r') { + s[i] = 0; + } else { + break; + } + } + return s; +}