From 5d1659f5f518b431ede43b30547d26e6cc4ef6b8 Mon Sep 17 00:00:00 2001 From: Gabor Adam Toth Date: Sat, 26 Nov 2011 15:03:10 +0100 Subject: [PATCH] table rendering --- include/psyc.h | 1 + include/psyc/packet.h | 14 ++++++- include/psyc/render.h | 15 ++----- src/packet.c | 40 +++++++++++++------ src/parse.c | 33 ++++++++------- src/render.c | 26 ++++++++++-- src/variable.c | 1 + .../{00-state-sync => 00-state-resync} | 0 test/test_psyc.c | 8 ++-- 9 files changed, 90 insertions(+), 48 deletions(-) rename test/packets/{00-state-sync => 00-state-resync} (100%) diff --git a/include/psyc.h b/include/psyc.h index e04b030..5c3076d 100644 --- a/include/psyc.h +++ b/include/psyc.h @@ -71,6 +71,7 @@ typedef enum { PSYC_TYPE_AMOUNT, PSYC_TYPE_COLOR, PSYC_TYPE_COUNTER, + PSYC_TYPE_DEF, PSYC_TYPE_DATE, PSYC_TYPE_DEGREE, PSYC_TYPE_ENTITY, diff --git a/include/psyc/packet.h b/include/psyc/packet.h index 24429a0..8dc92db 100644 --- a/include/psyc/packet.h +++ b/include/psyc/packet.h @@ -89,6 +89,12 @@ typedef struct { PsycListFlag flag; } PsycList; +typedef struct { + PsycList *list; + size_t width; + size_t length; +} PsycTable; + /** Intermediate struct for a PSYC packet */ typedef struct { PsycHeader routing; ///< Routing header. @@ -177,12 +183,16 @@ psyc_packet_length_check (PsycPacket *p); size_t psyc_packet_length_set (PsycPacket *p); -/** Initialize list. */ +/** Initialize a list. */ void psyc_list_init (PsycList *list, PsycString *elems, size_t num_elems, PsycListFlag flag); -/** Initialize packet. */ +/** Initialize a table. */ +void +psyc_table_init (PsycTable *table, size_t width, PsycList *list); + +/** Initialize a packet. */ void psyc_packet_init (PsycPacket *packet, PsycModifier *routing, size_t routinglen, diff --git a/include/psyc/render.h b/include/psyc/render.h index d08174c..4595ed0 100644 --- a/include/psyc/render.h +++ b/include/psyc/render.h @@ -31,16 +31,6 @@ typedef enum { PSYC_RENDER_SUCCESS = 0, } PsycRenderRC; -/** - * Return codes for psyc_render_list. - */ -typedef enum { - /// Error, buffer is too small to render the list. - PSYC_RENDER_LIST_ERROR = -1, - /// List is rendered successfully in the buffer. - PSYC_RENDER_LIST_SUCCESS = 0, -} PsycRenderListRC; - /** * Render a PSYC packet into a buffer. * @@ -67,9 +57,12 @@ psyc_render (PsycPacket *packet, char *buffer, size_t buflen); #ifdef __INLINE_PSYC_RENDER static inline #endif -PsycRenderListRC +PsycRenderRC psyc_render_list (PsycList *list, char *buffer, size_t buflen); +PsycRenderRC +psyc_render_table (PsycTable *table, char *buffer, size_t buflen); + /** @} */ // end of render group #endif diff --git a/src/packet.c b/src/packet.c index 6626812..c61ee8d 100644 --- a/src/packet.c +++ b/src/packet.c @@ -3,7 +3,7 @@ #include inline PsycListFlag -psyc_list_length_check (PsycList * list) +psyc_list_length_check (PsycList *list) { PsycListFlag flag = PSYC_LIST_NO_LENGTH; size_t i, length = 0; @@ -23,7 +23,7 @@ psyc_list_length_check (PsycList * list) } inline PsycListFlag -psyc_list_length (PsycList * list) +psyc_list_length (PsycList *list) { size_t i, length = 0; @@ -42,12 +42,16 @@ psyc_list_length (PsycList * list) return length; } -inline void -psyc_list_init (PsycList * list, PsycString * elems, size_t num_elems, +void +psyc_list_init (PsycList *list, PsycString *elems, size_t num_elems, PsycListFlag flag) { *list = (PsycList) { - num_elems, elems, 0, flag}; + .num_elems = num_elems, + .elems = elems, + .length = 0, + .flag = flag, + }; if (flag == PSYC_LIST_CHECK_LENGTH) // check if list elements need length list->flag = psyc_list_length_check(list); @@ -55,9 +59,19 @@ psyc_list_init (PsycList * list, PsycString * elems, size_t num_elems, list->length = psyc_list_length(list); } +void +psyc_table_init (PsycTable *table, size_t width, PsycList *list) +{ + *table = (PsycTable) { + .width = width, + .list = list, + }; + + table->length = (width > 0 ? psyc_num_length(width) + 2 : 0) + list->length; +} inline size_t -psyc_modifier_length (PsycModifier * m) +psyc_modifier_length (PsycModifier *m) { size_t length = 2; // oper\n if (m->name.length > 0) @@ -70,7 +84,7 @@ psyc_modifier_length (PsycModifier * m) } inline PsycPacketFlag -psyc_packet_length_check (PsycPacket * p) +psyc_packet_length_check (PsycPacket *p) { if (p->data.length == 1 && p->data.data[0] == PSYC_PACKET_DELIMITER_CHAR) return PSYC_PACKET_NEED_LENGTH; @@ -92,7 +106,7 @@ psyc_packet_length_check (PsycPacket * p) } inline size_t -psyc_packet_length_set (PsycPacket * p) +psyc_packet_length_set (PsycPacket *p) { size_t i; p->routinglen = 0; @@ -133,9 +147,9 @@ psyc_packet_length_set (PsycPacket * p) } inline void -psyc_packet_init (PsycPacket * p, - PsycModifier * routing, size_t routinglen, - PsycModifier * entity, size_t entitylen, +psyc_packet_init (PsycPacket *p, + PsycModifier *routing, size_t routinglen, + PsycModifier *entity, size_t entitylen, char *method, size_t methodlen, char *data, size_t datalen, char stateop, PsycPacketFlag flag) @@ -160,8 +174,8 @@ psyc_packet_init (PsycPacket * p, } inline void -psyc_packet_init_raw (PsycPacket * p, - PsycModifier * routing, size_t routinglen, +psyc_packet_init_raw (PsycPacket *p, + PsycModifier *routing, size_t routinglen, char *content, size_t contentlen, PsycPacketFlag flag) { diff --git a/src/parse.c b/src/parse.c index 0ccf9c5..f4701fe 100644 --- a/src/parse.c +++ b/src/parse.c @@ -164,6 +164,10 @@ psyc_parse (PsycParseState *state, char *oper, // in case we return insufficent, we rewind to this position. state->startc = state->cursor; + if (state->flags & PSYC_PARSE_START_AT_CONTENT + && (state->buffer.length == 0 || state->cursor >= state->buffer.length - 1)) + return PSYC_PARSE_COMPLETE; + // First we test if we can access the first char. if (state->cursor >= state->buffer.length) // Cursor is not inside the length. return PSYC_PARSE_INSUFFICIENT; @@ -263,22 +267,23 @@ psyc_parse (PsycParseState *state, char *oper, // In the body, the same applies, only that the // method does not start with a glyph. if (psyc_is_glyph(state->buffer.data[state->cursor])) { - ADVANCE_CURSOR_OR_RETURN(PSYC_PARSE_INSUFFICIENT); - if (state->content_parsed == 0 - && state->buffer.data[state->cursor] == '\n') { - *oper = *(state->buffer.data + state->cursor - 1); - switch (*oper) { - case PSYC_STATE_RESYNC: - state->content_parsed += 2; - return PSYC_PARSE_STATE_RESYNC; - case PSYC_STATE_RESET: - state->content_parsed += 2; - return PSYC_PARSE_STATE_RESET; - default: - return PSYC_PARSE_ERROR_MOD_NAME; + if (state->content_parsed == 0) { + ADVANCE_CURSOR_OR_RETURN(PSYC_PARSE_INSUFFICIENT); + if (state->buffer.data[state->cursor] == '\n') { + *oper = *(state->buffer.data + state->cursor - 1); + switch (*oper) { + case PSYC_STATE_RESYNC: + state->content_parsed++; + return PSYC_PARSE_STATE_RESYNC; + case PSYC_STATE_RESET: + state->content_parsed++; + return PSYC_PARSE_STATE_RESET; + default: + return PSYC_PARSE_ERROR_MOD_NAME; + } } + state->cursor--; } - state->cursor--; ret = psyc_parse_modifier(state, oper, name, value); state->content_parsed += state->cursor - pos; diff --git a/src/render.c b/src/render.c index 72508cc..9ba51ff 100644 --- a/src/render.c +++ b/src/render.c @@ -6,14 +6,14 @@ #ifdef __INLINE_PSYC_RENDER static inline #endif -PsycRenderListRC -psyc_render_list (PsycList * list, char *buffer, size_t buflen) +PsycRenderRC +psyc_render_list (PsycList *list, char *buffer, size_t buflen) { size_t i, cur = 0; PsycString *elem; if (list->length > buflen) // return error if list doesn't fit in buffer - return PSYC_RENDER_LIST_ERROR; + return PSYC_RENDER_ERROR; if (list->flag == PSYC_LIST_NEED_LENGTH) { for (i = 0; i < list->num_elems; i++) { @@ -34,9 +34,27 @@ psyc_render_list (PsycList * list, char *buffer, size_t buflen) } } +#ifdef DEBUG // Actual length should be equal to pre-calculated length at this point. assert(cur == list->length); - return PSYC_RENDER_LIST_SUCCESS; +#endif + return PSYC_RENDER_SUCCESS; +} + +PsycRenderRC +psyc_render_table (PsycTable *table, char *buffer, size_t buflen) +{ + size_t cur = 0; + + if (table->length > buflen) // return error if table doesn't fit in buffer + return PSYC_RENDER_ERROR; + + if (table->width > 0) { + cur = sprintf(buffer, "*%ld", table->width); + buffer[cur++] = ' '; + } + + return psyc_render_list(table->list, buffer + cur, buflen - cur); } static inline size_t diff --git a/src/variable.c b/src/variable.c index e762b74..396dc39 100644 --- a/src/variable.c +++ b/src/variable.c @@ -31,6 +31,7 @@ const PsycDictInt psyc_var_types[] = { {PSYC_C2STRI("_amount"), PSYC_TYPE_AMOUNT}, {PSYC_C2STRI("_color"), PSYC_TYPE_COLOR}, {PSYC_C2STRI("_date"), PSYC_TYPE_DATE}, + {PSYC_C2STRI("_def"), PSYC_TYPE_DEF}, {PSYC_C2STRI("_degree"), PSYC_TYPE_DEGREE}, {PSYC_C2STRI("_entity"), PSYC_TYPE_ENTITY}, {PSYC_C2STRI("_flag"), PSYC_TYPE_FLAG}, diff --git a/test/packets/00-state-sync b/test/packets/00-state-resync similarity index 100% rename from test/packets/00-state-sync rename to test/packets/00-state-resync diff --git a/test/test_psyc.c b/test/test_psyc.c index 18bfe9c..55699e1 100644 --- a/test/test_psyc.c +++ b/test/test_psyc.c @@ -170,8 +170,8 @@ test_input (int i, char *recvbuf, size_t nbytes) } // reset packet - packet->routingLength = 0; - packet->contentLength = 0; + packet->routinglen = 0; + packet->contentlen = 0; packet->length = 0; packet->flag = 0; @@ -262,7 +262,7 @@ test_input (int i, char *recvbuf, size_t nbytes) if (verbose >= 2) { printf("[%.*s]", (int)pvalue->length, pvalue->data); - if (parser->valueLength > pvalue->length) + if (parser->valuelen > pvalue->length) printf("..."); printf("\n"); } @@ -273,7 +273,7 @@ test_input (int i, char *recvbuf, size_t nbytes) if (verbose >= 3) printf("\t\t\t\t\t\t\t\t# n:%ld v:%ld c:%ld r:%ld\n", pname->length, pvalue->length, - parser->contentParsed, parser->routingLength); + parser->content_parsed, parser->routinglen); } switch (ret) {