Own fork of MbedSmartRest

Dependents:   MbedSmartRestMain MbedSmartRestMain

Fork of MbedSmartRest by Cumulocity Official

Committer:
Cumulocity
Date:
Sat Nov 15 11:21:01 2014 +0100
Revision:
11:e1bee9a77652
Parent:
6:cd7ba1ddb664
Child:
20:505d29d5bdfc
Updated from revision ce16ad78546a

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 11:e1bee9a77652 30 #include <assert.h>
Cumulocity 0:099f76422485 31
Cumulocity 0:099f76422485 32 #define STATE_BLANK 0
Cumulocity 0:099f76422485 33 #define STATE_STRUCTURE 1
Cumulocity 0:099f76422485 34 #define STATE_VALUE 2
Cumulocity 0:099f76422485 35 #define STATE_INQUOTES 3
Cumulocity 0:099f76422485 36 #define STATE_AFTERQUOTE 4
Cumulocity 0:099f76422485 37 #define STATE_COMPLETE 5
Cumulocity 0:099f76422485 38 #define STATE_ERROR 6
Cumulocity 0:099f76422485 39
Cumulocity 11:e1bee9a77652 40
Cumulocity 11:e1bee9a77652 41 /*-------------------------------------------------------------------------*/
Cumulocity 0:099f76422485 42 Parser::Parser()
Cumulocity 0:099f76422485 43 {
Cumulocity 11:e1bee9a77652 44 reset();
Cumulocity 0:099f76422485 45 }
Cumulocity 11:e1bee9a77652 46 /*-------------------------------------------------------------------------*/
Cumulocity 0:099f76422485 47 uint8_t Parser::readFrom(AbstractDataSource& source, ParsedRecord& record)
Cumulocity 0:099f76422485 48 {
Cumulocity 11:e1bee9a77652 49 int status = -1;
Cumulocity 11:e1bee9a77652 50 char read;
Cumulocity 0:099f76422485 51
Cumulocity 11:e1bee9a77652 52 reset();
Cumulocity 11:e1bee9a77652 53 record.clear();
Cumulocity 0:099f76422485 54
Cumulocity 11:e1bee9a77652 55 while ((_state < STATE_COMPLETE) &&
Cumulocity 11:e1bee9a77652 56 (((read = source.read()) > 0) ||
Cumulocity 11:e1bee9a77652 57 ((status = source.status()) == DS_STATUS_OK)))
Cumulocity 11:e1bee9a77652 58 parse(read);
Cumulocity 0:099f76422485 59
Cumulocity 11:e1bee9a77652 60 // successfully read record
Cumulocity 11:e1bee9a77652 61 if (_state == STATE_COMPLETE)
Cumulocity 11:e1bee9a77652 62 {
Cumulocity 11:e1bee9a77652 63 if (!record.set(_buffer, _count))
Cumulocity 11:e1bee9a77652 64 return PARSER_INTERNAL_ERROR;
Cumulocity 11:e1bee9a77652 65 return PARSER_SUCCESS;
Cumulocity 11:e1bee9a77652 66 }
Cumulocity 6:cd7ba1ddb664 67
Cumulocity 11:e1bee9a77652 68 if (_state == STATE_BLANK)
Cumulocity 11:e1bee9a77652 69 {
Cumulocity 11:e1bee9a77652 70 assert(status >= 0);
Cumulocity 11:e1bee9a77652 71 if (status == DS_STATUS_CLOSED)
Cumulocity 11:e1bee9a77652 72 return PARSER_END_OF_RESPONSE;
Cumulocity 11:e1bee9a77652 73 else if (status == DS_STATUS_TIMEOUT)
Cumulocity 11:e1bee9a77652 74 return PARSER_TIMEOUT_ERROR;
Cumulocity 11:e1bee9a77652 75 else
Cumulocity 11:e1bee9a77652 76 return PARSER_INTERNAL_ERROR;
Cumulocity 11:e1bee9a77652 77 }
Cumulocity 0:099f76422485 78
Cumulocity 11:e1bee9a77652 79 assert(status >= 0);
Cumulocity 11:e1bee9a77652 80 if (status == DS_STATUS_TIMEOUT)
Cumulocity 11:e1bee9a77652 81 return PARSER_TIMEOUT_ERROR;
Cumulocity 11:e1bee9a77652 82 else if (status == DS_STATUS_ERROR)
Cumulocity 11:e1bee9a77652 83 return PARSER_INTERNAL_ERROR;
Cumulocity 11:e1bee9a77652 84 else
Cumulocity 11:e1bee9a77652 85 return PARSER_PARSE_ERROR;
Cumulocity 0:099f76422485 86 }
Cumulocity 11:e1bee9a77652 87 /*-------------------------------------------------------------------------*/
Cumulocity 0:099f76422485 88 void Parser::parse(char c)
Cumulocity 0:099f76422485 89 {
Cumulocity 11:e1bee9a77652 90 if (_ptr-_buffer >= SMARTREST_PARSER_BUFFER_SIZE)
Cumulocity 11:e1bee9a77652 91 {
Cumulocity 11:e1bee9a77652 92 _state = STATE_ERROR;
Cumulocity 11:e1bee9a77652 93 return;
Cumulocity 11:e1bee9a77652 94 }
Cumulocity 0:099f76422485 95
Cumulocity 11:e1bee9a77652 96 switch (_state)
Cumulocity 11:e1bee9a77652 97 {
Cumulocity 11:e1bee9a77652 98 case STATE_BLANK:
Cumulocity 11:e1bee9a77652 99 _state = STATE_STRUCTURE;
Cumulocity 11:e1bee9a77652 100 case STATE_STRUCTURE:
Cumulocity 11:e1bee9a77652 101 switch (c)
Cumulocity 11:e1bee9a77652 102 {
Cumulocity 11:e1bee9a77652 103 case ' ':
Cumulocity 11:e1bee9a77652 104 case '\t':
Cumulocity 11:e1bee9a77652 105 case '\r':
Cumulocity 11:e1bee9a77652 106 break;
Cumulocity 11:e1bee9a77652 107 case '"':
Cumulocity 11:e1bee9a77652 108 _state = STATE_INQUOTES;
Cumulocity 11:e1bee9a77652 109 break;
Cumulocity 11:e1bee9a77652 110 case ',':
Cumulocity 11:e1bee9a77652 111 close();
Cumulocity 11:e1bee9a77652 112 break;
Cumulocity 11:e1bee9a77652 113 case '\0':
Cumulocity 11:e1bee9a77652 114 case '\n':
Cumulocity 11:e1bee9a77652 115 if ((_count == 0) && (_length == 0))
Cumulocity 11:e1bee9a77652 116 {
Cumulocity 11:e1bee9a77652 117 _state = STATE_BLANK;
Cumulocity 11:e1bee9a77652 118 }
Cumulocity 11:e1bee9a77652 119 else
Cumulocity 11:e1bee9a77652 120 {
Cumulocity 11:e1bee9a77652 121 close();
Cumulocity 11:e1bee9a77652 122 _state = STATE_COMPLETE;
Cumulocity 11:e1bee9a77652 123 }
Cumulocity 11:e1bee9a77652 124 break;
Cumulocity 11:e1bee9a77652 125 default:
Cumulocity 11:e1bee9a77652 126 if (_length > 0)
Cumulocity 11:e1bee9a77652 127 {
Cumulocity 11:e1bee9a77652 128 _state = STATE_ERROR;
Cumulocity 11:e1bee9a77652 129 }
Cumulocity 11:e1bee9a77652 130 else
Cumulocity 11:e1bee9a77652 131 {
Cumulocity 11:e1bee9a77652 132 _state = STATE_VALUE;
Cumulocity 11:e1bee9a77652 133 parse(c);
Cumulocity 11:e1bee9a77652 134 }
Cumulocity 11:e1bee9a77652 135 break;
Cumulocity 11:e1bee9a77652 136 }
Cumulocity 11:e1bee9a77652 137 break;
Cumulocity 11:e1bee9a77652 138 case STATE_VALUE:
Cumulocity 11:e1bee9a77652 139 switch (c)
Cumulocity 11:e1bee9a77652 140 {
Cumulocity 11:e1bee9a77652 141 case ',':
Cumulocity 11:e1bee9a77652 142 case '\n':
Cumulocity 11:e1bee9a77652 143 _state = STATE_STRUCTURE;
Cumulocity 11:e1bee9a77652 144 parse(c);
Cumulocity 11:e1bee9a77652 145 break;
Cumulocity 11:e1bee9a77652 146 case '"':
Cumulocity 11:e1bee9a77652 147 _state = STATE_ERROR;
Cumulocity 11:e1bee9a77652 148 break;
Cumulocity 11:e1bee9a77652 149 default:
Cumulocity 11:e1bee9a77652 150 if ((c == ' ') || (c == '\t') || (c == '\r'))
Cumulocity 11:e1bee9a77652 151 _trailing++;
Cumulocity 11:e1bee9a77652 152 else
Cumulocity 11:e1bee9a77652 153 _trailing = 0;
Cumulocity 11:e1bee9a77652 154 append(c);
Cumulocity 11:e1bee9a77652 155 break;
Cumulocity 11:e1bee9a77652 156 }
Cumulocity 11:e1bee9a77652 157 break;
Cumulocity 11:e1bee9a77652 158 case STATE_INQUOTES:
Cumulocity 11:e1bee9a77652 159 switch (c)
Cumulocity 11:e1bee9a77652 160 {
Cumulocity 11:e1bee9a77652 161 case '"':
Cumulocity 11:e1bee9a77652 162 _state = STATE_AFTERQUOTE;
Cumulocity 11:e1bee9a77652 163 break;
Cumulocity 11:e1bee9a77652 164 default:
Cumulocity 11:e1bee9a77652 165 append(c);
Cumulocity 11:e1bee9a77652 166 break;
Cumulocity 11:e1bee9a77652 167 }
Cumulocity 11:e1bee9a77652 168 break;
Cumulocity 11:e1bee9a77652 169 case STATE_AFTERQUOTE:
Cumulocity 11:e1bee9a77652 170 switch (c)
Cumulocity 11:e1bee9a77652 171 {
Cumulocity 11:e1bee9a77652 172 case '"':
Cumulocity 11:e1bee9a77652 173 append(c);
Cumulocity 11:e1bee9a77652 174 _state = STATE_INQUOTES;
Cumulocity 11:e1bee9a77652 175 break;
Cumulocity 11:e1bee9a77652 176 default:
Cumulocity 11:e1bee9a77652 177 _state = STATE_STRUCTURE;
Cumulocity 11:e1bee9a77652 178 parse(c);
Cumulocity 11:e1bee9a77652 179 break;
Cumulocity 11:e1bee9a77652 180 }
Cumulocity 11:e1bee9a77652 181 break;
Cumulocity 11:e1bee9a77652 182 }
Cumulocity 0:099f76422485 183 }
Cumulocity 11:e1bee9a77652 184 /*-------------------------------------------------------------------------*/
Cumulocity 0:099f76422485 185 void Parser::append(char c)
Cumulocity 0:099f76422485 186 {
Cumulocity 11:e1bee9a77652 187 *_ptr++ = c;
Cumulocity 11:e1bee9a77652 188 _length++;
Cumulocity 0:099f76422485 189 }
Cumulocity 11:e1bee9a77652 190 /*-------------------------------------------------------------------------*/
Cumulocity 0:099f76422485 191 void Parser::close()
Cumulocity 0:099f76422485 192 {
Cumulocity 11:e1bee9a77652 193 _ptr -= _trailing;
Cumulocity 11:e1bee9a77652 194 *_ptr++ = 0;
Cumulocity 11:e1bee9a77652 195 _trailing = 0;
Cumulocity 11:e1bee9a77652 196 _length = 0;
Cumulocity 11:e1bee9a77652 197 _count++;
Cumulocity 0:099f76422485 198 }
Cumulocity 11:e1bee9a77652 199 /*-------------------------------------------------------------------------*/
Cumulocity 0:099f76422485 200 void Parser::reset()
Cumulocity 0:099f76422485 201 {
Cumulocity 11:e1bee9a77652 202 _state = STATE_BLANK;
Cumulocity 11:e1bee9a77652 203 _ptr = _buffer;
Cumulocity 11:e1bee9a77652 204 _count = 0;
Cumulocity 11:e1bee9a77652 205 _trailing = 0;
Cumulocity 11:e1bee9a77652 206 _length = 0;
Cumulocity 0:099f76422485 207 }
Cumulocity 11:e1bee9a77652 208 /*-------------------------------------------------------------------------*/