added psyc_isListVar(), removed name param from psyc_parseList(), added more documentation

This commit is contained in:
tg(x) 2011-05-09 19:21:26 +02:00
parent f68021b26c
commit b1ae67135d
5 changed files with 55 additions and 37 deletions

View File

@ -133,6 +133,25 @@ psycBool psyc_isRoutingVar(const char *name, size_t len);
*/ */
psycType psyc_getVarType(const char *name, size_t len); psycType psyc_getVarType(const char *name, size_t len);
/**
* Is this a list variable name?
*/
static inline
psycBool psyc_isListVar(psycString *name)
{
return name->length < 5 || memcmp(name->ptr, "_list", 5) != 0 ||
(name->length > 5 && name->ptr[5] != '_') ? PSYC_FALSE : PSYC_TRUE;
}
/**
* Is this a list variable name?
*/
static inline
psycBool psyc_isListVar2(const char *name, size_t len)
{
return psyc_isListVar(&(psycString){len, name});
}
/** /**
* Checks if long keyword string inherits from short keyword string. * Checks if long keyword string inherits from short keyword string.
*/ */

View File

@ -54,7 +54,7 @@
* char oper; // operator of the variable (if any) * char oper; // operator of the variable (if any)
* @endcode * @endcode
* *
* They will be passd to the parsing function which will set them to * They will be passed to the parsing function which will set them to
* the according positions and lengths. * the according positions and lengths.
* *
* Now the real parsing begins. The parsing function needs to be called * Now the real parsing begins. The parsing function needs to be called
@ -91,7 +91,7 @@
* break; * break;
* case PSYC_PARSE_COMPLETE: // parsing of this packet is complete * case PSYC_PARSE_COMPLETE: // parsing of this packet is complete
* // You can simply continue parsing till you get the * // You can simply continue parsing till you get the
* // PSYC_PARSE_INSUFFICIENT code which means teh packet is incomplete. * // PSYC_PARSE_INSUFFICIENT code which means the line is incomplete.
* continue; * continue;
* default: // * default: //
* perror("Error %i happened :(\n", res); * perror("Error %i happened :(\n", res);
@ -192,10 +192,9 @@ typedef enum
*/ */
typedef enum typedef enum
{ {
PSYC_PARSE_LIST_ERROR_DELIM = -5, PSYC_PARSE_LIST_ERROR_DELIM = -4,
PSYC_PARSE_LIST_ERROR_LEN = -4, PSYC_PARSE_LIST_ERROR_LEN = -3,
PSYC_PARSE_LIST_ERROR_TYPE = -3, PSYC_PARSE_LIST_ERROR_TYPE = -2,
PSYC_PARSE_LIST_ERROR_NAME = -2,
PSYC_PARSE_LIST_ERROR = -1, PSYC_PARSE_LIST_ERROR = -1,
/// Completed parsing a list element. /// Completed parsing a list element.
PSYC_PARSE_LIST_ELEM = 1, PSYC_PARSE_LIST_ELEM = 1,
@ -389,26 +388,36 @@ const char * psyc_getParseRemainingBuffer (psycParseState *state)
/** /**
* Parse PSYC packets. * Parse PSYC packets.
* *
* Generalized line-based packet parser. * This function parses a full or partial PSYC packet while keeping parsing
* state in a state variable that you have to pass in every time, and returns
* whenever a modifier or the body is found. See psycParseRC for the possible
* return codes. When it returns oper, name & value will point to the respective
* parts of the buffer, no memory allocation is done.
* *
* This function never allocates heap memory. * @param state An initialized psycParseState.
* * @param oper In case of a modifier it will be set to the operator.
* @param state An initialized psycParseState * @param name In case of a modifier it will point to the name,
* @param oper A pointer to a character. In case of a variable, it will * in case of the body it will point to the method.
* be set to the operator of that variable * @param value In case of a modifier it will point to the value,
* @param name A pointer to a psycString. It will point to the name of * in case of the body it will point to the data.
* the variable or method and its length will be set accordingly
* @param value A pointer to a psycString. It will point to the
* value/body the variable/method and its length will be set accordingly
*/ */
psycParseRC psyc_parse (psycParseState *state, char *oper, psycParseRC psyc_parse (psycParseState *state, char *oper,
psycString *name, psycString *value); psycString *name, psycString *value);
/** /**
* List value parser. * List parser.
*
* This function parses a _list modifier value and returns one element a time
* while keeping parsing state in a state variable that you have to pass in
* every time. When it returns elem will point to the next element in value, no
* memory allocation is done.
*
* @param state An initialized psycParseListState.
* @param value Contains the list to be parsed.
* @param elem It will point to the next element in the list.
*/ */
psycParseListRC psyc_parseList (psycParseListState *state, psycString *name, psycParseListRC psyc_parseList (psycParseListState *state, psycString *value,
psycString *value, psycString *elem); psycString *elem);
/** @} */ // end of parse group /** @} */ // end of parse group

View File

@ -201,10 +201,7 @@ parseRC psyc_parseModifier (psycParseState *state, char *oper,
return PSYC_PARSE_ERROR_MOD_TAB; return PSYC_PARSE_ERROR_MOD_TAB;
} }
/** /** Parse PSYC packets. */
* Parse PSYC packets.
* Generalized line-based parser.
*/
psycParseRC psyc_parse (psycParseState *state, char *oper, psycParseRC psyc_parse (psycParseState *state, char *oper,
psycString *name, psycString *value) psycString *name, psycString *value)
{ {
@ -485,12 +482,9 @@ psycParseRC psyc_parse (psycParseState *state, char *oper,
return PSYC_PARSE_ERROR; // should not be reached return PSYC_PARSE_ERROR; // should not be reached
} }
/** /** List parser. */
* List value parser. psycParseListRC psyc_parseList (psycParseListState *state, psycString *value,
* @return see psycListRC. psycString *elem)
*/
psycParseListRC psyc_parseList (psycParseListState *state, psycString *name,
psycString *value, psycString *elem)
{ {
if (state->cursor >= state->buffer.length) if (state->cursor >= state->buffer.length)
return PSYC_PARSE_LIST_INCOMPLETE; return PSYC_PARSE_LIST_INCOMPLETE;
@ -499,10 +493,6 @@ psycParseListRC psyc_parseList (psycParseListState *state, psycString *name,
if (!state->type) // If type is not set we're at the start if (!state->type) // If type is not set we're at the start
{ {
if (name->length < 5 || memcmp(name->ptr, "_list", 5) != 0 ||
(name->length > 5 && name->ptr[5] != '_')) // name should be _list or should start with _list_
return PSYC_PARSE_LIST_ERROR_NAME;
// First character is either | for text lists, or a number for binary lists // First character is either | for text lists, or a number for binary lists
if (state->buffer.ptr[state->cursor] == '|') if (state->buffer.ptr[state->cursor] == '|')
{ {

View File

@ -59,7 +59,7 @@ 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 (name.length >= 5 && memcmp(name.ptr, "_list", 5) == 0) if (psyc_isListVar(&name))
{ {
if (verbose) if (verbose)
printf(">> LIST START\n"); printf(">> LIST START\n");
@ -67,7 +67,7 @@ int main (int argc, char **argv)
psyc_initParseListState(&listState); psyc_initParseListState(&listState);
psyc_setParseListBuffer(&listState, value); psyc_setParseListBuffer(&listState, value);
while ((ret = psyc_parseList(&listState, &name, &value, &elem))) while ((ret = psyc_parseList(&listState, &value, &elem)))
{ {
switch (ret) switch (ret)
{ {

View File

@ -443,7 +443,7 @@ int main (int argc, char **argv)
name.length = 0; name.length = 0;
value.length = 0; value.length = 0;
if (pname->length >= 5 && memcmp(pname->ptr, "_list", 5) == 0) if (psyc_isListVar(pname))
{ {
if (verbose >= 2) if (verbose >= 2)
printf("## LIST START\n"); printf("## LIST START\n");
@ -453,7 +453,7 @@ int main (int argc, char **argv)
do do
{ {
retl = psyc_parseList(&listState, pname, pvalue, &elem); retl = psyc_parseList(&listState, pvalue, &elem);
switch (retl) switch (retl)
{ {
case PSYC_PARSE_LIST_END: case PSYC_PARSE_LIST_END: