From f31a44ea6a423b4c982c06d7f5391292ff4c8079 Mon Sep 17 00:00:00 2001 From: "psyc://psyced.org/~lynX" <@> Date: Fri, 22 Apr 2011 11:50:13 +0200 Subject: [PATCH] RenderHeader --- .gitignore | 1 + include/psyc/render.h | 21 +++++++++++++-------- src/render.c | 36 +++++++++++++++++++++++++++++++----- test/Makefile | 4 +++- test/testRender.c | 42 ++++++++++++++++++++++++++++++++++++++++++ 5 files changed, 90 insertions(+), 14 deletions(-) create mode 100644 test/testRender.c diff --git a/.gitignore b/.gitignore index 81af87c..6804378 100644 --- a/.gitignore +++ b/.gitignore @@ -4,6 +4,7 @@ doc/man src/match test/testMatch test/testParser +test/testRender .config ~$* diff --git a/include/psyc/render.h b/include/psyc/render.h index 52bf0c8..c236aa4 100644 --- a/include/psyc/render.h +++ b/include/psyc/render.h @@ -1,3 +1,5 @@ +#include "syntax.h" + /** * Struct for keeping render state. */ @@ -6,25 +8,28 @@ typedef enum { PSYC_NEED_LENGTH = 1 } PSYC_RenderFlag; +typedef enum { + PSYC_FLAG_ROUTING = 1 +} PSYC_RenderHeaderFlag; + typedef struct { PSYC_RenderFlag flag; ///< flags for the renderer PSYC_Part part; ///< part of the packet being rendered size_t cursor; ///< current position in buffer - size_t start; ///< position where the rendered packet starts + size_t spot; ///< space for rendered length between the headers size_t contentLength; ///< length of the content part size_t length; ///< how big is the buffer we allocated - uint8_t buffer[]; ///< OMG a C99 feature! variable size buffer! + char buffer[]; ///< OMG a C99 feature! variable size buffer! } PSYC_RenderState; int PSYC_renderHeader(PSYC_RenderState* render, - const uint8_t* name, const size_t nlength, - const uint8_t* value, const size_t vlength, - const uint8_t flags, const uint8_t modifier); + const char* name, size_t nlength, + const char* value, size_t vlength, + PSYC_RenderHeaderFlag flags, char modifier); int PSYC_renderBody(PSYC_RenderState* render, - const uint8_t* method, const size_t mlength, - const uint8_t* data, const size_t dlength, - const uint8_t flags); + const char* method, size_t mlength, + const char* data, size_t dlength); int PSYC_doneRender(PSYC_RenderState* render, uint8_t** buf, size_t* written); diff --git a/src/render.c b/src/render.c index 2bdec60..bf5e039 100644 --- a/src/render.c +++ b/src/render.c @@ -1,13 +1,39 @@ #include "psyc/lib.h" #include "psyc/render.h" -#include "psyc/syntax.h" -/* render PSYC packets */ +int PSYC_renderHeader(PSYC_RenderState* r, + const char* name, size_t nlength, + const char* value, size_t vlength, + const PSYC_RenderHeaderFlag flags, + char modifier) { + unless (nlength) nlength = strlen(name); + // vlength 0 means an empty variable.. no cheating there + unless (modifier) modifier = C_GLYPH_MODIFIER_SET; + + r->buffer[r->cursor++] = modifier; + strncpy(&r->buffer[r->cursor], name, nlength); + r->cursor += nlength; + if (vlength) { + r->buffer[r->cursor++] = '\t'; + strncpy(&r->buffer[r->cursor], value, vlength); + r->cursor += vlength; + } + + if (flags == PSYC_FLAG_ROUTING) { + if (r->spot != 0) { + P1(("Too late to add a routing variable!\n")) + return -1; + } + } else if (r->spot == 0) { + // add "\n000000000" to buffer + // and make spot point to the first 0 + } + return 0; +} int PSYC_renderBody(PSYC_RenderState* render, - const uint8_t* method, const size_t mlength, - const uint8_t* data, const size_t dlength, - const uint8_t flags) { + const char* method, size_t mlength, + const char* data, size_t dlength) { // find out if this packet needs a prepended length if (dlength == 1 && data[0] == C_GLYPH_PACKET_DELIMITER) diff --git a/test/Makefile b/test/Makefile index 3f4dec0..fd61356 100644 --- a/test/Makefile +++ b/test/Makefile @@ -1,7 +1,7 @@ CFLAGS=-I../include -DDEBUG -g -O0 -Wall LDFLAGS=-L../src LOADLIBES=-lpsyc -TARGETS=testParser testMatch +TARGETS=testParser testMatch testRender all: $(TARGETS) @@ -10,3 +10,5 @@ test: clean: rm -f $(TARGETS) + +it: all diff --git a/test/testRender.c b/test/testRender.c new file mode 100644 index 0000000..c69b1c0 --- /dev/null +++ b/test/testRender.c @@ -0,0 +1,42 @@ +#include +#include "../include/psyc/lib.h" +#include "../include/psyc/render.h" + +#define myUNI "psyc://10.100.1000/~ludwig" + +/* example renderer generating a presence packet */ +int writePresence(const char *avail, int availlen, const char *desc, int desclen) { + PSYC_RenderState *pb; // a chunk of mem will host both struct and buf1 +// char *t; +// unsigned int l; + + unless (availlen) availlen = strlen(avail); + unless (desclen) desclen = strlen(desc); + + if (!(pb = malloc(512))) { + P0(("Out of memory\n")) + return -1; + } +// if (PSYC_initBuffer(pb, WHATEVER)) die("PSYC_initBuffer hates me"); + + (void) PSYC_renderHeader(pb, "_context", 0, + myUNI, sizeof(myUNI), PSYC_FLAG_ROUTING, 0); + + // the first call to PSYC_renderHeader() without PSYC_FLAG_ROUTING adds the + // extra newline to the buffer. later vars with PSYC_FLAG_ROUTING cause an error. + (void) PSYC_renderHeader(pb, "_degree_availability", 0, avail, availlen, 0, C_GLYPH_MODIFIER_ASSIGN); + (void) PSYC_renderHeader(pb, "_description_presence", 0, desc, desclen, 0, C_GLYPH_MODIFIER_ASSIGN); + // presence is to be assigned permanently in distributed state + + (void) PSYC_renderBody(pb, "_notice_presence", 0, + NULL, 0); // no data in packet +// (void) PSYC_doneRender(pb, &t, &l); +// write(stdout, t, l); + free(pb); + return 0; +} + + +int main() { + return writePresence("_here", 0, "I'm omnipresent right now", 0); +}