Own fork of MbedSmartRestMain
Dependencies: C027_Support C12832 LM75B MMA7660 MbedSmartRest mbed-rtos mbed
Fork of MbedSmartRestMain by
config/ConfigParser.cpp@98:e369fc75c000, 2015-05-07 (annotated)
- Committer:
- xinlei
- Date:
- Thu May 07 09:57:55 2015 +0000
- Revision:
- 98:e369fc75c000
- Child:
- 100:dbcd3bc51758
prepare for v2.1rc3.
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
xinlei | 98:e369fc75c000 | 1 | #include <string.h> |
xinlei | 98:e369fc75c000 | 2 | #include "ConfigParser.h" |
xinlei | 98:e369fc75c000 | 3 | #include "logging.h" |
xinlei | 98:e369fc75c000 | 4 | |
xinlei | 98:e369fc75c000 | 5 | bool ConfigParser::parse(const char *buf) |
xinlei | 98:e369fc75c000 | 6 | { |
xinlei | 98:e369fc75c000 | 7 | if (buf == NULL) return false; |
xinlei | 98:e369fc75c000 | 8 | Token tok; |
xinlei | 98:e369fc75c000 | 9 | ptrPF = &ConfigParser::parseKey; |
xinlei | 98:e369fc75c000 | 10 | parseOK = true; |
xinlei | 98:e369fc75c000 | 11 | for (const char*p = buf; *p;) { |
xinlei | 98:e369fc75c000 | 12 | p = lexConfig(p, tok); |
xinlei | 98:e369fc75c000 | 13 | (this->*ptrPF)(tok); |
xinlei | 98:e369fc75c000 | 14 | } |
xinlei | 98:e369fc75c000 | 15 | return parseOK; |
xinlei | 98:e369fc75c000 | 16 | } |
xinlei | 98:e369fc75c000 | 17 | |
xinlei | 98:e369fc75c000 | 18 | void ConfigParser::parseKey(Token& tok) |
xinlei | 98:e369fc75c000 | 19 | { |
xinlei | 98:e369fc75c000 | 20 | if (tok.type == Token::STRING) { |
xinlei | 98:e369fc75c000 | 21 | memset(key, 0, MAX_KEY_LEN); |
xinlei | 98:e369fc75c000 | 22 | size_t num = tok.len<MAX_VALUE_LEN ? tok.len : MAX_VALUE_LEN; |
xinlei | 98:e369fc75c000 | 23 | strncpy(key, tok.p, num); |
xinlei | 98:e369fc75c000 | 24 | ptrPF = &ConfigParser::parseAssignOp; |
xinlei | 98:e369fc75c000 | 25 | } else { |
xinlei | 98:e369fc75c000 | 26 | parseError(tok); |
xinlei | 98:e369fc75c000 | 27 | } |
xinlei | 98:e369fc75c000 | 28 | } |
xinlei | 98:e369fc75c000 | 29 | |
xinlei | 98:e369fc75c000 | 30 | void ConfigParser::parseAssignOp(Token& tok) |
xinlei | 98:e369fc75c000 | 31 | { |
xinlei | 98:e369fc75c000 | 32 | if (tok.type == Token::ASSIGN) { |
xinlei | 98:e369fc75c000 | 33 | ptrPF = &ConfigParser::parseValue; |
xinlei | 98:e369fc75c000 | 34 | } else { |
xinlei | 98:e369fc75c000 | 35 | parseError(tok); |
xinlei | 98:e369fc75c000 | 36 | } |
xinlei | 98:e369fc75c000 | 37 | } |
xinlei | 98:e369fc75c000 | 38 | |
xinlei | 98:e369fc75c000 | 39 | void ConfigParser::parseValue(Token& tok) |
xinlei | 98:e369fc75c000 | 40 | { |
xinlei | 98:e369fc75c000 | 41 | if (tok.type != Token::NONE) { |
xinlei | 98:e369fc75c000 | 42 | if (strncmp(key, "interval", MAX_KEY_LEN) == 0 && |
xinlei | 98:e369fc75c000 | 43 | tok.type != Token::INT) { |
xinlei | 98:e369fc75c000 | 44 | parseError(tok); |
xinlei | 98:e369fc75c000 | 45 | } else { |
xinlei | 98:e369fc75c000 | 46 | memset(value, 0, MAX_VALUE_LEN); |
xinlei | 98:e369fc75c000 | 47 | size_t num = tok.len<MAX_VALUE_LEN ? tok.len : MAX_VALUE_LEN; |
xinlei | 98:e369fc75c000 | 48 | strncpy(value, tok.p, num); |
xinlei | 98:e369fc75c000 | 49 | dict.set(key, value); |
xinlei | 98:e369fc75c000 | 50 | ptrPF = &ConfigParser::parseSemiColon; |
xinlei | 98:e369fc75c000 | 51 | } |
xinlei | 98:e369fc75c000 | 52 | } else { |
xinlei | 98:e369fc75c000 | 53 | parseError(tok); |
xinlei | 98:e369fc75c000 | 54 | } |
xinlei | 98:e369fc75c000 | 55 | } |
xinlei | 98:e369fc75c000 | 56 | |
xinlei | 98:e369fc75c000 | 57 | void ConfigParser::parseSemiColon(Token& tok) |
xinlei | 98:e369fc75c000 | 58 | { |
xinlei | 98:e369fc75c000 | 59 | if (tok.type == Token::SEMICOLON) { |
xinlei | 98:e369fc75c000 | 60 | ptrPF = &ConfigParser::parseKey; |
xinlei | 98:e369fc75c000 | 61 | } else { |
xinlei | 98:e369fc75c000 | 62 | parseError(tok); |
xinlei | 98:e369fc75c000 | 63 | } |
xinlei | 98:e369fc75c000 | 64 | } |
xinlei | 98:e369fc75c000 | 65 | |
xinlei | 98:e369fc75c000 | 66 | void ConfigParser::parseError(Token& tok) |
xinlei | 98:e369fc75c000 | 67 | { |
xinlei | 98:e369fc75c000 | 68 | parseOK = false; |
xinlei | 98:e369fc75c000 | 69 | aError("(%d) %.*s\n", tok.type, (int)tok.len, tok.p); |
xinlei | 98:e369fc75c000 | 70 | parseRecover(tok); |
xinlei | 98:e369fc75c000 | 71 | } |
xinlei | 98:e369fc75c000 | 72 | |
xinlei | 98:e369fc75c000 | 73 | void ConfigParser::parseRecover(Token& tok) |
xinlei | 98:e369fc75c000 | 74 | { |
xinlei | 98:e369fc75c000 | 75 | if (tok.type == Token::SEMICOLON) { |
xinlei | 98:e369fc75c000 | 76 | ptrPF = &ConfigParser::parseKey; |
xinlei | 98:e369fc75c000 | 77 | } else { |
xinlei | 98:e369fc75c000 | 78 | ptrPF = &ConfigParser::parseRecover; |
xinlei | 98:e369fc75c000 | 79 | } |
xinlei | 98:e369fc75c000 | 80 | } |