mirror of
git://git.psyc.eu/libpsyc
synced 2024-08-15 03:19:02 +00:00
moved types to psyc.h; added getVarType & isRoutingVar definitions; more doc
This commit is contained in:
parent
1b3d5704c0
commit
8e7236009a
3 changed files with 81 additions and 41 deletions
|
@ -1,9 +1,9 @@
|
||||||
/** @file psyc.h
|
/** @file psyc.h
|
||||||
*
|
*
|
||||||
* @brief Main psyc interface providing crucial functionality
|
* @brief Main PSYC interface providing crucial functionality.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
/** @mainpage Psyc Core Library
|
/** @mainpage PSYC Core Library
|
||||||
*
|
*
|
||||||
* @section intro_sec Introduction
|
* @section intro_sec Introduction
|
||||||
*
|
*
|
||||||
|
@ -12,21 +12,67 @@
|
||||||
* @section install_sec Installation
|
* @section install_sec Installation
|
||||||
*
|
*
|
||||||
* @subsection step1 Step 1: Opening the box
|
* @subsection step1 Step 1: Opening the box
|
||||||
*
|
*
|
||||||
* etc...
|
* etc...
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
#ifndef PSYC_H
|
||||||
|
# define PSYC_H
|
||||||
|
|
||||||
#include <stdint.h>
|
#include <stdint.h>
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
|
|
||||||
#define PSYC_EPOCH 1440444041 // 2015-08-24 21:20:41 CET (Monday)
|
#define PSYC_EPOCH 1440444041 // 2015-08-24 21:20:41 CET (Monday)
|
||||||
|
|
||||||
/** @brief Checks if short keyword string matches long keyword string
|
typedef enum
|
||||||
|
{
|
||||||
|
PSYC_FALSE = 0,
|
||||||
|
PSYC_TRUE = 1,
|
||||||
|
} PSYC_Bool;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Different types that a variable can have.
|
||||||
|
*
|
||||||
|
* This enum lists PSYC variable types that
|
||||||
|
* this library is capable of checking for
|
||||||
|
* validity. Other variable types are treated
|
||||||
|
* as opaque data.
|
||||||
|
*/
|
||||||
|
typedef enum
|
||||||
|
{
|
||||||
|
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,
|
||||||
|
} PSYC_Type;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get the type of variable name.
|
||||||
|
*/
|
||||||
|
PSYC_Type PSYC_getVarType(char* name, size_t len);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get the type of variable name.
|
||||||
|
*/
|
||||||
|
PSYC_Bool PSYC_isRoutingVar(char* name, size_t len);
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Checks if short keyword string matches long keyword string.
|
||||||
*/
|
*/
|
||||||
int PSYC_matches(char* sho, size_t slen,
|
int PSYC_matches(char* sho, size_t slen,
|
||||||
char* lon, size_t llen);
|
char* lon, size_t llen);
|
||||||
|
|
||||||
/** @brief Callback for PSYC_text() that produces a value for a match
|
/**
|
||||||
|
* Callback for PSYC_text() that produces a value for a match.
|
||||||
*
|
*
|
||||||
* The application looks up a match such as _fruit from [_fruit] and
|
* The application looks up a match such as _fruit from [_fruit] and
|
||||||
* if found writes its current value from its variable store into the
|
* if found writes its current value from its variable store into the
|
||||||
|
@ -37,7 +83,8 @@ int PSYC_matches(char* sho, size_t slen,
|
||||||
typedef int (*PSYC_textCB)(uint8_t* match, size_t mlen,
|
typedef int (*PSYC_textCB)(uint8_t* match, size_t mlen,
|
||||||
uint8_t** buffer, size_t * blen);
|
uint8_t** buffer, size_t * blen);
|
||||||
|
|
||||||
/** @brief Fills out text templates by asking a callback for content
|
/**
|
||||||
|
* Fills out text templates by asking a callback for content.
|
||||||
*
|
*
|
||||||
* Copies the contents of the template into the buffer while looking
|
* Copies the contents of the template into the buffer while looking
|
||||||
* for braceOpen and braceClose strings and calling the callback for
|
* for braceOpen and braceClose strings and calling the callback for
|
||||||
|
@ -54,3 +101,4 @@ int PSYC_text(uint8_t* template, size_t tlen,
|
||||||
PSYC_textCB lookupValue,
|
PSYC_textCB lookupValue,
|
||||||
char* braceOpen, char* braceClose);
|
char* braceOpen, char* braceClose);
|
||||||
|
|
||||||
|
#endif // PSYC_H
|
||||||
|
|
|
@ -19,29 +19,6 @@
|
||||||
#include <stdint.h>
|
#include <stdint.h>
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
|
|
||||||
/**
|
|
||||||
* Different types that a variable can have.
|
|
||||||
*
|
|
||||||
* This enum lists PSYC variable types that
|
|
||||||
* this library is capable of checking for
|
|
||||||
* validity. Other variable types are treated
|
|
||||||
* as opaque data.
|
|
||||||
*/
|
|
||||||
typedef enum {
|
|
||||||
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,
|
|
||||||
} PSYC_Type;
|
|
||||||
|
|
||||||
typedef enum
|
typedef enum
|
||||||
{
|
{
|
||||||
PSYC_HEADER_ONLY = 1,
|
PSYC_HEADER_ONLY = 1,
|
||||||
|
@ -61,12 +38,18 @@ typedef enum
|
||||||
PSYC_ERROR_LENGTH = -2,
|
PSYC_ERROR_LENGTH = -2,
|
||||||
PSYC_ERROR = -1,
|
PSYC_ERROR = -1,
|
||||||
PSYC_SUCCESS = 0,
|
PSYC_SUCCESS = 0,
|
||||||
|
/// 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_INSUFFICIENT = 1,
|
PSYC_INSUFFICIENT = 1,
|
||||||
/// Routing variable parsing done. Modifier, name & value contains the respective parts.
|
/// Routing variable parsing done.
|
||||||
|
/// Modifier, name & value contains the respective parts.
|
||||||
PSYC_ROUTING = 2,
|
PSYC_ROUTING = 2,
|
||||||
/// Entity variable parsing done. Modifier, name & value contains the respective parts.
|
/// Entity variable parsing done.
|
||||||
|
/// Modifier, name & value contains the respective parts.
|
||||||
PSYC_ENTITY = 3,
|
PSYC_ENTITY = 3,
|
||||||
/// Entity variable parsing is incomplete. Modifier & name are complete, value is incomplete.
|
/// Entity variable parsing is incomplete.
|
||||||
|
/// Modifier & name are complete, value is incomplete.
|
||||||
PSYC_ENTITY_INCOMPLETE = 4,
|
PSYC_ENTITY_INCOMPLETE = 4,
|
||||||
/// Body parsing done, name contains method, value contains body.
|
/// Body parsing done, name contains method, value contains body.
|
||||||
PSYC_BODY = 5,
|
PSYC_BODY = 5,
|
||||||
|
@ -74,7 +57,7 @@ typedef enum
|
||||||
PSYC_BODY_INCOMPLETE = 6,
|
PSYC_BODY_INCOMPLETE = 6,
|
||||||
/// Reached end of packet, parsing done.
|
/// Reached end of packet, parsing done.
|
||||||
PSYC_COMPLETE = 7,
|
PSYC_COMPLETE = 7,
|
||||||
// Binary value parsing incomplete, used internally.
|
/// Binary value parsing incomplete, used internally.
|
||||||
PSYC_INCOMPLETE = 8,
|
PSYC_INCOMPLETE = 8,
|
||||||
} PSYC_ReturnCode;
|
} PSYC_ReturnCode;
|
||||||
|
|
||||||
|
@ -89,8 +72,11 @@ typedef enum
|
||||||
PSYC_ERROR_LIST_TYPE = -3,
|
PSYC_ERROR_LIST_TYPE = -3,
|
||||||
PSYC_ERROR_LIST_NAME = -2,
|
PSYC_ERROR_LIST_NAME = -2,
|
||||||
PSYC_ERROR_LIST= -1,
|
PSYC_ERROR_LIST= -1,
|
||||||
|
/// Completed parsing a list element.
|
||||||
PSYC_LIST_ELEM = 1,
|
PSYC_LIST_ELEM = 1,
|
||||||
|
/// Reached end of buffer.
|
||||||
PSYC_LIST_END = 2,
|
PSYC_LIST_END = 2,
|
||||||
|
/// Binary list is incomplete.
|
||||||
PSYC_LIST_INCOMPLETE = 3,
|
PSYC_LIST_INCOMPLETE = 3,
|
||||||
} PSYC_ListReturnCode;
|
} PSYC_ListReturnCode;
|
||||||
|
|
||||||
|
@ -130,14 +116,14 @@ typedef struct
|
||||||
typedef struct
|
typedef struct
|
||||||
{
|
{
|
||||||
size_t cursor; ///< current position in buffer
|
size_t cursor; ///< current position in buffer
|
||||||
size_t startc; ///< line start position
|
size_t startc; ///< position where the parsing would be resumed
|
||||||
PSYC_Array buffer;
|
PSYC_Array buffer;
|
||||||
uint8_t flags;
|
uint8_t flags; ///< flags for the parser, see PSYC_Flag
|
||||||
PSYC_Part part; ///< part of the packet being parsed currently
|
PSYC_Part part; ///< part of the packet being parsed currently
|
||||||
|
|
||||||
size_t contentParsed; ///< number of bytes parsed from the content so far
|
size_t contentParsed; ///< number of bytes parsed from the content so far
|
||||||
size_t contentLength; ///< expected length of the content
|
size_t contentLength; ///< expected length of the content
|
||||||
char contentLengthFound; ///< is there a length given for this packet?
|
PSYC_Bool contentLengthFound; ///< is there a length given for this packet?
|
||||||
size_t valueParsed; ///< number of bytes parsed from the value so far
|
size_t valueParsed; ///< number of bytes parsed from the value so far
|
||||||
size_t valueLength; ///< expected length of the value
|
size_t valueLength; ///< expected length of the value
|
||||||
} PSYC_State;
|
} PSYC_State;
|
||||||
|
@ -175,7 +161,7 @@ inline PSYC_Array PSYC_createArray (uint8_t* const memory, size_t length)
|
||||||
* Initiates the state struct with flags.
|
* Initiates the state struct with flags.
|
||||||
*
|
*
|
||||||
* @param state Pointer to the state struct that should be initiated.
|
* @param state Pointer to the state struct that should be initiated.
|
||||||
* @param flags The flags that one ones to set, see PSYC_Flag.
|
* @param flags Flags to be set for the parser, see PSYC_Flag.
|
||||||
*/
|
*/
|
||||||
inline void PSYC_initState2 (PSYC_State* state, uint8_t flags )
|
inline void PSYC_initState2 (PSYC_State* state, uint8_t flags )
|
||||||
{
|
{
|
||||||
|
|
14
src/parser.c
14
src/parser.c
|
@ -5,6 +5,7 @@
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
#include <psyc/lib.h>
|
||||||
#include <psyc/parser.h>
|
#include <psyc/parser.h>
|
||||||
|
|
||||||
#define ADVANCE_CURSOR_OR_RETURN(ret) \
|
#define ADVANCE_CURSOR_OR_RETURN(ret) \
|
||||||
|
@ -82,8 +83,13 @@ inline PSYC_ReturnCode PSYC_parseName(PSYC_State* state, PSYC_Array* name)
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Parse binary data into value.
|
* Parse binary data.
|
||||||
* length is the expected length of the data, parsed is the number of bytes parsed so far
|
*
|
||||||
|
* @param state Parser state.
|
||||||
|
* @param value Start & length of parsed data is saved here.
|
||||||
|
* @param length Expected length of the data.
|
||||||
|
* @param parsed Number of bytes parsed so far.
|
||||||
|
*
|
||||||
* @return PSYC_COMPLETE or PSYC_INCOMPLETE
|
* @return PSYC_COMPLETE or PSYC_INCOMPLETE
|
||||||
*/
|
*/
|
||||||
inline PSYC_ReturnCode PSYC_parseBinaryValue(PSYC_State* state, PSYC_Array* value, size_t* length, size_t* parsed)
|
inline PSYC_ReturnCode PSYC_parseBinaryValue(PSYC_State* state, PSYC_Array* value, size_t* length, size_t* parsed)
|
||||||
|
@ -118,14 +124,14 @@ inline PSYC_ReturnCode PSYC_parseVar(PSYC_State* state, uint8_t* modifier, PSYC_
|
||||||
return PSYC_ERROR_VAR_NAME;
|
return PSYC_ERROR_VAR_NAME;
|
||||||
|
|
||||||
value->length = 0;
|
value->length = 0;
|
||||||
|
state->valueLength = 0;
|
||||||
|
state->valueParsed = 0;
|
||||||
|
|
||||||
// Parse the value.
|
// Parse the value.
|
||||||
// If we're in the content part check if it's a binary var.
|
// If we're in the content part check if it's a binary var.
|
||||||
if (state->part == PSYC_PART_CONTENT && state->buffer.ptr[state->cursor] == ' ') // binary arg
|
if (state->part == PSYC_PART_CONTENT && state->buffer.ptr[state->cursor] == ' ') // binary arg
|
||||||
{ // After SP the length follows.
|
{ // After SP the length follows.
|
||||||
ADVANCE_CURSOR_OR_RETURN(PSYC_INSUFFICIENT);
|
ADVANCE_CURSOR_OR_RETURN(PSYC_INSUFFICIENT);
|
||||||
state->valueLength = 0;
|
|
||||||
state->valueParsed = 0;
|
|
||||||
|
|
||||||
if (isNumeric(state->buffer.ptr[state->cursor]))
|
if (isNumeric(state->buffer.ptr[state->cursor]))
|
||||||
{
|
{
|
||||||
|
|
Loading…
Reference in a new issue