mirror of
git://git.psyc.eu/libpsyc
synced 2024-08-15 03:19:02 +00:00
table rendering
This commit is contained in:
parent
7c1c74dd93
commit
5d1659f5f5
9 changed files with 90 additions and 48 deletions
|
@ -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,
|
||||
|
|
|
@ -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,
|
||||
|
|
|
@ -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
|
||||
|
|
18
src/packet.c
18
src/packet.c
|
@ -42,12 +42,16 @@ psyc_list_length (PsycList * list)
|
|||
return length;
|
||||
}
|
||||
|
||||
inline void
|
||||
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,6 +59,16 @@ 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)
|
||||
|
|
13
src/parse.c
13
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])) {
|
||||
if (state->content_parsed == 0) {
|
||||
ADVANCE_CURSOR_OR_RETURN(PSYC_PARSE_INSUFFICIENT);
|
||||
if (state->content_parsed == 0
|
||||
&& state->buffer.data[state->cursor] == '\n') {
|
||||
if (state->buffer.data[state->cursor] == '\n') {
|
||||
*oper = *(state->buffer.data + state->cursor - 1);
|
||||
switch (*oper) {
|
||||
case PSYC_STATE_RESYNC:
|
||||
state->content_parsed += 2;
|
||||
state->content_parsed++;
|
||||
return PSYC_PARSE_STATE_RESYNC;
|
||||
case PSYC_STATE_RESET:
|
||||
state->content_parsed += 2;
|
||||
state->content_parsed++;
|
||||
return PSYC_PARSE_STATE_RESET;
|
||||
default:
|
||||
return PSYC_PARSE_ERROR_MOD_NAME;
|
||||
}
|
||||
}
|
||||
state->cursor--;
|
||||
}
|
||||
|
||||
ret = psyc_parse_modifier(state, oper, name, value);
|
||||
state->content_parsed += state->cursor - pos;
|
||||
|
|
24
src/render.c
24
src/render.c
|
@ -6,14 +6,14 @@
|
|||
#ifdef __INLINE_PSYC_RENDER
|
||||
static inline
|
||||
#endif
|
||||
PsycRenderListRC
|
||||
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
|
||||
|
|
|
@ -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},
|
||||
|
|
|
@ -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) {
|
||||
|
|
Loading…
Reference in a new issue