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

119 lines
3.2 KiB
C
Raw Normal View History

2011-04-20 20:22:55 +00:00
#include "psyc/lib.h"
#include "psyc/render.h"
inline int PSYC_renderModifier(PSYC_Modifier *m, char *buffer)
{
int cur = 0;
memcpy(buffer + cur++, &m->oper, 1);
memcpy(buffer + cur, m->name.ptr, m->name.length);
cur += m->name.length;
if (m->flag == PSYC_MODIFIER_NEED_LENGTH)
{
memcpy(buffer + cur++, " ", 1);
cur += sprintf(buffer + cur, "%ld", m->value.length);
}
memcpy(buffer + cur, m->value.ptr, m->value.length);
cur += m->value.length;
memcpy(buffer + cur++, "\n", 1);
return cur;
}
PSYC_RenderRC PSYC_render(PSYC_Packet *packet, char *buffer, size_t buflen)
{
size_t cur, i;
if (packet->length > buflen)
return PSYC_RENDER_ERROR;
for (i = 0; i < packet->routing.length; i++)
cur += PSYC_renderModifier(&packet->routing.ptr[i], buffer + cur);
if (packet->flag == PSYC_PACKET_NEED_LENGTH)
cur += sprintf(buffer + cur, "%ld", packet->contentLength);
memcpy(buffer + cur++, "\n", 1);
for (i = 0; i < packet->entity.length; i++)
cur += PSYC_renderModifier(&packet->entity.ptr[i], buffer + cur);
memcpy(buffer + cur, PSYC_C2ARG(PSYC_PACKET_DELIMITER));
return PSYC_RENDER_SUCCESS;
}
/*
inline void PSYC_initRenderState (PSYC_RenderState *state)
2011-04-22 18:33:22 +00:00
{
memset(state, 0, sizeof(PSYC_RenderState));
}
PSYC_RenderRC PSYC_renderModifier(PSYC_RenderState *state,
const char *name, size_t nlength,
const char *value, size_t vlength,
2011-04-22 20:59:15 +00:00
const PSYC_RenderFlag 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)
if (PSYC_isRoutingVar(name, nlength))
{ // 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
PSYC_RenderRC PSYC_renderBody(PSYC_RenderState *state,
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;
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
}
*/