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

functions for creating packet & modifier structs, rendering

This commit is contained in:
tg(x) 2011-04-25 14:20:13 +02:00
parent e047884a0d
commit 3afb723bf8
13 changed files with 267 additions and 99 deletions

View file

@ -97,66 +97,95 @@ typedef enum
typedef struct
{
size_t length;
const char* ptr;
} PSYC_Array;
const char *ptr;
} PSYC_String;
#define PSYC_C2ARRAY(string) { sizeof(string)-1, string }
/**
* Shortcut for creating a PSYC_String.
*
* @param memory Pointer to the buffer.
* @param length Length of that buffer.
*
* @return An instance of the PSYC_String struct.
*/
inline PSYC_String PSYC_newString (const char *str, size_t strlen);
#define PSYC_C2STR(string) {sizeof(string)-1, string}
#define PSYC_C2ARG(string) string, sizeof(string)-1
/* intermediate struct for a PSYC variable modification */
typedef struct
{
char oper; // not call it 'operator' as C++ may not like that..?
PSYC_Array name;
PSYC_Array value;
char oper; // not call it 'operator' as C++ may not like that..
PSYC_String name;
PSYC_String value;
PSYC_ModifierFlag flag;
} PSYC_Modifier;
typedef struct
{
size_t length;
PSYC_Modifier *ptr;
} PSYC_ModifierArray;
/* intermediate struct for a PSYC packet */
typedef struct
{
PSYC_Modifier** routing; // Routing header
PSYC_Modifier** entity; // Entitiy header
PSYC_Array method;
PSYC_Array data;
size_t length; /// Length of content part
PSYC_ModifierArray routing; ///< Routing header.
PSYC_ModifierArray entity; ///< Entitiy header.
PSYC_String method;
PSYC_String data;
size_t routingLength; ///< Length of routing part.
size_t contentLength; ///< Length of content part.
size_t length; ///< Total length of packet.
PSYC_PacketFlag flag;
} PSYC_Packet;
PSYC_Modifier PSYC_newModifier(char* oper, PSYC_Array* name, PSYC_Array* value, PSYC_ModifierFlag flag);
inline PSYC_Modifier PSYC_newModifier(char oper, PSYC_String *name, PSYC_String *value,
PSYC_ModifierFlag flag);
PSYC_Modifier PSYC_newModifier2(char* oper, char* name, size_t namelen, char* value, size_t valuelen, PSYC_ModifierFlag flag);
inline PSYC_Modifier PSYC_newModifier2(char oper,
const char *name, size_t namelen,
const char *value, size_t valuelen,
PSYC_ModifierFlag flag);
PSYC_Packet PSYC_newPacket(PSYC_Modifier** routing, PSYC_Modifier **entity, PSYC_Array* method, PSYC_Array* data, PSYC_PacketFlag flag);
inline PSYC_Packet PSYC_newPacket(PSYC_ModifierArray *routing,
PSYC_ModifierArray *entity,
PSYC_String *method, PSYC_String *data,
PSYC_PacketFlag flag);
PSYC_Packet PSYC_newPacket2(PSYC_Modifier** routing, PSYC_Modifier **entity, char* method, size_t methodlen, char* data, size_t datalen, PSYC_PacketFlag flag);
inline PSYC_Packet PSYC_newPacket2(PSYC_Modifier **routing, size_t routinglen,
PSYC_Modifier **entity, size_t entitylen,
const char *method, size_t methodlen,
const char *data, size_t datalen,
PSYC_PacketFlag flag);
/// Routing vars in alphabetical order.
extern const PSYC_Array PSYC_routingVars[];
extern const PSYC_String PSYC_routingVars[];
/// Number of routing vars.
extern const size_t PSYC_routingVarsNum;
/**
* Get the type of variable name.
*/
PSYC_Bool PSYC_isRoutingVar(const char* name, size_t len);
PSYC_Bool PSYC_isRoutingVar(const char *name, size_t len);
/**
* Get the type of variable name.
*/
PSYC_Type PSYC_getVarType(char* name, size_t len);
PSYC_Type PSYC_getVarType(char *name, size_t len);
/**
* Checks if long keyword string inherits from short keyword string.
*/
int PSYC_inherits(char* sho, size_t slen,
char* lon, size_t llen);
int PSYC_inherits(char *sho, size_t slen,
char *lon, size_t llen);
/**
* Checks if short keyword string matches long keyword string.
*/
int PSYC_matches(char* sho, size_t slen,
char* lon, size_t llen);
int PSYC_matches(char *sho, size_t slen,
char *lon, size_t llen);
/**
* Callback for PSYC_text() that produces a value for a match.
@ -167,8 +196,8 @@ int PSYC_matches(char* sho, size_t slen,
* number of bytes written. 0 is a legal return value. Should the
* callback return -1, PSYC_text leaves the original template text as is.
*/
typedef int (*PSYC_textCB)(uint8_t* match, size_t mlen,
uint8_t** buffer, size_t * blen);
typedef int (*PSYC_textCB)(uint8_t *match, size_t mlen,
uint8_t **buffer, size_t *blen);
/**
* Fills out text templates by asking a callback for content.
@ -183,9 +212,9 @@ typedef int (*PSYC_textCB)(uint8_t* match, size_t mlen,
*
* See also http://about.psyc.eu/psyctext
*/
int PSYC_text(uint8_t* template, size_t tlen,
uint8_t** buffer, size_t * blen,
PSYC_textCB lookupValue,
char* braceOpen, char* braceClose);
int PSYC_text(uint8_t *template, size_t tlen,
uint8_t **buffer, size_t *blen,
PSYC_textCB lookupValue,
char *braceOpen, char *braceClose);
#endif // PSYC_H

