mirror of
git://git.psyc.eu/libpsyc
synced 2024-08-15 03:19:02 +00:00
render: restructuring into more functions, added some docs
This commit is contained in:
parent
07d7a3f776
commit
ec69b5ee3f
4 changed files with 160 additions and 97 deletions
|
@ -158,21 +158,44 @@ typedef struct
|
||||||
|
|
||||||
inline int psyc_version();
|
inline int psyc_version();
|
||||||
|
|
||||||
|
/** Check if a modifier needs length */
|
||||||
|
inline psycModifierFlag psyc_checkModifierLength(psycModifier *m);
|
||||||
|
|
||||||
|
/** Get the total length of a modifier. */
|
||||||
|
inline size_t psyc_getModifierLength(psycModifier *m);
|
||||||
|
|
||||||
|
/** Create new modifier */
|
||||||
inline psycModifier psyc_newModifier(char oper, psycString *name, psycString *value,
|
inline psycModifier psyc_newModifier(char oper, psycString *name, psycString *value,
|
||||||
psycModifierFlag flag);
|
psycModifierFlag flag);
|
||||||
|
|
||||||
|
/** Create new modifier */
|
||||||
inline psycModifier psyc_newModifier2(char oper,
|
inline psycModifier psyc_newModifier2(char oper,
|
||||||
const char *name, size_t namelen,
|
const char *name, size_t namelen,
|
||||||
const char *value, size_t valuelen,
|
const char *value, size_t valuelen,
|
||||||
psycModifierFlag flag);
|
psycModifierFlag flag);
|
||||||
|
|
||||||
|
/** Check if a list needs length */
|
||||||
|
inline psycListFlag psyc_checkListLength(psycList *list);
|
||||||
|
|
||||||
|
/** Get the total length of a list. */
|
||||||
|
inline psycListFlag psyc_getListLength(psycList *list);
|
||||||
|
|
||||||
|
/** Check if a packet needs length */
|
||||||
|
inline psycPacketFlag psyc_checkPacketLength(psycPacket *p);
|
||||||
|
|
||||||
|
/** Calculate and set the length of packet parts and total packet length */
|
||||||
|
inline size_t psyc_setPacketLength(psycPacket *p);
|
||||||
|
|
||||||
|
/** Create new list */
|
||||||
inline psycList psyc_newList(psycString *elems, size_t num_elems, psycListFlag flag);
|
inline psycList psyc_newList(psycString *elems, size_t num_elems, psycListFlag flag);
|
||||||
|
|
||||||
|
/** Create new packet */
|
||||||
inline psycPacket psyc_newPacket(psycHeader *routing,
|
inline psycPacket psyc_newPacket(psycHeader *routing,
|
||||||
psycHeader *entity,
|
psycHeader *entity,
|
||||||
psycString *method, psycString *data,
|
psycString *method, psycString *data,
|
||||||
psycPacketFlag flag);
|
psycPacketFlag flag);
|
||||||
|
|
||||||
|
/** Create new packet */
|
||||||
inline psycPacket psyc_newPacket2(psycModifier *routing, size_t routinglen,
|
inline psycPacket psyc_newPacket2(psycModifier *routing, size_t routinglen,
|
||||||
psycModifier *entity, size_t entitylen,
|
psycModifier *entity, size_t entitylen,
|
||||||
const char *method, size_t methodlen,
|
const char *method, size_t methodlen,
|
||||||
|
|
181
src/packet.c
181
src/packet.c
|
@ -9,20 +9,39 @@ inline psycString psyc_newString(const char *str, size_t strlen)
|
||||||
return s;
|
return s;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
inline psycModifierFlag psyc_checkModifierLength(psycModifier *m)
|
||||||
|
{
|
||||||
|
psycModifierFlag flag;
|
||||||
|
|
||||||
|
if (m->value.length > PSYC_MODIFIER_SIZE_THRESHOLD)
|
||||||
|
flag = PSYC_MODIFIER_NEED_LENGTH;
|
||||||
|
else if (memchr(m->value.ptr, (int)'\n', m->value.length))
|
||||||
|
flag = PSYC_MODIFIER_NEED_LENGTH;
|
||||||
|
else
|
||||||
|
flag = PSYC_MODIFIER_NO_LENGTH;
|
||||||
|
|
||||||
|
return flag;
|
||||||
|
}
|
||||||
|
|
||||||
|
inline size_t psyc_getModifierLength(psycModifier *m)
|
||||||
|
{
|
||||||
|
size_t length = 1 + // oper
|
||||||
|
m->name.length + 1 + // name\t
|
||||||
|
m->value.length + 1; // value\n
|
||||||
|
|
||||||
|
if (m->flag == PSYC_MODIFIER_NEED_LENGTH) // add length of length if needed
|
||||||
|
length += log10((double)m->value.length) + 2; // SP length
|
||||||
|
|
||||||
|
return length;
|
||||||
|
}
|
||||||
|
|
||||||
inline psycModifier psyc_newModifier(char oper, psycString *name, psycString *value,
|
inline psycModifier psyc_newModifier(char oper, psycString *name, psycString *value,
|
||||||
psycModifierFlag flag)
|
psycModifierFlag flag)
|
||||||
{
|
{
|
||||||
psycModifier m = {oper, *name, *value, flag};
|
psycModifier m = {oper, *name, *value, flag};
|
||||||
|
|
||||||
if (flag == PSYC_MODIFIER_CHECK_LENGTH) // find out if it needs a length
|
if (flag == PSYC_MODIFIER_CHECK_LENGTH) // find out if it needs a length
|
||||||
{
|
m.flag = psyc_checkModifierLength(&m);
|
||||||
if (value->length > PSYC_MODIFIER_SIZE_THRESHOLD)
|
|
||||||
m.flag = PSYC_MODIFIER_NEED_LENGTH;
|
|
||||||
else if (memchr(value->ptr, (int)'\n', value->length))
|
|
||||||
m.flag = PSYC_MODIFIER_NEED_LENGTH;
|
|
||||||
else
|
|
||||||
m.flag = PSYC_MODIFIER_NO_LENGTH;
|
|
||||||
}
|
|
||||||
|
|
||||||
return m;
|
return m;
|
||||||
}
|
}
|
||||||
|
@ -38,14 +57,45 @@ inline psycModifier psyc_newModifier2(char oper,
|
||||||
return psyc_newModifier(oper, &n, &v, flag);
|
return psyc_newModifier(oper, &n, &v, flag);
|
||||||
}
|
}
|
||||||
|
|
||||||
inline size_t psyc_getModifierLength(psycModifier *m)
|
inline psycListFlag psyc_checkListLength(psycList *list)
|
||||||
{
|
{
|
||||||
size_t length = 1 + // oper
|
psycListFlag flag = PSYC_LIST_NO_LENGTH;
|
||||||
m->name.length + 1 + // name\t
|
size_t i, length = 0;
|
||||||
m->value.length + 1; // value\n
|
|
||||||
|
|
||||||
if (m->flag == PSYC_MODIFIER_NEED_LENGTH) // add length of length if needed
|
for (i = 0; i < list->num_elems; i++)
|
||||||
length += log10((double)m->value.length) + 2; // SP length
|
{
|
||||||
|
psycString *elem = &list->elems[i];
|
||||||
|
length += 1 + elem->length; // |elem
|
||||||
|
if (length > PSYC_MODIFIER_SIZE_THRESHOLD ||
|
||||||
|
memchr(elem->ptr, (int)'|', elem->length) ||
|
||||||
|
memchr(elem->ptr, (int)'\n', elem->length))
|
||||||
|
{
|
||||||
|
flag = PSYC_LIST_NEED_LENGTH;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return flag;
|
||||||
|
}
|
||||||
|
|
||||||
|
inline psycListFlag psyc_getListLength(psycList *list)
|
||||||
|
{
|
||||||
|
size_t i, length = 0;
|
||||||
|
|
||||||
|
if (list->flag == PSYC_LIST_NEED_LENGTH)
|
||||||
|
{
|
||||||
|
for (i = 0; i < list->num_elems; i++)
|
||||||
|
{
|
||||||
|
if (i > 0)
|
||||||
|
length++; // |
|
||||||
|
length += log10((double)list->elems[i].length) + 2 + list->elems[i].length; // length SP elem
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
for (i = 0; i < list->num_elems; i++)
|
||||||
|
length += 1 + list->elems[i].length; // |elem
|
||||||
|
}
|
||||||
|
|
||||||
return length;
|
return length;
|
||||||
}
|
}
|
||||||
|
@ -53,78 +103,67 @@ inline size_t psyc_getModifierLength(psycModifier *m)
|
||||||
inline psycList psyc_newList(psycString *elems, size_t num_elems, psycListFlag flag)
|
inline psycList psyc_newList(psycString *elems, size_t num_elems, psycListFlag flag)
|
||||||
{
|
{
|
||||||
psycList list = {num_elems, elems, 0, flag};
|
psycList list = {num_elems, elems, 0, flag};
|
||||||
size_t i;
|
|
||||||
|
|
||||||
if (flag == PSYC_LIST_CHECK_LENGTH) // check if list elements need length
|
if (flag == PSYC_LIST_CHECK_LENGTH) // check if list elements need length
|
||||||
{
|
list.flag = psyc_checkListLength(&list);
|
||||||
for (i = 0; i < num_elems; i++)
|
|
||||||
{
|
|
||||||
if (memchr(elems[i].ptr, (int)'|', elems[i].length) ||
|
|
||||||
memchr(elems[i].ptr, (int)'\n', elems[i].length))
|
|
||||||
{
|
|
||||||
list.flag = PSYC_LIST_NEED_LENGTH;
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if (list.flag == PSYC_LIST_NEED_LENGTH)
|
|
||||||
{
|
|
||||||
for (i = 0; i < num_elems; i++)
|
|
||||||
{
|
|
||||||
if (i > 0)
|
|
||||||
list.length++; // |
|
|
||||||
list.length += log10((double)elems[i].length) + 2 + elems[i].length; // length SP elem
|
|
||||||
}
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
for (i = 0; i < num_elems; i++)
|
|
||||||
list.length += 1 + elems[i].length; // |elem
|
|
||||||
}
|
|
||||||
|
|
||||||
|
list.length = psyc_getListLength(&list);
|
||||||
return list;
|
return list;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
inline psycPacketFlag psyc_checkPacketLength(psycPacket *p)
|
||||||
|
{
|
||||||
|
psycPacketFlag flag;
|
||||||
|
|
||||||
|
if (p->data.length == 1 && p->data.ptr[0] == C_GLYPH_PACKET_DELIMITER)
|
||||||
|
flag = PSYC_PACKET_NEED_LENGTH;
|
||||||
|
else if (p->data.length > PSYC_CONTENT_SIZE_THRESHOLD)
|
||||||
|
flag = PSYC_PACKET_NEED_LENGTH;
|
||||||
|
else if (memmem(p->data.ptr, p->data.length, PSYC_C2ARG(PSYC_PACKET_DELIMITER)))
|
||||||
|
flag = PSYC_PACKET_NEED_LENGTH;
|
||||||
|
else
|
||||||
|
flag = PSYC_PACKET_NO_LENGTH;
|
||||||
|
|
||||||
|
return flag;
|
||||||
|
}
|
||||||
|
|
||||||
|
inline size_t psyc_setPacketLength(psycPacket *p)
|
||||||
|
{
|
||||||
|
size_t i;
|
||||||
|
|
||||||
|
// add routing header length
|
||||||
|
for (i = 0; i < p->routing.lines; i++)
|
||||||
|
p->routingLength += psyc_getModifierLength(&(p->routing.modifiers[i]));
|
||||||
|
|
||||||
|
// add entity header length
|
||||||
|
for (i = 0; i < p->entity.lines; i++)
|
||||||
|
p->contentLength += psyc_getModifierLength(&(p->entity.modifiers[i]));
|
||||||
|
|
||||||
|
// add length of method, data & delimiter
|
||||||
|
if (p->method.length)
|
||||||
|
p->contentLength += p->method.length + 1; // method\n
|
||||||
|
if (p->data.length)
|
||||||
|
p->contentLength += p->data.length + 1; // data\n
|
||||||
|
|
||||||
|
// set total length: routing-header \n content |\n
|
||||||
|
p->length = p->routingLength + 1 + p->contentLength + sizeof(PSYC_PACKET_DELIMITER) - 2;
|
||||||
|
if (p->flag == PSYC_PACKET_NEED_LENGTH) // add length of length if needed
|
||||||
|
p->length += log10((double)p->data.length) + 1;
|
||||||
|
|
||||||
|
return p->length;
|
||||||
|
}
|
||||||
|
|
||||||
inline psycPacket psyc_newPacket(psycHeader *routing,
|
inline psycPacket psyc_newPacket(psycHeader *routing,
|
||||||
psycHeader *entity,
|
psycHeader *entity,
|
||||||
psycString *method, psycString *data,
|
psycString *method, psycString *data,
|
||||||
psycPacketFlag flag)
|
psycPacketFlag flag)
|
||||||
{
|
{
|
||||||
psycPacket p = {*routing, *entity, *method, *data, 0, 0, flag};
|
psycPacket p = {*routing, *entity, *method, *data, 0, 0, flag};
|
||||||
size_t i;
|
|
||||||
|
|
||||||
if (flag == PSYC_PACKET_CHECK_LENGTH) // find out if it needs a length
|
if (flag == PSYC_PACKET_CHECK_LENGTH) // find out if it needs a length
|
||||||
{
|
p.flag = psyc_checkPacketLength(&p);
|
||||||
if (data->length == 1 && data->ptr[0] == C_GLYPH_PACKET_DELIMITER)
|
|
||||||
p.flag = PSYC_PACKET_NEED_LENGTH;
|
|
||||||
else if (data->length > PSYC_CONTENT_SIZE_THRESHOLD)
|
|
||||||
p.flag = PSYC_PACKET_NEED_LENGTH;
|
|
||||||
else if (memmem(data->ptr, data->length, PSYC_C2ARG(PSYC_PACKET_DELIMITER)))
|
|
||||||
p.flag = PSYC_PACKET_NEED_LENGTH;
|
|
||||||
else
|
|
||||||
p.flag = PSYC_PACKET_NO_LENGTH;
|
|
||||||
}
|
|
||||||
|
|
||||||
// add routing header length
|
|
||||||
for (i = 0; i < routing->lines; i++)
|
|
||||||
p.routingLength += psyc_getModifierLength(&routing->modifiers[i]);
|
|
||||||
|
|
||||||
// add entity header length
|
|
||||||
for (i = 0; i < entity->lines; i++)
|
|
||||||
p.contentLength += psyc_getModifierLength(&entity->modifiers[i]);
|
|
||||||
|
|
||||||
// add length of method, data & delimiter
|
|
||||||
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
|
|
||||||
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;
|
|
||||||
|
|
||||||
|
psyc_setPacketLength(&p);
|
||||||
return p;
|
return p;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -1,29 +1,30 @@
|
||||||
#include <psyc.h>
|
|
||||||
#include <psyc/parser.h>
|
|
||||||
#include <unistd.h>
|
#include <unistd.h>
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include <fcntl.h>
|
#include <fcntl.h>
|
||||||
|
|
||||||
int main(int argc, char** argv)
|
#include <psyc/lib.h>
|
||||||
|
#include <psyc/parser.h>
|
||||||
|
|
||||||
|
int main(int argc, char **argv)
|
||||||
{
|
{
|
||||||
int indx, ret, verbose = argc > 2;
|
int idx, ret, verbose = argc > 2;
|
||||||
char buffer[2048], oper;
|
char buffer[2048], oper;
|
||||||
psycString name, value, elem;
|
psycString name, value, elem;
|
||||||
psycParseState state;
|
psycParseState state;
|
||||||
psycParseListState listState;
|
psycParseListState listState;
|
||||||
|
|
||||||
int file = open(argv[1],O_RDONLY);
|
int file = open(argv[1],O_RDONLY);
|
||||||
if(file < 0)
|
if (file < 0)
|
||||||
return -1;
|
return -1;
|
||||||
indx = read(file,(void*)buffer,sizeof(buffer));
|
idx = read(file,(void*)buffer,sizeof(buffer));
|
||||||
|
|
||||||
if (verbose) {
|
if (verbose) {
|
||||||
write(1, ">> INPUT\n", 9);
|
write(1, ">> INPUT\n", 9);
|
||||||
write(1, buffer, indx);
|
write(1, buffer, idx);
|
||||||
write(1, ">> PARSE\n", 9);
|
write(1, ">> PARSE\n", 9);
|
||||||
}
|
}
|
||||||
psyc_initParseState(&state);
|
psyc_initParseState(&state);
|
||||||
psyc_nextParseBuffer(&state, psyc_newString(buffer, indx));
|
psyc_nextParseBuffer(&state, psyc_newString(buffer, idx));
|
||||||
|
|
||||||
// try parsing that now
|
// try parsing that now
|
||||||
// while ((ret = psyc_parse(&state, &oper, &name, &value)))
|
// while ((ret = psyc_parse(&state, &oper, &name, &value)))
|
||||||
|
|
|
@ -68,7 +68,7 @@ int testList(const char *rendered, uint8_t verbose)
|
||||||
psyc_newModifier2(C_GLYPH_OPERATOR_SET, PSYC_C2ARG("_list_text"),
|
psyc_newModifier2(C_GLYPH_OPERATOR_SET, PSYC_C2ARG("_list_text"),
|
||||||
buf_text, list_text.length, list_text.flag),
|
buf_text, list_text.length, list_text.flag),
|
||||||
psyc_newModifier2(C_GLYPH_OPERATOR_SET, PSYC_C2ARG("_list_binary"),
|
psyc_newModifier2(C_GLYPH_OPERATOR_SET, PSYC_C2ARG("_list_binary"),
|
||||||
buf_bin, list_bin.length, list_text.flag),
|
buf_bin, list_bin.length, list_bin.flag),
|
||||||
};
|
};
|
||||||
|
|
||||||
psycPacket packet = psyc_newPacket2(routing, PSYC_NUM_ELEM(routing),
|
psycPacket packet = psyc_newPacket2(routing, PSYC_NUM_ELEM(routing),
|
||||||
|
@ -84,7 +84,7 @@ int testList(const char *rendered, uint8_t verbose)
|
||||||
return strncmp(rendered, buffer, packet.length);
|
return strncmp(rendered, buffer, packet.length);
|
||||||
}
|
}
|
||||||
|
|
||||||
int main(int argc, char* argv[]) {
|
int main(int argc, char **argv) {
|
||||||
uint8_t verbose = argc > 1;
|
uint8_t verbose = argc > 1;
|
||||||
|
|
||||||
if (testPresence(PSYC_C2ARG("_here"), PSYC_C2ARG("I'm omnipresent right now"), "\
|
if (testPresence(PSYC_C2ARG("_here"), PSYC_C2ARG("I'm omnipresent right now"), "\
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue