Own fork of MbedSmartRestMain

Dependencies:   C027_Support C12832 LM75B MMA7660 MbedSmartRest mbed-rtos mbed

Fork of MbedSmartRestMain by Cumulocity Official

config/ConfigParser.cpp

Committer:
xinlei
Date:
2015-05-07
Revision:
98:e369fc75c000
Child:
100:dbcd3bc51758

File content as of revision 98:e369fc75c000:

#include <string.h>
#include "ConfigParser.h"
#include "logging.h"

bool ConfigParser::parse(const char *buf)
{
        if (buf == NULL) return false;
        Token tok;
        ptrPF = &ConfigParser::parseKey;
        parseOK = true;
        for (const char*p = buf; *p;) {
                p = lexConfig(p, tok);
                (this->*ptrPF)(tok);
        }
        return parseOK;
}

void ConfigParser::parseKey(Token& tok)
{
        if (tok.type == Token::STRING) {
                memset(key, 0, MAX_KEY_LEN);
                size_t num = tok.len<MAX_VALUE_LEN ? tok.len : MAX_VALUE_LEN;
                strncpy(key, tok.p, num);
                ptrPF = &ConfigParser::parseAssignOp;
        } else {
                parseError(tok);
        }
}

void ConfigParser::parseAssignOp(Token& tok)
{
        if (tok.type == Token::ASSIGN) {
                ptrPF = &ConfigParser::parseValue;
        } else {
                parseError(tok);
        }
}

void ConfigParser::parseValue(Token& tok)
{
        if (tok.type != Token::NONE) {
                if (strncmp(key, "interval", MAX_KEY_LEN) == 0 &&
                    tok.type != Token::INT) {
                        parseError(tok);
                } else {
                        memset(value, 0, MAX_VALUE_LEN);
                        size_t num = tok.len<MAX_VALUE_LEN ? tok.len : MAX_VALUE_LEN;
                        strncpy(value, tok.p, num);
                        dict.set(key, value);
                        ptrPF = &ConfigParser::parseSemiColon;
                }
        } else {
                parseError(tok);
        }
}

void ConfigParser::parseSemiColon(Token& tok)
{
        if (tok.type == Token::SEMICOLON) {
                ptrPF = &ConfigParser::parseKey;
        } else {
                parseError(tok);
        }
}

void ConfigParser::parseError(Token& tok)
{
        parseOK = false;
        aError("(%d) %.*s\n", tok.type, (int)tok.len, tok.p);
        parseRecover(tok);
}

void ConfigParser::parseRecover(Token& tok)
{
        if (tok.type == Token::SEMICOLON) {
                ptrPF = &ConfigParser::parseKey;
        } else {
                ptrPF = &ConfigParser::parseRecover;
        }
}