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

refactoring - renamed functions

This commit is contained in:
Gabor Adam Toth 2011-10-31 20:04:16 +01:00
parent e82d444c3d
commit 6a994e2eb2
37 changed files with 570 additions and 691 deletions

25
.gitignore vendored
View file

@ -7,18 +7,19 @@ d/doc/html
d/doc/latex d/doc/latex
d/doc/man d/doc/man
src/match src/match
test/testMatch test/test_list
test/testParser test/test_match
test/testRender test/test_parser
test/testPsyc test/test_render
test/testPsycSpeed test/test_psyc
test/testJson test/test_psyc_speed
test/testJsonGlib test/test_json
test/testStrlen test/test_json_glib
test/testText test/test_strlen
test/isRoutingVar test/test_text
test/getVarType test/var_is_routing
test/parseUniform test/var_type
test/uniform_parse
perl/*.c perl/*.c
perl/*.so perl/*.so

View file

@ -97,19 +97,19 @@ extern (C) MatchVar varTypes[];
/** /**
* Get the type of variable name. * Get the type of variable name.
*/ */
Bool psyc_isRoutingVar (char[]* name); Bool psyc_var_is_routing (char* name, size_t len);
bool isRoutingVar (char[] name) bool isRoutingVar (char[] name)
{ {
return psyc_isRoutingVar(&name); return psyc_var_is_routing(name.ptr, name.length); //FIXME
} }
/** /**
* Get the type of variable name. * Get the type of variable name.
*/ */
Type psyc_getVarType(char *name, size_t len); Type psyc_var_type(char *name, size_t len);
alias psyc_getVarType getVarType; alias psyc_var_type getVarType;
/** /**
* Checks if long keyword string inherits from short keyword string. * Checks if long keyword string inherits from short keyword string.

View file

@ -96,7 +96,7 @@ struct Modifier
size_t length ( ) size_t length ( )
{ {
return psyc_getModifierLength (this); return psyc_modifier_length (this);
} }
private ModifierFlag checkLength ( ubyte[] value ) private ModifierFlag checkLength ( ubyte[] value )
@ -142,24 +142,24 @@ struct Packet
char[] method, ubyte[] data, char[] method, ubyte[] data,
PacketFlag flag = PacketFlag.CHECK_LENGTH) PacketFlag flag = PacketFlag.CHECK_LENGTH)
{ {
return psyc_newPacket (&routing, &entity, cast(ubyte[]*)&method, &data, flag); return psyc_packet_new(&routing, &entity, cast(ubyte[]*)&method, &data, flag); // FIXME
} }
static Packet opCall (Modifier[] routing, ubyte[] content, static Packet opCall (Modifier[] routing, ubyte[] content,
PacketFlag flag = PacketFlag.CHECK_LENGTH) PacketFlag flag = PacketFlag.CHECK_LENGTH)
{ {
return psyc_newRawPacket (&routing, &content, flag); return psyc_packet_new_raw(&routing, &content, flag); // FIXME
} }
size_t length ( ) size_t length ( )
{ {
psyc_setPacketLength(this); psyc_packet_length_set(this);
return this._length; return this._length;
} }
ubyte[] render ( ubyte[] buffer ) ubyte[] render ( ubyte[] buffer )
{ {
psyc_setPacketLength(this); psyc_packet_length_set(this);
with (RenderRC) with (RenderRC)
switch (psyc_render(this, buffer.ptr, buffer.length)) switch (psyc_render(this, buffer.ptr, buffer.length))
@ -189,56 +189,43 @@ struct Packet
/** /**
* \internal * \internal
*/ */
private size_t psyc_getModifierLength (Modifier *m); private size_t psyc_modifier_length (Modifier *m);
/** /**
* \internal * \internal
* Check if a list needs length. * Check if a list needs length.
*/ */
ListFlag psyc_checkListLength (List *list); ListFlag psyc_list_length_check (List *list);
/** /**
* \internal * \internal
* Get the total length of a list when rendered. * Get the total length of a list when rendered.
*/ */
ListFlag psyc_getListLength (List *list); ListFlag psyc_list_length (List *list);
/** /**
* \internal * \internal
* Check if a packet needs length. * Check if a packet needs length.
*/ */
PacketFlag psyc_checkPacketLength (Packet *p); PacketFlag psyc_packet_length_check (Packet *p);
/** /**
* Calculate and set the rendered length of packet parts and total packet length. * Calculate and set the rendered length of packet parts and total packet length.
*/ */
private size_t psyc_setPacketLength (Packet *p); private size_t psyc_packet_length_set (Packet *p);
/** Create new list. */ /** Create new list. */
List psyc_newList (String *elems, size_t num_elems, ListFlag flag); List psyc_list_new (String *elems, size_t num_elems, ListFlag flag);
/** Create new packet. */ /** Create new packet. */
private Packet psyc_newPacket (Modifier[]* routing, Packet psyc_packet_new (Modifier *routing, size_t routinglen,
Modifier[]* entity,
String *method, String *data,
PacketFlag flag);
/** Create new packet. */
Packet psyc_newPacket2 (Modifier *routing, size_t routinglen,
Modifier *entity, size_t entitylen, Modifier *entity, size_t entitylen,
char *method, size_t methodlen, char *method, size_t methodlen,
char *data, size_t datalen, char *data, size_t datalen,
PacketFlag flag); PacketFlag flag);
/** Create new packet with raw content. */ /** Create new packet with raw content. */
Packet psyc_newRawPacket (Modifier[] *routing, ubyte[] *content, Packet psyc_packet_new_raw (Modifier *routing, size_t routinglen,
PacketFlag flag);
/** Create new packet with raw content. */
Packet psyc_newRawPacket2 (Modifier *routing, size_t routinglen,
char *content, size_t contentlen, char *content, size_t contentlen,
PacketFlag flag); PacketFlag flag);

View file

@ -316,6 +316,6 @@ ParseRC psyc_parse(ParseState* state, char* oper, String* name, String* value);
/** /**
* List value parser. * List value parser.
*/ */
ParseListRC psyc_parseList(ParseListState* state, String *name, String* value, String* elem); ParseListRC psyc_parse_list(ParseListState* state, String *name, String* value, String* elem);
/** @} */ // end of parsing group /** @} */ // end of parsing group

View file

@ -25,7 +25,7 @@ enum RenderRC
} ; } ;
/** /**
* Return codes for psyc_renderList. * Return codes for psyc_render_list.
*/ */
enum RenderListRC enum RenderListRC
{ {
@ -41,20 +41,18 @@ enum RenderListRC
* The packet structure should contain the packet parts, either routing, entity, * The packet structure should contain the packet parts, either routing, entity,
* method & data, or routing & content when rendering raw content. * method & data, or routing & content when rendering raw content.
* It should also contain the contentLength & total length of the packet, * It should also contain the contentLength & total length of the packet,
* you can use psyc_setPacketLength() for calculating & setting these values. * you can use psyc_packet_length_set() for calculating & setting these values.
* This function renders packet->length bytes to the buffer, * This function renders packet->length bytes to the buffer,
* if buflen is less than that an error is returned. * if buflen is less than that an error is returned.
* *
* @see psyc_newPacket * @see psyc_packet_new
* @see psyc_newPacket2 * @see psyc_packet_new_raw
* @see psyc_newRawPacket * @see psyc_packet_length_set
* @see psyc_newRawPacket2
* @see psyc_setPacketLength
*/ */
RenderRC psyc_render (Packet *packet, ubyte *buffer, size_t buflen); RenderRC psyc_render (Packet *packet, ubyte *buffer, size_t buflen);
/** /**
* Render a PSYC list into a buffer. * Render a PSYC list into a buffer.
*/ */
RenderListRC psyc_renderList (List *list, ubyte *buffer, size_t buflen); RenderListRC psyc_render_list (List *list, ubyte *buffer, size_t buflen);

66
doc/renames Normal file
View file

@ -0,0 +1,66 @@
psyc_newString psyc_string_new
psyc_routingVars psyc_routing_vars
psyc_routingVarsNum psyc_routing_vars_num
psyc_varTypes psyc_var_types
psyc_varTypesNum psyc_var_types_num
psyc_isRoutingVar -
psyc_isRoutingVar2 psyc_var_is_routing
psyc_isListVar -
psyc_isListVar2 psyc_var_is_list
psyc_getVarType -
psyc_getVarType2 psyc_var_type
psyc_newModifier -
psyc_newModifier2 psyc_modifier_new
psyc_getModifierLength psyc_modifier_length
psyc_checkModifierLength psyc_modifier_length_check
psyc_getListLength psyc_list_length
psyc_checkListLength psyc_list_length_check
psyc_getNumLength psyc_num_length
psyc_newPacket -
psyc_newPacket2 psyc_packet_new
psyc_newRawPacket -
psyc_newRawPacket2 psyc_packet_new_raw
psyc_checkPacketLength psyc_packet_length_check
psyc_setPacketLength psyc_packet_length_set
psyc_parseUniform -
psyc_parseUniform2 psyc_uniform_parse
psyc_initTextState psyc_text_state_init
psyc_initTextState2 psyc_text_state_init_custom
psyc_setTextBuffer -
psyc_setTextBuffer2 psyc_text_buffer_set
psyc_getTextBytesWritten psyc_text_bytes_written
psyc_parseList psyc_parse_list
psyc_parseNumber -
psyc_parseNumber2 psyc_parse_number
psyc_parseTime -
psyc_parseTime2 psyc_parse_time
psyc_parseDate -
psyc_parseDate2 psyc_parse_date
psyc_initParseState -
psyc_initParseState2 psyc_parse_state_init
psyc_setParseBuffer -
psyc_setParseBuffer2 psyc_parse_buffer_set
psyc_initParseListState psyc_parse_list_state_init
psyc_setParseListBuffer -
psyc_setParseListBuffer2 psyc_parse_list_buffer_set
psyc_getParseContentLength psyc_parse_content_length
psyc_isParseContentLengthFound psyc_parse_content_length_found
psyc_getParseValueLength psyc_parse_value_length
psyc_isParseValueLengthFound psyc_parse_value_length_found
psyc_getParseCursor psyc_parse_cursor
psyc_getParseBufferLength psyc_parse_buffer_length
psyc_getParseRemainingLength psyc_parse_remaining_length
psyc_getParseRemainingBuffer psyc_parse_remaining_buffer
psyc_isGlyph psyc_is_glyph
psyc_isAlpha psyc_is_alpha
psyc_isAlphaNumeric psyc_is_alpha_numeric
psyc_isKwChar psyc_is_kw_char
psyc_isNameChar psyc_is_name_char
psyc_isHostChar psyc_is_host_char
psyc_renderList psyc_render_list

View file

@ -27,6 +27,8 @@
#define PSYC_S2ARG(str) (str).ptr, (str).length #define PSYC_S2ARG(str) (str).ptr, (str).length
#define PSYC_S2ARG2(str) (str).length, (str).ptr #define PSYC_S2ARG2(str) (str).length, (str).ptr
#define PSYC_NUM_ELEM(a) (sizeof(a) / sizeof(*(a)))
typedef enum typedef enum
{ {
PSYC_FALSE = 0, PSYC_FALSE = 0,
@ -99,22 +101,22 @@ typedef struct
typedef struct typedef struct
{ {
psycString name; psycString key;
int value; int value;
} psycMatchVar; } psycMatchVar;
/** /**
* Shortcut for creating a psycString. * Shortcut for creating a psycString.
* *
* @param memory Pointer to the buffer. * @param str Pointer to the buffer.
* @param length Length of that buffer. * @param len Length of that buffer.
* *
* @return An instance of the psycString struct. * @return An instance of the psycString struct.
*/ */
static inline static inline
psycString psyc_newString (const char *str, size_t slen) psycString psyc_string_new (char *str, size_t len)
{ {
psycString s = {slen, str}; psycString s = {len, str};
return s; return s;
} }
@ -124,69 +126,6 @@ unsigned int psyc_version ()
return 1; return 1;
} }
/// Routing variables in alphabetical order.
extern const psycString psyc_routingVars[];
// Variable types in alphabetical order.
extern const psycMatchVar psyc_varTypes[];
extern const size_t psyc_routingVarsNum;
extern const size_t psyc_varTypesNum;
/**
* Is this a routing variable name?
*/
psycBool psyc_isRoutingVar(psycString *name);
/**
* Is this a routing variable name?
*/
psycBool psyc_isRoutingVar2(const char *name, size_t len);
/**
* Get the type of variable name.
*/
psycType psyc_getVarType(psycString *name);
/**
* Get the type of variable name.
*/
psycType psyc_getVarType2(const char *name, size_t len);
/**
* Search for a variable name in an array.
*
* @param array The array to search, should be ordered alphabetically.
* @param size Size of array.
* @param name Name of variable to look for.
* @param namelen Length of name.
* @param startswith If true, look for any variable starting with name,
otherwise only exact matches are returned.
* @param matching A temporary array used for keeping track of results.
* Should be the same size as the array we're searching.
*
* @return The value of the matched variable in the array.
*/
int psyc_findVar(const psycMatchVar *array, size_t size,
const char *name, size_t namelen,
uint8_t startswith, int8_t *matching);
/**
* Is this a list variable name?
*/
static inline
psycBool psyc_isListVar2(const char *name, size_t len)
{
return len < 5 || memcmp(name, "_list", 5) != 0 ||
(len > 5 && name[5] != '_') ? PSYC_FALSE : PSYC_TRUE;
}
/**
* Is this a list variable name?
*/
static inline
psycBool psyc_isListVar(psycString *name)
{
return psyc_isListVar2(name->ptr, name->length);
}
/** /**
* Checks if long keyword string inherits from short keyword string. * Checks if long keyword string inherits from short keyword string.
@ -200,5 +139,26 @@ int psyc_inherits(char *sho, size_t slen,
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);
/**
* Check if keyword is in array.
*
* @param array The array to search, should be ordered alphabetically.
* @param size Size of array.
* @param kw Keyword to look for.
* @param kwlen Length of keyword.
* @param inherit If true, also look for anything inheriting from kw,
otherwise only exact matches are returned.
* @param matching A temporary array used for keeping track of results.
* Should be the same size as the array we're searching.
*
* @return The value of the matched variable in the array.
*/
int psyc_in_array (const psycMatchVar *array, size_t size,
const char *kw, size_t kwlen,
psycBool inherit, int8_t *matching);
#include "psyc/variable.h"
#define PSYC_H #define PSYC_H
#endif #endif

View file

@ -17,6 +17,7 @@
#include <psyc.h> #include <psyc.h>
#include <psyc/syntax.h> #include <psyc/syntax.h>
#include <math.h>
/** Modifier flags. */ /** Modifier flags. */
typedef enum typedef enum
@ -78,7 +79,7 @@ typedef struct
psycListFlag flag; psycListFlag flag;
} psycList; } psycList;
/** intermediate struct for a PSYC packet */ /** Intermediate struct for a PSYC packet */
typedef struct typedef struct
{ {
psycHeader routing; ///< Routing header. psycHeader routing; ///< Routing header.
@ -92,12 +93,21 @@ typedef struct
psycPacketFlag flag; ///< Packet flag. psycPacketFlag flag; ///< Packet flag.
} psycPacket; } psycPacket;
/**
* Return the number of digits a number has in its base 10 representation.
*/
static inline
size_t psyc_num_length (size_t n)
{
return n < 10 ? 1 : log10(n) + 1;
}
/** /**
* \internal * \internal
* Check if a modifier needs length. * Check if a modifier needs length.
*/ */
static inline static inline
psycModifierFlag psyc_checkModifierLength (psycModifier *m) psycModifierFlag psyc_modifier_length_check (psycModifier *m)
{ {
psycModifierFlag flag; psycModifierFlag flag;
@ -111,84 +121,63 @@ psycModifierFlag psyc_checkModifierLength (psycModifier *m)
return flag; return flag;
} }
/** Create new modifier. */
static inline
psycModifier psyc_newModifier (char oper, psycString *name, psycString *value,
psycModifierFlag flag)
{
psycModifier m = {oper, *name, *value, flag};
if (flag == PSYC_MODIFIER_CHECK_LENGTH) // find out if it needs a length
m.flag = psyc_checkModifierLength(&m);
return m;
}
/** Create new modifier */ /** Create new modifier */
static inline static inline
psycModifier psyc_newModifier2 (char oper, psycModifier psyc_modifier_new (char oper,
const char *name, size_t namelen, char *name, size_t namelen,
const char *value, size_t valuelen, char *value, size_t valuelen,
psycModifierFlag flag) psycModifierFlag flag){
{
psycString n = {namelen, name};
psycString v = {valuelen, value};
return psyc_newModifier(oper, &n, &v, flag); psycModifier m = {oper, {namelen, name}, {valuelen, value}, flag};
if (flag == PSYC_MODIFIER_CHECK_LENGTH) // find out if it needs a length
m.flag = psyc_modifier_length_check(&m);
return m;
} }
/** /**
* \internal * \internal
* Get the total length of a modifier when rendered. * Get the total length of a modifier when rendered.
*/ */
size_t psyc_getModifierLength (psycModifier *m); size_t psyc_modifier_length (psycModifier *m);
/** /**
* \internal * \internal
* Check if a list needs length. * Check if a list needs length.
*/ */
psycListFlag psyc_checkListLength (psycList *list); psycListFlag psyc_list_length_check (psycList *list);
/** /**
* \internal * \internal
* Get the total length of a list when rendered. * Get the total length of a list when rendered.
*/ */
psycListFlag psyc_getListLength (psycList *list); psycListFlag psyc_list_length (psycList *list);
/** /**
* \internal * \internal
* Check if a packet needs length. * Check if a packet needs length.
*/ */
psycPacketFlag psyc_checkPacketLength (psycPacket *p); psycPacketFlag psyc_packet_length_check (psycPacket *p);
/** /**
* Calculate and set the rendered length of packet parts and total packet length. * Calculate and set the rendered length of packet parts and total packet length.
*/ */
size_t psyc_setPacketLength (psycPacket *p); size_t psyc_packet_length_set (psycPacket *p);
/** Create new list. */ /** Create new list. */
psycList psyc_newList (psycString *elems, size_t num_elems, psycListFlag flag); psycList psyc_list_new (psycString *elems, size_t num_elems, psycListFlag flag);
/** Create new packet. */ /** Create new packet. */
psycPacket psyc_newPacket (psycHeader *routing, psycPacket psyc_packet_new (psycModifier *routing, size_t routinglen,
psycHeader *entity,
psycString *method, psycString *data,
psycPacketFlag flag);
/** Create new packet. */
psycPacket psyc_newPacket2 (psycModifier *routing, size_t routinglen,
psycModifier *entity, size_t entitylen, psycModifier *entity, size_t entitylen,
const char *method, size_t methodlen, char *method, size_t methodlen,
const char *data, size_t datalen, char *data, size_t datalen,
psycPacketFlag flag); psycPacketFlag flag);
/** Create new packet with raw content. */ /** Create new packet with raw content. */
psycPacket psyc_newRawPacket (psycHeader *routing, psycString *content, psycPacket psyc_packet_new_raw (psycModifier *routing, size_t routinglen,
psycPacketFlag flag); char *content, size_t contentlen,
/** Create new packet with raw content. */
psycPacket psyc_newRawPacket2 (psycModifier *routing, size_t routinglen,
const char *content, size_t contentlen,
psycPacketFlag flag); psycPacketFlag flag);
/** @} */ // end of packet group /** @} */ // end of packet group

