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

127 lines
3.2 KiB
C
Raw Normal View History

2011-05-08 21:40:26 +00:00
#include "lib.h"
#include <psyc/render.h>
#include <psyc/syntax.h>
2011-04-20 20:22:55 +00:00
2011-05-09 14:32:39 +00:00
psycRenderListRC psyc_renderList (psycList *list, char *buffer, size_t buflen)
2011-04-26 15:23:06 +00:00
{
size_t i, cur = 0;
psycString *elem;
2011-05-09 14:32:39 +00:00
if (list->length > buflen) // return error if list doesn't fit in buffer
return PSYC_RENDER_LIST_ERROR;
2011-04-26 15:23:06 +00:00
if (list->flag == PSYC_LIST_NEED_LENGTH)
{
for (i = 0; i < list->num_elems; i++)
{
elem = &list->elems[i];
if (i > 0)
buffer[cur++] = '|';
cur += itoa(elem->length, buffer + cur, 10);
buffer[cur++] = ' ';
memcpy(buffer + cur, elem->ptr, elem->length);
cur += elem->length;
}
}
else
{
for (i = 0; i < list->num_elems; i++)
{
elem = &list->elems[i];
buffer[cur++] = '|';
memcpy(buffer + cur, elem->ptr, elem->length);
cur += elem->length;
}
}
// actual length should be equal to pre-calculated length at this point
assert(cur == list->length);
2011-05-09 14:32:39 +00:00
return PSYC_RENDER_LIST_SUCCESS;
2011-04-26 15:23:06 +00:00
}
static inline
size_t psyc_renderModifier (psycModifier *mod, char *buffer)
{
2011-04-25 19:57:03 +00:00
size_t cur = 0;
2011-04-26 15:23:06 +00:00
buffer[cur++] = mod->oper;
memcpy(buffer + cur, mod->name.ptr, mod->name.length);
cur += mod->name.length;
if (cur <= 1)
return cur; // error, name can't be empty
2011-04-26 15:23:06 +00:00
if (mod->flag == PSYC_MODIFIER_NEED_LENGTH)
{
buffer[cur++] = ' ';
2011-04-26 15:23:06 +00:00
cur += itoa(mod->value.length, buffer + cur, 10);
}
2011-04-25 19:57:03 +00:00
buffer[cur++] = '\t';
2011-04-26 15:23:06 +00:00
memcpy(buffer + cur, mod->value.ptr, mod->value.length);
cur += mod->value.length;
buffer[cur++] = '\n';
return cur;
}
psycRenderRC psyc_render (psycPacket *packet, char *buffer, size_t buflen)
{
size_t i, cur = 0, len;
2011-05-09 14:32:39 +00:00
if (packet->length > buflen) // return error if packet doesn't fit in buffer
return PSYC_RENDER_ERROR;
2011-04-25 21:14:22 +00:00
// render routing modifiers
2011-04-25 12:39:58 +00:00
for (i = 0; i < packet->routing.lines; i++)
{
len = psyc_renderModifier(&packet->routing.modifiers[i], buffer + cur);
cur += len;
if (len <= 1)
return PSYC_RENDER_ERROR_MODIFIER_NAME_MISSING;
}
2011-04-25 21:14:22 +00:00
// add length if needed
if (packet->flag == PSYC_PACKET_NEED_LENGTH)
cur += itoa(packet->contentLength, buffer + cur, 10);
if (packet->flag == PSYC_PACKET_NEED_LENGTH || packet->content.length ||
2011-05-04 15:46:20 +00:00
packet->entity.lines || packet->method.length || packet->data.length)
buffer[cur++] = '\n'; // start of content part if there's content or length
if (packet->content.length) // render raw content if present
2011-04-25 19:57:03 +00:00
{
memcpy(buffer + cur, packet->content.ptr, packet->content.length);
cur += packet->content.length;
}
else
{
// render entity modifiers
for (i = 0; i < packet->entity.lines; i++)
cur += psyc_renderModifier(&packet->entity.modifiers[i], buffer + cur);
2011-05-04 15:46:20 +00:00
if (packet->method.length) // add method\n
{
memcpy(buffer + cur, packet->method.ptr, packet->method.length);
cur += packet->method.length;
buffer[cur++] = '\n';
if (packet->data.length) // add data\n
{
memcpy(buffer + cur, packet->data.ptr, packet->data.length);
cur += packet->data.length;
buffer[cur++] = '\n';
}
}
else if (packet->data.length) // error, we have data but no modifier
return PSYC_RENDER_ERROR_METHOD_MISSING;
2011-04-25 19:57:03 +00:00
}
2011-04-25 21:14:22 +00:00
// add packet delimiter
2011-04-25 20:57:08 +00:00
buffer[cur++] = C_GLYPH_PACKET_DELIMITER;
buffer[cur++] = '\n';
2011-04-25 21:14:22 +00:00
// actual length should be equal to pre-calculated length at this point
assert(cur == packet->length);
return PSYC_RENDER_SUCCESS;
}