Own fork of MbedSmartRestMain
Dependencies: C027_Support C12832 LM75B MMA7660 MbedSmartRest mbed-rtos mbed
Fork of MbedSmartRestMain by
config/ConfigParser.cpp@105:c61f0d62b625, 2015-05-11 (annotated)
- Committer:
- xinlei
- Date:
- Mon May 11 18:10:44 2015 +0000
- Revision:
- 105:c61f0d62b625
- Parent:
- 102:ede6611e064e
SmartRestSSLSocket and various bug-fixes for rc3
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 | 102:ede6611e064e | 11 | dict.clear(); |
xinlei | 98:e369fc75c000 | 12 | for (const char*p = buf; *p;) { |
xinlei | 98:e369fc75c000 | 13 | p = lexConfig(p, tok); |
xinlei | 98:e369fc75c000 | 14 | (this->*ptrPF)(tok); |
xinlei | 98:e369fc75c000 | 15 | } |
xinlei | 98:e369fc75c000 | 16 | return parseOK; |
xinlei | 98:e369fc75c000 | 17 | } |
xinlei | 98:e369fc75c000 | 18 | |
xinlei | 98:e369fc75c000 | 19 | void ConfigParser::parseKey(Token& tok) |
xinlei | 98:e369fc75c000 | 20 | { |
xinlei | 98:e369fc75c000 | 21 | if (tok.type == Token::STRING) { |
xinlei | 98:e369fc75c000 | 22 | memset(key, 0, MAX_KEY_LEN); |
xinlei | 98:e369fc75c000 | 23 | size_t num = tok.len<MAX_VALUE_LEN ? tok.len : MAX_VALUE_LEN; |
xinlei | 98:e369fc75c000 | 24 | strncpy(key, tok.p, num); |
xinlei | 98:e369fc75c000 | 25 | ptrPF = &ConfigParser::parseAssignOp; |
xinlei | 98:e369fc75c000 | 26 | } else { |
xinlei | 98:e369fc75c000 | 27 | parseError(tok); |
xinlei | 98:e369fc75c000 | 28 | } |
xinlei | 98:e369fc75c000 | 29 | } |
xinlei | 98:e369fc75c000 | 30 | |
xinlei | 98:e369fc75c000 | 31 | void ConfigParser::parseAssignOp(Token& tok) |
xinlei | 98:e369fc75c000 | 32 | { |
xinlei | 98:e369fc75c000 | 33 | if (tok.type == Token::ASSIGN) { |
xinlei | 98:e369fc75c000 | 34 | ptrPF = &ConfigParser::parseValue; |
xinlei | 98:e369fc75c000 | 35 | } else { |
xinlei | 98:e369fc75c000 | 36 | parseError(tok); |
xinlei | 98:e369fc75c000 | 37 | } |
xinlei | 98:e369fc75c000 | 38 | } |
xinlei | 98:e369fc75c000 | 39 | |
xinlei | 98:e369fc75c000 | 40 | void ConfigParser::parseValue(Token& tok) |
xinlei | 98:e369fc75c000 | 41 | { |
xinlei | 98:e369fc75c000 | 42 | if (tok.type != Token::NONE) { |
xinlei | 105:c61f0d62b625 | 43 | memset(value, 0, MAX_VALUE_LEN); |
xinlei | 105:c61f0d62b625 | 44 | size_t num = tok.len<MAX_VALUE_LEN ? tok.len : MAX_VALUE_LEN; |
xinlei | 105:c61f0d62b625 | 45 | strncpy(value, tok.p, num); |
xinlei | 105:c61f0d62b625 | 46 | dict.set(key, value); |
xinlei | 105:c61f0d62b625 | 47 | ptrPF = &ConfigParser::parseSemiColon; |
xinlei | 98:e369fc75c000 | 48 | } else { |
xinlei | 98:e369fc75c000 | 49 | parseError(tok); |
xinlei | 98:e369fc75c000 | 50 | } |
xinlei | 98:e369fc75c000 | 51 | } |
xinlei | 98:e369fc75c000 | 52 | |
xinlei | 98:e369fc75c000 | 53 | void ConfigParser::parseSemiColon(Token& tok) |
xinlei | 98:e369fc75c000 | 54 | { |
xinlei | 98:e369fc75c000 | 55 | if (tok.type == Token::SEMICOLON) { |
xinlei | 98:e369fc75c000 | 56 | ptrPF = &ConfigParser::parseKey; |
xinlei | 98:e369fc75c000 | 57 | } else { |
xinlei | 98:e369fc75c000 | 58 | parseError(tok); |
xinlei | 98:e369fc75c000 | 59 | } |
xinlei | 98:e369fc75c000 | 60 | } |
xinlei | 98:e369fc75c000 | 61 | |
xinlei | 98:e369fc75c000 | 62 | void ConfigParser::parseError(Token& tok) |
xinlei | 98:e369fc75c000 | 63 | { |
xinlei | 98:e369fc75c000 | 64 | parseOK = false; |
xinlei | 100:dbcd3bc51758 | 65 | aError("ConfParse: (%d) %.*s\n", tok.type, (int)tok.len, tok.p); |
xinlei | 98:e369fc75c000 | 66 | parseRecover(tok); |
xinlei | 98:e369fc75c000 | 67 | } |
xinlei | 98:e369fc75c000 | 68 | |
xinlei | 98:e369fc75c000 | 69 | void ConfigParser::parseRecover(Token& tok) |
xinlei | 98:e369fc75c000 | 70 | { |
xinlei | 98:e369fc75c000 | 71 | if (tok.type == Token::SEMICOLON) { |
xinlei | 98:e369fc75c000 | 72 | ptrPF = &ConfigParser::parseKey; |
xinlei | 98:e369fc75c000 | 73 | } else { |
xinlei | 98:e369fc75c000 | 74 | ptrPF = &ConfigParser::parseRecover; |
xinlei | 98:e369fc75c000 | 75 | } |
xinlei | 98:e369fc75c000 | 76 | } |