diff --git a/include/psyc.h b/include/psyc.h index a0bb5bb..c4e15bb 100644 --- a/include/psyc.h +++ b/include/psyc.h @@ -1,9 +1,9 @@ /** @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 * @@ -12,21 +12,67 @@ * @section install_sec Installation * * @subsection step1 Step 1: Opening the box - * + * * etc... */ +#ifndef PSYC_H +# define PSYC_H + #include #include #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, 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 * 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, 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 * 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, char* braceOpen, char* braceClose); +#endif // PSYC_H diff --git a/include/psyc/parser.h b/include/psyc/parser.h index f03f6b4..42a4d22 100644 --- a/include/psyc/parser.h +++ b/include/psyc/parser.h @@ -19,29 +19,6 @@ #include #include -/** - * 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 { PSYC_HEADER_ONLY = 1, @@ -61,12 +38,18 @@ typedef enum PSYC_ERROR_LENGTH = -2, PSYC_ERROR = -1, 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, -/// 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, -/// 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, -/// 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, /// Body parsing done, name contains method, value contains body. PSYC_BODY = 5, @@ -74,7 +57,7 @@ typedef enum PSYC_BODY_INCOMPLETE = 6, /// Reached end of packet, parsing done. PSYC_COMPLETE = 7, -// Binary value parsing incomplete, used internally. +/// Binary value parsing incomplete, used internally. PSYC_INCOMPLETE = 8, } PSYC_ReturnCode; @@ -89,8 +72,11 @@ typedef enum PSYC_ERROR_LIST_TYPE = -3, PSYC_ERROR_LIST_NAME = -2, PSYC_ERROR_LIST= -1, +/// Completed parsing a list element. PSYC_LIST_ELEM = 1, +/// Reached end of buffer. PSYC_LIST_END = 2, +/// Binary list is incomplete. PSYC_LIST_INCOMPLETE = 3, } PSYC_ListReturnCode; @@ -130,14 +116,14 @@ typedef struct typedef struct { 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; - uint8_t flags; + uint8_t flags; ///< flags for the parser, see PSYC_Flag PSYC_Part part; ///< part of the packet being parsed currently 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? + 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 valueLength; ///< expected length of the value } PSYC_State; @@ -175,7 +161,7 @@ inline PSYC_Array PSYC_createArray (uint8_t* const memory, size_t length) * Initiates the state struct with flags. * * @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 ) { diff --git a/src/parser.c b/src/parser.c index 8fa9ce2..5ce9aae 100644 --- a/src/parser.c +++ b/src/parser.c @@ -5,6 +5,7 @@ #include #endif +#include #include #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. - * length is the expected length of the data, parsed is the number of bytes parsed so far + * Parse binary data. + * + * @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 */ 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; value->length = 0; + state->valueLength = 0; + state->valueParsed = 0; // Parse the value. // 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 { // After SP the length follows. ADVANCE_CURSOR_OR_RETURN(PSYC_INSUFFICIENT); - state->valueLength = 0; - state->valueParsed = 0; if (isNumeric(state->buffer.ptr[state->cursor])) {