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

Dependents:   MbedSmartRestMain MbedSmartRestMain

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers Parser.h Source File

Parser.h

Go to the documentation of this file.
00001 /*
00002  * Parser.h
00003  *
00004  * Created on: Nov 1, 2013
00005  * * Authors: Vincent Wochnik <v.wochnik@gmail.com>
00006  *
00007  * Copyright (c) 2013 Cumulocity GmbH
00008  *
00009  * Permission is hereby granted, free of charge, to any person obtaining
00010  * a copy of this software and associated documentation files (the
00011  * "Software"), to deal in the Software without restriction, including
00012  * without limitation the rights to use, copy, modify, merge, publish,
00013  * distribute, sublicense, and/or sell copies of the Software, and to
00014  * permit persons to whom the Software is furnished to do so, subject to
00015  * the following conditions:
00016  *
00017  * The above copyright notice and this permission notice shall be
00018  * included in all copies or substantial portions of the Software.
00019  *
00020  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
00021  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
00022  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
00023  * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
00024  * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
00025  * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
00026  * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
00027  */
00028 
00029 /**
00030  * @file Parser.h
00031  * A CSV parser utilizing an AbstractDataSource instance for reading from
00032  * a connection.
00033  */
00034 
00035 #ifndef PARSER_H
00036 #define PARSER_H
00037 
00038 #include "config.h"
00039 #include <stddef.h>
00040 #include <stdint.h>
00041 #include "ParsedRecord.h"
00042 #include "AbstractDataSource.h"
00043 
00044 #ifndef SMARTREST_PARSER_BUFFER_SIZE
00045 #define SMARTREST_PARSER_BUFFER_SIZE 81
00046 #endif
00047 
00048 /** Return value indicating that no error occurred. */
00049 #define PARSER_SUCCESS 0
00050 /** Return value indicating that a transmission timeout occurred during
00051  * the transmission of a record. */
00052 #define PARSER_TIMEOUT_ERROR 1
00053 /** Return value indicating the end of response. */
00054 #define PARSER_END_OF_RESPONSE 2
00055 /** Return value indicating a parse error. */
00056 #define PARSER_PARSE_ERROR 3
00057 /** Return value indicating an internal error. */
00058 #define PARSER_INTERNAL_ERROR 4
00059 
00060 /*
00061  * A record parser reading from a data source.
00062  * 
00063  * Example:
00064  * @code
00065  * // given a concrete data sorce
00066  * AbstractDataSource source;
00067  * 
00068  * Parser parser;
00069  * ParsedRecord record;
00070  * uint8_t ret;
00071  * 
00072  * while ((ret = parser.readFrom(source, record)) == PARSER_SUCCESS) {
00073  *     // read record
00074  * }
00075 
00076  * // error handling
00077  * switch (ret) {
00078  * case PARSER_END_OF_RESPONSE:
00079  *     // no error; end of response
00080  *     break;
00081  * case PARSER_TIMEOUT_ERROR:
00082  *     // timeout error
00083  *     break;
00084  * default:
00085  *     // parse error
00086  *     break;
00087  * }
00088  * @encode
00089  */
00090 class Parser
00091 {
00092     public:
00093         /*
00094          * Creates a new Parser instance.
00095          */
00096         Parser();
00097 
00098         /*
00099          * Parses a row from a stream.
00100          * @param source the source to read from
00101          * @param row the object written to
00102          * @return a non-zero value if and only if an error occured.
00103          */
00104         uint8_t readFrom(AbstractDataSource&,ParsedRecord&);
00105 
00106     private:
00107         void parse(char);
00108         void append(char);
00109         void close();
00110         void reset();
00111 
00112     private:
00113         char _buffer[SMARTREST_PARSER_BUFFER_SIZE];
00114         uint8_t _state;
00115         char *_ptr;
00116         size_t _count, _length, _trailing;
00117 };
00118 
00119 #endif