1
0
Fork 0
mirror of git://git.psyc.eu/libpsyc synced 2024-08-15 03:19:02 +00:00
libpsyc/include/psyc/parse.h

874 lines
26 KiB
C
Raw Normal View History

2013-07-16 14:53:51 +00:00
/*
This file is part of libpsyc.
Copyright (C) 2011,2012 Carlo v. Loesch, Gabor X Toth, Mathias L. Baumann,
and other contributing authors.
libpsyc is free software: you can redistribute it and/or modify it under the
terms of the GNU Affero General Public License as published by the Free
Software Foundation, either version 3 of the License, or (at your option) any
later version. As a special exception, libpsyc is distributed with additional
permissions to link libpsyc libraries with non-AGPL works.
libpsyc is distributed in the hope that it will be useful, but WITHOUT
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for more
details.
You should have received a copy of the GNU Affero General Public License and
the linking exception along with libpsyc in a COPYING file.
*/
2011-05-09 07:02:15 +00:00
#ifndef PSYC_PARSE_H
2011-11-11 21:18:24 +00:00
#define PSYC_PARSE_H
2011-05-09 07:02:15 +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-04-19 20:27:38 +00:00
/**
2011-05-09 10:42:42 +00:00
* @defgroup parse Parsing Functions
*
2011-05-08 23:36:57 +00:00
* This module contains packet and list parsing functions.
2011-10-31 19:04:16 +00:00
* The parser adheres to the definition of a packet found at
*
2011-05-09 00:07:13 +00:00
* 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
2011-10-31 19:26:47 +00:00
* PsycParseState state;
2011-10-31 19:04:16 +00:00
* psyc_parse_state_init(&state, flags);
2011-05-08 23:36:57 +00:00
* @endcode
*
2011-10-31 19:04:16 +00:00
* With the flags parameter you can fine-tune what
2011-10-31 19:26:47 +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-11-14 21:02:02 +00:00
* // state is our initialized state from before
* psyc_parse_buffer_set(&state, raw_data, raw_len);
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
2011-10-31 19:26:47 +00:00
* PsycString name, // Name of the variable or method
2011-05-08 23:36:57 +00:00
* value; // Value of the variable or body
* char oper; // operator of the variable (if any)
* @endcode
*
* 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
* 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
*
* 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:
2011-10-31 19:04:16 +00:00
* printf("Variable: %.*s Value: %.*s Operator: %c\n",
2011-11-01 11:06:58 +00:00
* name.length, name.data,
* value.length, value.data,
2011-05-08 23:49:04 +00:00
* oper);
2011-11-01 11:06:58 +00:00
* // Note that the .data member still points at your original buffer. If
2011-05-08 23:36:57 +00:00
* // 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.
2011-10-31 19:04:16 +00:00
* printf("Method Name: %.*s Body: %.*s\n",
2011-11-01 11:06:58 +00:00
* name.length, name.data, // name of the method
* value.length, value.data); // 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
* // PSYC_PARSE_INSUFFICIENT code which means the line is incomplete.
2011-05-08 23:36:57 +00:00
* continue;
2011-10-31 19:04:16 +00:00
* default: //
2011-05-08 23:36:57 +00:00
* perror("Error %i happened :(\n", res);
* return res;
* }
2011-10-31 19:04:16 +00:00
* }
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
2011-10-31 19:26:47 +00:00
* 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-11-30 12:51:50 +00:00
#include "../psyc.h"
2011-04-15 23:42:36 +00:00
1984-04-04 00:44:05 +00:00
/** Options for selective parsing. */
2011-10-13 22:29:32 +00:00
typedef enum {
2011-11-11 21:18:24 +00:00
/// Default Flag. Parse everything.
PSYC_PARSE_ALL = 0,
/// Parse only the header
PSYC_PARSE_ROUTING_ONLY = 1,
/// Parse only the content.
/// Parsing starts at the content and the content must be complete.
PSYC_PARSE_START_AT_CONTENT = 2,
2011-10-31 19:26:47 +00:00
} PsycParseFlag;
2011-04-17 10:56:24 +00:00
/**
* The return value definitions for the packet parsing function.
* @see psyc_parse()
2011-04-19 20:57:49 +00:00
*/
2011-10-13 22:29:32 +00:00
typedef enum {
/// Error, no length is set for a modifier which is longer than PSYC_MODIFIER_SIZE_THRESHOLD.
PSYC_PARSE_ERROR_MOD_NO_LEN = -10,
/// Error, no length is set for the content but it is longer than PSYC_CONTENT_SIZE_THRESHOLD.
PSYC_PARSE_ERROR_NO_LEN = -9,
2011-11-11 21:18:24 +00:00
/// Error, packet is not ending with a valid delimiter.
PSYC_PARSE_ERROR_END = -8,
/// Error, expected NL after the method.
PSYC_PARSE_ERROR_METHOD = -7,
/// Error, expected NL after a modifier.
PSYC_PARSE_ERROR_MOD_NL = -6,
/// Error, modifier length is not numeric.
PSYC_PARSE_ERROR_MOD_LEN = -5,
/// Error, expected TAB before modifier value.
PSYC_PARSE_ERROR_MOD_TAB = -4,
/// Error, modifier name is missing.
PSYC_PARSE_ERROR_MOD_NAME = -3,
/// Error, expected NL after the content length.
PSYC_PARSE_ERROR_LENGTH = -2,
/// Error in packet.
PSYC_PARSE_ERROR = -1,
/// 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.
PSYC_PARSE_INSUFFICIENT = 1,
/// Routing modifier parsing done.
/// Operator, name & value contains the respective parts.
PSYC_PARSE_ROUTING = 2,
/// State sync operation.
PSYC_PARSE_STATE_RESYNC = 3,
/// State reset operation.
PSYC_PARSE_STATE_RESET = 4,
/// Start of an incomplete entity modifier.
/// Operator & name are complete, value is incomplete.
PSYC_PARSE_ENTITY_START = 5,
/// Continuation of an incomplete entity modifier.
PSYC_PARSE_ENTITY_CONT = 6,
/// End of an incomplete entity modifier.
PSYC_PARSE_ENTITY_END = 7,
/// Entity modifier parsing done in one go.
/// Operator, name & value contains the respective parts.
PSYC_PARSE_ENTITY = 8,
/// Start of an incomplete body.
/// Name contains method, value contains part of the body.
/// Used when packet length is given
PSYC_PARSE_BODY_START = 9,
/// Continuation of an incomplete body.
/// Used when packet length is given
PSYC_PARSE_BODY_CONT = 10,
/// End of an incomplete body.
/// Used when packet length is given
PSYC_PARSE_BODY_END = 11,
/// Body parsing done in one go, name contains method, value contains body.
PSYC_PARSE_BODY = 12,
/// Start of an incomplete content, value contains part of content.
/// Used when PSYC_PARSE_ROUTING_ONLY is set.
PSYC_PARSE_CONTENT_START = 9,
/// Continuation of an incomplete content.
/// Used when PSYC_PARSE_ROUTING_ONLY is set.
PSYC_PARSE_CONTENT_CONT = 10,
/// End of an incomplete content.
/// Used when PSYC_PARSE_ROUTING_ONLY is set.
PSYC_PARSE_CONTENT_END = 11,
/// Content parsing done in one go, value contains the whole content.
/// Used when PSYC_PARSE_ROUTING_ONLY is set.
PSYC_PARSE_CONTENT = 12,
/// Finished parsing packet.
PSYC_PARSE_COMPLETE = 13,
2011-10-31 19:26:47 +00:00
} PsycParseRC;
1984-04-04 00:44:05 +00:00
/** PSYC packet parts. */
typedef enum {
PSYC_PART_RESET = -1,
PSYC_PART_ROUTING = 0,
PSYC_PART_LENGTH = 1,
PSYC_PART_CONTENT = 2,
PSYC_PART_METHOD = 3,
PSYC_PART_DATA = 4,
PSYC_PART_END = 5,
} PsycPart;
/**
* The return value definitions for the list parsing function.
2011-10-31 19:04:16 +00:00
* @see psyc_parse_list()
*/
2011-10-13 22:29:32 +00:00
typedef enum {
/// Error, no length is set for an element which is longer than PSYC_ELEM_SIZE_THRESHOLD.
PSYC_PARSE_LIST_ERROR_ELEM_NO_LEN = -6,
PSYC_PARSE_LIST_ERROR_ELEM_LENGTH = -5,
PSYC_PARSE_LIST_ERROR_ELEM_TYPE = -4,
PSYC_PARSE_LIST_ERROR_ELEM_START = -3,
2011-11-11 21:18:24 +00:00
PSYC_PARSE_LIST_ERROR_TYPE = -2,
PSYC_PARSE_LIST_ERROR = -1,
/// Reached end of buffer.
/// 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.
PSYC_PARSE_LIST_INSUFFICIENT = 1,
/// Completed parsing the default type of the list.
PSYC_PARSE_LIST_TYPE = 2,
/// Start of an element is parsed but still not complete.
PSYC_PARSE_LIST_ELEM_START = 3,
/// Continuation of an incomplete element.
PSYC_PARSE_LIST_ELEM_CONT = 4,
/// Element parsing completed.
PSYC_PARSE_LIST_ELEM_END = 5,
2011-11-11 21:18:24 +00:00
/// Completed parsing a list element.
PSYC_PARSE_LIST_ELEM = 6,
/// Completed parsing the last element in the list.
PSYC_PARSE_LIST_ELEM_LAST = 7,
2011-11-11 21:18:24 +00:00
/// Reached end of buffer.
PSYC_PARSE_LIST_END = 8,
2011-10-31 19:26:47 +00:00
} PsycParseListRC;
2011-04-15 23:42:36 +00:00
2011-11-14 21:02:02 +00:00
typedef enum {
PSYC_LIST_PART_START = 0,
PSYC_LIST_PART_TYPE = 1,
PSYC_LIST_PART_ELEM_START = 2,
PSYC_LIST_PART_ELEM_TYPE = 3,
PSYC_LIST_PART_ELEM_LENGTH = 4,
PSYC_LIST_PART_ELEM = 5,
} PsycListPart;
typedef enum {
PSYC_PARSE_DICT_ERROR_VALUE = -9,
PSYC_PARSE_DICT_ERROR_VALUE_LENGTH = -8,
PSYC_PARSE_DICT_ERROR_VALUE_TYPE = -7,
PSYC_PARSE_DICT_ERROR_VALUE_START = -6,
PSYC_PARSE_DICT_ERROR_KEY = -5,
PSYC_PARSE_DICT_ERROR_KEY_LENGTH = -4,
PSYC_PARSE_DICT_ERROR_KEY_START = -3,
PSYC_PARSE_DICT_ERROR_TYPE = -2,
PSYC_PARSE_DICT_ERROR = -1,
/// Reached end of buffer.
/// 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.
PSYC_PARSE_DICT_INSUFFICIENT = 1,
/// Completed parsing the default type of the dict.
PSYC_PARSE_DICT_TYPE = 2,
/// Start of a key is parsed but still not complete.
PSYC_PARSE_DICT_KEY_START = 3,
/// Continuation of an incomplete key.
PSYC_PARSE_DICT_KEY_CONT = 4,
/// Last continuation of a key.
PSYC_PARSE_DICT_KEY_END = 5,
/// Completed parsing a key in one go.
PSYC_PARSE_DICT_KEY = 6,
/// Start of a value is parsed but still not complete.
PSYC_PARSE_DICT_VALUE_START = 7,
/// Continuation of an incomplete value.
PSYC_PARSE_DICT_VALUE_CONT = 8,
/// Last continuation of a value.
PSYC_PARSE_DICT_VALUE_END = 9,
2011-11-14 21:02:02 +00:00
/// Completed parsing a value.
PSYC_PARSE_DICT_VALUE = 10,
/// Completed parsing the last value.
PSYC_PARSE_DICT_VALUE_LAST = 11,
/// Reached end of buffer.
PSYC_PARSE_DICT_END = 12,
} PsycParseDictRC;
2011-11-14 21:02:02 +00:00
typedef enum {
PSYC_DICT_PART_START = 0,
PSYC_DICT_PART_TYPE = 1,
PSYC_DICT_PART_KEY_START = 2,
PSYC_DICT_PART_KEY_LENGTH = 3,
PSYC_DICT_PART_KEY = 4,
PSYC_DICT_PART_VALUE_START = 5,
PSYC_DICT_PART_VALUE_TYPE = 6,
PSYC_DICT_PART_VALUE_LENGTH = 7,
PSYC_DICT_PART_VALUE = 8,
} PsycDictPart;
typedef enum {
PSYC_PARSE_INDEX_ERROR_DICT = -6,
PSYC_PARSE_INDEX_ERROR_DICT_LENGTH = -5,
PSYC_PARSE_INDEX_ERROR_STRUCT = -4,
PSYC_PARSE_INDEX_ERROR_LIST = -3,
PSYC_PARSE_INDEX_ERROR_TYPE = -2,
PSYC_PARSE_INDEX_ERROR = -1,
/// Reached end of buffer.
/// 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.
PSYC_PARSE_INDEX_INSUFFICIENT = 1,
// Completed parsing a list index.
PSYC_PARSE_INDEX_LIST = 3,
// Completed parsing a list index at the end of the buffer.
PSYC_PARSE_INDEX_LIST_LAST = 4,
// Completed parsing a struct element name.
PSYC_PARSE_INDEX_STRUCT = 5,
// Completed parsing a struct element name at the end of the buffer.
PSYC_PARSE_INDEX_STRUCT_LAST = 6,
/// Start of a dict key is parsed but still not complete.
PSYC_PARSE_INDEX_DICT_START = 7,
/// Continuation of an incomplete dict key.
PSYC_PARSE_INDEX_DICT_CONT = 8,
/// Last continuation of a dict key.
PSYC_PARSE_INDEX_DICT_END = 9,
/// Completed parsing a dict key in one go.
PSYC_PARSE_INDEX_DICT = 10,
/// Reached end of buffer.
PSYC_PARSE_INDEX_END = 11,
} PsycParseIndexRC;
typedef enum {
PSYC_INDEX_PART_START = 0,
PSYC_INDEX_PART_TYPE = 1,
PSYC_INDEX_PART_LIST = 2,
PSYC_INDEX_PART_STRUCT = 3,
PSYC_INDEX_PART_DICT_LENGTH = 4,
PSYC_INDEX_PART_DICT = 5,
} PsycIndexPart;
typedef enum {
PSYC_PARSE_UPDATE_ERROR_VALUE = -24,
PSYC_PARSE_UPDATE_ERROR_LENGTH = -23,
PSYC_PARSE_UPDATE_ERROR_TYPE = -22,
PSYC_PARSE_UPDATE_ERROR_OPER = -21,
PSYC_PARSE_UPDATE_ERROR = -1,
/// Reached end of buffer.
/// 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.
PSYC_PARSE_UPDATE_INSUFFICIENT = 1,
// Completed parsing a list index.
PSYC_PARSE_UPDATE_INDEX_LIST = 3,
// Completed parsing a struct element name.
PSYC_PARSE_UPDATE_INDEX_STRUCT = 5,
/// Start of a dict key is parsed but still not complete.
PSYC_PARSE_UPDATE_INDEX_DICT_START = 7,
/// Continuation of an incomplete dict key.
PSYC_PARSE_UPDATE_INDEX_DICT_CONT = 8,
/// Last continuation of a dict key.
PSYC_PARSE_UPDATE_INDEX_DICT_END = 9,
/// Completed parsing a dict key in one go.
PSYC_PARSE_UPDATE_INDEX_DICT = 10,
/// Completed parsing the type.
PSYC_PARSE_UPDATE_TYPE = 21,
/// Completed parsing the type and reached end of buffer.
PSYC_PARSE_UPDATE_TYPE_END = 22,
/// Start of the value is parsed but still not complete.
PSYC_PARSE_UPDATE_VALUE_START = 23,
/// Continuation of incomplete value.
PSYC_PARSE_UPDATE_VALUE_CONT = 24,
/// Last continuation of the value.
PSYC_PARSE_UPDATE_VALUE_END = 25,
/// Completed parsing the value in one go.
PSYC_PARSE_UPDATE_VALUE = 26,
/// Reached end of buffer.
PSYC_PARSE_UPDATE_END = 27,
} PsycParseUpdateRC;
typedef enum {
PSYC_UPDATE_PART_START = 0,
PSYC_UPDATE_INDEX_PART_TYPE = 1,
PSYC_UPDATE_INDEX_PART_LIST = 2,
PSYC_UPDATE_INDEX_PART_STRUCT = 3,
PSYC_UPDATE_INDEX_PART_DICT_LENGTH = 4,
PSYC_UPDATE_INDEX_PART_DICT = 5,
PSYC_UPDATE_PART_TYPE = 12,
PSYC_UPDATE_PART_LENGTH = 13,
PSYC_UPDATE_PART_VALUE = 14,
} PsycUpdatePart;
2011-11-14 21:02:02 +00:00
/**
* Struct for keeping parser state.
*/
2011-10-13 22:29:32 +00:00
typedef struct {
PsycString buffer; ///< Buffer with data to be parsed.
2011-11-11 21:18:24 +00:00
size_t cursor; ///< Current position in buffer.
size_t startc; ///< Position where the parsing would be resumed.
size_t routinglen; ///< Length of routing part parsed so far.
size_t contentlen; ///< Expected length of the content.
size_t content_parsed; ///< Number of bytes parsed from the content so far.
size_t valuelen; ///< Expected length of the value.
size_t value_parsed; ///< Number of bytes parsed from the value so far.
PsycPart part; ///< Part of the packet being parsed currently.
uint8_t flags; ///< Flags for the parser, see PsycParseFlag.
uint8_t contentlen_found; ///< Is there a length given for this packet?
uint8_t valuelen_found; ///< Is there a length given for this modifier?
2011-10-31 19:26:47 +00:00
} PsycParseState;
2011-04-15 23:42:36 +00:00
/**
* Struct for keeping list parser state.
*/
2011-10-13 22:29:32 +00:00
typedef struct {
PsycString buffer; ///< Buffer with data to be parsed.
2011-11-11 21:18:24 +00:00
size_t cursor; ///< Current position in buffer.
size_t startc; ///< Line start position.
2011-04-19 17:41:25 +00:00
PsycString type; ///< List type.
size_t elemlen; ///< Expected length of the elem.
size_t elem_parsed; ///< Number of bytes parsed from the elem so far.
PsycListPart part; ///< Part of the list being parsed currently.
uint8_t elemlen_found; ///< Is there a length given for this element?
2011-10-31 19:26:47 +00:00
} PsycParseListState;
2011-04-19 17:41:25 +00:00
2011-11-14 21:02:02 +00:00
/**
* Struct for keeping dict parser state.
2011-11-14 21:02:02 +00:00
*/
typedef struct {
PsycString buffer; ///< Buffer with data to be parsed.
2011-11-14 21:02:02 +00:00
size_t cursor; ///< Current position in buffer.
size_t startc; ///< Line start position.
size_t elemlen; ///< Expected length of the key/value.
size_t elem_parsed; ///< Number of bytes parsed from the key/value so far.
PsycDictPart part; ///< Part of the dict being parsed currently.
uint8_t elemlen_found; ///< Is there a length given for this key/value?
} PsycParseDictState;
/**
* Struct for keeping index parser state.
*/
typedef struct {
PsycString buffer; ///< Buffer with data to be parsed.
size_t cursor; ///< Current position in buffer.
size_t startc; ///< Position where the parsing would be resumed.
size_t elemlen; ///< Expected length of an element.
size_t elem_parsed; ///< Number of bytes parsed from the elem so far.
PsycIndexPart part; ///< Part of the packet being parsed currently.
uint8_t elemlen_found; ///< Is there a length given for this element?
} PsycParseIndexState;
/**
* Struct for keeping update modifier parser state.
*/
typedef struct {
2011-11-14 21:02:02 +00:00
PsycString buffer; ///< Buffer with data to be parsed.
size_t cursor; ///< Current position in buffer.
size_t startc; ///< Position where the parsing would be resumed.
size_t elemlen; ///< Expected length of an element.
size_t elem_parsed; ///< Number of bytes parsed from the elem so far.
PsycUpdatePart part; ///< Part of the packet being parsed currently.
uint8_t elemlen_found; ///< Is there a length given for this element?
} PsycParseUpdateState;
2011-11-14 21:02:02 +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-10-31 19:26:47 +00:00
* @param flags Flags to be set for the parser, see PsycParseFlag.
* @see PsycParseFlag
2011-04-19 17:41:25 +00:00
*/
inline void
2011-11-11 21:18:24 +00:00
psyc_parse_state_init (PsycParseState *state, uint8_t flags)
{
2011-11-11 21:18:24 +00:00
memset(state, 0, sizeof(PsycParseState));
state->flags = flags;
2011-11-11 21:18:24 +00:00
if (flags & PSYC_PARSE_START_AT_CONTENT)
state->part = PSYC_PART_CONTENT;
}
2011-04-15 23:42:36 +00:00
/**
* Sets a new buffer in the parser state struct with data to be parsed.
*
* This function does NOT copy the buffer. It will parse whatever is
* at the memory pointed to by buffer.
*
* @param state Pointer to the initialized state of the parser
2011-10-31 19:04:16 +00:00
* @param buffer pointer to the data that should be parsed
* @param length length of the data in bytes
2011-10-31 19:26:47 +00:00
* @see PsycString
*/
inline void
2011-12-01 13:12:41 +00:00
psyc_parse_buffer_set (PsycParseState *state, const char *buffer, size_t length)
{
2011-12-01 13:12:41 +00:00
state->buffer = (PsycString) {length, (char*)buffer};
2011-11-11 21:18:24 +00:00
state->cursor = 0;
2011-11-11 21:18:24 +00:00
if (state->flags & PSYC_PARSE_START_AT_CONTENT) {
state->contentlen = length;
state->contentlen_found = PSYC_TRUE;
2011-11-11 21:18:24 +00:00
}
}
2011-04-19 19:54:44 +00:00
/**
* Initializes the list parser state.
2011-04-19 17:41:25 +00:00
*/
inline void
2011-11-11 21:18:24 +00:00
psyc_parse_list_state_init (PsycParseListState *state)
{
2011-11-11 21:18:24 +00:00
memset(state, 0, sizeof(PsycParseListState));
}
2011-04-19 17:41:25 +00:00
/**
* Sets a new buffer in the list parser state struct with data to be parsed.
*/
inline void
2011-12-01 13:12:41 +00:00
psyc_parse_list_buffer_set (PsycParseListState *state,
const char *buffer, size_t length)
{
2011-12-01 13:12:41 +00:00
state->buffer = (PsycString) {length, (char*)buffer};
2011-11-11 21:18:24 +00:00
state->cursor = 0;
}
2011-04-19 17:41:25 +00:00
/**
* Initializes the dict parser state.
*/
inline void
psyc_parse_dict_state_init (PsycParseDictState *state)
2011-11-14 21:02:02 +00:00
{
memset(state, 0, sizeof(PsycParseDictState));
2011-11-14 21:02:02 +00:00
}
/**
* Sets a new buffer in the dict parser state struct with data to be parsed.
2011-11-14 21:02:02 +00:00
*/
inline void
psyc_parse_dict_buffer_set (PsycParseDictState *state,
const char *buffer, size_t length)
2011-11-14 21:02:02 +00:00
{
state->buffer = (PsycString) {length, (char*)buffer};
state->cursor = 0;
2011-11-14 21:02:02 +00:00
}
/**
* Initializes the index parser state.
2011-11-14 21:02:02 +00:00
*/
inline void
psyc_parse_index_state_init (PsycParseIndexState *state)
{
memset(state, 0, sizeof(PsycParseIndexState));
}
/**
* Sets a new buffer in the index parser state struct with data to be parsed.
*/
inline void
psyc_parse_index_buffer_set (PsycParseIndexState *state,
const char *buffer, size_t length)
{
state->buffer = (PsycString) {length, (char*)buffer};
state->cursor = 0;
}
/**
* Initializes the update modifier parser state.
*/
inline void
psyc_parse_update_state_init (PsycParseUpdateState *state)
{
memset(state, 0, sizeof(PsycParseUpdateState));
}
/**
* Sets a new buffer in the update modifier parser state struct with data to be parsed.
*/
inline void
psyc_parse_update_buffer_set (PsycParseUpdateState *state,
2011-12-01 13:12:41 +00:00
const char *buffer, size_t length)
2011-11-14 21:02:02 +00:00
{
2011-12-01 13:12:41 +00:00
state->buffer = (PsycString) {length, (char*)buffer};
2011-11-14 21:02:02 +00:00
state->cursor = 0;
}
inline size_t
2011-11-11 21:18:24 +00:00
psyc_parse_content_length (PsycParseState *state)
{
return state->contentlen;
}
inline PsycBool
2011-11-11 21:18:24 +00:00
psyc_parse_content_length_found (PsycParseState *state)
{
2012-02-21 02:00:52 +00:00
return (PsycBool) state->contentlen_found;
}
inline size_t
2011-11-11 21:18:24 +00:00
psyc_parse_value_length (PsycParseState *state)
{
return state->valuelen;
}
inline PsycBool
2011-11-11 21:18:24 +00:00
psyc_parse_value_length_found (PsycParseState *state)
{
2012-02-21 02:00:52 +00:00
return (PsycBool) state->valuelen_found;
}
inline size_t
2011-11-11 21:18:24 +00:00
psyc_parse_cursor (PsycParseState *state)
{
2011-11-11 21:18:24 +00:00
return state->cursor;
}
inline size_t
2011-11-11 21:18:24 +00:00
psyc_parse_buffer_length (PsycParseState *state)
{
2011-11-11 21:18:24 +00:00
return state->buffer.length;
}
inline size_t
2011-11-11 21:18:24 +00:00
psyc_parse_remaining_length (PsycParseState *state)
{
2011-11-11 21:18:24 +00:00
return state->buffer.length - state->cursor;
}
inline const char *
2011-11-11 21:18:24 +00:00
psyc_parse_remaining_buffer (PsycParseState *state)
{
2011-11-11 21:18:24 +00:00
return state->buffer.data + state->cursor;
}
2011-04-18 08:09:35 +00:00
/**
* Parse PSYC packets.
*
* 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
2011-10-31 19:26:47 +00:00
* 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-10-31 19:26:47 +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
*/
#ifdef __INLINE_PSYC_PARSE
extern inline
#endif
2011-11-11 21:18:24 +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
/**
* 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.
*
2011-10-31 19:26:47 +00:00
* @param state An initialized PsycParseListState.
* @param elem It will point to the next element in the list.
2011-04-19 20:31:43 +00:00
*/
#ifdef __INLINE_PSYC_PARSE
extern inline
#endif
2011-11-11 21:18:24 +00:00
PsycParseListRC
psyc_parse_list (PsycParseListState *state, PsycString *type, PsycString *elem);
2011-04-19 19:55:22 +00:00
#ifdef __INLINE_PSYC_PARSE
extern inline
#endif
PsycParseDictRC
psyc_parse_dict (PsycParseDictState *state, PsycString *type, PsycString *elem);
#ifdef __INLINE_PSYC_PARSE
extern inline
#endif
PsycParseIndexRC
psyc_parse_index (PsycParseIndexState *state, PsycString *idx);
#ifdef __INLINE_PSYC_PARSE
extern inline
#endif
PsycParseUpdateRC
psyc_parse_update (PsycParseUpdateState *state, char *oper, PsycString *value);
2011-11-14 21:02:02 +00:00
inline size_t
psyc_parse_int (const char *value, size_t len, int64_t *n)
{
2011-11-11 21:18:24 +00:00
size_t c = 0;
uint8_t neg = 0;
2011-11-11 21:18:24 +00:00
if (!value)
return c;
2011-11-11 21:18:24 +00:00
if (value[0] == '-')
neg = ++c;
2011-11-11 21:18:24 +00:00
*n = 0;
while (c < len && value[c] >= '0' && value[c] <= '9')
*n = 10 * *n + (value[c++] - '0');
2011-11-11 21:18:24 +00:00
if (c != len)
return c;
2011-11-11 21:18:24 +00:00
if (neg)
*n = 0 - *n;
return c;
}
inline size_t
psyc_parse_uint (const char *value, size_t len, uint64_t *n)
2011-11-03 13:15:42 +00:00
{
2011-11-11 21:18:24 +00:00
size_t c = 0;
if (!value)
return c;
2011-11-03 13:15:42 +00:00
2011-11-11 21:18:24 +00:00
*n = 0;
while (c < len && value[c] >= '0' && value[c] <= '9')
*n = 10 * *n + (value[c++] - '0');
2011-11-03 13:15:42 +00:00
return c;
}
inline size_t
psyc_parse_list_index (const char *value, size_t len, int64_t *n)
{
if (!value || len == 0 || value[0] != '#')
return 0;
return psyc_parse_int(value + 1, len, n) + 1;
}
2011-10-13 22:29:32 +00:00
/**
* Determines if the argument is a glyph.
* Glyphs are: : = + - ? !
*/
inline PsycBool
2011-12-01 13:12:41 +00:00
psyc_is_oper (char g)
2011-10-13 22:29:32 +00:00
{
2011-11-11 21:18:24 +00:00
switch (g) {
case ':':
case '=':
case '+':
case '-':
case '@':
2011-11-11 21:18:24 +00:00
case '?':
case '!':
return PSYC_TRUE;
2011-11-11 21:18:24 +00:00
default:
return PSYC_FALSE;
2011-11-11 21:18:24 +00:00
}
2011-10-13 22:29:32 +00:00
}
/**
* Determines if the argument is numeric.
*/
inline char
psyc_is_numeric (char c)
2011-10-13 22:29:32 +00:00
{
2011-11-11 21:18:24 +00:00
return c >= '0' && c <= '9';
2011-10-13 22:29:32 +00:00
}
/**
* Determines if the argument is alphabetic.
*/
inline char
psyc_is_alpha (char c)
2011-10-13 22:29:32 +00:00
{
2011-11-11 21:18:24 +00:00
return (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z');
2011-10-13 22:29:32 +00:00
}
/**
* Determines if the argument is alphanumeric.
*/
inline char
psyc_is_alpha_numeric (char c)
2011-10-13 22:29:32 +00:00
{
2011-11-11 21:18:24 +00:00
return psyc_is_alpha(c) || psyc_is_numeric(c);
2011-10-13 22:29:32 +00:00
}
/**
* Determines if the argument is a keyword character.
* Keyword characters are: alphanumeric and _
*/
inline char
psyc_is_kw_char (char c)
2011-10-13 22:29:32 +00:00
{
2011-11-11 21:18:24 +00:00
return psyc_is_alpha_numeric(c) || c == '_';
2011-10-13 22:29:32 +00:00
}
/**
* Determines if the argument is a name character.
* Name characters are: see opaque_part in RFC 2396
*/
inline char
psyc_is_name_char (char c)
2011-10-13 22:29:32 +00:00
{
2011-11-11 21:18:24 +00:00
return psyc_is_alpha(c) || (c >= '$' && c <= ';')
|| c == '_' || c == '!' || c == '?' || c == '=' || c == '@' || c == '~';
2011-10-13 22:29:32 +00:00
}
/**
* Determines if the argument is a hostname character.
* Hostname characters are: alphanumeric and -
*/
inline char
psyc_is_host_char (char c)
2011-10-13 22:29:32 +00:00
{
2011-11-11 21:18:24 +00:00
return psyc_is_alpha_numeric(c) || c == '.' || c == '-';
2011-10-13 22:29:32 +00:00
}
/**
* Parse variable name or method name.
* It should contain one or more keyword characters.
* @return Number of characters parsed.
*/
inline size_t
psyc_parse_keyword (const char *data, size_t len)
{
size_t c = 0;
while (c < len && psyc_is_kw_char(data[c++]));
return c > 0 ? c - 1 : 0;
}
2011-05-09 10:42:42 +00:00
/** @} */ // end of parse group
2011-05-09 07:02:15 +00:00
#endif