mbed.org implementation of the abstract SmartREST library for the Cumulocity Platform SmartREST protocol.

Dependents:   MbedSmartRestMain MbedSmartRestMain

Committer:
Cumulocity
Date:
Thu Jul 03 20:38:04 2014 +0200
Revision:
0:099f76422485
Child:
4:059b8b90e0d9
Updated from revision 0413a0179eb6

Who changed what in which revision?

UserRevisionLine numberNew contents of line
Cumulocity 0:099f76422485 1 /*
Cumulocity 0:099f76422485 2 * Parser.cpp
Cumulocity 0:099f76422485 3 *
Cumulocity 0:099f76422485 4 * Created on: Nov 1, 2013
Cumulocity 0:099f76422485 5 * * Authors: Vincent Wochnik <v.wochnik@gmail.com>
Cumulocity 0:099f76422485 6 *
Cumulocity 0:099f76422485 7 * Copyright (c) 2013 Cumulocity GmbH
Cumulocity 0:099f76422485 8 *
Cumulocity 0:099f76422485 9 * Permission is hereby granted, free of charge, to any person obtaining
Cumulocity 0:099f76422485 10 * a copy of this software and associated documentation files (the
Cumulocity 0:099f76422485 11 * "Software"), to deal in the Software without restriction, including
Cumulocity 0:099f76422485 12 * without limitation the rights to use, copy, modify, merge, publish,
Cumulocity 0:099f76422485 13 * distribute, sublicense, and/or sell copies of the Software, and to
Cumulocity 0:099f76422485 14 * permit persons to whom the Software is furnished to do so, subject to
Cumulocity 0:099f76422485 15 * the following conditions:
Cumulocity 0:099f76422485 16 *
Cumulocity 0:099f76422485 17 * The above copyright notice and this permission notice shall be
Cumulocity 0:099f76422485 18 * included in all copies or substantial portions of the Software.
Cumulocity 0:099f76422485 19 *
Cumulocity 0:099f76422485 20 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
Cumulocity 0:099f76422485 21 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
Cumulocity 0:099f76422485 22 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
Cumulocity 0:099f76422485 23 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
Cumulocity 0:099f76422485 24 * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
Cumulocity 0:099f76422485 25 * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
Cumulocity 0:099f76422485 26 * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
Cumulocity 0:099f76422485 27 */
Cumulocity 0:099f76422485 28
Cumulocity 0:099f76422485 29 #include "Parser.h"
Cumulocity 0:099f76422485 30
Cumulocity 0:099f76422485 31 #define STATE_BLANK 0
Cumulocity 0:099f76422485 32 #define STATE_STRUCTURE 1
Cumulocity 0:099f76422485 33 #define STATE_VALUE 2
Cumulocity 0:099f76422485 34 #define STATE_INQUOTES 3
Cumulocity 0:099f76422485 35 #define STATE_AFTERQUOTE 4
Cumulocity 0:099f76422485 36 #define STATE_COMPLETE 5
Cumulocity 0:099f76422485 37 #define STATE_ERROR 6
Cumulocity 0:099f76422485 38
Cumulocity 0:099f76422485 39 Parser::Parser()
Cumulocity 0:099f76422485 40 {
Cumulocity 0:099f76422485 41 reset();
Cumulocity 0:099f76422485 42 }
Cumulocity 0:099f76422485 43
Cumulocity 0:099f76422485 44 uint8_t Parser::readFrom(AbstractDataSource& source, ParsedRecord& record)
Cumulocity 0:099f76422485 45 {
Cumulocity 0:099f76422485 46 uint8_t status; char read;
Cumulocity 0:099f76422485 47
Cumulocity 0:099f76422485 48 reset();
Cumulocity 0:099f76422485 49 record.set(NULL, 0);
Cumulocity 0:099f76422485 50
Cumulocity 0:099f76422485 51 while ((_state < STATE_COMPLETE) &&
Cumulocity 0:099f76422485 52 (((read = source.read()) > 0 ) ||
Cumulocity 0:099f76422485 53 ((status = source.status()) == DS_STATUS_OK)))
Cumulocity 0:099f76422485 54 parse(read);
Cumulocity 0:099f76422485 55
Cumulocity 0:099f76422485 56 if (_state == STATE_COMPLETE)
Cumulocity 0:099f76422485 57 record.set(_buffer, _count);
Cumulocity 0:099f76422485 58
Cumulocity 0:099f76422485 59 if (_state == STATE_COMPLETE)
Cumulocity 0:099f76422485 60 return PARSER_SUCCESS;
Cumulocity 0:099f76422485 61 else if (_state == STATE_BLANK)
Cumulocity 0:099f76422485 62 if (status == DS_STATUS_CLOSED)
Cumulocity 0:099f76422485 63 return PARSER_END_OF_RESPONSE;
Cumulocity 0:099f76422485 64 else
Cumulocity 0:099f76422485 65 return PARSER_TIMEOUT_ERROR;
Cumulocity 0:099f76422485 66 else
Cumulocity 0:099f76422485 67 if (status == DS_STATUS_TIMEOUT)
Cumulocity 0:099f76422485 68 return PARSER_TIMEOUT_ERROR;
Cumulocity 0:099f76422485 69 else
Cumulocity 0:099f76422485 70 return PARSER_PARSE_ERROR;
Cumulocity 0:099f76422485 71 }
Cumulocity 0:099f76422485 72
Cumulocity 0:099f76422485 73 void Parser::parse(char c)
Cumulocity 0:099f76422485 74 {
Cumulocity 0:099f76422485 75 if (_ptr-_buffer >= SMARTREST_PARSER_BUFFER_SIZE) {
Cumulocity 0:099f76422485 76 _state = STATE_ERROR;
Cumulocity 0:099f76422485 77 return;
Cumulocity 0:099f76422485 78 }
Cumulocity 0:099f76422485 79
Cumulocity 0:099f76422485 80 switch (_state) {
Cumulocity 0:099f76422485 81 case STATE_BLANK:
Cumulocity 0:099f76422485 82 _state = STATE_STRUCTURE;
Cumulocity 0:099f76422485 83 case STATE_STRUCTURE:
Cumulocity 0:099f76422485 84 switch (c) {
Cumulocity 0:099f76422485 85 case ' ':
Cumulocity 0:099f76422485 86 case '\t':
Cumulocity 0:099f76422485 87 case '\r':
Cumulocity 0:099f76422485 88 break;
Cumulocity 0:099f76422485 89 case '"':
Cumulocity 0:099f76422485 90 _state = STATE_INQUOTES;
Cumulocity 0:099f76422485 91 break;
Cumulocity 0:099f76422485 92 case ',':
Cumulocity 0:099f76422485 93 close();
Cumulocity 0:099f76422485 94 break;
Cumulocity 0:099f76422485 95 case '\0':
Cumulocity 0:099f76422485 96 case '\n':
Cumulocity 0:099f76422485 97 if ((_count == 0) && (_length == 0)) {
Cumulocity 0:099f76422485 98 _state = STATE_BLANK;
Cumulocity 0:099f76422485 99 } else {
Cumulocity 0:099f76422485 100 close();
Cumulocity 0:099f76422485 101 _state = STATE_COMPLETE;
Cumulocity 0:099f76422485 102 }
Cumulocity 0:099f76422485 103 break;
Cumulocity 0:099f76422485 104 default:
Cumulocity 0:099f76422485 105 if (_length > 0) {
Cumulocity 0:099f76422485 106 _state = STATE_ERROR;
Cumulocity 0:099f76422485 107 } else {
Cumulocity 0:099f76422485 108 _state = STATE_VALUE;
Cumulocity 0:099f76422485 109 parse(c);
Cumulocity 0:099f76422485 110 }
Cumulocity 0:099f76422485 111 break;
Cumulocity 0:099f76422485 112 }
Cumulocity 0:099f76422485 113 break;
Cumulocity 0:099f76422485 114 case STATE_VALUE:
Cumulocity 0:099f76422485 115 switch (c) {
Cumulocity 0:099f76422485 116 case ',':
Cumulocity 0:099f76422485 117 case '\n':
Cumulocity 0:099f76422485 118 _state = STATE_STRUCTURE;
Cumulocity 0:099f76422485 119 parse(c);
Cumulocity 0:099f76422485 120 break;
Cumulocity 0:099f76422485 121 case '"':
Cumulocity 0:099f76422485 122 _state = STATE_ERROR;
Cumulocity 0:099f76422485 123 break;
Cumulocity 0:099f76422485 124 default:
Cumulocity 0:099f76422485 125 if ((c == ' ') || (c == '\t') || (c == '\r'))
Cumulocity 0:099f76422485 126 _trailing++;
Cumulocity 0:099f76422485 127 else
Cumulocity 0:099f76422485 128 _trailing = 0;
Cumulocity 0:099f76422485 129 append(c);
Cumulocity 0:099f76422485 130 break;
Cumulocity 0:099f76422485 131 }
Cumulocity 0:099f76422485 132 break;
Cumulocity 0:099f76422485 133 case STATE_INQUOTES:
Cumulocity 0:099f76422485 134 switch (c) {
Cumulocity 0:099f76422485 135 case '"':
Cumulocity 0:099f76422485 136 _state = STATE_AFTERQUOTE;
Cumulocity 0:099f76422485 137 break;
Cumulocity 0:099f76422485 138 default:
Cumulocity 0:099f76422485 139 append(c);
Cumulocity 0:099f76422485 140 break;
Cumulocity 0:099f76422485 141 }
Cumulocity 0:099f76422485 142 break;
Cumulocity 0:099f76422485 143 case STATE_AFTERQUOTE:
Cumulocity 0:099f76422485 144 switch (c) {
Cumulocity 0:099f76422485 145 case '"':
Cumulocity 0:099f76422485 146 append(c);
Cumulocity 0:099f76422485 147 _state = STATE_INQUOTES;
Cumulocity 0:099f76422485 148 break;
Cumulocity 0:099f76422485 149 default:
Cumulocity 0:099f76422485 150 _state = STATE_STRUCTURE;
Cumulocity 0:099f76422485 151 parse(c);
Cumulocity 0:099f76422485 152 break;
Cumulocity 0:099f76422485 153 }
Cumulocity 0:099f76422485 154 break;
Cumulocity 0:099f76422485 155 }
Cumulocity 0:099f76422485 156 }
Cumulocity 0:099f76422485 157
Cumulocity 0:099f76422485 158 void Parser::append(char c)
Cumulocity 0:099f76422485 159 {
Cumulocity 0:099f76422485 160 *_ptr++ = c;
Cumulocity 0:099f76422485 161 _length++;
Cumulocity 0:099f76422485 162 }
Cumulocity 0:099f76422485 163
Cumulocity 0:099f76422485 164 void Parser::close()
Cumulocity 0:099f76422485 165 {
Cumulocity 0:099f76422485 166 _ptr -= _trailing;
Cumulocity 0:099f76422485 167 *_ptr++ = 0;
Cumulocity 0:099f76422485 168 _trailing = 0;
Cumulocity 0:099f76422485 169 _length = 0;
Cumulocity 0:099f76422485 170 _count++;
Cumulocity 0:099f76422485 171 }
Cumulocity 0:099f76422485 172
Cumulocity 0:099f76422485 173 void Parser::reset()
Cumulocity 0:099f76422485 174 {
Cumulocity 0:099f76422485 175 _state = STATE_BLANK;
Cumulocity 0:099f76422485 176 _ptr = _buffer;
Cumulocity 0:099f76422485 177 _count = 0;
Cumulocity 0:099f76422485 178 _trailing = 0;
Cumulocity 0:099f76422485 179 _length = 0;
Cumulocity 0:099f76422485 180 }