Official reference client implementation for Cumulocity SmartREST on u-blox C027.
Dependencies: C027_Support C12832 LM75B MMA7660 MbedSmartRest mbed-rtos mbed
Fork of MbedSmartRestMain by
util/lex.cpp
- Committer:
- xinlei
- Date:
- 2015-04-20
- Revision:
- 94:61d44636f020
- Child:
- 99:e369fc75c000
File content as of revision 94:61d44636f020:
#include <ctype.h> #include <string.h> #include "lex.h" const char* skipHTTPHeader(const char* p) { const char* ptr = p; if (p) ptr = strstr(p, "\r\n\r\n"); if (ptr) { return ptr+4; } else { return NULL; } } const char* lex(const char *p, Token& t) { if (p == NULL) { t.type = Token::NONE; t.p = NULL; t.len = 0; return NULL; } t.type = Token::NONE; size_t i = 0; while (p[i] && isspace(p[i])) ++i; bool opening = false; if (p[i] == '"') { opening = true; ++i; } size_t j = i; size_t dots = 0; bool hasChar = false; bool escaping = false; bool hasDigit = false; for (;p[j]; ++j) { if (p[j] == '"') { if (!opening) { // inline quote t.type = Token::ERROR; } else if (!escaping) { // escaping quote escaping = true; } else { hasChar = true; escaping = false; } } else { if (escaping && opening) { break; } else if (p[j] == '.') { hasChar = true; dots++; } else if (p[j] == ',' || iscntrl(p[j])) { if (opening) hasChar = true; else break; } else if (isdigit(p[j])) { hasDigit = true; } else { hasChar = true; } } } t.p = p+i; size_t k = j; while (k > i && (p[k]==',' || isspace(p[k]))) --k; t.len = k-i+1; if (escaping && opening) { opening = false; t.len -= 1; } if (t.type != Token::ERROR) { if (opening) { t.type = Token::ERROR; } else if (hasChar) { t.type = Token::STRING; } else if (hasDigit) { switch (dots) { case 0: t.type = Token::INT; break; case 1: t.type = Token::FLOAT; break; default: t.type = Token::STRING; break; } } else { t.type = Token::NONE; } } while (p[j]) { if (isspace(p[j])) { ++j; } else if (p[j]==',') { ++j; break; } else { break; } } return p+j; } size_t strncpyEscape(char* dest, const char *source, size_t num) { if (source == NULL) return 0; size_t i = 0, j = 0; for (bool escaping = false; i < num; ++i) { if (source[i] != '"') { dest[j++] = source[i]; } else { if (escaping) { dest[j++] = source[i]; escaping = false; } else { escaping = true; } } } return j; }