2011-04-20 20:22:55 +00:00
|
|
|
#include "psyc/lib.h"
|
|
|
|
#include "psyc/render.h"
|
2011-04-25 21:14:22 +00:00
|
|
|
#include "psyc/syntax.h"
|
2011-04-20 20:22:55 +00:00
|
|
|
|
2011-04-26 15:23:06 +00:00
|
|
|
inline psycRenderRC psyc_renderList(psycList *list, char *buffer, size_t buflen)
|
|
|
|
{
|
|
|
|
size_t i, cur = 0;
|
|
|
|
psycString *elem;
|
|
|
|
|
|
|
|
if (list->length > buflen)
|
|
|
|
return PSYC_RENDER_ERROR; // return error if list doesn't fit in buffer
|
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return PSYC_RENDER_SUCCESS;
|
|
|
|
}
|
|
|
|
|
|
|
|
inline size_t psyc_renderModifier(psycModifier *mod, char *buffer)
|
2011-04-25 12:20:13 +00:00
|
|
|
{
|
2011-04-25 19:57:03 +00:00
|
|
|
size_t cur = 0;
|
2011-04-25 12:20:13 +00:00
|
|
|
|
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 (mod->flag == PSYC_MODIFIER_NEED_LENGTH)
|
2011-04-25 12:20:13 +00:00
|
|
|
{
|
2011-04-25 12:47:16 +00:00
|
|
|
buffer[cur++] = ' ';
|
2011-04-26 15:23:06 +00:00
|
|
|
//cur += sprintf(buffer + cur, "%ld", mod->value.length);
|
|
|
|
cur += itoa(mod->value.length, buffer + cur, 10);
|
2011-04-25 12:20:13 +00:00
|
|
|
}
|
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;
|
2011-04-25 12:47:16 +00:00
|
|
|
buffer[cur++] = '\n';
|
2011-04-25 12:20:13 +00:00
|
|
|
|
|
|
|
return cur;
|
|
|
|
}
|
|
|
|
|
2011-04-25 21:40:38 +00:00
|
|
|
psycRenderRC psyc_render(psycPacket *packet, char *buffer, size_t buflen)
|
2011-04-25 12:20:13 +00:00
|
|
|
{
|
2011-04-25 19:57:03 +00:00
|
|
|
size_t i, cur = 0;
|
2011-04-25 12:20:13 +00:00
|
|
|
|
|
|
|
if (packet->length > buflen)
|
2011-04-25 21:14:22 +00:00
|
|
|
return PSYC_RENDER_ERROR; // return error if packet doesn't fit in buffer
|
2011-04-25 12:20:13 +00:00
|
|
|
|
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++)
|
2011-04-25 21:40:38 +00:00
|
|
|
cur += psyc_renderModifier(&packet->routing.modifiers[i], buffer + cur);
|
2011-04-25 12:20:13 +00:00
|
|
|
|
2011-04-25 21:14:22 +00:00
|
|
|
// add length if needed
|
2011-04-25 14:11:47 +00:00
|
|
|
if (packet->flag == PSYC_PACKET_NEED_LENGTH) {
|
2011-04-25 19:57:03 +00:00
|
|
|
//cur += sprintf(buffer + cur, "%ld", packet->contentLength);
|
2011-04-25 14:11:47 +00:00
|
|
|
cur += itoa(packet->contentLength, buffer + cur, 10);
|
|
|
|
}
|
2011-04-25 12:20:13 +00:00
|
|
|
|
2011-04-29 01:15:36 +00:00
|
|
|
if (packet->entity.lines || packet->method.length || packet->data.length)
|
|
|
|
buffer[cur++] = '\n'; // start of content part if there's content
|
2011-04-25 12:20:13 +00:00
|
|
|
|
2011-04-25 21:14:22 +00:00
|
|
|
// render entity modifiers
|
2011-04-25 12:39:58 +00:00
|
|
|
for (i = 0; i < packet->entity.lines; i++)
|
2011-04-25 21:40:38 +00:00
|
|
|
cur += psyc_renderModifier(&packet->entity.modifiers[i], buffer + cur);
|
2011-04-25 19:57:03 +00:00
|
|
|
|
2011-04-25 21:14:22 +00:00
|
|
|
if (packet->method.length) // add method\n
|
2011-04-25 19:57:03 +00:00
|
|
|
{
|
|
|
|
memcpy(buffer + cur, packet->method.ptr, packet->method.length);
|
|
|
|
cur += packet->method.length;
|
|
|
|
buffer[cur++] = '\n';
|
|
|
|
}
|
2011-04-25 12:20:13 +00:00
|
|
|
|
2011-04-25 21:14:22 +00:00
|
|
|
if (packet->data.length) // add data\n
|
2011-04-25 19:57:03 +00:00
|
|
|
{
|
2011-04-26 21:16:56 +00:00
|
|
|
memcpy(buffer + cur, packet->data.ptr, packet->data.length);
|
2011-04-25 19:57:03 +00:00
|
|
|
cur += packet->data.length;
|
|
|
|
buffer[cur++] = '\n';
|
|
|
|
}
|
2011-04-25 12:20:13 +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 20:31:21 +00:00
|
|
|
|
2011-04-25 21:14:22 +00:00
|
|
|
// actual length should be equal to pre-calculated length at this point
|
2011-04-25 20:31:21 +00:00
|
|
|
assert(cur == packet->length);
|
2011-04-25 12:20:13 +00:00
|
|
|
return PSYC_RENDER_SUCCESS;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2011-04-25 21:40:38 +00:00
|
|
|
inline void psyc_initRenderState (psycRenderState *state)
|
2011-04-22 18:33:22 +00:00
|
|
|
{
|
2011-04-25 21:40:38 +00:00
|
|
|
memset(state, 0, sizeof(psycRenderState));
|
2011-04-22 18:33:22 +00:00
|
|
|
}
|
|
|
|
|
2011-04-25 21:40:38 +00:00
|
|
|
psycRenderRC psyc_renderModifier(psycRenderState *state,
|
2011-04-25 12:20:13 +00:00
|
|
|
const char *name, size_t nlength,
|
|
|
|
const char *value, size_t vlength,
|
2011-04-25 21:40:38 +00:00
|
|
|
const psycRenderFlag flags, char oper)
|
2011-04-22 15:09:32 +00:00
|
|
|
{
|
|
|
|
size_t startc = state->cursor;
|
|
|
|
|
2011-04-22 09:50:13 +00:00
|
|
|
unless (nlength) nlength = strlen(name);
|
|
|
|
// vlength 0 means an empty variable.. no cheating there
|
2011-04-22 20:59:15 +00:00
|
|
|
unless (oper) oper = C_GLYPH_OPERATOR_SET;
|
2011-04-22 09:50:13 +00:00
|
|
|
|
2011-04-22 20:59:15 +00:00
|
|
|
state->buffer[state->cursor++] = oper;
|
2011-04-22 15:09:32 +00:00
|
|
|
strncpy(&state->buffer[state->cursor], name, nlength);
|
|
|
|
state->cursor += nlength;
|
|
|
|
|
|
|
|
if (vlength)
|
|
|
|
{
|
|
|
|
state->buffer[state->cursor++] = '\t';
|
|
|
|
strncpy(&state->buffer[state->cursor], value, vlength);
|
|
|
|
state->cursor += vlength;
|
2011-04-22 09:50:13 +00:00
|
|
|
}
|
|
|
|
|
2011-04-22 15:09:32 +00:00
|
|
|
//if (flags == PSYC_RENDER_ROUTING)
|
2011-04-25 21:40:38 +00:00
|
|
|
if (psyc_isRoutingVar(name, nlength))
|
2011-04-22 15:09:32 +00:00
|
|
|
{ // no more routing headers allowed after content started
|
|
|
|
if (state->part != PSYC_PART_ROUTING)
|
|
|
|
{
|
|
|
|
P1(("Too late to add a routing variable!\n"));
|
|
|
|
return PSYC_RENDER_ERROR_ROUTING;
|
2011-04-22 09:50:13 +00:00
|
|
|
}
|
2011-04-22 15:09:32 +00:00
|
|
|
}
|
|
|
|
else if (state->part == PSYC_PART_ROUTING)
|
|
|
|
{ // first entity header, set part to content
|
|
|
|
state->part = PSYC_PART_CONTENT;
|
2011-04-22 09:50:13 +00:00
|
|
|
// add "\n000000000" to buffer
|
|
|
|
// and make spot point to the first 0
|
|
|
|
}
|
2011-04-22 15:09:32 +00:00
|
|
|
|
|
|
|
// update content length if we're in the content part
|
|
|
|
if (state->part == PSYC_PART_CONTENT)
|
|
|
|
state->contentLength += state->cursor - startc;
|
|
|
|
|
|
|
|
return PSYC_RENDER_SUCCESS;
|
2011-04-22 09:50:13 +00:00
|
|
|
}
|
2011-04-20 20:22:55 +00:00
|
|
|
|
2011-04-25 21:40:38 +00:00
|
|
|
psycRenderRC psyc_renderBody(psycRenderState *state,
|
2011-04-25 12:20:13 +00:00
|
|
|
const char *method, size_t mlength,
|
|
|
|
const char *data, size_t dlength)
|
2011-04-22 15:09:32 +00:00
|
|
|
{
|
2011-04-22 18:22:20 +00:00
|
|
|
if (state->flag == PSYC_RENDER_CHECK_LENGTH)
|
2011-04-22 15:09:32 +00:00
|
|
|
{
|
|
|
|
// find out if this packet needs a prepended length
|
|
|
|
if (dlength == 1 && data[0] == C_GLYPH_PACKET_DELIMITER)
|
2011-04-22 18:22:20 +00:00
|
|
|
state->flag = PSYC_RENDER_NEED_LENGTH;
|
2011-04-22 15:09:32 +00:00
|
|
|
else if (dlength > 404)
|
2011-04-22 18:22:20 +00:00
|
|
|
state->flag = PSYC_RENDER_NEED_LENGTH;
|
2011-04-25 12:20:13 +00:00
|
|
|
else if (memmem(data, dlength, PSYC_C2ARG(PSYC_PACKET_DELIMITER)))
|
2011-04-22 18:22:20 +00:00
|
|
|
state->flag = PSYC_RENDER_NEED_LENGTH;
|
2011-04-22 15:09:32 +00:00
|
|
|
else
|
2011-04-22 18:22:20 +00:00
|
|
|
state->flag = PSYC_RENDER_NO_LENGTH;
|
2011-04-22 15:09:32 +00:00
|
|
|
}
|
2011-04-20 20:22:55 +00:00
|
|
|
|
|
|
|
// TBD
|
2011-04-22 15:09:32 +00:00
|
|
|
|
|
|
|
return PSYC_RENDER_SUCCESS;
|
2011-04-20 20:22:55 +00:00
|
|
|
}
|
2011-04-25 12:20:13 +00:00
|
|
|
*/
|