mirror of
git://git.psyc.eu/libpsyc
synced 2024-08-15 03:19:02 +00:00
fixes & test for rendering
This commit is contained in:
parent
8d47fdee40
commit
4fbaf4ecec
6 changed files with 75 additions and 25 deletions
|
@ -125,7 +125,7 @@ typedef struct
|
|||
typedef struct
|
||||
{
|
||||
size_t lines;
|
||||
PSYC_Modifier **modifiers;
|
||||
PSYC_Modifier *modifiers;
|
||||
} PSYC_ModifierArray;
|
||||
|
||||
/* intermediate struct for a PSYC packet */
|
||||
|
@ -154,8 +154,8 @@ inline PSYC_Packet PSYC_newPacket(PSYC_ModifierArray *routing,
|
|||
PSYC_String *method, PSYC_String *data,
|
||||
PSYC_PacketFlag flag);
|
||||
|
||||
inline PSYC_Packet PSYC_newPacket2(PSYC_Modifier **routing, size_t routinglen,
|
||||
PSYC_Modifier **entity, size_t entitylen,
|
||||
inline PSYC_Packet PSYC_newPacket2(PSYC_Modifier *routing, size_t routinglen,
|
||||
PSYC_Modifier *entity, size_t entitylen,
|
||||
const char *method, size_t methodlen,
|
||||
const char *data, size_t datalen,
|
||||
PSYC_PacketFlag flag);
|
||||
|
|
|
@ -11,6 +11,8 @@
|
|||
#define unless(COND) if (!(COND))
|
||||
#define until(COND) while (!(COND))
|
||||
|
||||
#define PSYC_NUM_ELEM(a) (sizeof(a) / sizeof(*(a)))
|
||||
|
||||
#if !defined(_GNU_SOURCE) && !defined(__FBSDID)
|
||||
void * memmem(const void *l, size_t l_len, const void *s, size_t s_len);
|
||||
#endif
|
||||
|
|
25
src/psyc.c
25
src/psyc.c
|
@ -3,7 +3,7 @@
|
|||
|
||||
#include <math.h>
|
||||
|
||||
inline PSYC_String PSYC_newString (const char *str, size_t strlen)
|
||||
inline PSYC_String PSYC_newString(const char *str, size_t strlen)
|
||||
{
|
||||
PSYC_String s = {strlen, str};
|
||||
return s;
|
||||
|
@ -40,7 +40,7 @@ inline PSYC_Modifier PSYC_newModifier2(char oper,
|
|||
|
||||
inline size_t PSYC_getModifierLength(PSYC_Modifier *m)
|
||||
{
|
||||
size_t length =
|
||||
size_t length = 1 + // oper
|
||||
m->name.length + 1 + // name\t
|
||||
m->value.length + 1; // value\n
|
||||
|
||||
|
@ -70,27 +70,30 @@ inline PSYC_Packet PSYC_newPacket(PSYC_ModifierArray *routing,
|
|||
p.flag = PSYC_PACKET_NO_LENGTH;
|
||||
}
|
||||
|
||||
// calculate routing header length
|
||||
// add routing header length
|
||||
for (i = 0; i < routing->lines; i++)
|
||||
p.routingLength += PSYC_getModifierLength(routing->modifiers[i]);
|
||||
p.routingLength += PSYC_getModifierLength(&routing->modifiers[i]);
|
||||
|
||||
// calculate entity header length
|
||||
// add entity header length
|
||||
for (i = 0; i < entity->lines; i++)
|
||||
p.contentLength += PSYC_getModifierLength(routing->modifiers[i]);
|
||||
p.contentLength += PSYC_getModifierLength(&entity->modifiers[i]);
|
||||
|
||||
// add length of method, data & delimiter
|
||||
p.contentLength += method->length + 1 + data->length; // method \n data
|
||||
if (method->length)
|
||||
p.contentLength += method->length + 1; // method\n
|
||||
if (data->length)
|
||||
p.contentLength += data->length + 1; // data\n
|
||||
|
||||
// set total length: routing-header \n content \n|\n
|
||||
p.length = p.routingLength + 1 + p.contentLength + sizeof(PSYC_PACKET_DELIMITER) - 1;
|
||||
// set total length: routing-header \n content |\n
|
||||
p.length = p.routingLength + 1 + p.contentLength + sizeof(PSYC_PACKET_DELIMITER) - 2;
|
||||
if (flag == PSYC_PACKET_NEED_LENGTH) // add length of length if needed
|
||||
p.length += log10((double)data->length) + 1;
|
||||
|
||||
return p;
|
||||
}
|
||||
|
||||
inline PSYC_Packet PSYC_newPacket2(PSYC_Modifier **routing, size_t routinglen,
|
||||
PSYC_Modifier **entity, size_t entitylen,
|
||||
inline PSYC_Packet PSYC_newPacket2(PSYC_Modifier *routing, size_t routinglen,
|
||||
PSYC_Modifier *entity, size_t entitylen,
|
||||
const char *method, size_t methodlen,
|
||||
const char *data, size_t datalen,
|
||||
PSYC_PacketFlag flag)
|
||||
|
|
29
src/render.c
29
src/render.c
|
@ -3,7 +3,7 @@
|
|||
|
||||
inline int PSYC_renderModifier(PSYC_Modifier *m, char *buffer)
|
||||
{
|
||||
int cur = 0;
|
||||
size_t cur = 0;
|
||||
|
||||
buffer[cur++] = m->oper;
|
||||
memcpy(buffer + cur, m->name.ptr, m->name.length);
|
||||
|
@ -11,9 +11,11 @@ inline int PSYC_renderModifier(PSYC_Modifier *m, char *buffer)
|
|||
if (m->flag == PSYC_MODIFIER_NEED_LENGTH)
|
||||
{
|
||||
buffer[cur++] = ' ';
|
||||
// cur += sprintf(buffer + cur, "%ld", m->value.length);
|
||||
//cur += sprintf(buffer + cur, "%ld", m->value.length);
|
||||
cur += itoa(m->value.length, buffer + cur, 10);
|
||||
}
|
||||
|
||||
buffer[cur++] = '\t';
|
||||
memcpy(buffer + cur, m->value.ptr, m->value.length);
|
||||
cur += m->value.length;
|
||||
buffer[cur++] = '\n';
|
||||
|
@ -23,26 +25,39 @@ inline int PSYC_renderModifier(PSYC_Modifier *m, char *buffer)
|
|||
|
||||
PSYC_RenderRC PSYC_render(PSYC_Packet *packet, char *buffer, size_t buflen)
|
||||
{
|
||||
size_t cur, i;
|
||||
size_t i, cur = 0;
|
||||
|
||||
if (packet->length > buflen)
|
||||
return PSYC_RENDER_ERROR;
|
||||
|
||||
for (i = 0; i < packet->routing.lines; i++)
|
||||
cur += PSYC_renderModifier(packet->routing.modifiers[i], buffer + cur);
|
||||
cur += PSYC_renderModifier(&packet->routing.modifiers[i], buffer + cur);
|
||||
|
||||
if (packet->flag == PSYC_PACKET_NEED_LENGTH) {
|
||||
// cur += sprintf(buffer + cur, "%ld", packet->contentLength);
|
||||
//cur += sprintf(buffer + cur, "%ld", packet->contentLength);
|
||||
cur += itoa(packet->contentLength, buffer + cur, 10);
|
||||
}
|
||||
|
||||
buffer[cur++] = '\n';
|
||||
|
||||
for (i = 0; i < packet->entity.lines; i++)
|
||||
cur += PSYC_renderModifier(packet->entity.modifiers[i], buffer + cur);
|
||||
cur += PSYC_renderModifier(&packet->entity.modifiers[i], buffer + cur);
|
||||
|
||||
memcpy(buffer + cur, PSYC_C2ARG(PSYC_PACKET_DELIMITER));
|
||||
if (packet->method.length)
|
||||
{
|
||||
memcpy(buffer + cur, packet->method.ptr, packet->method.length);
|
||||
cur += packet->method.length;
|
||||
buffer[cur++] = '\n';
|
||||
}
|
||||
|
||||
if (packet->data.length)
|
||||
{
|
||||
memcpy(buffer + cur, packet->data.ptr, packet->method.length);
|
||||
cur += packet->data.length;
|
||||
buffer[cur++] = '\n';
|
||||
}
|
||||
|
||||
memcpy(buffer + cur, PSYC_PACKET_DELIMITER + 1, 2);
|
||||
return PSYC_RENDER_SUCCESS;
|
||||
}
|
||||
|
||||
|
|
|
@ -29,7 +29,7 @@ const size_t PSYC_routingVarsNum = sizeof(PSYC_routingVars) / sizeof(*PSYC_routi
|
|||
/**
|
||||
* Get the type of variable name.
|
||||
*/
|
||||
PSYC_Bool PSYC_isRoutingVar(const char* name, size_t len)
|
||||
PSYC_Bool PSYC_isRoutingVar(const char *name, size_t len)
|
||||
{
|
||||
size_t cursor = 1;
|
||||
int8_t matching[PSYC_routingVarsNum]; // indexes of matching vars
|
||||
|
@ -68,7 +68,7 @@ PSYC_Bool PSYC_isRoutingVar(const char* name, size_t len)
|
|||
/**
|
||||
* Get the type of variable name.
|
||||
*/
|
||||
PSYC_Type PSYC_getVarType(char* name, size_t len)
|
||||
PSYC_Type PSYC_getVarType(char *name, size_t len)
|
||||
{
|
||||
return PSYC_TYPE_UNKNOWN;
|
||||
}
|
||||
|
|
|
@ -1,11 +1,40 @@
|
|||
#include <stdlib.h>
|
||||
#include <unistd.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) {
|
||||
int writePresence(const char *avail, int availlen, const char *desc, int desclen)
|
||||
{
|
||||
PSYC_Modifier routing[] = {
|
||||
PSYC_newModifier2(C_GLYPH_OPERATOR_SET, PSYC_C2ARG("_source"), PSYC_C2ARG(myUNI),
|
||||
PSYC_MODIFIER_ROUTING),
|
||||
PSYC_newModifier2(C_GLYPH_OPERATOR_SET, PSYC_C2ARG("_context"), PSYC_C2ARG(myUNI),
|
||||
PSYC_MODIFIER_CHECK_LENGTH),
|
||||
};
|
||||
|
||||
PSYC_Modifier entity[] = {
|
||||
// presence is to be assigned permanently in distributed state
|
||||
PSYC_newModifier2(C_GLYPH_OPERATOR_ASSIGN, PSYC_C2ARG("_degree_availability"),
|
||||
avail, availlen, PSYC_PACKET_CHECK_LENGTH),
|
||||
PSYC_newModifier2(C_GLYPH_OPERATOR_ASSIGN, PSYC_C2ARG("_description_presence"),
|
||||
desc, desclen, PSYC_PACKET_CHECK_LENGTH),
|
||||
};
|
||||
|
||||
PSYC_Packet packet = PSYC_newPacket2(routing, PSYC_NUM_ELEM(routing),
|
||||
entity, PSYC_NUM_ELEM(entity),
|
||||
PSYC_C2ARG("_notice_presence"),
|
||||
NULL, 0,
|
||||
//PSYC_C2ARG("foobar"),
|
||||
PSYC_PACKET_CHECK_LENGTH);
|
||||
|
||||
char buffer[512];
|
||||
PSYC_render(&packet, buffer, sizeof(buffer));
|
||||
write(0, buffer, packet.length);
|
||||
|
||||
/*
|
||||
PSYC_RenderState *pb; // a chunk of mem will host both struct and buf1
|
||||
// char *t;
|
||||
// unsigned int l;
|
||||
|
@ -33,10 +62,11 @@ int writePresence(const char *avail, int availlen, const char *desc, int desclen
|
|||
// (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);
|
||||
return writePresence(PSYC_C2ARG("_here"), PSYC_C2ARG("I'm omnipresent right now"));
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue