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:
parent
7db73bacda
commit
5a1e4825ad
13 changed files with 267 additions and 99 deletions
|
@ -97,66 +97,95 @@ typedef enum
|
||||||
typedef struct
|
typedef struct
|
||||||
{
|
{
|
||||||
size_t length;
|
size_t length;
|
||||||
const char* ptr;
|
const char *ptr;
|
||||||
} PSYC_Array;
|
} 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
|
#define PSYC_C2ARG(string) string, sizeof(string)-1
|
||||||
|
|
||||||
/* intermediate struct for a PSYC variable modification */
|
/* intermediate struct for a PSYC variable modification */
|
||||||
typedef struct
|
typedef struct
|
||||||
{
|
{
|
||||||
char oper; // not call it 'operator' as C++ may not like that..?
|
char oper; // not call it 'operator' as C++ may not like that..
|
||||||
PSYC_Array name;
|
PSYC_String name;
|
||||||
PSYC_Array value;
|
PSYC_String value;
|
||||||
PSYC_ModifierFlag flag;
|
PSYC_ModifierFlag flag;
|
||||||
} PSYC_Modifier;
|
} PSYC_Modifier;
|
||||||
|
|
||||||
|
typedef struct
|
||||||
|
{
|
||||||
|
size_t length;
|
||||||
|
PSYC_Modifier *ptr;
|
||||||
|
} PSYC_ModifierArray;
|
||||||
|
|
||||||
/* intermediate struct for a PSYC packet */
|
/* intermediate struct for a PSYC packet */
|
||||||
typedef struct
|
typedef struct
|
||||||
{
|
{
|
||||||
PSYC_Modifier** routing; // Routing header
|
PSYC_ModifierArray routing; ///< Routing header.
|
||||||
PSYC_Modifier** entity; // Entitiy header
|
PSYC_ModifierArray entity; ///< Entitiy header.
|
||||||
PSYC_Array method;
|
PSYC_String method;
|
||||||
PSYC_Array data;
|
PSYC_String data;
|
||||||
size_t length; /// Length of content part
|
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_PacketFlag flag;
|
||||||
} PSYC_Packet;
|
} 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.
|
/// Routing vars in alphabetical order.
|
||||||
extern const PSYC_Array PSYC_routingVars[];
|
extern const PSYC_String PSYC_routingVars[];
|
||||||
/// Number of routing vars.
|
/// Number of routing vars.
|
||||||
extern const size_t PSYC_routingVarsNum;
|
extern const size_t PSYC_routingVarsNum;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Get the type of variable name.
|
* 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.
|
* 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.
|
* Checks if long keyword string inherits from short keyword string.
|
||||||
*/
|
*/
|
||||||
int PSYC_inherits(char* sho, size_t slen,
|
int PSYC_inherits(char *sho, size_t slen,
|
||||||
char* lon, size_t llen);
|
char *lon, size_t llen);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Checks if short keyword string matches long keyword string.
|
* 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);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Callback for PSYC_text() that produces a value for a match.
|
* 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
|
* number of bytes written. 0 is a legal return value. Should the
|
||||||
* callback return -1, PSYC_text leaves the original template text as is.
|
* callback return -1, PSYC_text leaves the original template text as is.
|
||||||
*/
|
*/
|
||||||
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);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Fills out text templates by asking a callback for content.
|
* 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
|
* See also http://about.psyc.eu/psyctext
|
||||||
*/
|
*/
|
||||||
int PSYC_text(uint8_t* template, size_t tlen,
|
int PSYC_text(uint8_t *template, size_t tlen,
|
||||||
uint8_t** buffer, size_t * blen,
|
uint8_t **buffer, size_t *blen,
|
||||||
PSYC_textCB lookupValue,
|
PSYC_textCB lookupValue,
|
||||||
char* braceOpen, char* braceClose);
|
char *braceOpen, char *braceClose);
|
||||||
|
|
||||||
#endif // PSYC_H
|
#endif // PSYC_H
|
||||||
|
|
|
@ -87,7 +87,7 @@ typedef struct
|
||||||
{
|
{
|
||||||
size_t cursor; ///< current position in buffer
|
size_t cursor; ///< current position in buffer
|
||||||
size_t startc; ///< position where the parsing would be resumed
|
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
|
uint8_t flags; ///< flags for the parser, see PSYC_ParseFlag
|
||||||
PSYC_Part part; ///< part of the packet being parsed currently
|
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 cursor; ///< current position in buffer
|
||||||
size_t startc; ///< line start position
|
size_t startc; ///< line start position
|
||||||
PSYC_Array buffer;
|
PSYC_String buffer;
|
||||||
PSYC_ListType type; ///< list type
|
PSYC_ListType type; ///< list type
|
||||||
|
|
||||||
size_t elemParsed; ///< number of bytes parsed from the elem so far
|
size_t elemParsed; ///< number of bytes parsed from the elem so far
|
||||||
size_t elemLength; ///< expected length of the elem
|
size_t elemLength; ///< expected length of the elem
|
||||||
} PSYC_ParseListState;
|
} 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.
|
* 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_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);
|
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 state An initialized PSYC_ParseState
|
||||||
* @param operator A pointer to a character. In case of a variable, it will
|
* @param operator A pointer to a character. In case of a variable, it will
|
||||||
* be set to the operator of that variable
|
* 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
|
* 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
|
* 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.
|
* 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
|
#endif // PSYC_PARSER_H
|
||||||
|
|
||||||
|
|
|
@ -32,11 +32,14 @@ typedef struct
|
||||||
char buffer[]; ///< OMG a C99 feature! variable size buffer!
|
char buffer[]; ///< OMG a C99 feature! variable size buffer!
|
||||||
} PSYC_RenderState;
|
} PSYC_RenderState;
|
||||||
|
|
||||||
|
PSYC_RenderRC PSYC_render(PSYC_Packet *packet, char *buffer, size_t buflen);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Initiates the state struct.
|
* Initiates the state struct.
|
||||||
*
|
*
|
||||||
* @param state Pointer to the state struct that should be initiated.
|
* @param state Pointer to the state struct that should be initiated.
|
||||||
*/
|
*/
|
||||||
|
/*
|
||||||
inline void PSYC_initRenderState (PSYC_RenderState* state);
|
inline void PSYC_initRenderState (PSYC_RenderState* state);
|
||||||
|
|
||||||
int PSYC_renderModifier(PSYC_RenderState* render,
|
int PSYC_renderModifier(PSYC_RenderState* render,
|
||||||
|
@ -47,5 +50,5 @@ int PSYC_renderModifier(PSYC_RenderState* render,
|
||||||
int PSYC_renderBody(PSYC_RenderState* render,
|
int PSYC_renderBody(PSYC_RenderState* render,
|
||||||
const char* method, size_t mlength,
|
const char* method, size_t mlength,
|
||||||
const char* data, size_t dlength);
|
const char* data, size_t dlength);
|
||||||
|
*/
|
||||||
#endif // PSYC_RENDER_H
|
#endif // PSYC_RENDER_H
|
||||||
|
|
|
@ -10,6 +10,11 @@
|
||||||
# define PSYC_CONTENT_SIZE_THRESHOLD 444
|
# define PSYC_CONTENT_SIZE_THRESHOLD 444
|
||||||
#endif
|
#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 C_GLYPH_PACKET_DELIMITER '|'
|
||||||
#define S_GLYPH_PACKET_DELIMITER "|"
|
#define S_GLYPH_PACKET_DELIMITER "|"
|
||||||
#define PSYC_PACKET_DELIMITER "\n|\n"
|
#define PSYC_PACKET_DELIMITER "\n|\n"
|
||||||
|
|
|
@ -2,8 +2,8 @@ CFLAGS=-I../include -DDEBUG=2 -DPSYC_COMPILE_LIBRARY -g -O0 -Wall
|
||||||
CC=cc
|
CC=cc
|
||||||
# CC=clang
|
# CC=clang
|
||||||
|
|
||||||
S=parser.c match.c render.c memmem.c variable.c
|
S=psyc.c parser.c match.c render.c memmem.c variable.c
|
||||||
O=parser.o match.o render.o memmem.o variable.o
|
O=psyc.o parser.o match.o render.o memmem.o variable.o
|
||||||
|
|
||||||
default:
|
default:
|
||||||
@/bin/echo -e "Usage:\n\tmake diet - compile with diet libc\n\tmake lib - compile with normal gnu libc"
|
@/bin/echo -e "Usage:\n\tmake diet - compile with diet libc\n\tmake lib - compile with normal gnu libc"
|
||||||
|
|
20
src/parser.c
20
src/parser.c
|
@ -15,12 +15,6 @@
|
||||||
return ret; \
|
return ret; \
|
||||||
}
|
}
|
||||||
|
|
||||||
inline PSYC_Array PSYC_createArray (const char* memory, size_t length)
|
|
||||||
{
|
|
||||||
PSYC_Array arr = {length, memory};
|
|
||||||
return arr;
|
|
||||||
}
|
|
||||||
|
|
||||||
inline void PSYC_initParseState (PSYC_ParseState* state)
|
inline void PSYC_initParseState (PSYC_ParseState* state)
|
||||||
{
|
{
|
||||||
memset(state, 0, sizeof(PSYC_ParseState));
|
memset(state, 0, sizeof(PSYC_ParseState));
|
||||||
|
@ -37,13 +31,13 @@ inline void PSYC_initParseListState (PSYC_ParseListState* state)
|
||||||
memset(state, 0, sizeof(PSYC_ParseListState));
|
memset(state, 0, sizeof(PSYC_ParseListState));
|
||||||
}
|
}
|
||||||
|
|
||||||
inline void PSYC_nextParseBuffer (PSYC_ParseState* state, PSYC_Array newBuf)
|
inline void PSYC_nextParseBuffer (PSYC_ParseState* state, PSYC_String newBuf)
|
||||||
{
|
{
|
||||||
state->buffer = newBuf;
|
state->buffer = newBuf;
|
||||||
state->cursor = 0;
|
state->cursor = 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
inline void PSYC_nextParseListBuffer (PSYC_ParseListState* state, PSYC_Array newBuf)
|
inline void PSYC_nextParseListBuffer (PSYC_ParseListState* state, PSYC_String newBuf)
|
||||||
{
|
{
|
||||||
state->buffer = newBuf;
|
state->buffer = newBuf;
|
||||||
state->cursor = 0;
|
state->cursor = 0;
|
||||||
|
@ -107,7 +101,7 @@ inline char isKwChar(uint8_t c)
|
||||||
* It should contain one or more keyword characters.
|
* It should contain one or more keyword characters.
|
||||||
* @return PSYC_PARSE_ERROR or PSYC_PARSE_SUCCESS
|
* @return PSYC_PARSE_ERROR or PSYC_PARSE_SUCCESS
|
||||||
*/
|
*/
|
||||||
inline PSYC_ParseRC PSYC_parseName(PSYC_ParseState* state, PSYC_Array* name)
|
inline PSYC_ParseRC PSYC_parseName(PSYC_ParseState* state, PSYC_String* name)
|
||||||
{
|
{
|
||||||
name->ptr = state->buffer.ptr + state->cursor;
|
name->ptr = state->buffer.ptr + state->cursor;
|
||||||
name->length = 0;
|
name->length = 0;
|
||||||
|
@ -131,7 +125,7 @@ inline PSYC_ParseRC PSYC_parseName(PSYC_ParseState* state, PSYC_Array* name)
|
||||||
*
|
*
|
||||||
* @return PSYC_PARSE_COMPLETE or PSYC_PARSE_INCOMPLETE
|
* @return PSYC_PARSE_COMPLETE or PSYC_PARSE_INCOMPLETE
|
||||||
*/
|
*/
|
||||||
inline PSYC_ParseRC PSYC_parseBinaryValue(PSYC_ParseState* state, PSYC_Array* value, size_t* length, size_t* parsed)
|
inline PSYC_ParseRC PSYC_parseBinaryValue(PSYC_ParseState* state, PSYC_String* value, size_t* length, size_t* parsed)
|
||||||
{
|
{
|
||||||
size_t remaining = *length - *parsed;
|
size_t remaining = *length - *parsed;
|
||||||
value->ptr = state->buffer.ptr + state->cursor;
|
value->ptr = state->buffer.ptr + state->cursor;
|
||||||
|
@ -154,7 +148,7 @@ inline PSYC_ParseRC PSYC_parseBinaryValue(PSYC_ParseState* state, PSYC_Array* va
|
||||||
* Parse simple or binary variable.
|
* Parse simple or binary variable.
|
||||||
* @return PSYC_PARSE_ERROR or PSYC_PARSE_SUCCESS
|
* @return PSYC_PARSE_ERROR or PSYC_PARSE_SUCCESS
|
||||||
*/
|
*/
|
||||||
inline PSYC_ParseRC PSYC_parseModifier(PSYC_ParseState* state, char* oper, PSYC_Array* name, PSYC_Array* value)
|
inline PSYC_ParseRC PSYC_parseModifier(PSYC_ParseState* state, char* oper, PSYC_String* name, PSYC_String* value)
|
||||||
{
|
{
|
||||||
*oper = *(state->buffer.ptr + state->cursor);
|
*oper = *(state->buffer.ptr + state->cursor);
|
||||||
ADVANCE_CURSOR_OR_RETURN(PSYC_PARSE_INSUFFICIENT);
|
ADVANCE_CURSOR_OR_RETURN(PSYC_PARSE_INSUFFICIENT);
|
||||||
|
@ -218,7 +212,7 @@ inline PSYC_ParseRC PSYC_parseModifier(PSYC_ParseState* state, char* oper, PSYC_
|
||||||
* Parse PSYC packets.
|
* Parse PSYC packets.
|
||||||
* Generalized line-based parser.
|
* Generalized line-based parser.
|
||||||
*/
|
*/
|
||||||
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)
|
||||||
{
|
{
|
||||||
int ret; // a return value
|
int ret; // a return value
|
||||||
size_t pos; // a cursor position
|
size_t pos; // a cursor position
|
||||||
|
@ -408,7 +402,7 @@ PSYC_ParseRC PSYC_parse(PSYC_ParseState* state, char* oper, PSYC_Array* name, PS
|
||||||
* List value parser.
|
* List value parser.
|
||||||
* @return see PSYC_ListRC.
|
* @return see PSYC_ListRC.
|
||||||
*/
|
*/
|
||||||
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)
|
||||||
{
|
{
|
||||||
if (state->cursor >= state->buffer.length)
|
if (state->cursor >= state->buffer.length)
|
||||||
return PSYC_PARSE_LIST_INCOMPLETE;
|
return PSYC_PARSE_LIST_INCOMPLETE;
|
||||||
|
|
104
src/psyc.c
Normal file
104
src/psyc.c
Normal file
|
@ -0,0 +1,104 @@
|
||||||
|
#include <psyc/lib.h>
|
||||||
|
#include <psyc/syntax.h>
|
||||||
|
|
||||||
|
#include <math.h>
|
||||||
|
|
||||||
|
inline PSYC_String PSYC_newString (const char *str, size_t strlen)
|
||||||
|
{
|
||||||
|
PSYC_String s = {strlen, str};
|
||||||
|
return s;
|
||||||
|
}
|
||||||
|
|
||||||
|
inline PSYC_Modifier PSYC_newModifier(char oper, PSYC_String *name, PSYC_String *value,
|
||||||
|
PSYC_ModifierFlag flag)
|
||||||
|
{
|
||||||
|
PSYC_Modifier m = {oper, *name, *value, flag};
|
||||||
|
|
||||||
|
if (flag == PSYC_MODIFIER_CHECK_LENGTH) // find out if it needs a length
|
||||||
|
{
|
||||||
|
if (value->length > PSYC_MODIFIER_SIZE_THRESHOLD)
|
||||||
|
m.flag = PSYC_PACKET_NEED_LENGTH;
|
||||||
|
else if (memchr(value->ptr, (int)'\n', value->length))
|
||||||
|
m.flag = PSYC_PACKET_NEED_LENGTH;
|
||||||
|
else
|
||||||
|
m.flag = PSYC_PACKET_NO_LENGTH;
|
||||||
|
}
|
||||||
|
|
||||||
|
return m;
|
||||||
|
}
|
||||||
|
|
||||||
|
inline PSYC_Modifier PSYC_newModifier2(char oper,
|
||||||
|
const char *name, size_t namelen,
|
||||||
|
const char *value, size_t valuelen,
|
||||||
|
PSYC_ModifierFlag flag)
|
||||||
|
{
|
||||||
|
PSYC_String n = {namelen, name};
|
||||||
|
PSYC_String v = {valuelen, value};
|
||||||
|
|
||||||
|
return PSYC_newModifier(oper, &n, &v, flag);
|
||||||
|
}
|
||||||
|
|
||||||
|
inline size_t PSYC_getModifierLength(PSYC_Modifier *m)
|
||||||
|
{
|
||||||
|
size_t length =
|
||||||
|
m->name.length + 1 + // name\t
|
||||||
|
m->value.length + 1; // value\n
|
||||||
|
|
||||||
|
if (m->flag == PSYC_MODIFIER_NEED_LENGTH) // add length of length if needed
|
||||||
|
length += log10((double)m->value.length) + 2; // SP length
|
||||||
|
|
||||||
|
return length;
|
||||||
|
}
|
||||||
|
|
||||||
|
inline PSYC_Packet PSYC_newPacket(PSYC_ModifierArray *routing,
|
||||||
|
PSYC_ModifierArray *entity,
|
||||||
|
PSYC_String *method, PSYC_String *data,
|
||||||
|
PSYC_PacketFlag flag)
|
||||||
|
{
|
||||||
|
PSYC_Packet p = {*routing, *entity, *method, *data, 0, 0, flag};
|
||||||
|
size_t i;
|
||||||
|
|
||||||
|
if (flag == PSYC_PACKET_CHECK_LENGTH) // find out if it needs a length
|
||||||
|
{
|
||||||
|
if (data->length == 1 && data->ptr[0] == C_GLYPH_PACKET_DELIMITER)
|
||||||
|
p.flag = PSYC_PACKET_NEED_LENGTH;
|
||||||
|
else if (data->length > PSYC_CONTENT_SIZE_THRESHOLD)
|
||||||
|
p.flag = PSYC_PACKET_NEED_LENGTH;
|
||||||
|
else if (memmem(data->ptr, data->length, PSYC_C2ARG(PSYC_PACKET_DELIMITER)))
|
||||||
|
p.flag = PSYC_PACKET_NEED_LENGTH;
|
||||||
|
else
|
||||||
|
p.flag = PSYC_PACKET_NO_LENGTH;
|
||||||
|
}
|
||||||
|
|
||||||
|
// calculate routing header length
|
||||||
|
for (i = 0; i < routing->length; i++)
|
||||||
|
p.routingLength += PSYC_getModifierLength(&routing->ptr[i]);
|
||||||
|
|
||||||
|
// calculate entity header length
|
||||||
|
for (i = 0; i < entity->length; i++)
|
||||||
|
p.contentLength += PSYC_getModifierLength(&routing->ptr[i]);
|
||||||
|
|
||||||
|
// add length of method, data & delimiter
|
||||||
|
p.contentLength += method->length + 1 + data->length; // method \n data
|
||||||
|
|
||||||
|
// set total length: routing-header \n content \n|\n
|
||||||
|
p.length = p.routingLength + 1 + p.contentLength + sizeof(PSYC_PACKET_DELIMITER) - 1;
|
||||||
|
if (flag == PSYC_PACKET_NEED_LENGTH) // add length of length if needed
|
||||||
|
p.length += log10((double)data->length) + 1;
|
||||||
|
|
||||||
|
return p;
|
||||||
|
}
|
||||||
|
|
||||||
|
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)
|
||||||
|
{
|
||||||
|
PSYC_ModifierArray r = {routinglen, *routing};
|
||||||
|
PSYC_ModifierArray e = {entitylen, *entity};
|
||||||
|
PSYC_String m = {methodlen, method};
|
||||||
|
PSYC_String d = {datalen, data};
|
||||||
|
|
||||||
|
return PSYC_newPacket(&r, &e, &m, &d, flag);
|
||||||
|
}
|
61
src/render.c
61
src/render.c
|
@ -1,14 +1,57 @@
|
||||||
#include "psyc/lib.h"
|
#include "psyc/lib.h"
|
||||||
#include "psyc/render.h"
|
#include "psyc/render.h"
|
||||||
|
|
||||||
inline void PSYC_initRenderState (PSYC_RenderState* state)
|
inline int PSYC_renderModifier(PSYC_Modifier *m, char *buffer)
|
||||||
|
{
|
||||||
|
int cur = 0;
|
||||||
|
|
||||||
|
memcpy(buffer + cur++, &m->oper, 1);
|
||||||
|
memcpy(buffer + cur, m->name.ptr, m->name.length);
|
||||||
|
cur += m->name.length;
|
||||||
|
if (m->flag == PSYC_MODIFIER_NEED_LENGTH)
|
||||||
|
{
|
||||||
|
memcpy(buffer + cur++, " ", 1);
|
||||||
|
cur += sprintf(buffer + cur, "%ld", m->value.length);
|
||||||
|
}
|
||||||
|
memcpy(buffer + cur, m->value.ptr, m->value.length);
|
||||||
|
cur += m->value.length;
|
||||||
|
memcpy(buffer + cur++, "\n", 1);
|
||||||
|
|
||||||
|
return cur;
|
||||||
|
}
|
||||||
|
|
||||||
|
PSYC_RenderRC PSYC_render(PSYC_Packet *packet, char *buffer, size_t buflen)
|
||||||
|
{
|
||||||
|
size_t cur, i;
|
||||||
|
|
||||||
|
if (packet->length > buflen)
|
||||||
|
return PSYC_RENDER_ERROR;
|
||||||
|
|
||||||
|
for (i = 0; i < packet->routing.length; i++)
|
||||||
|
cur += PSYC_renderModifier(&packet->routing.ptr[i], buffer + cur);
|
||||||
|
|
||||||
|
if (packet->flag == PSYC_PACKET_NEED_LENGTH)
|
||||||
|
cur += sprintf(buffer + cur, "%ld", packet->contentLength);
|
||||||
|
|
||||||
|
memcpy(buffer + cur++, "\n", 1);
|
||||||
|
|
||||||
|
for (i = 0; i < packet->entity.length; i++)
|
||||||
|
cur += PSYC_renderModifier(&packet->entity.ptr[i], buffer + cur);
|
||||||
|
|
||||||
|
memcpy(buffer + cur, PSYC_C2ARG(PSYC_PACKET_DELIMITER));
|
||||||
|
|
||||||
|
return PSYC_RENDER_SUCCESS;
|
||||||
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
|
inline void PSYC_initRenderState (PSYC_RenderState *state)
|
||||||
{
|
{
|
||||||
memset(state, 0, sizeof(PSYC_RenderState));
|
memset(state, 0, sizeof(PSYC_RenderState));
|
||||||
}
|
}
|
||||||
|
|
||||||
PSYC_RenderRC PSYC_renderModifier(PSYC_RenderState* state,
|
PSYC_RenderRC PSYC_renderModifier(PSYC_RenderState *state,
|
||||||
const char* name, size_t nlength,
|
const char *name, size_t nlength,
|
||||||
const char* value, size_t vlength,
|
const char *value, size_t vlength,
|
||||||
const PSYC_RenderFlag flags, char oper)
|
const PSYC_RenderFlag flags, char oper)
|
||||||
{
|
{
|
||||||
size_t startc = state->cursor;
|
size_t startc = state->cursor;
|
||||||
|
@ -51,10 +94,9 @@ PSYC_RenderRC PSYC_renderModifier(PSYC_RenderState* state,
|
||||||
return PSYC_RENDER_SUCCESS;
|
return PSYC_RENDER_SUCCESS;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* render PSYC packets */
|
PSYC_RenderRC PSYC_renderBody(PSYC_RenderState *state,
|
||||||
PSYC_RenderRC PSYC_renderBody(PSYC_RenderState* state,
|
const char *method, size_t mlength,
|
||||||
const char* method, size_t mlength,
|
const char *data, size_t dlength)
|
||||||
const char* data, size_t dlength)
|
|
||||||
{
|
{
|
||||||
if (state->flag == PSYC_RENDER_CHECK_LENGTH)
|
if (state->flag == PSYC_RENDER_CHECK_LENGTH)
|
||||||
{
|
{
|
||||||
|
@ -63,7 +105,7 @@ PSYC_RenderRC PSYC_renderBody(PSYC_RenderState* state,
|
||||||
state->flag = PSYC_RENDER_NEED_LENGTH;
|
state->flag = PSYC_RENDER_NEED_LENGTH;
|
||||||
else if (dlength > 404)
|
else if (dlength > 404)
|
||||||
state->flag = PSYC_RENDER_NEED_LENGTH;
|
state->flag = PSYC_RENDER_NEED_LENGTH;
|
||||||
else if (memmem(data, dlength, PSYC_PACKET_DELIMITER, sizeof(PSYC_PACKET_DELIMITER)))
|
else if (memmem(data, dlength, PSYC_C2ARG(PSYC_PACKET_DELIMITER)))
|
||||||
state->flag = PSYC_RENDER_NEED_LENGTH;
|
state->flag = PSYC_RENDER_NEED_LENGTH;
|
||||||
else
|
else
|
||||||
state->flag = PSYC_RENDER_NO_LENGTH;
|
state->flag = PSYC_RENDER_NO_LENGTH;
|
||||||
|
@ -73,3 +115,4 @@ PSYC_RenderRC PSYC_renderBody(PSYC_RenderState* state,
|
||||||
|
|
||||||
return PSYC_RENDER_SUCCESS;
|
return PSYC_RENDER_SUCCESS;
|
||||||
}
|
}
|
||||||
|
*/
|
||||||
|
|
|
@ -1,28 +1,27 @@
|
||||||
#include <psyc.h>
|
#include <psyc.h>
|
||||||
#include <stdint.h>
|
#include <stdint.h>
|
||||||
|
|
||||||
const PSYC_Array PSYC_routingVars[] = /// Routing variables in alphabetical order.
|
/// Routing variables in alphabetical order.
|
||||||
|
const PSYC_String PSYC_routingVars[] =
|
||||||
{
|
{
|
||||||
{17, "_amount_fragments"},
|
PSYC_C2STR("_amount_fragments"),
|
||||||
{ 8, "_context"},
|
PSYC_C2STR("_context"),
|
||||||
// { 6, "_count"}, // older PSYC
|
//PSYC_C2STR("_count"), // older PSYC
|
||||||
{ 8, "_counter"}, // the name for this is supposed to be _count, not _counter
|
PSYC_C2STR("_counter"), // the name for this is supposed to be _count, not _counter
|
||||||
{ 9, "_fragment"},
|
PSYC_C2STR("_fragment"),
|
||||||
// { 7, "_length"}, // older PSYC
|
//PSYC_C2STR("_length"), // older PSYC
|
||||||
{ 7, "_source"},
|
PSYC_C2STR("_source"),
|
||||||
PSYC_C2ARRAY("_source_identification"),
|
PSYC_C2STR("_source_identification"),
|
||||||
// {22, "_source_identification"},
|
PSYC_C2STR("_source_relay"),
|
||||||
PSYC_C2ARRAY("_source_relay"),
|
PSYC_C2STR("_source_relay_relay"), // until you have a better idea.. is this really in use?
|
||||||
// {13, "_source_relay"},
|
PSYC_C2STR("_tag"),
|
||||||
{19, "_source_relay_relay"}, // until you have a better idea.. is this really in use?
|
PSYC_C2STR("_tag_relay"),
|
||||||
{ 4, "_tag"},
|
//PSYC_C2STR("_tag_reply"), // older PSYC
|
||||||
{10, "_tag_relay"},
|
PSYC_C2STR("_target"),
|
||||||
// {10, "_tag_reply"}, // older PSYC
|
PSYC_C2STR("_target_forward"),
|
||||||
{ 7, "_target"},
|
PSYC_C2STR("_target_relay"),
|
||||||
{15, "_target_forward"},
|
//PSYC_C2STR(19, "_understand_modules"), // older PSYC
|
||||||
{13, "_target_relay"},
|
//PSYC_C2STR(14, "_using_modules"), // older PSYC
|
||||||
// {19, "_understand_modules"}, // older PSYC
|
|
||||||
// {14, "_using_modules"}, // older PSYC
|
|
||||||
};
|
};
|
||||||
|
|
||||||
const size_t PSYC_routingVarsNum = sizeof(PSYC_routingVars) / sizeof(*PSYC_routingVars);
|
const size_t PSYC_routingVarsNum = sizeof(PSYC_routingVars) / sizeof(*PSYC_routingVars);
|
||||||
|
|
|
@ -1,6 +1,6 @@
|
||||||
CFLAGS=-I../include -DDEBUG -g -O0 -Wall
|
CFLAGS=-I../include -DDEBUG -g -O0 -Wall
|
||||||
LDFLAGS=-L../src
|
LDFLAGS=-L../src
|
||||||
LOADLIBES=-lpsyc
|
LOADLIBES=-lpsyc -lm
|
||||||
TARGETS=testParser testMatch testRender isRoutingVar
|
TARGETS=testParser testMatch testRender isRoutingVar
|
||||||
|
|
||||||
all: $(TARGETS)
|
all: $(TARGETS)
|
||||||
|
|
|
@ -5,7 +5,7 @@
|
||||||
|
|
||||||
int main(int argc, char** argv)
|
int main(int argc, char** argv)
|
||||||
{
|
{
|
||||||
char* vars[] =
|
const char* vars[] =
|
||||||
{
|
{
|
||||||
"_source",
|
"_source",
|
||||||
"_source_relay",
|
"_source_relay",
|
||||||
|
@ -19,6 +19,7 @@ int main(int argc, char** argv)
|
||||||
int i;
|
int i;
|
||||||
for (i = 0; i < sizeof(vars) / sizeof(*vars); i++)
|
for (i = 0; i < sizeof(vars) / sizeof(*vars); i++)
|
||||||
{
|
{
|
||||||
|
printf(">> %s: %ld %ld\n", vars[i], sizeof(vars[i]), sizeof(*vars[i]));
|
||||||
printf("%s: %d\n", vars[i], PSYC_isRoutingVar(vars[i], strlen(vars[i])));
|
printf("%s: %d\n", vars[i], PSYC_isRoutingVar(vars[i], strlen(vars[i])));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -8,7 +8,7 @@ int main(int argc, char** argv)
|
||||||
{
|
{
|
||||||
int index, ret;
|
int index, ret;
|
||||||
char buffer[2048], oper;
|
char buffer[2048], oper;
|
||||||
PSYC_Array name, value, elem;
|
PSYC_String name, value, elem;
|
||||||
PSYC_ParseState state;
|
PSYC_ParseState state;
|
||||||
PSYC_ParseListState listState;
|
PSYC_ParseListState listState;
|
||||||
|
|
||||||
|
@ -22,7 +22,7 @@ int main(int argc, char** argv)
|
||||||
write(1, ">> PARSE\n", 9);
|
write(1, ">> PARSE\n", 9);
|
||||||
|
|
||||||
PSYC_initParseState(&state);
|
PSYC_initParseState(&state);
|
||||||
PSYC_nextParseBuffer(&state, PSYC_createArray(buffer, index));
|
PSYC_nextParseBuffer(&state, PSYC_newString(buffer, index));
|
||||||
|
|
||||||
// try parsing that now
|
// try parsing that now
|
||||||
while ((ret = PSYC_parse(&state, &oper, &name, &value)))
|
while ((ret = PSYC_parse(&state, &oper, &name, &value)))
|
||||||
|
|
|
@ -19,13 +19,13 @@ int writePresence(const char *avail, int availlen, const char *desc, int desclen
|
||||||
}
|
}
|
||||||
// if (PSYC_initBuffer(pb, WHATEVER)) die("PSYC_initBuffer hates me");
|
// if (PSYC_initBuffer(pb, WHATEVER)) die("PSYC_initBuffer hates me");
|
||||||
|
|
||||||
(void) PSYC_renderVar(pb, "_context", 0,
|
(void) PSYC_renderModifier(pb, "_context", 0,
|
||||||
myUNI, sizeof(myUNI), PSYC_RENDER_ROUTING, 0);
|
myUNI, sizeof(myUNI), PSYC_RENDER_ROUTING, 0);
|
||||||
|
|
||||||
// the first call to PSYC_renderHeader() without PSYC_RENDER_ROUTING adds the
|
// the first call to PSYC_renderHeader() without PSYC_RENDER_ROUTING adds the
|
||||||
// extra newline to the buffer. later vars with PSYC_RENDER_ROUTING cause an error.
|
// extra newline to the buffer. later vars with PSYC_RENDER_ROUTING cause an error.
|
||||||
(void) PSYC_renderVar(pb, "_degree_availability", 0, avail, availlen, 0, C_GLYPH_OPERATOR_ASSIGN);
|
(void) PSYC_renderModifier(pb, "_degree_availability", 0, avail, availlen, 0, C_GLYPH_OPERATOR_ASSIGN);
|
||||||
(void) PSYC_renderVar(pb, "_description_presence", 0, desc, desclen, 0, C_GLYPH_OPERATOR_ASSIGN);
|
(void) PSYC_renderModifier(pb, "_description_presence", 0, desc, desclen, 0, C_GLYPH_OPERATOR_ASSIGN);
|
||||||
// presence is to be assigned permanently in distributed state
|
// presence is to be assigned permanently in distributed state
|
||||||
|
|
||||||
(void) PSYC_renderBody(pb, "_notice_presence", 0,
|
(void) PSYC_renderBody(pb, "_notice_presence", 0,
|
||||||
|
|
Loading…
Reference in a new issue