View file

@ -25,11 +25,10 @@
* *
* @code * @code
* psycParseState state; * psycParseState state;
* * psyc_parse_state_init(&state, flags);
* psyc_initParseState(&state);
* @endcode * @endcode
* *
* Note that there is also psyc_initParseState2 if you want to fine-tune what * With the flags parameter you can fine-tune what
* part of the packet should be parsed. @see psycParseFlag * part of the packet should be parsed. @see psycParseFlag
* *
* Next, you have to tell the parser what it should parse. Assuming the variable * Next, you have to tell the parser what it should parse. Assuming the variable
@ -40,7 +39,7 @@
* char* raw_data; // points to our (possibly incomplete) packet * char* raw_data; // points to our (possibly incomplete) packet
* size_t raw_len; // how many bytes of data * size_t raw_len; // how many bytes of data
* *
* psyc_setParseBuffer(&state, raw_data, raw_len); // state is our initialized state from before * psyc_parse_buffer_set(&state, raw_data, raw_len); // state is our initialized state from before
* @endcode * @endcode
* *
* Now the the variables that will save the output of the parser need to be * Now the the variables that will save the output of the parser need to be
@ -193,7 +192,7 @@ typedef enum {
/** /**
* The return value definitions for the list parsing function. * The return value definitions for the list parsing function.
* @see psyc_parseList() * @see psyc_parse_list()
*/ */
typedef enum { typedef enum {
PSYC_PARSE_LIST_ERROR_DELIM = -4, PSYC_PARSE_LIST_ERROR_DELIM = -4,
@ -244,24 +243,11 @@ typedef struct {
* Initializes the state struct. * Initializes the state struct.
* *
* @param state Pointer to the state struct that should be initialized. * @param state Pointer to the state struct that should be initialized.
* @see psyc_initParseState2
*/
static inline
void psyc_initParseState (psycParseState *state)
{
memset(state, 0, sizeof(psycParseState));
}
/**
* Initializes the state struct with flags.
*
* @param state Pointer to the state struct that should be initialized.
* @param flags Flags to be set for the parser, see psycParseFlag. * @param flags Flags to be set for the parser, see psycParseFlag.
* @see psyc_initParseState
* @see psycParseFlag * @see psycParseFlag
*/ */
static inline static inline
void psyc_initParseState2 (psycParseState *state, uint8_t flags) void psyc_parse_state_init (psycParseState *state, uint8_t flags)
{ {
memset(state, 0, sizeof(psycParseState)); memset(state, 0, sizeof(psycParseState));
state->flags = flags; state->flags = flags;
@ -270,28 +256,6 @@ void psyc_initParseState2 (psycParseState *state, uint8_t flags)
state->part = PSYC_PART_CONTENT; state->part = PSYC_PART_CONTENT;
} }
/**
* Sets a new buffer in the parser state struct with data to be parsed.
*
* This function does NOT copy the buffer. It will parse whatever is
* at the memory pointed to by buffer.
*
* @param state Pointer to the initialized state of the parser
* @param buffer the buffer that should be parsed now
* @see psycString
*/
static inline
void psyc_setParseBuffer (psycParseState *state, psycString buffer)
{
state->buffer = buffer;
state->cursor = 0;
if (state->flags & PSYC_PARSE_START_AT_CONTENT) {
state->contentLength = buffer.length;
state->contentLengthFound = PSYC_TRUE;
}
}
/** /**
* Sets a new buffer in the parser state struct with data to be parsed. * Sets a new buffer in the parser state struct with data to be parsed.
* *
@ -304,10 +268,15 @@ void psyc_setParseBuffer (psycParseState *state, psycString buffer)
* @see psycString * @see psycString
*/ */
static inline static inline
void psyc_setParseBuffer2 (psycParseState *state, const char *buffer, size_t length) void psyc_parse_buffer_set (psycParseState *state, char *buffer, size_t length)
{ {
psycString buf = {length, buffer}; state->buffer = (psycString) {length, buffer};
psyc_setParseBuffer(state, buf); state->cursor = 0;
if (state->flags & PSYC_PARSE_START_AT_CONTENT) {
state->contentLength = length;
state->contentLengthFound = PSYC_TRUE;
}
} }
/** /**
@ -316,7 +285,7 @@ void psyc_setParseBuffer2 (psycParseState *state, const char *buffer, size_t len
* @param state Pointer to the list state struct that should be initialized. * @param state Pointer to the list state struct that should be initialized.
*/ */
static inline static inline
void psyc_initParseListState (psycParseListState *state) void psyc_parse_list_state_init (psycParseListState *state)
{ {
memset(state, 0, sizeof(psycParseListState)); memset(state, 0, sizeof(psycParseListState));
} }
@ -325,63 +294,56 @@ void psyc_initParseListState (psycParseListState *state)
* Sets a new buffer in the list parser state struct with data to be parsed. * Sets a new buffer in the list parser state struct with data to be parsed.
*/ */
static inline static inline
void psyc_setParseListBuffer (psycParseListState *state, psycString buffer) void psyc_parse_list_buffer_set (psycParseListState *state, char *buffer, size_t length)
{ {
state->buffer = buffer; state->buffer = (psycString) {length, buffer};
state->cursor = 0; state->cursor = 0;
} }
static inline static inline
void psyc_setParseListBuffer2 (psycParseListState *state, const char *buffer, size_t length) size_t psyc_parse_content_length (psycParseState *state)
{
psycString buf = {length, buffer};
psyc_setParseListBuffer(state, buf);
}
static inline
size_t psyc_getParseContentLength (psycParseState *state)
{ {
return state->contentLength; return state->contentLength;
} }
static inline static inline
psycBool psyc_isParseContentLengthFound (psycParseState *state) psycBool psyc_parse_content_length_found (psycParseState *state)
{ {
return state->contentLengthFound; return state->contentLengthFound;
} }
static inline static inline
size_t psyc_getParseValueLength (psycParseState *state) size_t psyc_parse_value_length (psycParseState *state)
{ {
return state->valueLength; return state->valueLength;
} }
static inline static inline
psycBool psyc_isParseValueLengthFound (psycParseState *state) psycBool psyc_parse_value_length_found (psycParseState *state)
{ {
return state->valueLengthFound; return state->valueLengthFound;
} }
static inline static inline
size_t psyc_getParseCursor (psycParseState *state) size_t psyc_parse_cursor (psycParseState *state)
{ {
return state->cursor; return state->cursor;
} }
static inline static inline
size_t psyc_getParseBufferLength (psycParseState *state) size_t psyc_parse_buffer_length (psycParseState *state)
{ {
return state->buffer.length; return state->buffer.length;
} }
static inline static inline
size_t psyc_getParseRemainingLength (psycParseState *state) size_t psyc_parse_remaining_length (psycParseState *state)
{ {
return state->buffer.length - state->cursor; return state->buffer.length - state->cursor;
} }
static inline static inline
const char * psyc_getParseRemainingBuffer (psycParseState *state) const char * psyc_parse_remaining_buffer (psycParseState *state)
{ {
return state->buffer.ptr + state->cursor; return state->buffer.ptr + state->cursor;
} }
@ -422,10 +384,10 @@ psycParseRC psyc_parse (psycParseState *state, char *oper,
#ifdef __INLINE_PSYC_PARSE #ifdef __INLINE_PSYC_PARSE
static inline static inline
#endif #endif
psycParseListRC psyc_parseList (psycParseListState *state, psycString *elem); psycParseListRC psyc_parse_list (psycParseListState *state, psycString *elem);
static inline static inline
psycBool psyc_parseNumber2 (const char *value, size_t len, ssize_t *n) psycBool psyc_parse_number (const char *value, size_t len, ssize_t *n)
{ {
size_t c = 0; size_t c = 0;
uint8_t neg = 0; uint8_t neg = 0;
@ -450,45 +412,27 @@ psycBool psyc_parseNumber2 (const char *value, size_t len, ssize_t *n)
} }
static inline static inline
psycBool psyc_parseNumber (psycString *value, ssize_t *n) psycBool psyc_parse_time (const char *value, size_t len, time_t *t)
{ {
return psyc_parseNumber2(value->ptr, value->length, n); return psyc_parse_number(value, len, t);
} }
static inline static inline
psycBool psyc_parseTime2 (const char *value, size_t len, time_t *t) psycBool psyc_parse_date (const char *value, size_t len, time_t *t)
{ {
return psyc_parseNumber2(value, len, t); if (psyc_parse_number(value, len, t)) {
}
static inline
psycBool psyc_parseTime (psycString *value, time_t *t)
{
return psyc_parseNumber2(value->ptr, value->length, t);
}
static inline
psycBool psyc_parseDate2 (const char *value, size_t len, time_t *t)
{
if (psyc_parseNumber2(value, len, t)) {
*t += PSYC_EPOCH; *t += PSYC_EPOCH;
return PSYC_TRUE; return PSYC_TRUE;
} }
return PSYC_FALSE; return PSYC_FALSE;
} }
static inline
psycBool psyc_parseDate (psycString *value, time_t *t)
{
return psyc_parseDate2(value->ptr, value->length, t);
}
/** /**
* Determines if the argument is a glyph. * Determines if the argument is a glyph.
* Glyphs are: : = + - ? ! * Glyphs are: : = + - ? !
*/ */
static inline static inline
char psyc_isGlyph (uint8_t g) char psyc_is_glyph (uint8_t g)
{ {
switch(g) { switch(g) {
case ':': case ':':
@ -507,7 +451,7 @@ char psyc_isGlyph (uint8_t g)
* Determines if the argument is numeric. * Determines if the argument is numeric.
*/ */
static inline static inline
char psyc_isNumeric (uint8_t c) char psyc_is_numeric (uint8_t c)
{ {
return c >= '0' && c <= '9'; return c >= '0' && c <= '9';
} }
@ -516,7 +460,7 @@ char psyc_isNumeric (uint8_t c)
* Determines if the argument is alphabetic. * Determines if the argument is alphabetic.
*/ */
static inline static inline
char psyc_isAlpha (uint8_t c) char psyc_is_alpha (uint8_t c)
{ {
return (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z'); return (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z');
} }
@ -525,9 +469,9 @@ char psyc_isAlpha (uint8_t c)
* Determines if the argument is alphanumeric. * Determines if the argument is alphanumeric.
*/ */
static inline static inline
char psyc_isAlphaNumeric (uint8_t c) char psyc_is_alpha_numeric (uint8_t c)
{ {
return psyc_isAlpha(c) || psyc_isNumeric(c); return psyc_is_alpha(c) || psyc_is_numeric(c);
} }
/** /**
@ -535,9 +479,9 @@ char psyc_isAlphaNumeric (uint8_t c)
* Keyword characters are: alphanumeric and _ * Keyword characters are: alphanumeric and _
*/ */
static inline static inline
char psyc_isKwChar (uint8_t c) char psyc_is_kw_char (uint8_t c)
{ {
return psyc_isAlphaNumeric(c) || c == '_'; return psyc_is_alpha_numeric(c) || c == '_';
} }
/** /**
@ -545,9 +489,9 @@ char psyc_isKwChar (uint8_t c)
* Name characters are: see opaque_part in RFC 2396 * Name characters are: see opaque_part in RFC 2396
*/ */
static inline static inline
char psyc_isNameChar (uint8_t c) char psyc_is_name_char (uint8_t c)
{ {
return psyc_isAlpha(c) || (c >= '$' && c <= ';') || return psyc_is_alpha(c) || (c >= '$' && c <= ';') ||
c == '_' || c == '!' || c == '?' || c == '=' || c == '@' || c == '~'; c == '_' || c == '!' || c == '?' || c == '=' || c == '@' || c == '~';
} }
@ -556,9 +500,9 @@ char psyc_isNameChar (uint8_t c)
* Hostname characters are: alphanumeric and - * Hostname characters are: alphanumeric and -
*/ */
static inline static inline
char psyc_isHostChar (uint8_t c) char psyc_is_host_char (uint8_t c)
{ {
return psyc_isAlphaNumeric(c) || c == '.' || c == '-'; return psyc_is_alpha_numeric(c) || c == '.' || c == '-';
} }
/** @} */ // end of parse group /** @} */ // end of parse group

View file

@ -32,7 +32,7 @@ typedef enum
} psycRenderRC; } psycRenderRC;
/** /**
* Return codes for psyc_renderList. * Return codes for psyc_render_list.
*/ */
typedef enum typedef enum
{ {
@ -48,15 +48,13 @@ typedef enum
* The packet structure should contain the packet parts, either routing, entity, * The packet structure should contain the packet parts, either routing, entity,
* method & data, or routing & content when rendering raw content. * method & data, or routing & content when rendering raw content.
* It should also contain the contentLength & total length of the packet, * It should also contain the contentLength & total length of the packet,
* you can use psyc_setPacketLength() for calculating & setting these values. * you can use psyc_packet_length_set() for calculating & setting these values.
* This function renders packet->length bytes to the buffer, * This function renders packet->length bytes to the buffer,
* if buflen is less than that an error is returned. * if buflen is less than that an error is returned.
* *
* @see psyc_newPacket * @see psyc_packet_new
* @see psyc_newPacket2 * @see psyc_packet_new_raw
* @see psyc_newRawPacket * @see psyc_packet_length_set
* @see psyc_newRawPacket2
* @see psyc_setPacketLength
*/ */
#ifdef __INLINE_PSYC_RENDER #ifdef __INLINE_PSYC_RENDER
static inline static inline
@ -69,7 +67,7 @@ psycRenderRC psyc_render (psycPacket *packet, char *buffer, size_t buflen);
#ifdef __INLINE_PSYC_RENDER #ifdef __INLINE_PSYC_RENDER
static inline static inline
#endif #endif
psycRenderListRC psyc_renderList (psycList *list, char *buffer, size_t buflen); psycRenderListRC psyc_render_list (psycList *list, char *buffer, size_t buflen);
/** @} */ // end of render group /** @} */ // end of render group

View file

@ -76,8 +76,8 @@ typedef psycTextValueRC (*psycTextCB)(const char *name, size_t len, psycString *
* @param buflen Length of output buffer. * @param buflen Length of output buffer.
*/ */
static inline static inline
void psyc_initTextState (psycTextState *state, void psyc_text_state_init (psycTextState *state,
const char *tmpl, size_t tmplen, char *tmpl, size_t tmplen,
char *buffer, size_t buflen) char *buffer, size_t buflen)
{ {
state->cursor = 0; state->cursor = 0;
@ -102,11 +102,11 @@ void psyc_initTextState (psycTextState *state,
* @param closelen Length of closing brace. * @param closelen Length of closing brace.
*/ */
static inline static inline
void psyc_initTextState2 (psycTextState *state, void psyc_text_state_init_custom (psycTextState *state,
const char *tmpl, size_t tmplen, char *tmpl, size_t tmplen,
char *buffer, size_t buflen, char *buffer, size_t buflen,
const char *open, size_t openlen, char *open, size_t openlen,
const char *close, size_t closelen) char *close, size_t closelen)
{ {
state->cursor = 0; state->cursor = 0;
state->written = 0; state->written = 0;
@ -120,25 +120,15 @@ void psyc_initTextState2 (psycTextState *state,
* Sets a new output buffer in the PSYC text state struct. * Sets a new output buffer in the PSYC text state struct.
*/ */
static inline static inline
void psyc_setTextBuffer (psycTextState *state, psycString buffer) void psyc_text_buffer_set (psycTextState *state,
char *buffer, size_t length)
{ {
state->buffer = buffer; state->buffer = (psycString){length, buffer};
state->written = 0; state->written = 0;
} }
/**
* Sets a new buffer in the PSYC text state struct.
*/
static inline static inline
void psyc_setTextBuffer2 (psycTextState *state, size_t psyc_text_bytes_written (psycTextState *state)
char *buffer, size_t length)
{
psycString buf = {length, buffer};
psyc_setTextBuffer(state, buf);
}
static inline
size_t psyc_getTextBytesWritten (psycTextState *state)
{ {
return state->written; return state->written;
} }
@ -151,10 +141,10 @@ size_t psyc_getTextBytesWritten (psycTextState *state)
* string between these braces. Should the callback return * string between these braces. Should the callback return
* PSYC_TEXT_VALUE_NOT_FOUND, the original template text is copied as is. * PSYC_TEXT_VALUE_NOT_FOUND, the original template text is copied as is.
* *
* Before calling this function psyc_initTextState should be called to initialize * Before calling this function psyc_text_state_init should be called to initialize
* the state struct. By default PSYC's "[" and "]" are used but you can provide * the state struct. By default PSYC's "[" and "]" are used but you can provide
* any other brace strings such as "${" and "}" or "<!--" and "-->" if you use * any other brace strings such as "${" and "}" or "<!--" and "-->" if you use
* the psyc_initTextState2 variant. * the psyc_text_state_init_custom variant.
* *
* @see http://about.psyc.eu/psyctext * @see http://about.psyc.eu/psyctext
**/ **/

View file

@ -73,9 +73,7 @@ typedef enum {
PSYC_ENTITY_SERVICE = '$', PSYC_ENTITY_SERVICE = '$',
} psycEntityType; } psycEntityType;
int psyc_parseUniform2(psycUniform *uni, const char *str, size_t length); int psyc_uniform_parse (psycUniform *uni, char *str, size_t length);
int psyc_parseUniform(psycUniform *uni, psycString *str);
#define PSYC_UNIFORM_H #define PSYC_UNIFORM_H
#endif #endif

37
include/psyc/variable.h Normal file
View file

@ -0,0 +1,37 @@
#ifndef PSYC_VARIABLE_H
/**
* @file psyc/variable.h
*/
/// Routing variables in alphabetical order.
extern const psycString psyc_routing_vars[];
// Variable types in alphabetical order.
extern const psycMatchVar psyc_var_types[];
extern const size_t psyc_routing_vars_num;
extern const size_t psyc_var_types_num;
/**
* Is this a routing variable name?
*/
psycBool psyc_var_is_routing (const char *name, size_t len);
/**
* Get the type of variable name.
*/
psycType psyc_var_type (const char *name, size_t len);
/**
* Is this a list variable name?
*/
static inline
psycBool psyc_var_is_list (const char *name, size_t len)
{
return len < 5 || memcmp(name, "_list", 5) != 0 ||
(len > 5 && name[5] != '_') ? PSYC_FALSE : PSYC_TRUE;
}
#define PSYC_VARIABLE_H
#endif

View file

@ -74,14 +74,14 @@ PIKEFUN string psyc_text(string template, mapping vars) {
// FIXME: // FIXME:
char buffer[512]; char buffer[512];
psyc_initTextState(&state, (char *) STR0(template), template->len, buffer, 512); psyc_text_state_init(&state, (char *) STR0(template), template->len, buffer, 512);
do do
{ {
ret = psyc_text(&state, lookup_value_mapping, vars); ret = psyc_text(&state, lookup_value_mapping, vars);
len += psyc_getTextBytesWritten(&state); len += psyc_text_bytes_written(&state);
switch (ret) { switch (ret) {
case PSYC_TEXT_INCOMPLETE: // need to realloc buffer case PSYC_TEXT_INCOMPLETE: // need to realloc buffer
//psyc_setTextBuffer2(&state, buffer + len, BUFSIZE - len); //psyc_text_buffer_set(&state, buffer + len, BUFSIZE - len);
break; break;
case PSYC_TEXT_COMPLETE: // we're done case PSYC_TEXT_COMPLETE: // we're done
RETURN make_shared_binary_string(buffer, len); RETURN make_shared_binary_string(buffer, len);
@ -99,7 +99,7 @@ PIKEFUN string psyc_text(string template, mapping vars) {
*! 0 otherwise *! 0 otherwise
*/ */
PIKEFUN int is_routingvar(string name) { PIKEFUN int is_routingvar(string name) {
RETURN psyc_isRoutingVar2((char *) STR0(name), name->len); RETURN psyc_var_is_routing((char *) STR0(name), name->len);
} }
/*! @decl string render(mapping rvars, mapping evars, string method, string|void body) /*! @decl string render(mapping rvars, mapping evars, string method, string|void body)
@ -130,7 +130,7 @@ PIKEFUN string render(mapping rvars, mapping evars, string method, string|void b
if (k->ind.type == PIKE_T_STRING) { if (k->ind.type == PIKE_T_STRING) {
switch(k->val.type) { switch(k->val.type) {
case PIKE_T_STRING: case PIKE_T_STRING:
rheaders.modifiers[rheaders.lines++] = psyc_newModifier2(oper, rheaders.modifiers[rheaders.lines++] = psyc_modifier_new(oper,
(char *)STR0(k->ind.u.string), (char *)STR0(k->ind.u.string),
k->ind.u.string->len, k->ind.u.string->len,
(char *)STR0(k->val.u.string), (char *)STR0(k->val.u.string),
@ -197,10 +197,10 @@ PIKEFUN string render(mapping rvars, mapping evars, string method, string|void b
Pike_error("psyc_render: unsupported data type in list\n"); Pike_error("psyc_render: unsupported data type in list\n");
} }
} }
list = psyc_newList(elems, k->val.u.array->size, PSYC_LIST_CHECK_LENGTH); list = psyc_list_new(elems, k->val.u.array->size, PSYC_LIST_CHECK_LENGTH);
struct pike_string *listbuf = begin_shared_string(list.length); struct pike_string *listbuf = begin_shared_string(list.length);
psyc_renderList(&list, (char *) STR0(listbuf), listbuf->len); psyc_render_list(&list, (char *) STR0(listbuf), listbuf->len);
end_shared_string(listbuf); end_shared_string(listbuf);
val = (char *) STR0(listbuf); val = (char *) STR0(listbuf);
vallen = listbuf->len; vallen = listbuf->len;
@ -211,7 +211,7 @@ PIKEFUN string render(mapping rvars, mapping evars, string method, string|void b
Pike_error("psyc_render: unsupported value in evars\n"); Pike_error("psyc_render: unsupported value in evars\n");
break; break;
} }
eheaders.modifiers[eheaders.lines++] = psyc_newModifier2(oper, eheaders.modifiers[eheaders.lines++] = psyc_modifier_new(oper,
key, keylen, key, keylen,
val, vallen, val, vallen,
PSYC_MODIFIER_CHECK_LENGTH); PSYC_MODIFIER_CHECK_LENGTH);
@ -221,7 +221,7 @@ PIKEFUN string render(mapping rvars, mapping evars, string method, string|void b
} }
if (body != NULL) { if (body != NULL) {
packet = psyc_newPacket2(rheaders.modifiers, packet = psyc_packet_new(rheaders.modifiers,
rheaders.lines, rheaders.lines,
eheaders.modifiers, eheaders.modifiers,
eheaders.lines, eheaders.lines,
@ -229,7 +229,7 @@ PIKEFUN string render(mapping rvars, mapping evars, string method, string|void b
(const char *)STR0(body), body->len, (const char *)STR0(body), body->len,
PSYC_PACKET_CHECK_LENGTH); PSYC_PACKET_CHECK_LENGTH);
} else { // body arg was not given } else { // body arg was not given
packet = psyc_newPacket2(rheaders.modifiers, packet = psyc_packet_new(rheaders.modifiers,
rheaders.lines, rheaders.lines,
eheaders.modifiers, eheaders.modifiers,
eheaders.lines, eheaders.lines,
@ -260,7 +260,7 @@ PIKECLASS Parser {
CVAR struct string_builder incomplete; CVAR struct string_builder incomplete;
INIT { INIT {
psyc_initParseState(&THIS->parser); psyc_parse_state_init(&THIS->parser, PSYC_PARSE_ALL);
THIS->buffer = NULL; THIS->buffer = NULL;
THIS->handle_packet = find_identifier("handle_packet", Pike_fp->current_object->prog); THIS->handle_packet = find_identifier("handle_packet", Pike_fp->current_object->prog);
THIS->handle_error = find_identifier("handle_error", Pike_fp->current_object->prog); THIS->handle_error = find_identifier("handle_error", Pike_fp->current_object->prog);
@ -296,7 +296,7 @@ PIKECLASS Parser {
data = tmp; data = tmp;
THIS->buffer = NULL; THIS->buffer = NULL;
} }
psyc_setParseBuffer2(&THIS->parser, psyc_parse_buffer_set(&THIS->parser,
(char *) STR0(data), data->len); (char *) STR0(data), data->len);
do { do {
ret = psyc_parse(&THIS->parser, &oper, &name, &value); ret = psyc_parse(&THIS->parser, &oper, &name, &value);
@ -308,7 +308,7 @@ PIKECLASS Parser {
make_shared_binary_string(value.ptr, value.length)); make_shared_binary_string(value.ptr, value.length));
break; break;
case PSYC_PARSE_ENTITY_START: // entity var with length case PSYC_PARSE_ENTITY_START: // entity var with length
init_string_builder_alloc(&THIS->incomplete, psyc_getParseValueLength(&THIS->parser), 0); init_string_builder_alloc(&THIS->incomplete, psyc_parse_value_length(&THIS->parser), 0);
// fall thru // fall thru
case PSYC_PARSE_ENTITY_CONT: case PSYC_PARSE_ENTITY_CONT:
string_builder_append(&THIS->incomplete, MKPCHARP(value.ptr, 0), value.length); string_builder_append(&THIS->incomplete, MKPCHARP(value.ptr, 0), value.length);
@ -327,12 +327,12 @@ PIKECLASS Parser {
//printf("E %.*s -> %.*s\n", (int)name.length, name.ptr, (int)value.length, value.ptr); //printf("E %.*s -> %.*s\n", (int)name.length, name.ptr, (int)value.length, value.ptr);
do { do {
err = 0; err = 0;
int type = psyc_getVarType(&name); int type = psyc_var_type(PSYC_S2ARG(&name));
struct svalue sv; struct svalue sv;
time_t timmy; time_t timmy;
switch(type) { switch(type) {
case PSYC_TYPE_DATE: case PSYC_TYPE_DATE:
if (psyc_parseDate(&value, &timmy)) { if (psyc_parse_date(&value, &timmy)) {
sv.type = PIKE_T_INT; sv.u.integer = timmy; sv.type = PIKE_T_INT; sv.u.integer = timmy;
mapping_string_insert(THIS->evars, mapping_string_insert(THIS->evars,
make_shared_binary_string(name.ptr, name.length), make_shared_binary_string(name.ptr, name.length),
@ -342,7 +342,7 @@ PIKECLASS Parser {
} }
break; break;
case PSYC_TYPE_TIME: case PSYC_TYPE_TIME:
if (psyc_parseTime(&value, &timmy)) { if (psyc_parse_time(&value, &timmy)) {
sv.type = PIKE_T_INT; sv.u.integer = timmy; sv.type = PIKE_T_INT; sv.u.integer = timmy;
mapping_string_insert(THIS->evars, mapping_string_insert(THIS->evars,
make_shared_binary_string(name.ptr, name.length), make_shared_binary_string(name.ptr, name.length),
@ -382,10 +382,10 @@ PIKECLASS Parser {
psycParseListState listState; psycParseListState listState;
psycString elem = (psycString) {0, 0}; psycString elem = (psycString) {0, 0};
psyc_initParseListState(&listState); psyc_parse_list_state_init(&listState);
psyc_setParseListBuffer(&listState, value); psyc_parse_list_buffer_set(&listState, PSYC_S2ARG(value));
do { do {
retl = psyc_parseList(&listState, &elem); retl = psyc_parse_list(&listState, &elem);
switch(retl) { switch(retl) {
case PSYC_PARSE_LIST_END: // last element case PSYC_PARSE_LIST_END: // last element
retl = 0; retl = 0;
@ -421,7 +421,7 @@ PIKECLASS Parser {
} }
break; break;
case PSYC_PARSE_BODY_START: // if length was given this is used for body case PSYC_PARSE_BODY_START: // if length was given this is used for body
init_string_builder_alloc(&THIS->incomplete, psyc_getParseValueLength(&THIS->parser), 0); init_string_builder_alloc(&THIS->incomplete, psyc_parse_value_length(&THIS->parser), 0);
case PSYC_PARSE_BODY_CONT: case PSYC_PARSE_BODY_CONT:
string_builder_append(&THIS->incomplete, MKPCHARP(value.ptr, 0), value.length); string_builder_append(&THIS->incomplete, MKPCHARP(value.ptr, 0), value.length);
break; break;
@ -460,9 +460,9 @@ PIKECLASS Parser {
THIS->body = NULL; THIS->body = NULL;
break; break;
case PSYC_PARSE_INSUFFICIENT: // not enough data case PSYC_PARSE_INSUFFICIENT: // not enough data
if (psyc_getParseRemainingBuffer(&THIS->parser) > 0) { if (psyc_parse_remaining_buffer(&THIS->parser) > 0) {
THIS->buffer = make_shared_binary_string(psyc_getParseRemainingBuffer(&THIS->parser), THIS->buffer = make_shared_binary_string(psyc_parse_remaining_buffer(&THIS->parser),
psyc_getParseRemainingLength(&THIS->parser)); psyc_parse_remaining_length(&THIS->parser));
} }
return; return;
default: // fatal error default: // fatal error

View file

@ -11,8 +11,6 @@
#define unless(COND) if (!(COND)) #define unless(COND) if (!(COND))
#define until(COND) while (!(COND)) #define until(COND) while (!(COND))
#define PSYC_NUM_ELEM(a) (sizeof(a) / sizeof(*(a)))
#if !defined(__USE_GNU) && !(defined(__FBSDID) && defined(__BSD_VISIBLE)) #if !defined(__USE_GNU) && !(defined(__FBSDID) && defined(__BSD_VISIBLE))
void * memmem(const void *l, size_t l_len, const void *s, size_t s_len); void * memmem(const void *l, size_t l_len, const void *s, size_t s_len);
#endif #endif

View file

@ -100,6 +100,62 @@ failed:
return 1; return 1;
} }
/**
* Check if keyword is in array.
*
* @param array The array to search, should be ordered alphabetically.
* @param size Size of array.
* @param kw Keyword to look for.
* @param kwlen Length of keyword.
* @param inherit If true, look for any keyword inheriting from name,
otherwise only exact matches are returned.
* @param matching A temporary array used for keeping track of results.
* Should be the same size as the array we're searching.
*
* @return The value of the matched variable in the array.
*/
int psyc_in_array (const psycMatchVar *array, size_t size,
const char *kw, size_t kwlen,
psycBool inherit, int8_t *matching)
{
size_t cursor = 1;
uint8_t i, m = 0;
//memset(&matching, -1, sizeof matching);
if (kwlen < 2 || kw[0] != '_')
return 0;
// first find the keywords with matching length
for (i=0; i<size; i++)
if (kwlen == array[i].key.length ||
(inherit && kwlen > array[i].key.length && kw[array[i].key.length] == '_'))
matching[m++] = i;
matching[m] = -1; // mark the end of matching indexes
while (cursor < kwlen && matching[0] >= 0) {
for (i = m = 0; i < size; i++) {
if (matching[i] < 0)
break; // reached the end of possible matches
if (cursor < array[matching[i]].key.length &&
array[matching[i]].key.ptr[cursor] == kw[cursor])
matching[m++] = matching[i]; // found a match, update matching indexes
else if (cursor == array[matching[i]].key.length && kw[cursor] == '_')
return array[matching[0]].value; // _ after the end of a matching prefix
else if (array[matching[i]].key.ptr[cursor] > kw[cursor])
break; // passed the possible matches in alphabetical order in the array
}
if (m < size)
matching[m] = -1; // mark the end of matching indexes
cursor++;
}
// return first match if found
return matching[0] >= 0 ? array[matching[0]].value : 0;
}
#ifdef CMDTOOL #ifdef CMDTOOL
int main(int argc, char **argv) { int main(int argc, char **argv) {
if (argc != 3) { if (argc != 3) {

View file

@ -2,16 +2,8 @@
#include <psyc/syntax.h> #include <psyc/syntax.h>
#include <psyc/packet.h> #include <psyc/packet.h>
#include <math.h>
static inline
size_t psyc_getNumLength(size_t n)
{
return n < 10 ? 1 : log10(n) + 1;
}
inline inline
psycListFlag psyc_checkListLength (psycList *list) psycListFlag psyc_list_length_check (psycList *list)
{ {
psycListFlag flag = PSYC_LIST_NO_LENGTH; psycListFlag flag = PSYC_LIST_NO_LENGTH;
size_t i, length = 0; size_t i, length = 0;
@ -33,7 +25,7 @@ psycListFlag psyc_checkListLength (psycList *list)
} }
inline inline
psycListFlag psyc_getListLength (psycList *list) psycListFlag psyc_list_length (psycList *list)
{ {
size_t i, length = 0; size_t i, length = 0;
@ -43,7 +35,7 @@ psycListFlag psyc_getListLength (psycList *list)
{ {
if (i > 0) if (i > 0)
length++; // | length++; // |
length += psyc_getNumLength(list->elems[i].length) + 1 + list->elems[i].length; // length SP elem length += psyc_num_length(list->elems[i].length) + 1 + list->elems[i].length; // length SP elem
} }
} }
else else
@ -56,33 +48,33 @@ psycListFlag psyc_getListLength (psycList *list)
} }
inline inline
psycList psyc_newList(psycString *elems, size_t num_elems, psycListFlag flag) psycList psyc_list_new (psycString *elems, size_t num_elems, psycListFlag flag)
{ {
psycList list = {num_elems, elems, 0, flag}; psycList list = {num_elems, elems, 0, flag};
if (flag == PSYC_LIST_CHECK_LENGTH) // check if list elements need length if (flag == PSYC_LIST_CHECK_LENGTH) // check if list elements need length
list.flag = psyc_checkListLength(&list); list.flag = psyc_list_length_check(&list);
list.length = psyc_getListLength(&list); list.length = psyc_list_length(&list);
return list; return list;
} }
inline inline
size_t psyc_getModifierLength (psycModifier *m) size_t psyc_modifier_length (psycModifier *m)
{ {
size_t length = 1 + // oper size_t length = 1 + // oper
m->name.length + 1 + // name\t m->name.length + 1 + // name\t
m->value.length + 1; // value\n m->value.length + 1; // value\n
if (m->flag == PSYC_MODIFIER_NEED_LENGTH) // add length of length if needed if (m->flag == PSYC_MODIFIER_NEED_LENGTH) // add length of length if needed
length += psyc_getNumLength(m->value.length) + 1; // SP length length += psyc_num_length(m->value.length) + 1; // SP length
return length; return length;
} }
inline inline
psycPacketFlag psyc_checkPacketLength(psycPacket *p) psycPacketFlag psyc_packet_length_check (psycPacket *p)
{ {
if (p->data.length == 1 && p->data.ptr[0] == C_GLYPH_PACKET_DELIMITER) if (p->data.length == 1 && p->data.ptr[0] == C_GLYPH_PACKET_DELIMITER)
return PSYC_PACKET_NEED_LENGTH; return PSYC_PACKET_NEED_LENGTH;
@ -104,7 +96,7 @@ psycPacketFlag psyc_checkPacketLength(psycPacket *p)
} }
inline inline
size_t psyc_setPacketLength(psycPacket *p) size_t psyc_packet_length_set (psycPacket *p)
{ {
size_t i; size_t i;
p->routingLength = 0; p->routingLength = 0;
@ -112,7 +104,7 @@ size_t psyc_setPacketLength(psycPacket *p)
// add routing header length // add routing header length
for (i = 0; i < p->routing.lines; i++) for (i = 0; i < p->routing.lines; i++)
p->routingLength += psyc_getModifierLength(&(p->routing.modifiers[i])); p->routingLength += psyc_modifier_length(&(p->routing.modifiers[i]));
if (p->content.length) if (p->content.length)
p->contentLength = p->content.length; p->contentLength = p->content.length;
@ -120,7 +112,7 @@ size_t psyc_setPacketLength(psycPacket *p)
{ {
// add entity header length // add entity header length
for (i = 0; i < p->entity.lines; i++) for (i = 0; i < p->entity.lines; i++)
p->contentLength += psyc_getModifierLength(&(p->entity.modifiers[i])); p->contentLength += psyc_modifier_length(&(p->entity.modifiers[i]));
// add length of method, data & delimiter // add length of method, data & delimiter
if (p->method.length) if (p->method.length)
@ -136,60 +128,39 @@ size_t psyc_setPacketLength(psycPacket *p)
p->length++; // add \n at the start of the content part p->length++; // add \n at the start of the content part
if (p->flag == PSYC_PACKET_NEED_LENGTH) // add length of length if needed if (p->flag == PSYC_PACKET_NEED_LENGTH) // add length of length if needed
p->length += psyc_getNumLength(p->contentLength); p->length += psyc_num_length(p->contentLength);
return p->length; return p->length;
} }
inline inline
psycPacket psyc_newPacket (psycHeader *routing, psycHeader *entity, psycPacket psyc_packet_new (psycModifier *routing, size_t routinglen,
psycString *method, psycString *data,
psycPacketFlag flag)
{
psycPacket p = {*routing, *entity, *method, *data, {0,0}, 0, 0, flag};
if (flag == PSYC_PACKET_CHECK_LENGTH) // find out if it needs a length
p.flag = psyc_checkPacketLength(&p);
psyc_setPacketLength(&p);
return p;
}
inline
psycPacket psyc_newPacket2 (psycModifier *routing, size_t routinglen,
psycModifier *entity, size_t entitylen, psycModifier *entity, size_t entitylen,
const char *method, size_t methodlen, char *method, size_t methodlen,
const char *data, size_t datalen, char *data, size_t datalen,
psycPacketFlag flag) psycPacketFlag flag)
{ {
psycHeader r = {routinglen, routing}; psycPacket p = {{routinglen, routing}, {entitylen, entity},
psycHeader e = {entitylen, entity}; {methodlen, method}, {datalen, data}, {0,0}, 0, 0, flag};
psycString m = {methodlen, method};
psycString d = {datalen, data};
return psyc_newPacket(&r, &e, &m, &d, flag);
}
inline
psycPacket psyc_newRawPacket (psycHeader *routing, psycString *content,
psycPacketFlag flag)
{
psycPacket p = {*routing, {0,0}, {0,0}, {0,0}, *content, 0, 0, flag};
if (flag == PSYC_PACKET_CHECK_LENGTH) // find out if it needs a length if (flag == PSYC_PACKET_CHECK_LENGTH) // find out if it needs a length
p.flag = psyc_checkPacketLength(&p); p.flag = psyc_packet_length_check(&p);
psyc_setPacketLength(&p); psyc_packet_length_set(&p);
return p; return p;
} }
inline inline
psycPacket psyc_newRawPacket2 (psycModifier *routing, size_t routinglen, psycPacket psyc_packet_new_raw (psycModifier *routing, size_t routinglen,
const char *content, size_t contentlen, char *content, size_t contentlen,
psycPacketFlag flag) psycPacketFlag flag)
{ {
psycHeader r = {routinglen, routing}; psycPacket p = {{routinglen, routing}, {0,0}, {0,0}, {0,0},
psycString c = {contentlen, content}; {contentlen, content}, 0, 0, flag};
return psyc_newRawPacket(&r, &c, flag); if (flag == PSYC_PACKET_CHECK_LENGTH) // find out if it needs a length
p.flag = psyc_packet_length_check(&p);
psyc_packet_length_set(&p);
return p;
} }

View file

@ -29,12 +29,12 @@ typedef enum {
* @return PARSE_ERROR or PARSE_SUCCESS * @return PARSE_ERROR or PARSE_SUCCESS
*/ */
static inline static inline
parseRC psyc_parseKeyword (psycParseState *state, psycString *name) parseRC psyc_parse_keyword (psycParseState *state, psycString *name)
{ {
name->ptr = state->buffer.ptr + state->cursor; name->ptr = state->buffer.ptr + state->cursor;
name->length = 0; name->length = 0;
while (psyc_isKwChar(state->buffer.ptr[state->cursor])) while (psyc_is_kw_char(state->buffer.ptr[state->cursor]))
{ {
name->length++; // was a valid char, increase length name->length++; // was a valid char, increase length
ADVANCE_CURSOR_OR_RETURN(PSYC_PARSE_INSUFFICIENT); ADVANCE_CURSOR_OR_RETURN(PSYC_PARSE_INSUFFICIENT);
@ -54,7 +54,7 @@ parseRC psyc_parseKeyword (psycParseState *state, psycString *name)
* @return PARSE_COMPLETE or PARSE_INCOMPLETE * @return PARSE_COMPLETE or PARSE_INCOMPLETE
*/ */
static inline static inline
parseRC psyc_parseBinaryValue (psycParseState *state, psycString *value, parseRC psyc_parse_binary_value (psycParseState *state, psycString *value,
size_t *length, size_t *parsed) size_t *length, size_t *parsed)
{ {
size_t remaining = *length - *parsed; size_t remaining = *length - *parsed;
@ -81,13 +81,13 @@ parseRC psyc_parseBinaryValue (psycParseState *state, psycString *value,
* @return PARSE_ERROR or PARSE_SUCCESS * @return PARSE_ERROR or PARSE_SUCCESS
*/ */
static inline static inline
parseRC psyc_parseModifier (psycParseState *state, char *oper, parseRC psyc_parse_modifier (psycParseState *state, char *oper,
psycString *name, psycString *value) psycString *name, psycString *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);
parseRC ret = psyc_parseKeyword(state, name); parseRC ret = psyc_parse_keyword(state, name);
if (ret == PARSE_ERROR) if (ret == PARSE_ERROR)
return PSYC_PARSE_ERROR_MOD_NAME; return PSYC_PARSE_ERROR_MOD_NAME;
else if (ret != PARSE_SUCCESS) else if (ret != PARSE_SUCCESS)
@ -105,7 +105,7 @@ parseRC psyc_parseModifier (psycParseState *state, char *oper,
{ // After SP the length follows. { // After SP the length follows.
ADVANCE_CURSOR_OR_RETURN(PSYC_PARSE_INSUFFICIENT); ADVANCE_CURSOR_OR_RETURN(PSYC_PARSE_INSUFFICIENT);
if (psyc_isNumeric(state->buffer.ptr[state->cursor])) if (psyc_is_numeric(state->buffer.ptr[state->cursor]))
{ {
state->valueLengthFound = 1; state->valueLengthFound = 1;
do do
@ -113,7 +113,7 @@ parseRC psyc_parseModifier (psycParseState *state, char *oper,
length = 10 * length + state->buffer.ptr[state->cursor] - '0'; length = 10 * length + state->buffer.ptr[state->cursor] - '0';
ADVANCE_CURSOR_OR_RETURN(PSYC_PARSE_INSUFFICIENT); ADVANCE_CURSOR_OR_RETURN(PSYC_PARSE_INSUFFICIENT);
} }
while (psyc_isNumeric(state->buffer.ptr[state->cursor])); while (psyc_is_numeric(state->buffer.ptr[state->cursor]));
state->valueLength = length; state->valueLength = length;
} }
else else
@ -126,7 +126,7 @@ parseRC psyc_parseModifier (psycParseState *state, char *oper,
if (++(state->cursor) >= state->buffer.length) if (++(state->cursor) >= state->buffer.length)
return length ? PARSE_INCOMPLETE : PARSE_SUCCESS; // if length=0 we're done return length ? PARSE_INCOMPLETE : PARSE_SUCCESS; // if length=0 we're done
ret = psyc_parseBinaryValue(state, value, &(state->valueLength), &(state->valueParsed)); ret = psyc_parse_binary_value(state, value, &(state->valueLength), &(state->valueParsed));
if (ret == PARSE_INCOMPLETE) if (ret == PARSE_INCOMPLETE)
return ret; return ret;
@ -197,9 +197,9 @@ psycParseRC psyc_parse (psycParseState *state, char *oper,
// Each line of the header starts with a glyph, // Each line of the header starts with a glyph,
// i.e. :_name, -_name +_name etc, // i.e. :_name, -_name +_name etc,
// so just test if the first char is a glyph. // so just test if the first char is a glyph.
if (psyc_isGlyph(state->buffer.ptr[state->cursor])) // is the first char a glyph? if (psyc_is_glyph(state->buffer.ptr[state->cursor])) // is the first char a glyph?
{ // it is a glyph, so a variable starts here { // it is a glyph, so a variable starts here
ret = psyc_parseModifier(state, oper, name, value); ret = psyc_parse_modifier(state, oper, name, value);
state->routingLength += state->cursor - pos; state->routingLength += state->cursor - pos;
return ret == PARSE_SUCCESS ? PSYC_PARSE_ROUTING : ret; return ret == PARSE_SUCCESS ? PSYC_PARSE_ROUTING : ret;
} }
@ -212,7 +212,7 @@ psycParseRC psyc_parse (psycParseState *state, char *oper,
case PSYC_PART_LENGTH: case PSYC_PART_LENGTH:
// End of header, content starts with an optional length then a NL // End of header, content starts with an optional length then a NL
if (psyc_isNumeric(state->buffer.ptr[state->cursor])) if (psyc_is_numeric(state->buffer.ptr[state->cursor]))
{ {
state->contentLengthFound = 1; state->contentLengthFound = 1;
state->contentLength = 0; state->contentLength = 0;
@ -222,7 +222,7 @@ psycParseRC psyc_parse (psycParseState *state, char *oper,
state->contentLength = 10 * state->contentLength + state->buffer.ptr[state->cursor] - '0'; state->contentLength = 10 * state->contentLength + state->buffer.ptr[state->cursor] - '0';
ADVANCE_CURSOR_OR_RETURN(PSYC_PARSE_INSUFFICIENT); ADVANCE_CURSOR_OR_RETURN(PSYC_PARSE_INSUFFICIENT);
} }
while (psyc_isNumeric(state->buffer.ptr[state->cursor])); while (psyc_is_numeric(state->buffer.ptr[state->cursor]));
} }
if (state->buffer.ptr[state->cursor] == '\n') // start of content if (state->buffer.ptr[state->cursor] == '\n') // start of content
@ -257,7 +257,7 @@ psycParseRC psyc_parse (psycParseState *state, char *oper,
// In case of an incomplete binary variable resume parsing it. // In case of an incomplete binary variable resume parsing it.
if (state->valueParsed < state->valueLength) if (state->valueParsed < state->valueLength)
{ {
ret = psyc_parseBinaryValue(state, value, &(state->valueLength), &(state->valueParsed)); ret = psyc_parse_binary_value(state, value, &(state->valueLength), &(state->valueParsed));
state->contentParsed += value->length; state->contentParsed += value->length;
if (ret == PARSE_INCOMPLETE) if (ret == PARSE_INCOMPLETE)
@ -280,9 +280,9 @@ psycParseRC psyc_parse (psycParseState *state, char *oper,
// So just test if the first char is a glyph. // So just test if the first char is a glyph.
// In the body, the same applies, only that the // In the body, the same applies, only that the
// method does not start with a glyph. // method does not start with a glyph.
if (psyc_isGlyph(state->buffer.ptr[state->cursor])) if (psyc_is_glyph(state->buffer.ptr[state->cursor]))
{ {
ret = psyc_parseModifier(state, oper, name, value); ret = psyc_parse_modifier(state, oper, name, value);
state->contentParsed += state->cursor - pos; state->contentParsed += state->cursor - pos;
if (ret == PARSE_INCOMPLETE) if (ret == PARSE_INCOMPLETE)
@ -302,7 +302,7 @@ psycParseRC psyc_parse (psycParseState *state, char *oper,
case PSYC_PART_METHOD: case PSYC_PART_METHOD:
pos = state->cursor; pos = state->cursor;
ret = psyc_parseKeyword(state, name); ret = psyc_parse_keyword(state, name);
if (ret == PARSE_INSUFFICIENT) if (ret == PARSE_INSUFFICIENT)
return ret; return ret;
@ -352,7 +352,7 @@ psycParseRC psyc_parse (psycParseState *state, char *oper,
} }
if (state->valueParsed < state->valueLength) if (state->valueParsed < state->valueLength)
{ {
ret = psyc_parseBinaryValue(state, value, &(state->valueLength), &(state->valueParsed)); ret = psyc_parse_binary_value(state, value, &(state->valueLength), &(state->valueParsed));
state->contentParsed += value->length; state->contentParsed += value->length;
if (ret == PARSE_INCOMPLETE) if (ret == PARSE_INCOMPLETE)
@ -437,7 +437,7 @@ psycParseRC psyc_parse (psycParseState *state, char *oper,
#ifdef __INLINE_PSYC_PARSE #ifdef __INLINE_PSYC_PARSE
static inline static inline
#endif #endif
psycParseListRC psyc_parseList (psycParseListState *state, psycString *elem) psycParseListRC psyc_parse_list (psycParseListState *state, psycString *elem)
{ {
if (state->cursor >= state->buffer.length) if (state->cursor >= state->buffer.length)
return PSYC_PARSE_LIST_INCOMPLETE; return PSYC_PARSE_LIST_INCOMPLETE;
@ -452,7 +452,7 @@ psycParseListRC psyc_parseList (psycParseListState *state, psycString *elem)
state->type = PSYC_LIST_TEXT; state->type = PSYC_LIST_TEXT;
state->cursor++; state->cursor++;
} }
else if (psyc_isNumeric(state->buffer.ptr[state->cursor])) else if (psyc_is_numeric(state->buffer.ptr[state->cursor]))
state->type = PSYC_LIST_BINARY; state->type = PSYC_LIST_BINARY;
else else
return PSYC_PARSE_LIST_ERROR_TYPE; return PSYC_PARSE_LIST_ERROR_TYPE;
@ -480,14 +480,14 @@ psycParseListRC psyc_parseList (psycParseListState *state, psycString *elem)
if (!(state->elemParsed < state->elemLength)) if (!(state->elemParsed < state->elemLength))
{ {
// Element starts with a number. // Element starts with a number.
if (psyc_isNumeric(state->buffer.ptr[state->cursor])) if (psyc_is_numeric(state->buffer.ptr[state->cursor]))
{ {
do do
{ {
state->elemLength = 10 * state->elemLength + state->buffer.ptr[state->cursor] - '0'; state->elemLength = 10 * state->elemLength + state->buffer.ptr[state->cursor] - '0';
ADVANCE_CURSOR_OR_RETURN(PSYC_PARSE_LIST_INCOMPLETE); ADVANCE_CURSOR_OR_RETURN(PSYC_PARSE_LIST_INCOMPLETE);
} }
while (psyc_isNumeric(state->buffer.ptr[state->cursor])); while (psyc_is_numeric(state->buffer.ptr[state->cursor]));
} }
else else
return PSYC_PARSE_LIST_ERROR_LEN; return PSYC_PARSE_LIST_ERROR_LEN;
@ -504,7 +504,7 @@ psycParseListRC psyc_parseList (psycParseListState *state, psycString *elem)
// Start or resume parsing the binary data // Start or resume parsing the binary data
if (state->elemParsed < state->elemLength) if (state->elemParsed < state->elemLength)
{ {
if (psyc_parseBinaryValue((psycParseState*)state, elem, if (psyc_parse_binary_value((psycParseState*)state, elem,
&(state->elemLength), &(state->elemParsed)) == PARSE_INCOMPLETE) &(state->elemLength), &(state->elemParsed)) == PARSE_INCOMPLETE)
return PSYC_PARSE_LIST_INCOMPLETE; return PSYC_PARSE_LIST_INCOMPLETE;

View file

@ -7,7 +7,7 @@ struct psycParser;
* @param pstate pointer to an allocated * @param pstate pointer to an allocated
* psycParser struct. * psycParser struct.
*/ */
void psyc_initState(struct psycParser* pstate); void psyc_parse_state_init(struct psycParser* pstate);
/** @brief parses a packet /** @brief parses a packet
@ -38,7 +38,7 @@ void psyc_initState(struct psycParser* pstate);
* raw data that is to be processed. * raw data that is to be processed.
* @param length the amount of bytes to parse * @param length the amount of bytes to parse
* @param pstate pointer to a preallocated * @param pstate pointer to a preallocated
* and initialized (psyc_initState) * and initialized (psyc_parse_state_init)
* instance of the struct state * instance of the struct state
* *
*/ */

View file

@ -5,7 +5,7 @@
#ifdef __INLINE_PSYC_RENDER #ifdef __INLINE_PSYC_RENDER
static inline static inline
#endif #endif
psycRenderListRC psyc_renderList (psycList *list, char *buffer, size_t buflen) psycRenderListRC psyc_render_list (psycList *list, char *buffer, size_t buflen)
{ {
size_t i, cur = 0; size_t i, cur = 0;
psycString *elem; psycString *elem;
@ -43,7 +43,7 @@ psycRenderListRC psyc_renderList (psycList *list, char *buffer, size_t buflen)
} }
static inline static inline
size_t psyc_renderModifier (psycModifier *mod, char *buffer) size_t psyc_render_modifier (psycModifier *mod, char *buffer)
{ {
size_t cur = 0; size_t cur = 0;
@ -80,7 +80,7 @@ psycRenderRC psyc_render (psycPacket *packet, char *buffer, size_t buflen)
// render routing modifiers // render routing modifiers
for (i = 0; i < packet->routing.lines; i++) for (i = 0; i < packet->routing.lines; i++)
{ {
len = psyc_renderModifier(&packet->routing.modifiers[i], buffer + cur); len = psyc_render_modifier(&packet->routing.modifiers[i], buffer + cur);
cur += len; cur += len;
if (len <= 1) if (len <= 1)
return PSYC_RENDER_ERROR_MODIFIER_NAME_MISSING; return PSYC_RENDER_ERROR_MODIFIER_NAME_MISSING;
@ -103,7 +103,7 @@ psycRenderRC psyc_render (psycPacket *packet, char *buffer, size_t buflen)
{ {
// render entity modifiers // render entity modifiers
for (i = 0; i < packet->entity.lines; i++) for (i = 0; i < packet->entity.lines; i++)
cur += psyc_renderModifier(&packet->entity.modifiers[i], buffer + cur); cur += psyc_render_modifier(&packet->entity.modifiers[i], buffer + cur);
if (packet->method.length) // add method\n if (packet->method.length) // add method\n
{ {

View file

@ -3,7 +3,7 @@
#include "psyc/uniform.h" #include "psyc/uniform.h"
#include "psyc/parse.h" #include "psyc/parse.h"
int psyc_parseUniform2 (psycUniform *uni, const char *str, size_t length) int psyc_uniform_parse (psycUniform *uni, char *str, size_t length)
{ {
char c; char c;
psycString *p; psycString *p;
@ -19,7 +19,7 @@ int psyc_parseUniform2 (psycUniform *uni, const char *str, size_t length)
uni->scheme.ptr = str; uni->scheme.ptr = str;
uni->scheme.length = pos++; uni->scheme.length = pos++;
break; break;
} else if (!psyc_isHostChar(c)) } else if (!psyc_is_host_char(c))
return PSYC_PARSE_UNIFORM_INVALID_SCHEME; return PSYC_PARSE_UNIFORM_INVALID_SCHEME;
pos++; pos++;
} }
@ -52,7 +52,7 @@ int psyc_parseUniform2 (psycUniform *uni, const char *str, size_t length)
break; break;
case PSYC_UNIFORM_HOST: case PSYC_UNIFORM_HOST:
if (psyc_isHostChar(c)) { if (psyc_is_host_char(c)) {
uni->host.length++; uni->host.length++;
break; break;
} }
@ -74,7 +74,7 @@ int psyc_parseUniform2 (psycUniform *uni, const char *str, size_t length)
break; break;
case PSYC_UNIFORM_PORT: case PSYC_UNIFORM_PORT:
if (psyc_isNumeric(c)) { if (psyc_is_numeric(c)) {
uni->port.length++; uni->port.length++;
break; break;
} }
@ -118,7 +118,7 @@ int psyc_parseUniform2 (psycUniform *uni, const char *str, size_t length)
break; break;
case PSYC_UNIFORM_RESOURCE: case PSYC_UNIFORM_RESOURCE:
if (psyc_isNameChar(c)) { if (psyc_is_name_char(c)) {
uni->resource.length++; uni->resource.length++;
break; break;
} else if (c == '#') { } else if (c == '#') {
@ -129,7 +129,7 @@ int psyc_parseUniform2 (psycUniform *uni, const char *str, size_t length)
} else return PSYC_PARSE_UNIFORM_INVALID_RESOURCE; } else return PSYC_PARSE_UNIFORM_INVALID_RESOURCE;
case PSYC_UNIFORM_CHANNEL: case PSYC_UNIFORM_CHANNEL:
if (psyc_isNameChar(c)) { if (psyc_is_name_char(c)) {
uni->channel.length++; uni->channel.length++;
break; break;
} else return PSYC_PARSE_UNIFORM_INVALID_CHANNEL; } else return PSYC_PARSE_UNIFORM_INVALID_CHANNEL;
@ -165,8 +165,3 @@ int psyc_parseUniform2 (psycUniform *uni, const char *str, size_t length)
uni->valid = 1; uni->valid = 1;
return uni->type; return uni->type;
} }
int psyc_parseUniform (psycUniform *uni, psycString *str)
{
return psyc_parseUniform2(uni, str->ptr, str->length);
}

View file

@ -3,7 +3,7 @@
/// Routing variables in alphabetical order. /// Routing variables in alphabetical order.
const psycString psyc_routingVars[] = const psycString psyc_routing_vars[] =
{ {
PSYC_C2STR("_amount_fragments"), PSYC_C2STR("_amount_fragments"),
PSYC_C2STR("_context"), PSYC_C2STR("_context"),
@ -27,7 +27,7 @@ const psycString psyc_routingVars[] =
}; };
// Variable types in alphabetical order. // Variable types in alphabetical order.
const psycMatchVar psyc_varTypes[] = const psycMatchVar psyc_var_types[] =
{ {
{PSYC_C2STR("_amount"), PSYC_TYPE_AMOUNT}, {PSYC_C2STR("_amount"), PSYC_TYPE_AMOUNT},
{PSYC_C2STR("_color"), PSYC_TYPE_COLOR}, {PSYC_C2STR("_color"), PSYC_TYPE_COLOR},
@ -43,43 +43,42 @@ const psycMatchVar psyc_varTypes[] =
{PSYC_C2STR("_time"), PSYC_TYPE_TIME}, {PSYC_C2STR("_time"), PSYC_TYPE_TIME},
}; };
const size_t psyc_routingVarsNum = PSYC_NUM_ELEM(psyc_routingVars); const size_t psyc_routing_vars_num = PSYC_NUM_ELEM(psyc_routing_vars);
const size_t psyc_varTypesNum = PSYC_NUM_ELEM(psyc_varTypes); const size_t psyc_var_types_num = PSYC_NUM_ELEM(psyc_var_types);
/** /**
* Get the type of variable name. * Get the type of variable name.
*/ */
inline inline
psycBool psyc_isRoutingVar2(const char *name, size_t len) psycBool psyc_var_is_routing (const char *name, size_t len)
{ {
//return psyc_matchArray(psyc_routingVars, PSYC_NUM_ELEM(psyc_routingVars), name, len, 0);
size_t cursor = 1; size_t cursor = 1;
uint8_t i, m = 0; uint8_t i, m = 0;
int8_t matching[psyc_routingVarsNum]; // indexes of matching vars int8_t matching[psyc_routing_vars_num]; // indexes of matching vars
if (len < 2 || name[0] != '_') if (len < 2 || name[0] != '_')
return PSYC_FALSE; return PSYC_FALSE;
// first find the vars with matching length // first find the vars with matching length
for (i=0; i<psyc_routingVarsNum; i++) for (i=0; i<psyc_routing_vars_num; i++)
if (len == psyc_routingVars[i].length) if (len == psyc_routing_vars[i].length)
matching[m++] = i; matching[m++] = i;
matching[m] = -1; // mark the end of matching indexes matching[m] = -1; // mark the end of matching indexes
while (cursor < len && matching[0] >= 0) while (cursor < len && matching[0] >= 0)
{ {
for (i = m = 0; i < psyc_routingVarsNum; i++) for (i = m = 0; i < psyc_routing_vars_num; i++)
{ {
if (matching[i] < 0) if (matching[i] < 0)
break; // reached the end of possible matches break; // reached the end of possible matches
if (psyc_routingVars[matching[i]].ptr[cursor] == name[cursor]) if (psyc_routing_vars[matching[i]].ptr[cursor] == name[cursor])
matching[m++] = matching[i]; // found a match, update matching indexes matching[m++] = matching[i]; // found a match, update matching indexes
else if (psyc_routingVars[matching[i]].ptr[cursor] > name[cursor]) else if (psyc_routing_vars[matching[i]].ptr[cursor] > name[cursor])
break; // passed the possible matches in alphabetical order in the array break; // passed the possible matches in alphabetical order in the array
} }
if (m < psyc_routingVarsNum) if (m < psyc_routing_vars_num)
matching[m] = -1; // mark the end of matching indexes matching[m] = -1; // mark the end of matching indexes
cursor++; cursor++;
@ -88,114 +87,13 @@ psycBool psyc_isRoutingVar2(const char *name, size_t len)
return matching[0] >= 0 ? PSYC_TRUE : PSYC_FALSE; return matching[0] >= 0 ? PSYC_TRUE : PSYC_FALSE;
} }
psycBool psyc_isRoutingVar(psycString *name)
{
return psyc_isRoutingVar2(name->ptr, name->length);
}
/** /**
* Get the type of variable name. * Get the type of variable name.
*/ */
inline inline
psycType psyc_getVarType2(const char *name, size_t len) psycType psyc_var_type (const char *name, size_t len)
{ {
//return psyc_matchArray(psyc_varTypes, PSYC_NUM_ELEM(psyc_varTypes), name, len, 1); int8_t m[psyc_var_types_num];
size_t cursor = 1; return psyc_in_array(psyc_var_types, psyc_var_types_num,
uint8_t i, m = 0; name, len, PSYC_YES, (int8_t*)&m);
int8_t matching[psyc_varTypesNum]; // indexes of matching vars
if (len < 2 || name[0] != '_')
return 0;
// first find the vars with matching length
for (i=0; i<psyc_varTypesNum; i++)
if (len == psyc_varTypes[i].name.length || (len > psyc_varTypes[i].name.length && name[psyc_varTypes[i].name.length] == '_'))
matching[m++] = i;
matching[m] = -1; // mark the end of matching indexes
while (cursor < len && matching[0] >= 0)
{
for (i = m = 0; i < psyc_varTypesNum; i++)
{
if (matching[i] < 0)
break; // reached the end of possible matches
if (cursor < psyc_varTypes[matching[i]].name.length &&
psyc_varTypes[matching[i]].name.ptr[cursor] == name[cursor])
matching[m++] = matching[i]; // found a match, update matching indexes
else if (cursor == psyc_varTypes[matching[i]].name.length && name[cursor] == '_')
return psyc_varTypes[matching[0]].value; // _ after the end of a matching prefix
else if (psyc_varTypes[matching[i]].name.ptr[cursor] > name[cursor])
break; // passed the possible matches in alphabetical order in the array
}
if (m < psyc_varTypesNum)
matching[m] = -1; // mark the end of matching indexes
cursor++;
}
// return first match if found
return matching[0] >= 0 ? psyc_varTypes[matching[0]].value : 0;
}
psycType psyc_getVarType(psycString *name)
{
return psyc_getVarType2(name->ptr, name->length);
}
/**
* Search for a variable name in an array.
*
* @param array The array to search, should be ordered alphabetically.
* @param size Size of array.
* @param name Name of variable to look for.
* @param namelen Length of name.
* @param startswith If true, look for any variable starting with name,
otherwise only exact matches are returned.
* @param matching A temporary array used for keeping track of results.
* Should be the same size as the array we're searching.
*
* @return The value of the matched variable in the array.
*/
int psyc_findVar(const psycMatchVar *array, size_t size,
const char *name, size_t namelen,
uint8_t startswith, int8_t *matching)
{
size_t cursor = 1;
uint8_t i, m = 0;
//memset(&matching, -1, sizeof matching);
if (namelen < 2 || name[0] != '_')
return 0;
// first find the vars with matching length
for (i=0; i<size; i++)
if (namelen == array[i].name.length ||
(startswith && namelen > array[i].name.length &&
name[array[i].name.length] == '_'))
matching[m++] = i;
matching[m] = -1; // mark the end of matching indexes
while (cursor < namelen && matching[0] >= 0)
{
for (i = m = 0; i < size; i++)
{
if (matching[i] < 0)
break;
if (array[matching[i]].name.ptr[cursor] == name[cursor])
matching[m++] = matching[i]; // found a match, update matching indexes
else if (array[matching[i]].name.ptr[cursor] > name[cursor])
break; // passed the possible matches in alphabetical order
}
if (m < size)
matching[m] = -1; // mark the end of matching indexes
cursor++;
}
// return first match if found
return matching[0] >= 0 ? array[matching[0]].value : 0;
} }

View file

@ -3,7 +3,7 @@ DEBUG = 2
CFLAGS = -I../include -I../src -Wall -std=c99 ${OPT} CFLAGS = -I../include -I../src -Wall -std=c99 ${OPT}
LDFLAGS = -L../lib LDFLAGS = -L../lib
LOADLIBES = -lpsyc -lm LOADLIBES = -lpsyc -lm
TARGETS = testPsyc testPsycSpeed testParser testMatch testRender testText isRoutingVar getVarType parseUniform TARGETS = test_psyc test_psyc_speed test_parser test_match test_render test_text var_is_routing var_type uniform_parse
O = test.o O = test.o
WRAPPER = WRAPPER =
DIET = diet DIET = diet
@ -20,16 +20,16 @@ endif
all: ${TARGETS} all: ${TARGETS}
it: all it: all
testPsyc: LOADLIBES := ${LOADLIBES} ${LOADLIBES_NET} test_psyc: LOADLIBES := ${LOADLIBES} ${LOADLIBES_NET}
testPsycSpeed: LOADLIBES := ${LOADLIBES} ${LOADLIBES_NET} test_psyc_speed: LOADLIBES := ${LOADLIBES} ${LOADLIBES_NET}
#testPsycSpeed: LOADLIBES := ${LOADLIBES_NET} #test_psyc_speed: LOADLIBES := ${LOADLIBES_NET}
testJson: LOADLIBES := ${LOADLIBES_NET} -ljson test_json: LOADLIBES := ${LOADLIBES_NET} -ljson
testJsonGlib: CFLAGS := ${CFLAGS} -I/usr/include/json-glib-1.0 -I/usr/include/glib-2.0 -I/usr/lib/glib-2.0/include test_json_glib: CFLAGS := ${CFLAGS} -I/usr/include/json-glib-1.0 -I/usr/include/glib-2.0 -I/usr/lib/glib-2.0/include
testJsonGlib: LOADLIBES := ${LOADLIBES_NET} -ljson-glib-1.0 test_json_glib: LOADLIBES := ${LOADLIBES_NET} -ljson-glib-1.0
testStrlen: LOADLIBES := ${LOADLIBES_NET} test_strlen: LOADLIBES := ${LOADLIBES_NET}
diet: WRAPPER = ${DIET} diet: WRAPPER = ${DIET}
diet: all diet: all
@ -42,14 +42,14 @@ clean:
rm -f ${TARGETS} $O rm -f ${TARGETS} $O
test: ${TARGETS} test: ${TARGETS}
./testRender ./test_render
./testMatch ./test_match
./testText ./test_text
./isRoutingVar ./var_is_routing
./getVarType ./var_type
./parseUniform ./uniform_parse
x=0; for f in packets/[0-9]*; do echo ">> $$f"; ./testPsyc -f $$f | ${DIFF} -u $$f -; x=$$((x+$$?)); done; exit $$x x=0; for f in packets/[0-9]*; do echo ">> $$f"; ./test_psyc -f $$f | ${DIFF} -u $$f -; x=$$((x+$$?)); done; exit $$x
x=0; for f in packets/[0-9]*; do echo ">> $$f"; ./testPsyc -rf $$f | ${DIFF} -u $$f -; x=$$((x+$$?)); done; exit $$x x=0; for f in packets/[0-9]*; do echo ">> $$f"; ./test_psyc -rf $$f | ${DIFF} -u $$f -; x=$$((x+$$?)); done; exit $$x
.NOTPARALLEL: nettestrun .NOTPARALLEL: nettestrun
@ -58,12 +58,12 @@ nettest: nettestfull nettestsplit
nettestrun: srvstart pkt srvkill nettestrun: srvstart pkt srvkill
nettestfull: nettestfull:
${MAKE} nettestrun; x=$$?; pkill -x testPsyc; exit $$x ${MAKE} nettestrun; x=$$?; pkill -x test_psyc; exit $$x
${MAKE} nettestrun srv_args=-r; x=$$?; pkill -x testPsyc; exit $$x ${MAKE} nettestrun srv_args=-r; x=$$?; pkill -x test_psyc; exit $$x
split_max = 10 split_max = 10
nettestsplit: nettestsplit:
x=0; for n in `seq 1 ${split_max}`; do ${MAKE} nettestrun srv_args="-b $$n" && ${MAKE} nettestrun srv_args="-r -b $$n" || break; done; x=$$?; pkill -x testPsyc; exit $$x x=0; for n in `seq 1 ${split_max}`; do ${MAKE} nettestrun srv_args="-b $$n" && ${MAKE} nettestrun srv_args="-r -b $$n" || break; done; x=$$?; pkill -x test_psyc; exit $$x
pkt: pkt:
x=0; for f in packets/[0-9]*; do echo ">> $$f"; cat $$f | nc localhost ${PORT} | ${DIFF} -u $$f -; x=$$((x+$$?)); done; exit $$x x=0; for f in packets/[0-9]*; do echo ">> $$f"; cat $$f | nc localhost ${PORT} | ${DIFF} -u $$f -; x=$$((x+$$?)); done; exit $$x
@ -75,37 +75,37 @@ pkterr:
for f in packets/err-*; do echo ">> $$f"; cat $$f | nc localhost ${PORT}; done for f in packets/err-*; do echo ">> $$f"; cat $$f | nc localhost ${PORT}; done
srvstart: srvstart:
pkill -x testPsyc; exit 0 pkill -x test_psyc; exit 0
./testPsyc -p ${PORT} -S ${srv_args} & ./test_psyc -p ${PORT} -S ${srv_args} &
srvkill: srvkill:
pkill -x testPsyc pkill -x test_psyc
bench: bench-genpkts bench-psyc bench-psyc-bin bench-json bench-json-bin bench-xml bench: bench-genpkts bench-psyc bench-psyc-bin bench-json bench-json-bin bench-xml
bench-dir: bench-dir:
@mkdir -p ../bench/results @mkdir -p ../bench/results
bench-psyc: bench-dir testStrlen testPsycSpeed bench-psyc: bench-dir test_strlen test_psyc_speed
for f in ../bench/packets/*.psyc; do bf=`basename $$f`; echo strlen: $$bf; ./testStrlen -sc 1000000 -f $$f | ${TEE} -a ../bench/results/$$bf.strlen; done for f in ../bench/packets/*.psyc; do bf=`basename $$f`; echo strlen: $$bf; ./test_strlen -sc 1000000 -f $$f | ${TEE} -a ../bench/results/$$bf.strlen; done
for f in ../bench/packets/*.psyc; do bf=`basename $$f`; echo libpsyc: $$f; ./testPsycSpeed -sc 1000000 -f $$f | ${TEE} -a ../bench/results/$$bf; done for f in ../bench/packets/*.psyc; do bf=`basename $$f`; echo libpsyc: $$f; ./test_psyc_speed -sc 1000000 -f $$f | ${TEE} -a ../bench/results/$$bf; done
bench-psyc-bin: bench-dir testStrlen testPsycSpeed bench-psyc-bin: bench-dir test_strlen test_psyc_speed
for f in `ls ../bench/packets/binary/*.psyc | sort -r`; do bf=`basename $$f`; echo "libpsyc: $$f * 1000000"; ./testPsycSpeed -sc 1000000 -f $$f | ${TEE} -a ../bench/results/$$bf; done for f in `ls ../bench/packets/binary/*.psyc | sort -r`; do bf=`basename $$f`; echo "libpsyc: $$f * 1000000"; ./test_psyc_speed -sc 1000000 -f $$f | ${TEE} -a ../bench/results/$$bf; done
c=1000000; for f in `ls ../bench/packets/binary/*.psyc | sort -r`; do bf=`basename $$f`; echo "strlen: $$bf * $$c"; ./testStrlen -sc $$c -f $$f | ${TEE} -a ../bench/results/$$bf.strlen; c=$$((c/10)); done c=1000000; for f in `ls ../bench/packets/binary/*.psyc | sort -r`; do bf=`basename $$f`; echo "strlen: $$bf * $$c"; ./test_strlen -sc $$c -f $$f | ${TEE} -a ../bench/results/$$bf.strlen; c=$$((c/10)); done
bench-json: bench-dir testJson testJsonGlib bench-json: bench-dir test_json test_json_glib
# for f in ../bench/packets/*.json; do bf=`basename $$f`; echo strlen: $$bf; ./testStrlen -sc 1000000 -f $$f | ${TEE} -a ../bench/results/$$bf.strlen; done # for f in ../bench/packets/*.json; do bf=`basename $$f`; echo strlen: $$bf; ./test_strlen -sc 1000000 -f $$f | ${TEE} -a ../bench/results/$$bf.strlen; done
for f in ../bench/packets/*.json; do bf=`basename $$f`; echo json-c: $$bf; ./testJson -snc 1000000 -f $$f | ${TEE} -a ../bench/results/$$bf; done for f in ../bench/packets/*.json; do bf=`basename $$f`; echo json-c: $$bf; ./test_json -snc 1000000 -f $$f | ${TEE} -a ../bench/results/$$bf; done
for f in ../bench/packets/*.json; do bf=`basename $$f`; echo json-glib: $$bf; ./testJsonGlib -snc 1000000 -f $$f | ${TEE} -a ../bench/results/$$bf-glib; done for f in ../bench/packets/*.json; do bf=`basename $$f`; echo json-glib: $$bf; ./test_json_glib -snc 1000000 -f $$f | ${TEE} -a ../bench/results/$$bf-glib; done
bench-json-bin: bench-dir testJson testJsonGlib bench-json-bin: bench-dir test_json test_json_glib
c=1000000; for f in `ls ../bench/packets/binary/*.json | sort -r`; do bf=`basename $$f`; echo "json-c: $$bf * $$c"; ./testJson -snc $$c -f $$f | ${TEE} -a ../bench/results/$$bf; c=$$((c/10)); done c=1000000; for f in `ls ../bench/packets/binary/*.json | sort -r`; do bf=`basename $$f`; echo "json-c: $$bf * $$c"; ./test_json -snc $$c -f $$f | ${TEE} -a ../bench/results/$$bf; c=$$((c/10)); done
c=1000000; for f in `ls ../bench/packets/binary/*.json | sort -r`; do bf=`basename $$f`; echo "json-glib: $$bf * $$c"; ./testJsonGlib -snc $$c -f $$f | ${TEE} -a ../bench/results/$$bf-glib; c=$$((c/10)); done c=1000000; for f in `ls ../bench/packets/binary/*.json | sort -r`; do bf=`basename $$f`; echo "json-glib: $$bf * $$c"; ./test_json_glib -snc $$c -f $$f | ${TEE} -a ../bench/results/$$bf-glib; c=$$((c/10)); done
bench-xml: bench-dir bench-xml: bench-dir
@[[ -n "${xmlbench}" ]] || (echo xmlbench path needs to be set with xmlbench=/path/to/xmlbench; exit 1) @[[ -n "${xmlbench}" ]] || (echo xmlbench path needs to be set with xmlbench=/path/to/xmlbench; exit 1)
# for f in ../bench/packets/*.xml; do bf=`basename $$f`; echo strlen: $$bf; ./testStrlen -sc 1000000 -f $$f | ${TEE} -a ../bench/results/$$bf.strlen; done # for f in ../bench/packets/*.xml; do bf=`basename $$f`; echo strlen: $$bf; ./test_strlen -sc 1000000 -f $$f | ${TEE} -a ../bench/results/$$bf.strlen; done
for f in ../bench/packets/*.xml; do bf=`basename $$f`; echo libxml: $$bf; ${xmlbench}/parse/libxml 1000000 $$f | ${TEE} -a ../bench/results/$$bf-libxml; done for f in ../bench/packets/*.xml; do bf=`basename $$f`; echo libxml: $$bf; ${xmlbench}/parse/libxml 1000000 $$f | ${TEE} -a ../bench/results/$$bf-libxml; done
for f in ../bench/packets/*.xml; do bf=`basename $$f`; echo libxml-sax: $$bf; ${xmlbench}/parse/libxml-sax 1000000 $$f | ${TEE} -a ../bench/results/$$bf-libxml-sax; done for f in ../bench/packets/*.xml; do bf=`basename $$f`; echo libxml-sax: $$bf; ${xmlbench}/parse/libxml-sax 1000000 $$f | ${TEE} -a ../bench/results/$$bf-libxml-sax; done
for f in ../bench/packets/*.xml; do bf=`basename $$f`; echo rapidxml: $$bf; ${xmlbench}/parse/rapidxml 1000000 $$f | ${TEE} -a ../bench/results/$$bf-rapidxml; done for f in ../bench/packets/*.xml; do bf=`basename $$f`; echo rapidxml: $$bf; ${xmlbench}/parse/rapidxml 1000000 $$f | ${TEE} -a ../bench/results/$$bf-rapidxml; done

View file

@ -1,21 +0,0 @@
#include <psyc.h>
#include <unistd.h>
#include <stdio.h>
#include <fcntl.h>
#include <lib.h>
int main() {
unless (psyc_getVarType2(PSYC_C2ARG("_list"))) return 1;
unless (psyc_getVarType2(PSYC_C2ARG("_list_foo"))) return 2;
unless (psyc_getVarType2(PSYC_C2ARG("_color_red"))) return 3;
if (psyc_getVarType2(PSYC_C2ARG("_last"))) return 4;
if (psyc_getVarType2(PSYC_C2ARG("_lost_foo"))) return 5;
if (psyc_getVarType2(PSYC_C2ARG("_colorful"))) return 6;
if (psyc_getVarType2(PSYC_C2ARG("_foo"))) return 7;
if (psyc_getVarType2(PSYC_C2ARG("bar"))) return 8;
if (psyc_getVarType2(PSYC_C2ARG("______"))) return 9;
if (psyc_getVarType2(PSYC_C2ARG("_"))) return 10;
puts("psyc_getVarType passed all tests.");
return 0; // passed all tests
}

View file

@ -1,39 +0,0 @@
#include <psyc.h>
#include <unistd.h>
#include <stdio.h>
#include <fcntl.h>
#include <lib.h>
int main() {
#if 0
const char* vars[] =
{
"_source",
"_source_relay",
"_source_foo",
"_sourcherry",
"_foo",
"bar",
"_",
};
int i;
for (i = 0; i < sizeof(vars) / sizeof(*vars); i++)
{
printf(">> %s: %d %d\n", vars[i], sizeof(vars[i]), sizeof(*vars[i]));
printf("%s: %d\n", vars[i], psyc_isRoutingVar2(vars[i], strlen(vars[i])));
}
#else
unless (psyc_isRoutingVar2(PSYC_C2ARG("_source"))) return 1;
unless (psyc_isRoutingVar2(PSYC_C2ARG("_source_relay"))) return 2;
if (psyc_isRoutingVar2(PSYC_C2ARG("_source_foo"))) return 3;
if (psyc_isRoutingVar2(PSYC_C2ARG("_sourcherry"))) return 4;
if (psyc_isRoutingVar2(PSYC_C2ARG("_sour"))) return 5;
if (psyc_isRoutingVar2(PSYC_C2ARG("_foo"))) return 6;
if (psyc_isRoutingVar2(PSYC_C2ARG("bar"))) return 7;
if (psyc_isRoutingVar2(PSYC_C2ARG("_"))) return 8;
puts("psyc_isRoutingVar passed all tests.");
#endif
return 0; // passed all tests
}

View file

@ -28,12 +28,9 @@ int main (int argc, char **argv)
printf(">> PARSE\n"); printf(">> PARSE\n");
} }
if (routing_only) psyc_parse_state_init(&state, routing_only ?
psyc_initParseState2(&state, PSYC_PARSE_ROUTING_ONLY); PSYC_PARSE_ROUTING_ONLY : PSYC_PARSE_ALL);
else psyc_parse_buffer_set(&state, buffer, idx);
psyc_initParseState(&state);
psyc_setParseBuffer2(&state, buffer, idx);
// try parsing that now // try parsing that now
do do
@ -59,15 +56,15 @@ int main (int argc, char **argv)
(int)name.length, name.ptr, (int)name.length, name.ptr,
(int)value.length, value.ptr); (int)value.length, value.ptr);
if (psyc_isListVar(&name)) if (psyc_var_is_list(PSYC_S2ARG(name)))
{ {
if (verbose) if (verbose)
printf(">> LIST START\n"); printf(">> LIST START\n");
psyc_initParseListState(&listState); psyc_parse_list_state_init(&listState);
psyc_setParseListBuffer(&listState, value); psyc_parse_list_buffer_set(&listState, PSYC_S2ARG(value));
while ((ret = psyc_parseList(&listState, &elem))) while ((ret = psyc_parse_list(&listState, &elem)))
{ {
switch (ret) switch (ret)
{ {

View file

@ -37,10 +37,8 @@ void resetString (psycString *s, uint8_t freeptr);
// initialize parser & packet variables // initialize parser & packet variables
void test_init (int i) { void test_init (int i) {
// reset parser state & packet // reset parser state & packet
if (routing_only) psyc_parse_state_init(&parsers[i], routing_only ?
psyc_initParseState2(&parsers[i], PSYC_PARSE_ROUTING_ONLY); PSYC_PARSE_ROUTING_ONLY : PSYC_PARSE_ALL);
else
psyc_initParseState(&parsers[i]);
memset(&packets[i], 0, sizeof(psycPacket)); memset(&packets[i], 0, sizeof(psycPacket));
memset(&routing[i], 0, sizeof(psycModifier) * ROUTING_LINES); memset(&routing[i], 0, sizeof(psycModifier) * ROUTING_LINES);
@ -76,7 +74,7 @@ int test_input (int i, char *recvbuf, size_t nbytes) {
size_t len; size_t len;
// Set buffer with data for the parser. // Set buffer with data for the parser.
psyc_setParseBuffer2(parser, parsebuf, contbytes + nbytes); psyc_parse_buffer_set(parser, parsebuf, contbytes + nbytes);
contbytes = 0; contbytes = 0;
oper = 0; oper = 0;
name.length = 0; name.length = 0;
@ -111,7 +109,7 @@ int test_input (int i, char *recvbuf, size_t nbytes) {
if (ret == PSYC_PARSE_ENTITY || ret == PSYC_PARSE_ENTITY_END) { if (ret == PSYC_PARSE_ENTITY || ret == PSYC_PARSE_ENTITY_END) {
packet->entity.lines++; packet->entity.lines++;
mod->flag = psyc_isParseValueLengthFound(parser) ? mod->flag = psyc_parse_value_length_found(parser) ?
PSYC_MODIFIER_NEED_LENGTH : PSYC_MODIFIER_NO_LENGTH; PSYC_MODIFIER_NEED_LENGTH : PSYC_MODIFIER_NO_LENGTH;
} }
break; break;
@ -133,7 +131,7 @@ int test_input (int i, char *recvbuf, size_t nbytes) {
ret = -1; ret = -1;
if (!no_render) { if (!no_render) {
packet->flag = psyc_isParseContentLengthFound(parser) ? packet->flag = psyc_parse_content_length_found(parser) ?
PSYC_PACKET_NEED_LENGTH : PSYC_PACKET_NO_LENGTH; PSYC_PACKET_NEED_LENGTH : PSYC_PACKET_NO_LENGTH;
if (routing_only) { if (routing_only) {
@ -141,7 +139,7 @@ int test_input (int i, char *recvbuf, size_t nbytes) {
resetString(&(packet->data), 0); resetString(&(packet->data), 0);
} }
psyc_setPacketLength(packet); psyc_packet_length_set(packet);
if (psyc_render(packet, sendbuf, SEND_BUF_SIZE) == PSYC_RENDER_SUCCESS) { if (psyc_render(packet, sendbuf, SEND_BUF_SIZE) == PSYC_RENDER_SUCCESS) {
if (!quiet) { if (!quiet) {
@ -190,13 +188,13 @@ int test_input (int i, char *recvbuf, size_t nbytes) {
if (verbose >= 2) if (verbose >= 2)
printf("# Insufficient data.\n"); printf("# Insufficient data.\n");
contbytes = psyc_getParseRemainingLength(parser); contbytes = psyc_parse_remaining_length(parser);
if (contbytes > 0) { // copy end of parsebuf before start of recvbuf if (contbytes > 0) { // copy end of parsebuf before start of recvbuf
if (verbose >= 3) if (verbose >= 3)
printf("# remaining = [%.*s]\n", (int)contbytes, psyc_getParseRemainingBuffer(parser)); printf("# remaining = [%.*s]\n", (int)contbytes, psyc_parse_remaining_buffer(parser));
assert(contbytes <= CONT_BUF_SIZE); // make sure it's still in the buffer assert(contbytes <= CONT_BUF_SIZE); // make sure it's still in the buffer
memmove(recvbuf - contbytes, psyc_getParseRemainingBuffer(parser), contbytes); memmove(recvbuf - contbytes, psyc_parse_remaining_buffer(parser), contbytes);
} }
ret = 0; ret = 0;
break; break;
@ -237,8 +235,8 @@ int test_input (int i, char *recvbuf, size_t nbytes) {
if (value.length) { if (value.length) {
if (!pvalue->length) { if (!pvalue->length) {
if (psyc_isParseValueLengthFound(parser)) if (psyc_parse_value_length_found(parser))
len = psyc_getParseValueLength(parser); len = psyc_parse_value_length(parser);
else else
len = value.length; len = value.length;
pvalue->ptr = malloc(len); pvalue->ptr = malloc(len);
@ -272,15 +270,15 @@ int test_input (int i, char *recvbuf, size_t nbytes) {
name.length = 0; name.length = 0;
value.length = 0; value.length = 0;
if (psyc_isListVar(pname)) { if (psyc_var_is_list(PSYC_S2ARG(*pname))) {
if (verbose >= 2) if (verbose >= 2)
printf("## LIST START\n"); printf("## LIST START\n");
psyc_initParseListState(&listState); psyc_parse_list_state_init(&listState);
psyc_setParseListBuffer(&listState, *pvalue); psyc_parse_list_buffer_set(&listState, PSYC_S2ARG(*pvalue));
do { do {
retl = psyc_parseList(&listState, &elem); retl = psyc_parse_list(&listState, &elem);
switch (retl) { switch (retl) {
case PSYC_PARSE_LIST_END: case PSYC_PARSE_LIST_END:
retl = 0; retl = 0;

View file

@ -26,10 +26,8 @@ size_t count = 1, recv_buf_size;
psycParseState parser; psycParseState parser;
void test_init (int i) { void test_init (int i) {
if (routing_only) psyc_parse_state_init(&parser, routing_only ?
psyc_initParseState2(&parser, PSYC_PARSE_ROUTING_ONLY); PSYC_PARSE_ROUTING_ONLY : PSYC_PARSE_ALL);
else
psyc_initParseState(&parser);
} }
int test_input (int i, char *recvbuf, size_t nbytes) { int test_input (int i, char *recvbuf, size_t nbytes) {
@ -37,7 +35,7 @@ int test_input (int i, char *recvbuf, size_t nbytes) {
psycString name, value; psycString name, value;
int ret; int ret;
psyc_setParseBuffer2(&parser, recvbuf, nbytes); psyc_parse_buffer_set(&parser, recvbuf, nbytes);
for (;;) { for (;;) {
ret = psyc_parse(&parser, &oper, &name, &value); ret = psyc_parse(&parser, &oper, &name, &value);

View file

@ -12,19 +12,19 @@ int testPresence (const char *avail, int availlen,
const char *rendered, uint8_t verbose) const char *rendered, uint8_t verbose)
{ {
psycModifier routing[] = { psycModifier routing[] = {
psyc_newModifier2(C_GLYPH_OPERATOR_SET, PSYC_C2ARG("_context"), PSYC_C2ARG(myUNI), psyc_modifier_new(C_GLYPH_OPERATOR_SET, PSYC_C2ARG("_context"), PSYC_C2ARG(myUNI),
PSYC_MODIFIER_ROUTING), PSYC_MODIFIER_ROUTING),
}; };
psycModifier entity[] = { psycModifier entity[] = {
// presence is to be assigned permanently in distributed state // presence is to be assigned permanently in distributed state
psyc_newModifier2(C_GLYPH_OPERATOR_ASSIGN, PSYC_C2ARG("_degree_availability"), psyc_modifier_new(C_GLYPH_OPERATOR_ASSIGN, PSYC_C2ARG("_degree_availability"),
avail, availlen, PSYC_MODIFIER_CHECK_LENGTH), avail, availlen, PSYC_MODIFIER_CHECK_LENGTH),
psyc_newModifier2(C_GLYPH_OPERATOR_ASSIGN, PSYC_C2ARG("_description_presence"), psyc_modifier_new(C_GLYPH_OPERATOR_ASSIGN, PSYC_C2ARG("_description_presence"),
desc, desclen, PSYC_MODIFIER_CHECK_LENGTH), desc, desclen, PSYC_MODIFIER_CHECK_LENGTH),
}; };
psycPacket packet = psyc_newPacket2(routing, PSYC_NUM_ELEM(routing), psycPacket packet = psyc_packet_new(routing, PSYC_NUM_ELEM(routing),
entity, PSYC_NUM_ELEM(entity), entity, PSYC_NUM_ELEM(entity),
PSYC_C2ARG("_notice_presence"), PSYC_C2ARG("_notice_presence"),
NULL, 0, NULL, 0,
@ -40,9 +40,9 @@ int testPresence (const char *avail, int availlen,
int testList (const char *rendered, uint8_t verbose) int testList (const char *rendered, uint8_t verbose)
{ {
psycModifier routing[] = { psycModifier routing[] = {
psyc_newModifier2(C_GLYPH_OPERATOR_SET, PSYC_C2ARG("_source"), PSYC_C2ARG(myUNI), psyc_modifier_new(C_GLYPH_OPERATOR_SET, PSYC_C2ARG("_source"), PSYC_C2ARG(myUNI),
PSYC_MODIFIER_ROUTING), PSYC_MODIFIER_ROUTING),
psyc_newModifier2(C_GLYPH_OPERATOR_SET, PSYC_C2ARG("_context"), PSYC_C2ARG(myUNI), psyc_modifier_new(C_GLYPH_OPERATOR_SET, PSYC_C2ARG("_context"), PSYC_C2ARG(myUNI),
PSYC_MODIFIER_ROUTING), PSYC_MODIFIER_ROUTING),
}; };
@ -59,21 +59,21 @@ int testList (const char *rendered, uint8_t verbose)
}; };
psycList list_text, list_bin; psycList list_text, list_bin;
list_text = psyc_newList(elems_text, PSYC_NUM_ELEM(elems_text), PSYC_LIST_CHECK_LENGTH); list_text = psyc_list_new(elems_text, PSYC_NUM_ELEM(elems_text), PSYC_LIST_CHECK_LENGTH);
list_bin = psyc_newList(elems_bin, PSYC_NUM_ELEM(elems_bin), PSYC_LIST_CHECK_LENGTH); list_bin = psyc_list_new(elems_bin, PSYC_NUM_ELEM(elems_bin), PSYC_LIST_CHECK_LENGTH);
char buf_text[32], buf_bin[32]; char buf_text[32], buf_bin[32];
psyc_renderList(&list_text, buf_text, sizeof(buf_text)); psyc_render_list(&list_text, buf_text, sizeof(buf_text));
psyc_renderList(&list_bin, buf_bin, sizeof(buf_bin)); psyc_render_list(&list_bin, buf_bin, sizeof(buf_bin));
psycModifier entity[] = { psycModifier entity[] = {
psyc_newModifier2(C_GLYPH_OPERATOR_SET, PSYC_C2ARG("_list_text"), psyc_modifier_new(C_GLYPH_OPERATOR_SET, PSYC_C2ARG("_list_text"),
buf_text, list_text.length, list_text.flag), buf_text, list_text.length, list_text.flag),
psyc_newModifier2(C_GLYPH_OPERATOR_SET, PSYC_C2ARG("_list_binary"), psyc_modifier_new(C_GLYPH_OPERATOR_SET, PSYC_C2ARG("_list_binary"),
buf_bin, list_bin.length, list_bin.flag), buf_bin, list_bin.length, list_bin.flag),
}; };
psycPacket packet = psyc_newPacket2(routing, PSYC_NUM_ELEM(routing), psycPacket packet = psyc_packet_new(routing, PSYC_NUM_ELEM(routing),
entity, PSYC_NUM_ELEM(entity), entity, PSYC_NUM_ELEM(entity),
PSYC_C2ARG("_test_list"), PSYC_C2ARG("_test_list"),
PSYC_C2ARG("list test"), PSYC_C2ARG("list test"),

View file

@ -38,17 +38,17 @@ int testText (char *template, size_t tmplen, char *buffer, size_t buflen, psycSt
size_t length = 0; size_t length = 0;
psycTextRC ret; psycTextRC ret;
psyc_initTextState(&state, template, tmplen, buffer, buflen); psyc_text_state_init(&state, template, tmplen, buffer, buflen);
do do
{ {
ret = psyc_text(&state, getValue, NULL); ret = psyc_text(&state, getValue, NULL);
length += psyc_getTextBytesWritten(&state); length += psyc_text_bytes_written(&state);
switch (ret) switch (ret)
{ {
case PSYC_TEXT_INCOMPLETE: case PSYC_TEXT_INCOMPLETE:
if (verbose) if (verbose)
printf("# %.*s...\n", (int)length, buffer); printf("# %.*s...\n", (int)length, buffer);
psyc_setTextBuffer2(&state, buffer + length, BUFSIZE - length); psyc_text_buffer_set(&state, buffer + length, BUFSIZE - length);
break; break;
case PSYC_TEXT_COMPLETE: case PSYC_TEXT_COMPLETE:
if (verbose) if (verbose)

View file

@ -8,7 +8,7 @@ testUniform(char *str, int ret) {
psycUniform *uni = malloc(sizeof(psycUniform)); psycUniform *uni = malloc(sizeof(psycUniform));
memset(uni, 0, sizeof(psycUniform)); memset(uni, 0, sizeof(psycUniform));
printf("%s\n", str); printf("%s\n", str);
int r = psyc_parseUniform2(uni, str, strlen(str)); int r = psyc_uniform_parse(uni, str, strlen(str));
PP(("[%.*s] : [%.*s] [%.*s] : [%.*s] [%.*s] / [%.*s] # [%.*s]\n[%.*s] [%.*s]\n[%.*s]\n\n", PP(("[%.*s] : [%.*s] [%.*s] : [%.*s] [%.*s] / [%.*s] # [%.*s]\n[%.*s] [%.*s]\n[%.*s]\n\n",
(int)PSYC_S2ARG2(uni->scheme), (int)PSYC_S2ARG2(uni->scheme),
@ -24,7 +24,7 @@ testUniform(char *str, int ret) {
free(uni); free(uni);
if (r != ret) { if (r != ret) {
fprintf(stderr, "ERROR: psyc_parseUniform returned %d instead of %d\n", r, ret); fprintf(stderr, "ERROR: psyc_uniform_parse returned %d instead of %d\n", r, ret);
exit(1); exit(1);
} }
} }
@ -46,6 +46,6 @@ int main() {
testUniform("psyc://host:d/~foo", PSYC_PARSE_UNIFORM_INVALID_PORT); testUniform("psyc://host:d/~foo", PSYC_PARSE_UNIFORM_INVALID_PORT);
testUniform("psyc://1234567890abcdef:1g/~foo", PSYC_PARSE_UNIFORM_INVALID_TRANSPORT); testUniform("psyc://1234567890abcdef:1g/~foo", PSYC_PARSE_UNIFORM_INVALID_TRANSPORT);
printf("SUCCESS: psyc_parseUniform passed all tests.\n"); printf("SUCCESS: psyc_uniform_parse passed all tests.\n");
return 0; return 0;
} }

39
test/var_is_routing.c Normal file
View file

@ -0,0 +1,39 @@
#include <psyc.h>
#include <unistd.h>
#include <stdio.h>
#include <fcntl.h>
#include <lib.h>
int main() {
#if 0
const char* vars[] =
{
"_source",
"_source_relay",
"_source_foo",
"_sourcherry",
"_foo",
"bar",
"_",
};
int i;
for (i = 0; i < sizeof(vars) / sizeof(*vars); i++)
{
printf(">> %s: %d %d\n", vars[i], sizeof(vars[i]), sizeof(*vars[i]));
printf("%s: %d\n", vars[i], psyc_var_is_routing(vars[i], strlen(vars[i])));
}
#else
unless (psyc_var_is_routing(PSYC_C2ARG("_source"))) return 1;
unless (psyc_var_is_routing(PSYC_C2ARG("_source_relay"))) return 2;
if (psyc_var_is_routing(PSYC_C2ARG("_source_foo"))) return 3;
if (psyc_var_is_routing(PSYC_C2ARG("_sourcherry"))) return 4;
if (psyc_var_is_routing(PSYC_C2ARG("_sour"))) return 5;
if (psyc_var_is_routing(PSYC_C2ARG("_foo"))) return 6;
if (psyc_var_is_routing(PSYC_C2ARG("bar"))) return 7;
if (psyc_var_is_routing(PSYC_C2ARG("_"))) return 8;
puts("psyc_var_is_routing passed all tests.");
#endif
return 0; // passed all tests
}

21
test/var_type.c Normal file
View file

@ -0,0 +1,21 @@
#include <psyc.h>
#include <unistd.h>
#include <stdio.h>
#include <fcntl.h>
#include <lib.h>
int main() {
unless (psyc_var_type(PSYC_C2ARG("_list"))) return 1;
unless (psyc_var_type(PSYC_C2ARG("_list_foo"))) return 2;
unless (psyc_var_type(PSYC_C2ARG("_color_red"))) return 3;
if (psyc_var_type(PSYC_C2ARG("_last"))) return 4;
if (psyc_var_type(PSYC_C2ARG("_lost_foo"))) return 5;
if (psyc_var_type(PSYC_C2ARG("_colorful"))) return 6;
if (psyc_var_type(PSYC_C2ARG("_foo"))) return 7;
if (psyc_var_type(PSYC_C2ARG("bar"))) return 8;
if (psyc_var_type(PSYC_C2ARG("______"))) return 9;
if (psyc_var_type(PSYC_C2ARG("_"))) return 10;
puts("psyc_var_type passed all tests.");
return 0; // passed all tests
}