RenderHeader

This commit is contained in:
psyc://psyced.org/~lynX 2011-04-22 11:50:13 +02:00
parent 9fd39c72a1
commit 893a318b82
5 changed files with 90 additions and 14 deletions

1
.gitignore vendored
View File

@ -4,6 +4,7 @@ doc/man
src/match
test/testMatch
test/testParser
test/testRender
.config
~$*

View File

@ -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);

View File

@ -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)

View File

@ -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

42
test/testRender.c Normal file
View File

@ -0,0 +1,42 @@
#include <stdlib.h>
#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);
}