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

table rendering

This commit is contained in:
Gabor Adam Toth 2011-11-26 15:03:10 +01:00
parent 7c1c74dd93
commit 5d1659f5f5
9 changed files with 90 additions and 48 deletions

View file

@ -71,6 +71,7 @@ typedef enum {
PSYC_TYPE_AMOUNT, PSYC_TYPE_AMOUNT,
PSYC_TYPE_COLOR, PSYC_TYPE_COLOR,
PSYC_TYPE_COUNTER, PSYC_TYPE_COUNTER,
PSYC_TYPE_DEF,
PSYC_TYPE_DATE, PSYC_TYPE_DATE,
PSYC_TYPE_DEGREE, PSYC_TYPE_DEGREE,
PSYC_TYPE_ENTITY, PSYC_TYPE_ENTITY,

View file

@ -89,6 +89,12 @@ typedef struct {
PsycListFlag flag; PsycListFlag flag;
} PsycList; } PsycList;
typedef struct {
PsycList *list;
size_t width;
size_t length;
} PsycTable;
/** Intermediate struct for a PSYC packet */ /** Intermediate struct for a PSYC packet */
typedef struct { typedef struct {
PsycHeader routing; ///< Routing header. PsycHeader routing; ///< Routing header.
@ -177,12 +183,16 @@ psyc_packet_length_check (PsycPacket *p);
size_t size_t
psyc_packet_length_set (PsycPacket *p); psyc_packet_length_set (PsycPacket *p);
/** Initialize list. */ /** Initialize a list. */
void void
psyc_list_init (PsycList *list, PsycString *elems, size_t num_elems, psyc_list_init (PsycList *list, PsycString *elems, size_t num_elems,
PsycListFlag flag); PsycListFlag flag);
/** Initialize packet. */ /** Initialize a table. */
void
psyc_table_init (PsycTable *table, size_t width, PsycList *list);
/** Initialize a packet. */
void void
psyc_packet_init (PsycPacket *packet, psyc_packet_init (PsycPacket *packet,
PsycModifier *routing, size_t routinglen, PsycModifier *routing, size_t routinglen,

View file

@ -31,16 +31,6 @@ typedef enum {
PSYC_RENDER_SUCCESS = 0, PSYC_RENDER_SUCCESS = 0,
} PsycRenderRC; } 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. * Render a PSYC packet into a buffer.
* *
@ -67,9 +57,12 @@ psyc_render (PsycPacket *packet, char *buffer, size_t buflen);
#ifdef __INLINE_PSYC_RENDER #ifdef __INLINE_PSYC_RENDER
static inline static inline
#endif #endif
PsycRenderListRC PsycRenderRC
psyc_render_list (PsycList *list, char *buffer, size_t buflen); 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 /** @} */ // end of render group
#endif #endif

View file

@ -3,7 +3,7 @@
#include <psyc/packet.h> #include <psyc/packet.h>
inline PsycListFlag inline PsycListFlag
psyc_list_length_check (PsycList * list) 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;
@ -23,7 +23,7 @@ psyc_list_length_check (PsycList * list)
} }
inline PsycListFlag inline PsycListFlag
psyc_list_length (PsycList * list) psyc_list_length (PsycList *list)
{ {
size_t i, length = 0; size_t i, length = 0;
@ -42,12 +42,16 @@ psyc_list_length (PsycList * list)
return length; return length;
} }
inline void void
psyc_list_init (PsycList * list, PsycString * elems, size_t num_elems, psyc_list_init (PsycList *list, PsycString *elems, size_t num_elems,
PsycListFlag flag) PsycListFlag flag)
{ {
*list = (PsycList) { *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 if (flag == PSYC_LIST_CHECK_LENGTH) // check if list elements need length
list->flag = psyc_list_length_check(list); 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); 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 inline size_t
psyc_modifier_length (PsycModifier * m) psyc_modifier_length (PsycModifier *m)
{ {
size_t length = 2; // oper\n size_t length = 2; // oper\n
if (m->name.length > 0) if (m->name.length > 0)
@ -70,7 +84,7 @@ psyc_modifier_length (PsycModifier * m)
} }
inline PsycPacketFlag 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) if (p->data.length == 1 && p->data.data[0] == PSYC_PACKET_DELIMITER_CHAR)
return PSYC_PACKET_NEED_LENGTH; return PSYC_PACKET_NEED_LENGTH;
@ -92,7 +106,7 @@ psyc_packet_length_check (PsycPacket * p)
} }
inline size_t inline size_t
psyc_packet_length_set (PsycPacket * p) psyc_packet_length_set (PsycPacket *p)
{ {
size_t i; size_t i;
p->routinglen = 0; p->routinglen = 0;
@ -133,9 +147,9 @@ psyc_packet_length_set (PsycPacket * p)
} }
inline void inline void
psyc_packet_init (PsycPacket * p, psyc_packet_init (PsycPacket *p,
PsycModifier * routing, size_t routinglen, PsycModifier *routing, size_t routinglen,
PsycModifier * entity, size_t entitylen, PsycModifier *entity, size_t entitylen,
char *method, size_t methodlen, char *method, size_t methodlen,
char *data, size_t datalen, char *data, size_t datalen,
char stateop, PsycPacketFlag flag) char stateop, PsycPacketFlag flag)
@ -160,8 +174,8 @@ psyc_packet_init (PsycPacket * p,
} }
inline void inline void
psyc_packet_init_raw (PsycPacket * p, psyc_packet_init_raw (PsycPacket *p,
PsycModifier * routing, size_t routinglen, PsycModifier *routing, size_t routinglen,
char *content, size_t contentlen, char *content, size_t contentlen,
PsycPacketFlag flag) PsycPacketFlag flag)
{ {

View file

@ -164,6 +164,10 @@ psyc_parse (PsycParseState *state, char *oper,
// in case we return insufficent, we rewind to this position. // in case we return insufficent, we rewind to this position.
state->startc = state->cursor; 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. // First we test if we can access the first char.
if (state->cursor >= state->buffer.length) // Cursor is not inside the length. if (state->cursor >= state->buffer.length) // Cursor is not inside the length.
return PSYC_PARSE_INSUFFICIENT; return PSYC_PARSE_INSUFFICIENT;
@ -263,22 +267,23 @@ psyc_parse (PsycParseState *state, char *oper,
// 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_is_glyph(state->buffer.data[state->cursor])) { if (psyc_is_glyph(state->buffer.data[state->cursor])) {
if (state->content_parsed == 0) {
ADVANCE_CURSOR_OR_RETURN(PSYC_PARSE_INSUFFICIENT); ADVANCE_CURSOR_OR_RETURN(PSYC_PARSE_INSUFFICIENT);
if (state->content_parsed == 0 if (state->buffer.data[state->cursor] == '\n') {
&& state->buffer.data[state->cursor] == '\n') {
*oper = *(state->buffer.data + state->cursor - 1); *oper = *(state->buffer.data + state->cursor - 1);
switch (*oper) { switch (*oper) {
case PSYC_STATE_RESYNC: case PSYC_STATE_RESYNC:
state->content_parsed += 2; state->content_parsed++;
return PSYC_PARSE_STATE_RESYNC; return PSYC_PARSE_STATE_RESYNC;
case PSYC_STATE_RESET: case PSYC_STATE_RESET:
state->content_parsed += 2; state->content_parsed++;
return PSYC_PARSE_STATE_RESET; return PSYC_PARSE_STATE_RESET;
default: default:
return PSYC_PARSE_ERROR_MOD_NAME; return PSYC_PARSE_ERROR_MOD_NAME;
} }
} }
state->cursor--; state->cursor--;
}
ret = psyc_parse_modifier(state, oper, name, value); ret = psyc_parse_modifier(state, oper, name, value);
state->content_parsed += state->cursor - pos; state->content_parsed += state->cursor - pos;

View file

@ -6,14 +6,14 @@
#ifdef __INLINE_PSYC_RENDER #ifdef __INLINE_PSYC_RENDER
static inline static inline
#endif #endif
PsycRenderListRC PsycRenderRC
psyc_render_list (PsycList * list, char *buffer, size_t buflen) psyc_render_list (PsycList *list, char *buffer, size_t buflen)
{ {
size_t i, cur = 0; size_t i, cur = 0;
PsycString *elem; PsycString *elem;
if (list->length > buflen) // return error if list doesn't fit in buffer 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) { if (list->flag == PSYC_LIST_NEED_LENGTH) {
for (i = 0; i < list->num_elems; i++) { 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. // Actual length should be equal to pre-calculated length at this point.
assert(cur == list->length); 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 static inline size_t

View file

@ -31,6 +31,7 @@ const PsycDictInt psyc_var_types[] = {
{PSYC_C2STRI("_amount"), PSYC_TYPE_AMOUNT}, {PSYC_C2STRI("_amount"), PSYC_TYPE_AMOUNT},
{PSYC_C2STRI("_color"), PSYC_TYPE_COLOR}, {PSYC_C2STRI("_color"), PSYC_TYPE_COLOR},
{PSYC_C2STRI("_date"), PSYC_TYPE_DATE}, {PSYC_C2STRI("_date"), PSYC_TYPE_DATE},
{PSYC_C2STRI("_def"), PSYC_TYPE_DEF},
{PSYC_C2STRI("_degree"), PSYC_TYPE_DEGREE}, {PSYC_C2STRI("_degree"), PSYC_TYPE_DEGREE},
{PSYC_C2STRI("_entity"), PSYC_TYPE_ENTITY}, {PSYC_C2STRI("_entity"), PSYC_TYPE_ENTITY},
{PSYC_C2STRI("_flag"), PSYC_TYPE_FLAG}, {PSYC_C2STRI("_flag"), PSYC_TYPE_FLAG},

View file

@ -170,8 +170,8 @@ test_input (int i, char *recvbuf, size_t nbytes)
} }
// reset packet // reset packet
packet->routingLength = 0; packet->routinglen = 0;
packet->contentLength = 0; packet->contentlen = 0;
packet->length = 0; packet->length = 0;
packet->flag = 0; packet->flag = 0;
@ -262,7 +262,7 @@ test_input (int i, char *recvbuf, size_t nbytes)
if (verbose >= 2) { if (verbose >= 2) {
printf("[%.*s]", (int)pvalue->length, pvalue->data); printf("[%.*s]", (int)pvalue->length, pvalue->data);
if (parser->valueLength > pvalue->length) if (parser->valuelen > pvalue->length)
printf("..."); printf("...");
printf("\n"); printf("\n");
} }
@ -273,7 +273,7 @@ test_input (int i, char *recvbuf, size_t nbytes)
if (verbose >= 3) if (verbose >= 3)
printf("\t\t\t\t\t\t\t\t# n:%ld v:%ld c:%ld r:%ld\n", printf("\t\t\t\t\t\t\t\t# n:%ld v:%ld c:%ld r:%ld\n",
pname->length, pvalue->length, pname->length, pvalue->length,
parser->contentParsed, parser->routingLength); parser->content_parsed, parser->routinglen);
} }
switch (ret) { switch (ret) {