diff --git a/include/psyc.h b/include/psyc.h index cafc026..2847bfa 100644 --- a/include/psyc.h +++ b/include/psyc.h @@ -133,6 +133,25 @@ psycBool psyc_isRoutingVar(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. */ diff --git a/include/psyc/parse.h b/include/psyc/parse.h index 2673e6b..4a5b492 100644 --- a/include/psyc/parse.h +++ b/include/psyc/parse.h @@ -54,7 +54,7 @@ * char oper; // operator of the variable (if any) * @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. * * Now the real parsing begins. The parsing function needs to be called @@ -91,7 +91,7 @@ * break; * case PSYC_PARSE_COMPLETE: // parsing of this packet is complete * // 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; * default: // * perror("Error %i happened :(\n", res); @@ -192,10 +192,9 @@ typedef enum */ typedef enum { - PSYC_PARSE_LIST_ERROR_DELIM = -5, - PSYC_PARSE_LIST_ERROR_LEN = -4, - PSYC_PARSE_LIST_ERROR_TYPE = -3, - PSYC_PARSE_LIST_ERROR_NAME = -2, + PSYC_PARSE_LIST_ERROR_DELIM = -4, + PSYC_PARSE_LIST_ERROR_LEN = -3, + PSYC_PARSE_LIST_ERROR_TYPE = -2, PSYC_PARSE_LIST_ERROR = -1, /// Completed parsing a list element. PSYC_PARSE_LIST_ELEM = 1, @@ -389,26 +388,36 @@ const char * psyc_getParseRemainingBuffer (psycParseState *state) /** * 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 A pointer to a character. In case of a variable, it will - * be set to the operator of that variable - * @param name A pointer to a psycString. It will point to the name of - * 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 + * @param state An initialized psycParseState. + * @param oper In case of a modifier it will be set to the operator. + * @param name In case of a modifier it will point to the name, + * in case of the body it will point to the method. + * @param value In case of a modifier it will point to the value, + * in case of the body it will point to the data. */ psycParseRC psyc_parse (psycParseState *state, char *oper, 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, - psycString *value, psycString *elem); +psycParseListRC psyc_parseList (psycParseListState *state, psycString *value, + psycString *elem); /** @} */ // end of parse group diff --git a/src/parse.c b/src/parse.c index d11db10..0bfc6c1 100644 --- a/src/parse.c +++ b/src/parse.c @@ -201,10 +201,7 @@ parseRC psyc_parseModifier (psycParseState *state, char *oper, return PSYC_PARSE_ERROR_MOD_TAB; } -/** - * Parse PSYC packets. - * Generalized line-based parser. - */ +/** Parse PSYC packets. */ psycParseRC psyc_parse (psycParseState *state, char *oper, psycString *name, psycString *value) { @@ -485,12 +482,9 @@ psycParseRC psyc_parse (psycParseState *state, char *oper, return PSYC_PARSE_ERROR; // should not be reached } -/** - * List value parser. - * @return see psycListRC. - */ -psycParseListRC psyc_parseList (psycParseListState *state, psycString *name, - psycString *value, psycString *elem) +/** List parser. */ +psycParseListRC psyc_parseList (psycParseListState *state, psycString *value, + psycString *elem) { if (state->cursor >= state->buffer.length) 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 (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 if (state->buffer.ptr[state->cursor] == '|') { diff --git a/test/testParser.c b/test/testParser.c index bbba26f..e312910 100644 --- a/test/testParser.c +++ b/test/testParser.c @@ -59,7 +59,7 @@ int main (int argc, char **argv) (int)name.length, name.ptr, (int)value.length, value.ptr); - if (name.length >= 5 && memcmp(name.ptr, "_list", 5) == 0) + if (psyc_isListVar(&name)) { if (verbose) printf(">> LIST START\n"); @@ -67,7 +67,7 @@ int main (int argc, char **argv) psyc_initParseListState(&listState); psyc_setParseListBuffer(&listState, value); - while ((ret = psyc_parseList(&listState, &name, &value, &elem))) + while ((ret = psyc_parseList(&listState, &value, &elem))) { switch (ret) { diff --git a/test/testServer.c b/test/testServer.c index 620580f..78dbcc1 100644 --- a/test/testServer.c +++ b/test/testServer.c @@ -443,7 +443,7 @@ int main (int argc, char **argv) name.length = 0; value.length = 0; - if (pname->length >= 5 && memcmp(pname->ptr, "_list", 5) == 0) + if (psyc_isListVar(pname)) { if (verbose >= 2) printf("## LIST START\n"); @@ -453,7 +453,7 @@ int main (int argc, char **argv) do { - retl = psyc_parseList(&listState, pname, pvalue, &elem); + retl = psyc_parseList(&listState, pvalue, &elem); switch (retl) { case PSYC_PARSE_LIST_END: