2011-05-09 07:02:15 +00:00
|
|
|
#ifndef PSYC_PARSE_H
|
|
|
|
|
2011-04-20 12:52:40 +00:00
|
|
|
/**
|
2011-05-09 07:02:15 +00:00
|
|
|
* @file psyc/parse.h
|
2011-05-09 12:37:57 +00:00
|
|
|
* @brief Interface for PSYC packet parsing.
|
2011-04-19 20:27:38 +00:00
|
|
|
*
|
2011-05-09 12:37:57 +00:00
|
|
|
* All parsing functions and the definitions they use are defined here.
|
2011-05-05 22:13:37 +00:00
|
|
|
*/
|
2011-04-19 20:27:38 +00:00
|
|
|
|
2011-04-20 12:52:40 +00:00
|
|
|
/**
|
2011-05-09 10:42:42 +00:00
|
|
|
* @defgroup parse Parsing Functions
|
2011-04-20 12:52:40 +00:00
|
|
|
*
|
2011-05-08 23:36:57 +00:00
|
|
|
* This module contains packet and list parsing functions.
|
2011-05-09 00:07:13 +00:00
|
|
|
* The parser adheres to the definition of a packet found at
|
|
|
|
*
|
|
|
|
* http://about.psyc.eu/Spec:Packet
|
|
|
|
*
|
|
|
|
* and the according terms are used throughout this documentation and in the
|
|
|
|
* return codes. You should be at least
|
2011-05-15 18:26:52 +00:00
|
|
|
* vaguely familiar with differences between "body" and "content" as
|
|
|
|
* well as "routing variable" and "entity variable".
|
2011-05-09 00:07:13 +00:00
|
|
|
*
|
2011-05-08 23:36:57 +00:00
|
|
|
*
|
|
|
|
* To parse a packet you first have to initialize a state:
|
|
|
|
*
|
|
|
|
* @code
|
|
|
|
* psycParseState state;
|
|
|
|
*
|
|
|
|
* psyc_initParseState(&state);
|
|
|
|
* @endcode
|
|
|
|
*
|
|
|
|
* Note that there is also psyc_initParseState2 if you want to fine-tune what
|
2011-05-08 23:49:04 +00:00
|
|
|
* part of the packet should be parsed. @see psycParseFlag
|
2011-05-08 23:36:57 +00:00
|
|
|
*
|
|
|
|
* Next, you have to tell the parser what it should parse. Assuming the variable
|
|
|
|
* raw_data points to our packet and raw_len contains the length, you can pass
|
|
|
|
* it to the parser as follows:
|
|
|
|
*
|
|
|
|
* @code
|
|
|
|
* char* raw_data; // points to our (possibly incomplete) packet
|
|
|
|
* size_t raw_len; // how many bytes of data
|
|
|
|
*
|
2011-06-02 13:01:26 +00:00
|
|
|
* psyc_setParseBuffer(&state, raw_data, raw_len); // state is our initialized state from before
|
2011-05-08 23:36:57 +00:00
|
|
|
* @endcode
|
|
|
|
*
|
|
|
|
* Now the the variables that will save the output of the parser need to be
|
|
|
|
* declared:
|
|
|
|
*
|
|
|
|
* @code
|
|
|
|
* psycString name, // Name of the variable or method
|
|
|
|
* value; // Value of the variable or body
|
|
|
|
* char oper; // operator of the variable (if any)
|
|
|
|
* @endcode
|
|
|
|
*
|
2011-05-09 17:21:26 +00:00
|
|
|
* They will be passed to the parsing function which will set them to
|
2011-05-08 23:36:57 +00:00
|
|
|
* the according positions and lengths.
|
|
|
|
*
|
|
|
|
* Now the real parsing begins. The parsing function needs to be called
|
2011-05-15 20:48:51 +00:00
|
|
|
* repeatedly with various actions in between, depending on the return values.
|
|
|
|
*
|
|
|
|
* A simplified example follows, see test/testPsyc.c for actual code that
|
2011-06-12 12:16:39 +00:00
|
|
|
* handles incomplete packets as well.
|
2011-05-08 23:36:57 +00:00
|
|
|
*
|
|
|
|
* @code
|
|
|
|
*
|
2011-05-15 20:48:51 +00:00
|
|
|
* int ret;
|
2011-05-08 23:36:57 +00:00
|
|
|
*
|
|
|
|
* do // run the parsing in a loop, each time parsing one line
|
|
|
|
* {
|
|
|
|
* name.length = value.length = oper = 0; // reset the output variables
|
|
|
|
*
|
|
|
|
* ret = psyc_parse(&state, &oper, &name, &value); // call the parsing function
|
|
|
|
*
|
|
|
|
* switch (ret) // look at the return value
|
|
|
|
* {
|
|
|
|
* case PSYC_PARSE_ROUTING: // it is a routing variable
|
|
|
|
* case PSYC_PARSE_ENTITY: // it is a entity variable
|
|
|
|
* // Name, value and operator of the variable can now be found in the
|
|
|
|
* // respective variables:
|
|
|
|
* printf("Variable: %.*s Value: %.*s Operator: %c\n",
|
|
|
|
* name.length, name.ptr,
|
|
|
|
* value.length, value.ptr,
|
2011-05-08 23:49:04 +00:00
|
|
|
* oper);
|
2011-05-08 23:36:57 +00:00
|
|
|
* // Note that the .ptr member still points at your original buffer. If
|
|
|
|
* // you want to reuse that buffer for the next packet, you better copy it
|
|
|
|
* // before passing it to the parser or you copy each variable now.
|
|
|
|
* break;
|
|
|
|
* case PSYC_PARSE_BODY: // it is the method and the body of the packet.
|
|
|
|
* printf("Method Name: %.*s Body: %.*s\n",
|
|
|
|
* name.length, name.ptr, // name of the method
|
2011-05-09 00:07:13 +00:00
|
|
|
* value.length, value.ptr); // value of the body
|
2011-05-08 23:36:57 +00:00
|
|
|
* break;
|
2011-05-08 23:53:30 +00:00
|
|
|
* case PSYC_PARSE_COMPLETE: // parsing of this packet is complete
|
2011-05-08 23:36:57 +00:00
|
|
|
* // You can simply continue parsing till you get the
|
2011-05-09 17:21:26 +00:00
|
|
|
* // PSYC_PARSE_INSUFFICIENT code which means the line is incomplete.
|
2011-05-08 23:36:57 +00:00
|
|
|
* continue;
|
|
|
|
* default: //
|
|
|
|
* perror("Error %i happened :(\n", res);
|
|
|
|
* return res;
|
|
|
|
* }
|
2011-05-15 22:01:02 +00:00
|
|
|
* }
|
|
|
|
* while (ret > 0)
|
2011-05-08 23:36:57 +00:00
|
|
|
* @endcode
|
|
|
|
*
|
2011-05-09 07:11:59 +00:00
|
|
|
* This simple example does not consider some more complex cases when you
|
|
|
|
* receive incomplete packets but still want to access the data. This code would
|
2011-05-08 23:36:57 +00:00
|
|
|
* simply reject incomplete packets as error. A more detailed tutorial for
|
2011-05-09 07:11:59 +00:00
|
|
|
* incomplete packets will follow. In the mean time, have look at the return
|
|
|
|
* codes in psycParseRC and their explanations. @see psycParseRC
|
2011-04-19 20:57:49 +00:00
|
|
|
*/
|
|
|
|
|
2011-05-08 23:53:30 +00:00
|
|
|
/** @{ */ // begin of parser group
|
|
|
|
|
2010-02-20 16:40:09 +00:00
|
|
|
#include <stdint.h>
|
2011-04-15 23:42:36 +00:00
|
|
|
#include <string.h>
|
2011-05-03 20:56:54 +00:00
|
|
|
#include <psyc.h>
|
2011-04-15 23:42:36 +00:00
|
|
|
|
2011-04-20 14:28:15 +00:00
|
|
|
typedef enum
|
2011-04-17 10:56:24 +00:00
|
|
|
{
|
2011-05-15 19:10:27 +00:00
|
|
|
/// Default Flag. Parse everything.
|
|
|
|
PSYC_PARSE_ALL = 0,
|
2011-04-30 13:48:52 +00:00
|
|
|
/// Parse only the header
|
2011-04-30 15:07:01 +00:00
|
|
|
PSYC_PARSE_ROUTING_ONLY = 1,
|
2011-05-09 14:32:39 +00:00
|
|
|
/// Parse only the content.
|
|
|
|
/// Parsing starts at the content and the content must be complete.
|
2011-04-30 15:07:01 +00:00
|
|
|
PSYC_PARSE_START_AT_CONTENT = 2,
|
2011-04-25 21:40:38 +00:00
|
|
|
} psycParseFlag;
|
2011-04-17 10:56:24 +00:00
|
|
|
|
2011-04-20 12:52:40 +00:00
|
|
|
/**
|
|
|
|
* The return value definitions for the packet parsing function.
|
2011-04-25 21:40:38 +00:00
|
|
|
* @see psyc_parse()
|
2011-04-19 20:57:49 +00:00
|
|
|
*/
|
2011-04-20 14:28:15 +00:00
|
|
|
typedef enum
|
2011-04-17 10:05:14 +00:00
|
|
|
{
|
2011-05-09 14:32:39 +00:00
|
|
|
/// Error, packet is not ending with a valid delimiter.
|
2011-05-07 17:25:18 +00:00
|
|
|
PSYC_PARSE_ERROR_END = -8,
|
2011-05-09 14:32:39 +00:00
|
|
|
/// Error, expected NL after the method.
|
2011-04-27 16:37:03 +00:00
|
|
|
PSYC_PARSE_ERROR_METHOD = -7,
|
2011-05-09 14:32:39 +00:00
|
|
|
/// Error, expected NL after a modifier.
|
2011-04-27 16:37:03 +00:00
|
|
|
PSYC_PARSE_ERROR_MOD_NL = -6,
|
2011-05-09 14:32:39 +00:00
|
|
|
/// Error, modifier length is not numeric.
|
2011-04-27 16:37:03 +00:00
|
|
|
PSYC_PARSE_ERROR_MOD_LEN = -5,
|
2011-05-09 14:32:39 +00:00
|
|
|
/// Error, expected TAB before modifier value.
|
2011-04-27 16:37:03 +00:00
|
|
|
PSYC_PARSE_ERROR_MOD_TAB = -4,
|
2011-05-09 14:32:39 +00:00
|
|
|
/// Error, modifier name is missing.
|
2011-04-27 16:37:03 +00:00
|
|
|
PSYC_PARSE_ERROR_MOD_NAME = -3,
|
2011-05-09 14:32:39 +00:00
|
|
|
/// Error, expected NL after the content length.
|
2011-04-22 15:09:32 +00:00
|
|
|
PSYC_PARSE_ERROR_LENGTH = -2,
|
2011-05-09 14:32:39 +00:00
|
|
|
/// Error in packet.
|
2011-04-22 15:09:32 +00:00
|
|
|
PSYC_PARSE_ERROR = -1,
|
2011-05-09 14:32:39 +00:00
|
|
|
/// Buffer contains insufficient amount of data.
|
|
|
|
/// Fill another buffer and concatenate it with the end of the current buffer,
|
|
|
|
/// from the cursor position to the end.
|
2011-04-22 15:09:32 +00:00
|
|
|
PSYC_PARSE_INSUFFICIENT = 1,
|
2011-05-09 14:32:39 +00:00
|
|
|
/// Routing modifier parsing done.
|
|
|
|
/// Operator, name & value contains the respective parts.
|
2011-04-22 15:09:32 +00:00
|
|
|
PSYC_PARSE_ROUTING = 2,
|
2011-05-09 14:32:39 +00:00
|
|
|
/// Start of an incomplete entity modifier.
|
|
|
|
/// Operator & name are complete, value is incomplete.
|
2011-05-07 17:25:18 +00:00
|
|
|
PSYC_PARSE_ENTITY_START = 3,
|
2011-05-09 14:32:39 +00:00
|
|
|
/// Continuation of an incomplete entity modifier.
|
2011-05-07 17:25:18 +00:00
|
|
|
PSYC_PARSE_ENTITY_CONT = 4,
|
2011-05-09 14:32:39 +00:00
|
|
|
/// End of an incomplete entity modifier.
|
2011-05-07 17:25:18 +00:00
|
|
|
PSYC_PARSE_ENTITY_END = 5,
|
2011-05-09 14:32:39 +00:00
|
|
|
/// Entity modifier parsing done in one go.
|
|
|
|
/// Operator, name & value contains the respective parts.
|
2011-05-07 17:25:18 +00:00
|
|
|
PSYC_PARSE_ENTITY = 6,
|
2011-06-12 12:25:22 +00:00
|
|
|
/// Start of an incomplete body.
|
2011-05-09 14:32:39 +00:00
|
|
|
/// Name contains method, value contains part of the body.
|
2011-06-12 12:25:22 +00:00
|
|
|
/// Used when packet length is given
|
2011-05-07 17:25:18 +00:00
|
|
|
PSYC_PARSE_BODY_START = 7,
|
2011-05-09 14:32:39 +00:00
|
|
|
/// Continuation of an incomplete body.
|
2011-06-12 12:25:22 +00:00
|
|
|
/// Used when packet length is given
|
2011-05-07 17:25:18 +00:00
|
|
|
PSYC_PARSE_BODY_CONT = 8,
|
2011-05-09 14:32:39 +00:00
|
|
|
/// End of an incomplete body.
|
2011-06-12 12:25:22 +00:00
|
|
|
/// Used when packet length is given
|
2011-05-07 17:25:18 +00:00
|
|
|
PSYC_PARSE_BODY_END = 9,
|
2011-05-09 14:32:39 +00:00
|
|
|
/// Body parsing done in one go, name contains method, value contains body.
|
2011-05-07 17:25:18 +00:00
|
|
|
PSYC_PARSE_BODY = 10,
|
2011-05-09 14:32:39 +00:00
|
|
|
/// Start of an incomplete content, value contains part of content.
|
|
|
|
/// Used when PSYC_PARSE_ROUTING_ONLY is set.
|
2011-05-07 17:25:18 +00:00
|
|
|
PSYC_PARSE_CONTENT_START = 7,
|
2011-06-02 13:01:26 +00:00
|
|
|
/// Continuation of an incomplete content.
|
2011-05-09 14:32:39 +00:00
|
|
|
/// Used when PSYC_PARSE_ROUTING_ONLY is set.
|
2011-05-07 17:25:18 +00:00
|
|
|
PSYC_PARSE_CONTENT_CONT = 8,
|
2011-06-02 13:01:26 +00:00
|
|
|
/// End of an incomplete content.
|
2011-05-09 14:32:39 +00:00
|
|
|
/// Used when PSYC_PARSE_ROUTING_ONLY is set.
|
2011-05-07 17:25:18 +00:00
|
|
|
PSYC_PARSE_CONTENT_END = 9,
|
2011-05-09 14:32:39 +00:00
|
|
|
/// Content parsing done in one go, value contains the whole content.
|
|
|
|
/// Used when PSYC_PARSE_ROUTING_ONLY is set.
|
2011-05-07 17:25:18 +00:00
|
|
|
PSYC_PARSE_CONTENT = 10,
|
2011-05-09 14:32:39 +00:00
|
|
|
/// Finished parsing packet.
|
2011-05-07 16:13:59 +00:00
|
|
|
PSYC_PARSE_COMPLETE = 11,
|
2011-04-25 21:40:38 +00:00
|
|
|
} psycParseRC;
|
2011-04-19 07:21:00 +00:00
|
|
|
|
2011-04-20 12:52:40 +00:00
|
|
|
/**
|
|
|
|
* The return value definitions for the list parsing function.
|
2011-04-25 21:40:38 +00:00
|
|
|
* @see psyc_parseList()
|
2011-04-20 12:52:40 +00:00
|
|
|
*/
|
2011-04-20 14:28:15 +00:00
|
|
|
typedef enum
|
2011-04-19 17:41:25 +00:00
|
|
|
{
|
2011-05-09 17:21:26 +00:00
|
|
|
PSYC_PARSE_LIST_ERROR_DELIM = -4,
|
|
|
|
PSYC_PARSE_LIST_ERROR_LEN = -3,
|
|
|
|
PSYC_PARSE_LIST_ERROR_TYPE = -2,
|
2011-05-08 22:14:48 +00:00
|
|
|
PSYC_PARSE_LIST_ERROR = -1,
|
2011-05-09 14:32:39 +00:00
|
|
|
/// Completed parsing a list element.
|
2011-04-22 15:09:32 +00:00
|
|
|
PSYC_PARSE_LIST_ELEM = 1,
|
2011-05-09 14:32:39 +00:00
|
|
|
/// Reached end of buffer.
|
2011-04-22 15:09:32 +00:00
|
|
|
PSYC_PARSE_LIST_END = 2,
|
2011-05-09 14:32:39 +00:00
|
|
|
/// Binary list is incomplete.
|
2011-04-22 15:09:32 +00:00
|
|
|
PSYC_PARSE_LIST_INCOMPLETE = 3,
|
2011-04-25 21:40:38 +00:00
|
|
|
} psycParseListRC;
|
2011-04-15 23:42:36 +00:00
|
|
|
|
2011-04-20 12:52:40 +00:00
|
|
|
/**
|
|
|
|
* Struct for keeping parser state.
|
|
|
|
*/
|
2011-04-19 17:41:25 +00:00
|
|
|
typedef struct
|
2011-04-15 23:42:36 +00:00
|
|
|
{
|
2011-05-09 14:32:39 +00:00
|
|
|
size_t cursor; ///< Current position in buffer.
|
|
|
|
size_t startc; ///< Position where the parsing would be resumed.
|
|
|
|
psycString buffer; ///< Buffer with data to be parsed.
|
|
|
|
uint8_t flags; ///< Flags for the parser, see psycParseFlag.
|
|
|
|
psycPart part; ///< Part of the packet being parsed currently.
|
2011-04-15 23:42:36 +00:00
|
|
|
|
2011-05-09 14:32:39 +00:00
|
|
|
size_t routingLength; ///< Length of routing part parsed so far.
|
|
|
|
size_t contentParsed; ///< Number of bytes parsed from the content so far.
|
|
|
|
size_t contentLength; ///< Expected length of the content.
|
|
|
|
psycBool contentLengthFound; ///< Is there a length given for this packet?
|
|
|
|
size_t valueParsed; ///< Number of bytes parsed from the value so far.
|
|
|
|
size_t valueLength; ///< Expected length of the value.
|
|
|
|
psycBool valueLengthFound; ///< Is there a length given for this modifier?
|
2011-04-25 21:40:38 +00:00
|
|
|
} psycParseState;
|
2011-04-15 23:42:36 +00:00
|
|
|
|
2011-04-20 12:52:40 +00:00
|
|
|
/**
|
|
|
|
* Struct for keeping list parser state.
|
|
|
|
*/
|
2011-04-19 17:41:25 +00:00
|
|
|
typedef struct
|
|
|
|
{
|
2011-05-09 14:32:39 +00:00
|
|
|
size_t cursor; ///< Current position in buffer.
|
|
|
|
size_t startc; ///< Line start position.
|
|
|
|
psycString buffer; ///< Buffer with data to be parsed.
|
|
|
|
psycListType type; ///< List type.
|
2011-04-19 17:41:25 +00:00
|
|
|
|
2011-05-09 14:32:39 +00:00
|
|
|
size_t elemParsed; ///< Number of bytes parsed from the elem so far.
|
|
|
|
size_t elemLength; ///< Expected length of the elem.
|
2011-04-25 21:40:38 +00:00
|
|
|
} psycParseListState;
|
2011-04-19 17:41:25 +00:00
|
|
|
|
2011-04-19 19:54:44 +00:00
|
|
|
/**
|
2011-05-03 23:30:09 +00:00
|
|
|
* Initializes the state struct.
|
2011-04-17 12:47:25 +00:00
|
|
|
*
|
2011-05-03 23:30:09 +00:00
|
|
|
* @param state Pointer to the state struct that should be initialized.
|
2011-05-08 23:36:57 +00:00
|
|
|
* @see psyc_initParseState2
|
2011-04-19 17:41:25 +00:00
|
|
|
*/
|
2011-05-03 21:11:13 +00:00
|
|
|
static inline
|
2011-05-04 02:01:21 +00:00
|
|
|
void psyc_initParseState (psycParseState *state)
|
2011-05-03 20:56:54 +00:00
|
|
|
{
|
|
|
|
memset(state, 0, sizeof(psycParseState));
|
|
|
|
}
|
2011-04-17 10:56:24 +00:00
|
|
|
|
2011-04-19 19:54:44 +00:00
|
|
|
/**
|
2011-05-03 23:30:09 +00:00
|
|
|
* Initializes the state struct with flags.
|
2011-04-18 08:09:35 +00:00
|
|
|
*
|
2011-05-03 23:30:09 +00:00
|
|
|
* @param state Pointer to the state struct that should be initialized.
|
2011-04-25 21:40:38 +00:00
|
|
|
* @param flags Flags to be set for the parser, see psycParseFlag.
|
2011-05-08 23:36:57 +00:00
|
|
|
* @see psyc_initParseState
|
|
|
|
* @see psycParseFlag
|
2011-04-19 17:41:25 +00:00
|
|
|
*/
|
2011-05-03 21:11:13 +00:00
|
|
|
static inline
|
2011-05-04 02:01:21 +00:00
|
|
|
void psyc_initParseState2 (psycParseState *state, uint8_t flags)
|
2011-05-03 20:56:54 +00:00
|
|
|
{
|
|
|
|
memset(state, 0, sizeof(psycParseState));
|
|
|
|
state->flags = flags;
|
|
|
|
|
|
|
|
if (flags & PSYC_PARSE_START_AT_CONTENT)
|
|
|
|
state->part = PSYC_PART_CONTENT;
|
|
|
|
}
|
2011-04-15 23:42:36 +00:00
|
|
|
|
2011-05-03 20:24:50 +00:00
|
|
|
/**
|
|
|
|
* Sets a new buffer in the parser state struct with data to be parsed.
|
2011-05-03 20:56:54 +00:00
|
|
|
*
|
2011-05-08 22:39:41 +00:00
|
|
|
* This function does NOT copy the buffer. It will parse whatever is
|
|
|
|
* at the memory pointed to by buffer.
|
|
|
|
*
|
2011-05-03 20:56:54 +00:00
|
|
|
* @param state Pointer to the initialized state of the parser
|
|
|
|
* @param buffer the buffer that should be parsed now
|
|
|
|
* @see psycString
|
2011-05-03 20:24:50 +00:00
|
|
|
*/
|
2011-05-03 21:11:13 +00:00
|
|
|
static inline
|
2011-05-04 02:01:21 +00:00
|
|
|
void psyc_setParseBuffer (psycParseState *state, psycString buffer)
|
2011-05-03 20:56:54 +00:00
|
|
|
{
|
|
|
|
state->buffer = buffer;
|
|
|
|
state->cursor = 0;
|
2011-05-03 20:24:50 +00:00
|
|
|
|
2011-05-03 20:56:54 +00:00
|
|
|
if (state->flags & PSYC_PARSE_START_AT_CONTENT)
|
|
|
|
{
|
|
|
|
state->contentLength = buffer.length;
|
|
|
|
state->contentLengthFound = PSYC_TRUE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Sets a new buffer in the parser state struct with data to be parsed.
|
|
|
|
*
|
2011-05-08 22:39:41 +00:00
|
|
|
* This function does NOT copy the buffer. It will parse whatever is
|
|
|
|
* at the memory pointed to by buffer.
|
|
|
|
*
|
2011-05-03 20:56:54 +00:00
|
|
|
* @param state Pointer to the initialized state of the parser
|
|
|
|
* @param buffer pointer to the data that should be parsed
|
|
|
|
* @param length length of the data in bytes
|
|
|
|
* @see psycString
|
|
|
|
*/
|
2011-05-03 21:11:13 +00:00
|
|
|
static inline
|
2011-05-28 17:39:42 +00:00
|
|
|
void psyc_setParseBuffer2 (psycParseState *state, const char *buffer, size_t length)
|
2011-05-03 20:56:54 +00:00
|
|
|
{
|
2011-05-06 13:26:28 +00:00
|
|
|
psycString buf = {length, buffer};
|
|
|
|
psyc_setParseBuffer(state, buf);
|
2011-05-03 20:56:54 +00:00
|
|
|
}
|
2011-05-03 20:24:50 +00:00
|
|
|
|
2011-04-19 19:54:44 +00:00
|
|
|
/**
|
2011-05-03 23:30:09 +00:00
|
|
|
* Initializes the list state struct.
|
2011-04-19 17:41:25 +00:00
|
|
|
*
|
2011-05-03 23:30:09 +00:00
|
|
|
* @param state Pointer to the list state struct that should be initialized.
|
2011-04-19 17:41:25 +00:00
|
|
|
*/
|
2011-05-03 21:11:13 +00:00
|
|
|
static inline
|
2011-05-04 02:01:21 +00:00
|
|
|
void psyc_initParseListState (psycParseListState *state)
|
2011-05-03 20:56:54 +00:00
|
|
|
{
|
|
|
|
memset(state, 0, sizeof(psycParseListState));
|
|
|
|
}
|
2011-04-19 17:41:25 +00:00
|
|
|
|
2011-05-03 20:24:50 +00:00
|
|
|
/**
|
|
|
|
* Sets a new buffer in the list parser state struct with data to be parsed.
|
|
|
|
*/
|
2011-05-03 21:11:13 +00:00
|
|
|
static inline
|
2011-05-04 02:01:21 +00:00
|
|
|
void psyc_setParseListBuffer (psycParseListState *state, psycString buffer)
|
2011-05-03 20:56:54 +00:00
|
|
|
{
|
|
|
|
state->buffer = buffer;
|
|
|
|
state->cursor = 0;
|
|
|
|
}
|
2011-04-19 17:41:25 +00:00
|
|
|
|
2011-05-03 21:11:13 +00:00
|
|
|
static inline
|
2011-05-28 17:39:42 +00:00
|
|
|
void psyc_setParseListBuffer2 (psycParseListState *state, const char *buffer, size_t length)
|
2011-05-03 20:56:54 +00:00
|
|
|
{
|
2011-05-06 13:26:28 +00:00
|
|
|
psycString buf = {length, buffer};
|
|
|
|
psyc_setParseListBuffer(state, buf);
|
2011-05-03 20:56:54 +00:00
|
|
|
}
|
2011-04-15 23:42:36 +00:00
|
|
|
|
2011-05-03 21:11:13 +00:00
|
|
|
static inline
|
2011-05-04 02:01:21 +00:00
|
|
|
size_t psyc_getParseContentLength (psycParseState *state)
|
2011-05-03 20:56:54 +00:00
|
|
|
{
|
2011-05-04 02:01:21 +00:00
|
|
|
return state->contentLength;
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline
|
|
|
|
psycBool psyc_isParseContentLengthFound (psycParseState *state)
|
|
|
|
{
|
|
|
|
return state->contentLengthFound;
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline
|
|
|
|
size_t psyc_getParseValueLength (psycParseState *state)
|
|
|
|
{
|
|
|
|
return state->valueLength;
|
|
|
|
}
|
|
|
|
|
2011-05-04 15:59:51 +00:00
|
|
|
static inline
|
|
|
|
psycBool psyc_isParseValueLengthFound (psycParseState *state)
|
|
|
|
{
|
|
|
|
return state->valueLengthFound;
|
|
|
|
}
|
|
|
|
|
2011-05-04 02:01:21 +00:00
|
|
|
static inline
|
|
|
|
size_t psyc_getParseCursor (psycParseState *state)
|
|
|
|
{
|
|
|
|
return state->cursor;
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline
|
|
|
|
size_t psyc_getParseBufferLength (psycParseState *state)
|
|
|
|
{
|
|
|
|
return state->buffer.length;
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline
|
|
|
|
size_t psyc_getParseRemainingLength (psycParseState *state)
|
|
|
|
{
|
|
|
|
return state->buffer.length - state->cursor;
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline
|
|
|
|
const char * psyc_getParseRemainingBuffer (psycParseState *state)
|
|
|
|
{
|
|
|
|
return state->buffer.ptr + state->cursor;
|
2011-05-03 20:56:54 +00:00
|
|
|
}
|
2011-04-18 08:09:35 +00:00
|
|
|
|
2011-04-20 12:52:40 +00:00
|
|
|
/**
|
|
|
|
* Parse PSYC packets.
|
|
|
|
*
|
2011-05-09 17:21:26 +00:00
|
|
|
* This function parses a full or partial PSYC packet while keeping parsing
|
|
|
|
* state in a state variable that you have to pass in every time, and returns
|
|
|
|
* whenever a modifier or the body is found. See psycParseRC for the possible
|
|
|
|
* return codes. When it returns oper, name & value will point to the respective
|
|
|
|
* parts of the buffer, no memory allocation is done.
|
2011-04-20 12:52:40 +00:00
|
|
|
*
|
2011-05-09 17:21:26 +00:00
|
|
|
* @param state An initialized psycParseState.
|
|
|
|
* @param oper In case of a modifier it will be set to the operator.
|
|
|
|
* @param name In case of a modifier it will point to the name,
|
|
|
|
* in case of the body it will point to the method.
|
|
|
|
* @param value In case of a modifier it will point to the value,
|
|
|
|
* in case of the body it will point to the data.
|
2011-04-19 20:31:43 +00:00
|
|
|
*/
|
2011-05-16 22:27:26 +00:00
|
|
|
#ifdef __INLINE_PSYC_PARSE
|
|
|
|
static inline
|
|
|
|
#endif
|
2011-05-04 02:01:21 +00:00
|
|
|
psycParseRC psyc_parse (psycParseState *state, char *oper,
|
|
|
|
psycString *name, psycString *value);
|
2011-04-19 17:41:25 +00:00
|
|
|
|
2011-04-19 20:31:43 +00:00
|
|
|
/**
|
2011-05-09 17:21:26 +00:00
|
|
|
* List parser.
|
|
|
|
*
|
|
|
|
* This function parses a _list modifier value and returns one element a time
|
|
|
|
* while keeping parsing state in a state variable that you have to pass in
|
|
|
|
* every time. When it returns elem will point to the next element in value, no
|
|
|
|
* memory allocation is done.
|
|
|
|
*
|
|
|
|
* @param state An initialized psycParseListState.
|
|
|
|
* @param value Contains the list to be parsed.
|
|
|
|
* @param elem It will point to the next element in the list.
|
2011-04-19 20:31:43 +00:00
|
|
|
*/
|
2011-05-16 22:27:26 +00:00
|
|
|
#ifdef __INLINE_PSYC_PARSE
|
|
|
|
static inline
|
|
|
|
#endif
|
2011-05-13 15:02:31 +00:00
|
|
|
psycParseListRC psyc_parseList (psycParseListState *state, psycString *elem);
|
2011-04-19 19:55:22 +00:00
|
|
|
|
2011-05-20 00:58:32 +00:00
|
|
|
static inline
|
|
|
|
psycBool psyc_parseNumber2 (const char *value, size_t len, ssize_t *n)
|
|
|
|
{
|
|
|
|
size_t c = 0;
|
|
|
|
uint8_t neg = 0;
|
|
|
|
|
|
|
|
if (!value)
|
|
|
|
return PSYC_FALSE;
|
|
|
|
|
|
|
|
if (value[0] == '-')
|
|
|
|
neg = ++c;
|
|
|
|
|
|
|
|
*n = 0;
|
|
|
|
while (c < len && value[c] >= '0' && value[c] <= '9')
|
|
|
|
*n = 10 * *n + (value[c++] - '0');
|
|
|
|
|
|
|
|
if (c != len)
|
|
|
|
return PSYC_FALSE;
|
|
|
|
|
|
|
|
if (neg)
|
|
|
|
*n = 0 - *n;
|
|
|
|
|
|
|
|
return PSYC_TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline
|
|
|
|
psycBool psyc_parseNumber (psycString *value, ssize_t *n)
|
|
|
|
{
|
|
|
|
return psyc_parseNumber2(value->ptr, value->length, n);
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline
|
|
|
|
psycBool psyc_parseTime2 (const char *value, size_t len, time_t *t)
|
|
|
|
{
|
|
|
|
return psyc_parseNumber2(value, len, t);
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline
|
|
|
|
psycBool psyc_parseTime (psycString *value, time_t *t)
|
|
|
|
{
|
|
|
|
return psyc_parseNumber2(value->ptr, value->length, t);
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline
|
|
|
|
psycBool psyc_parseDate2 (const char *value, size_t len, time_t *t)
|
|
|
|
{
|
|
|
|
if (psyc_parseNumber2(value, len, t)) {
|
|
|
|
*t += PSYC_EPOCH;
|
|
|
|
return PSYC_TRUE;
|
|
|
|
}
|
|
|
|
return PSYC_FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline
|
|
|
|
psycBool psyc_parseDate (psycString *value, time_t *t)
|
|
|
|
{
|
|
|
|
return psyc_parseDate2(value->ptr, value->length, t);
|
|
|
|
}
|
|
|
|
|
2011-05-09 10:42:42 +00:00
|
|
|
/** @} */ // end of parse group
|
2011-05-09 07:02:15 +00:00
|
|
|
|
|
|
|
#define PSYC_PARSE_H
|
|
|
|
#endif
|