View file

@ -87,7 +87,7 @@ typedef struct
{
size_t cursor; ///< current position in buffer
size_t startc; ///< position where the parsing would be resumed
PSYC_Array buffer; ///< buffer with data to be parsed
PSYC_String buffer; ///< buffer with data to be parsed
uint8_t flags; ///< flags for the parser, see PSYC_ParseFlag
PSYC_Part part; ///< part of the packet being parsed currently
@ -105,23 +105,13 @@ typedef struct
{
size_t cursor; ///< current position in buffer
size_t startc; ///< line start position
PSYC_Array buffer;
PSYC_String buffer;
PSYC_ListType type; ///< list type
size_t elemParsed; ///< number of bytes parsed from the elem so far
size_t elemLength; ///< expected length of the elem
} PSYC_ParseListState;
/**
* Shortcut for creating an array.
*
* @param memory Pointer to the buffer.
* @param length Length of that buffer.
*
* @return An instance of the PSYC_Array struct.
*/
inline PSYC_Array PSYC_createArray (const char* memory, size_t length);
/**
* Initiates the state struct.
*
@ -144,9 +134,9 @@ inline void PSYC_initParseState2 (PSYC_ParseState* state, uint8_t flags);
*/
inline void PSYC_initParseListState (PSYC_ParseListState* state);
inline void PSYC_nextParseBuffer (PSYC_ParseState* state, PSYC_Array newBuf);
inline void PSYC_nextParseBuffer (PSYC_ParseState* state, PSYC_String newBuf);
inline void PSYC_nextParseListBuffer (PSYC_ParseListState* state, PSYC_Array newBuf);
inline void PSYC_nextParseListBuffer (PSYC_ParseListState* state, PSYC_String newBuf);
inline size_t PSYC_getContentLength (PSYC_ParseState* s);
@ -158,17 +148,17 @@ inline size_t PSYC_getContentLength (PSYC_ParseState* s);
* @param state An initialized PSYC_ParseState
* @param operator A pointer to a character. In case of a variable, it will
* be set to the operator of that variable
* @param name A pointer to a PSYC_Array. It will point to the name of
* @param name A pointer to a PSYC_String. 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
* @param value A pointer to a PSYC_String. It will point to the
* value/body the variable/method and its length will be set accordingly
*/
PSYC_ParseRC PSYC_parse(PSYC_ParseState* state, char* oper, PSYC_Array* name, PSYC_Array* value);
PSYC_ParseRC PSYC_parse(PSYC_ParseState* state, char* oper, PSYC_String* name, PSYC_String* value);
/**
* List value parser.
*/
PSYC_ParseListRC PSYC_parseList(PSYC_ParseListState* state, PSYC_Array *name, PSYC_Array* value, PSYC_Array* elem);
PSYC_ParseListRC PSYC_parseList(PSYC_ParseListState* state, PSYC_String *name, PSYC_String* value, PSYC_String* elem);
#endif // PSYC_PARSER_H

View file

@ -32,11 +32,14 @@ typedef struct
char buffer[]; ///< OMG a C99 feature! variable size buffer!
} PSYC_RenderState;
PSYC_RenderRC PSYC_render(PSYC_Packet *packet, char *buffer, size_t buflen);
/**
* Initiates the state struct.
*
* @param state Pointer to the state struct that should be initiated.
*/
/*
inline void PSYC_initRenderState (PSYC_RenderState* state);
int PSYC_renderModifier(PSYC_RenderState* render,
@ -47,5 +50,5 @@ int PSYC_renderModifier(PSYC_RenderState* render,
int PSYC_renderBody(PSYC_RenderState* render,
const char* method, size_t mlength,
const char* data, size_t dlength);
*/
#endif // PSYC_RENDER_H

View file

@ -10,6 +10,11 @@
# define PSYC_CONTENT_SIZE_THRESHOLD 444
#endif
/* beyond this a modifier value length must be provided */
#ifndef PSYC_MODIFIER_SIZE_THRESHOLD
# define PSYC_MODIFIER_SIZE_THRESHOLD 404
#endif
#define C_GLYPH_PACKET_DELIMITER '|'
#define S_GLYPH_PACKET_DELIMITER "|"
#define PSYC_PACKET_DELIMITER "\n|\n"