libpsyc/include/psyc/parser.h

248 lines
5.8 KiB
C
Raw Normal View History

/**
2011-04-19 20:27:38 +00:00
* @file psyc/parser.h
* @brief Interface for various psyc parser functions.
2011-04-19 20:27:38 +00:00
*
* All parsing functions and the definitions they use are
2011-04-19 20:27:38 +00:00
* defined in this file.
*/
/**
* @defgroup parsing Parsing Functions
*
* This module contains all parsing functions.
* @{
2011-04-19 20:57:49 +00:00
*/
2011-04-19 20:01:51 +00:00
#ifndef PSYC_PARSER_H
# define PSYC_PARSER_H
2010-02-20 16:40:09 +00:00
#include <stdint.h>
2011-04-15 23:42:36 +00:00
#include <string.h>
/**
* Different types that a variable can have.
2011-04-19 20:57:49 +00:00
*
2011-04-20 13:36:56 +00:00
* This enum lists PSYC variable types that
* this library is capable of checking for
* validity. Other variable types are treated
* as opaque data.
2011-04-19 20:57:49 +00:00
*/
2011-04-20 13:36:56 +00:00
typedef enum {
2011-04-19 20:00:24 +00:00
PSYC_TYPE_AMOUNT,
PSYC_TYPE_COLOR,
PSYC_TYPE_DATE,
PSYC_TYPE_DEGREE,
PSYC_TYPE_ENTITY,
PSYC_TYPE_FLAG,
PSYC_TYPE_LANGUAGE,
PSYC_TYPE_LIST,
PSYC_TYPE_NICK,
PSYC_TYPE_PAGE,
PSYC_TYPE_UNIFORM,
PSYC_TYPE_TIME,
2011-04-20 13:36:56 +00:00
} PSYC_Type;
2011-04-19 20:00:24 +00:00
2011-04-20 14:28:15 +00:00
typedef enum
2011-04-17 10:56:24 +00:00
{
2011-04-19 20:31:43 +00:00
PSYC_HEADER_ONLY = 1,
2011-04-20 14:28:15 +00:00
} PSYC_Flag;
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-04-20 14:28:15 +00:00
typedef enum
2011-04-17 10:05:14 +00:00
{
PSYC_ERROR_END = -7,
PSYC_ERROR_METHOD = -6,
PSYC_ERROR_VAR_LEN = -5,
PSYC_ERROR_VAR_TAB = -4,
PSYC_ERROR_VAR_NAME = -3,
PSYC_ERROR_LENGTH = -2,
PSYC_ERROR = -1,
PSYC_SUCCESS = 0,
2011-04-20 14:28:15 +00:00
PSYC_INSUFFICIENT = 1,
/// Routing variable parsing done. Modifier, name & value contains the respective parts.
PSYC_ROUTING = 2,
/// Entity variable parsing done. Modifier, name & value contains the respective parts.
PSYC_ENTITY = 3,
/// Entity variable parsing is incomplete. Modifier & name are complete, value is incomplete.
PSYC_ENTITY_INCOMPLETE = 4,
/// Body parsing done, name contains method, value contains body.
PSYC_BODY = 5,
/// Body parsing is incomplete, name contains method, value contains part of the body.
PSYC_BODY_INCOMPLETE = 6,
/// Reached end of packet, parsing done.
PSYC_COMPLETE = 7,
// Binary value parsing incomplete, used internally.
PSYC_INCOMPLETE = 8,
} PSYC_ReturnCode;
/**
* The return value definitions for the list parsing function.
* @see PSYC_parseList()
*/
2011-04-20 14:28:15 +00:00
typedef enum
2011-04-19 17:41:25 +00:00
{
PSYC_ERROR_LIST_DELIM = -5,
PSYC_ERROR_LIST_LEN = -4,
PSYC_ERROR_LIST_TYPE = -3,
PSYC_ERROR_LIST_NAME = -2,
PSYC_ERROR_LIST= -1,
PSYC_LIST_ELEM = 1,
PSYC_LIST_END = 2,
PSYC_LIST_INCOMPLETE = 3,
2011-04-20 14:28:15 +00:00
} PSYC_ListReturnCode;
2011-04-19 17:41:25 +00:00
/**
* PSYC packet parts.
*/
2011-04-20 14:28:15 +00:00
typedef enum
{
PSYC_PART_RESET = -1,
PSYC_PART_HEADER = 0,
PSYC_PART_LENGTH,
PSYC_PART_CONTENT,
PSYC_PART_METHOD,
PSYC_PART_DATA,
PSYC_PART_END,
2011-04-20 14:28:15 +00:00
} PSYC_Part;
2011-04-17 10:05:14 +00:00
/**
* List types.
* Possible types are text and binary.
*/
2011-04-20 14:28:15 +00:00
typedef enum
2011-04-19 17:41:25 +00:00
{
PSYC_LIST_TEXT = 1,
PSYC_LIST_BINARY = 2,
2011-04-20 14:28:15 +00:00
} PSYC_ListType;
2011-04-15 23:42:36 +00:00
2011-04-19 17:41:25 +00:00
typedef struct
2011-04-15 23:42:36 +00:00
{
2011-04-19 10:26:51 +00:00
size_t length;
const uint8_t * ptr; // just an unsigned char for opaque data
2011-04-15 23:42:36 +00:00
} PSYC_Array;
/**
* Struct for keeping parser state.
*/
2011-04-19 17:41:25 +00:00
typedef struct
2011-04-15 23:42:36 +00:00
{
2011-04-19 19:54:44 +00:00
size_t cursor; ///< current position in buffer
size_t startc; ///< line start position
2011-04-15 23:42:36 +00:00
PSYC_Array buffer;
uint8_t flags;
2011-04-20 14:28:15 +00:00
PSYC_Part part; ///< part of the packet being parsed currently
2011-04-15 23:42:36 +00:00
2011-04-19 19:54:44 +00:00
size_t contentParsed; ///< number of bytes parsed from the content so far
size_t contentLength; ///< expected length of the content
char 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
2011-04-15 23:42:36 +00:00
} PSYC_State;
/**
* Struct for keeping list parser state.
*/
2011-04-19 17:41:25 +00:00
typedef struct
{
2011-04-19 19:54:44 +00:00
size_t cursor; ///< current position in buffer
size_t startc; ///< line start position
2011-04-19 17:41:25 +00:00
PSYC_Array buffer;
2011-04-20 14:28:15 +00:00
PSYC_ListType type; ///< list type
2011-04-19 17:41:25 +00:00
2011-04-19 19:54:44 +00:00
size_t elemParsed; ///< number of bytes parsed from the elem so far
size_t elemLength; ///< expected length of the elem
2011-04-19 17:41:25 +00:00
} PSYC_ListState;
#ifndef PSYC_COMPILE_LIBRARY
2011-04-19 19:54:44 +00:00
/**
* Shortcut for creating an array.
2011-04-17 12:47:25 +00:00
*
2011-04-19 17:41:25 +00:00
* @param memory Pointer to the buffer.
* @param length Length of that buffer.
2011-04-17 12:47:25 +00:00
*
2011-04-19 19:54:44 +00:00
* @return An instance of the PSYC_Array struct.
*/
2011-04-19 10:26:51 +00:00
inline PSYC_Array PSYC_createArray (uint8_t* const memory, size_t length)
2011-04-15 23:42:36 +00:00
{
PSYC_Array arr = {length, memory};
return arr;
}
2011-04-18 08:09:35 +00:00
2011-04-19 19:54:44 +00:00
/**
* Initiates the state struct with flags.
2011-04-17 12:47:25 +00:00
*
2011-04-19 17:41:25 +00:00
* @param state Pointer to the state struct that should be initiated.
2011-04-20 14:28:15 +00:00
* @param flags The flags that one ones to set, see PSYC_Flag.
2011-04-19 17:41:25 +00:00
*/
2011-04-17 10:56:24 +00:00
inline void PSYC_initState2 (PSYC_State* state, uint8_t flags )
{
memset(state, 0, sizeof(PSYC_State));
state->flags = flags;
}
2011-04-19 19:54:44 +00:00
/**
* Initiates the state struct.
2011-04-18 08:09:35 +00:00
*
2011-04-19 17:41:25 +00:00
* @param state Pointer to the state struct that should be initiated.
*/
2011-04-15 23:42:36 +00:00
inline void PSYC_initState (PSYC_State* state)
{
memset(state, 0, sizeof(PSYC_State));
}
2011-04-19 19:54:44 +00:00
/**
* Initiates the list state struct.
2011-04-19 17:41:25 +00:00
*
* @param state Pointer to the list state struct that should be initiated.
*/
inline void PSYC_initListState (PSYC_ListState* state)
{
memset(state, 0, sizeof(PSYC_ListState));
}
2011-04-15 23:42:36 +00:00
inline void PSYC_nextBuffer (PSYC_State* state, PSYC_Array newBuf)
{
2011-04-19 17:41:25 +00:00
state->buffer = newBuf;
state->cursor = 0;
}
inline void PSYC_nextListBuffer (PSYC_ListState* state, PSYC_Array newBuf)
{
state->buffer = newBuf;
2011-04-18 08:09:35 +00:00
state->cursor = 0;
2011-04-15 23:42:36 +00:00
}
2011-04-19 10:26:51 +00:00
inline size_t PSYC_getContentLength (PSYC_State* s)
2011-04-15 23:42:36 +00:00
{
2011-04-18 08:09:35 +00:00
return s->contentLength;
2011-04-15 23:42:36 +00:00
}
2011-04-18 08:09:35 +00:00
#endif
2011-04-15 23:42:36 +00:00
/**
* Parse PSYC packets.
*
2011-04-19 20:57:49 +00:00
* Generalized line-based packet parser.
*
2011-04-19 20:57:49 +00:00
* @param state An initialized PSYC_State
* @param modifier A pointer to a character. In case of a variable, it will
* be set to the modifier of that variable
2011-04-19 20:57:49 +00:00
* @param name A pointer to a PSYC_Array. It will point to the name of
* the variable or method and its length will be set accordingly
* @param value A pointer to a PSYC_Array. It will point to the
* value/body the variable/method and its length will be set accordingly
2011-04-19 20:31:43 +00:00
*/
2011-04-20 14:28:15 +00:00
PSYC_ReturnCode PSYC_parse(PSYC_State* state, uint8_t* modifier, PSYC_Array* name, PSYC_Array* value);
2011-04-19 17:41:25 +00:00
2011-04-19 20:31:43 +00:00
/**
* List value parser.
*/
2011-04-20 14:28:15 +00:00
PSYC_ListReturnCode PSYC_parseList(PSYC_ListState* state, PSYC_Array *name, PSYC_Array* value, PSYC_Array* elem);
2011-04-19 19:55:22 +00:00
2011-04-19 20:01:51 +00:00
#endif // PSYC_PARSER_H
2011-04-19 20:57:49 +00:00
/** @} */ // end of parsing group