Own fork of MbedSmartRest

Dependents:   MbedSmartRestMain MbedSmartRestMain

Fork of MbedSmartRest by Cumulocity Official

Committer:
Cumulocity
Date:
Mon Jul 28 12:11:02 2014 +0200
Revision:
6:cd7ba1ddb664
Parent:
4:059b8b90e0d9
Child:
11:e1bee9a77652
Updated from revision 908949dc5088

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 4:059b8b90e0d9 49 record.clear();
Cumulocity 0:099f76422485 50
Cumulocity 0:099f76422485 51 while ((_state < STATE_COMPLETE) &&
Cumulocity 6:cd7ba1ddb664 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 6:cd7ba1ddb664 56 // successfully read record
Cumulocity 4:059b8b90e0d9 57 if (_state == STATE_COMPLETE) {
Cumulocity 4:059b8b90e0d9 58 if (!record.set(_buffer, _count))
Cumulocity 4:059b8b90e0d9 59 return PARSER_INTERNAL_ERROR;
Cumulocity 6:cd7ba1ddb664 60 return PARSER_SUCCESS;
Cumulocity 6:cd7ba1ddb664 61 }
Cumulocity 6:cd7ba1ddb664 62
Cumulocity 6:cd7ba1ddb664 63 if (_state == STATE_BLANK) {
Cumulocity 6:cd7ba1ddb664 64 if (status == DS_STATUS_CLOSED)
Cumulocity 6:cd7ba1ddb664 65 return PARSER_END_OF_RESPONSE;
Cumulocity 6:cd7ba1ddb664 66 else if (status == DS_STATUS_TIMEOUT)
Cumulocity 6:cd7ba1ddb664 67 return PARSER_TIMEOUT_ERROR;
Cumulocity 6:cd7ba1ddb664 68 else
Cumulocity 6:cd7ba1ddb664 69 return PARSER_INTERNAL_ERROR;
Cumulocity 4:059b8b90e0d9 70 }
Cumulocity 0:099f76422485 71
Cumulocity 6:cd7ba1ddb664 72 if (status == DS_STATUS_TIMEOUT)
Cumulocity 6:cd7ba1ddb664 73 return PARSER_TIMEOUT_ERROR;
Cumulocity 6:cd7ba1ddb664 74 else if (status == DS_STATUS_ERROR)
Cumulocity 6:cd7ba1ddb664 75 return PARSER_INTERNAL_ERROR;
Cumulocity 0:099f76422485 76 else
Cumulocity 6:cd7ba1ddb664 77 return PARSER_PARSE_ERROR;
Cumulocity 0:099f76422485 78 }
Cumulocity 0:099f76422485 79
Cumulocity 0:099f76422485 80 void Parser::parse(char c)
Cumulocity 0:099f76422485 81 {
Cumulocity 0:099f76422485 82 if (_ptr-_buffer >= SMARTREST_PARSER_BUFFER_SIZE) {
Cumulocity 0:099f76422485 83 _state = STATE_ERROR;
Cumulocity 0:099f76422485 84 return;
Cumulocity 0:099f76422485 85 }
Cumulocity 0:099f76422485 86
Cumulocity 0:099f76422485 87 switch (_state) {
Cumulocity 0:099f76422485 88 case STATE_BLANK:
Cumulocity 0:099f76422485 89 _state = STATE_STRUCTURE;
Cumulocity 0:099f76422485 90 case STATE_STRUCTURE:
Cumulocity 0:099f76422485 91 switch (c) {
Cumulocity 0:099f76422485 92 case ' ':
Cumulocity 0:099f76422485 93 case '\t':
Cumulocity 0:099f76422485 94 case '\r':
Cumulocity 0:099f76422485 95 break;
Cumulocity 0:099f76422485 96 case '"':
Cumulocity 0:099f76422485 97 _state = STATE_INQUOTES;
Cumulocity 0:099f76422485 98 break;
Cumulocity 0:099f76422485 99 case ',':
Cumulocity 0:099f76422485 100 close();
Cumulocity 0:099f76422485 101 break;
Cumulocity 0:099f76422485 102 case '\0':
Cumulocity 0:099f76422485 103 case '\n':
Cumulocity 0:099f76422485 104 if ((_count == 0) && (_length == 0)) {
Cumulocity 0:099f76422485 105 _state = STATE_BLANK;
Cumulocity 0:099f76422485 106 } else {
Cumulocity 0:099f76422485 107 close();
Cumulocity 0:099f76422485 108 _state = STATE_COMPLETE;
Cumulocity 0:099f76422485 109 }
Cumulocity 0:099f76422485 110 break;
Cumulocity 0:099f76422485 111 default:
Cumulocity 0:099f76422485 112 if (_length > 0) {
Cumulocity 0:099f76422485 113 _state = STATE_ERROR;
Cumulocity 0:099f76422485 114 } else {
Cumulocity 0:099f76422485 115 _state = STATE_VALUE;
Cumulocity 0:099f76422485 116 parse(c);
Cumulocity 0:099f76422485 117 }
Cumulocity 0:099f76422485 118 break;
Cumulocity 0:099f76422485 119 }
Cumulocity 0:099f76422485 120 break;
Cumulocity 0:099f76422485 121 case STATE_VALUE:
Cumulocity 0:099f76422485 122 switch (c) {
Cumulocity 0:099f76422485 123 case ',':
Cumulocity 0:099f76422485 124 case '\n':
Cumulocity 0:099f76422485 125 _state = STATE_STRUCTURE;
Cumulocity 0:099f76422485 126 parse(c);
Cumulocity 0:099f76422485 127 break;
Cumulocity 0:099f76422485 128 case '"':
Cumulocity 0:099f76422485 129 _state = STATE_ERROR;
Cumulocity 0:099f76422485 130 break;
Cumulocity 0:099f76422485 131 default:
Cumulocity 0:099f76422485 132 if ((c == ' ') || (c == '\t') || (c == '\r'))
Cumulocity 0:099f76422485 133 _trailing++;
Cumulocity 0:099f76422485 134 else
Cumulocity 0:099f76422485 135 _trailing = 0;
Cumulocity 0:099f76422485 136 append(c);
Cumulocity 0:099f76422485 137 break;
Cumulocity 0:099f76422485 138 }
Cumulocity 0:099f76422485 139 break;
Cumulocity 0:099f76422485 140 case STATE_INQUOTES:
Cumulocity 0:099f76422485 141 switch (c) {
Cumulocity 0:099f76422485 142 case '"':
Cumulocity 0:099f76422485 143 _state = STATE_AFTERQUOTE;
Cumulocity 0:099f76422485 144 break;
Cumulocity 0:099f76422485 145 default:
Cumulocity 0:099f76422485 146 append(c);
Cumulocity 0:099f76422485 147 break;
Cumulocity 0:099f76422485 148 }
Cumulocity 0:099f76422485 149 break;
Cumulocity 0:099f76422485 150 case STATE_AFTERQUOTE:
Cumulocity 0:099f76422485 151 switch (c) {
Cumulocity 0:099f76422485 152 case '"':
Cumulocity 0:099f76422485 153 append(c);
Cumulocity 0:099f76422485 154 _state = STATE_INQUOTES;
Cumulocity 0:099f76422485 155 break;
Cumulocity 0:099f76422485 156 default:
Cumulocity 0:099f76422485 157 _state = STATE_STRUCTURE;
Cumulocity 0:099f76422485 158 parse(c);
Cumulocity 0:099f76422485 159 break;
Cumulocity 0:099f76422485 160 }
Cumulocity 0:099f76422485 161 break;
Cumulocity 0:099f76422485 162 }
Cumulocity 0:099f76422485 163 }
Cumulocity 0:099f76422485 164
Cumulocity 0:099f76422485 165 void Parser::append(char c)
Cumulocity 0:099f76422485 166 {
Cumulocity 0:099f76422485 167 *_ptr++ = c;
Cumulocity 0:099f76422485 168 _length++;
Cumulocity 0:099f76422485 169 }
Cumulocity 0:099f76422485 170
Cumulocity 0:099f76422485 171 void Parser::close()
Cumulocity 0:099f76422485 172 {
Cumulocity 0:099f76422485 173 _ptr -= _trailing;
Cumulocity 0:099f76422485 174 *_ptr++ = 0;
Cumulocity 0:099f76422485 175 _trailing = 0;
Cumulocity 0:099f76422485 176 _length = 0;
Cumulocity 0:099f76422485 177 _count++;
Cumulocity 0:099f76422485 178 }
Cumulocity 0:099f76422485 179
Cumulocity 0:099f76422485 180 void Parser::reset()
Cumulocity 0:099f76422485 181 {
Cumulocity 0:099f76422485 182 _state = STATE_BLANK;
Cumulocity 0:099f76422485 183 _ptr = _buffer;
Cumulocity 0:099f76422485 184 _count = 0;
Cumulocity 0:099f76422485 185 _trailing = 0;
Cumulocity 0:099f76422485 186 _length = 0;
Cumulocity 0:099f76422485 187 }