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

refactoring - init functions

This commit is contained in:
Gabor Adam Toth 2011-11-03 14:27:01 +01:00
parent 19c7cadad2
commit 8906e67529
10 changed files with 158 additions and 173 deletions

9
.gitignore vendored
View file

@ -43,7 +43,8 @@ python/*.py
*.rej
*.log
*.pem
*old
*bak
*diff
*orig
*.old
*.bak
*.orig
*.diff
*.patch

View file

@ -142,13 +142,17 @@ struct Packet
char[] method, ubyte[] data,
PacketFlag flag = PacketFlag.CHECK_LENGTH)
{
return psyc_packet_new(&routing, &entity, cast(ubyte[]*)&method, &data, flag); // FIXME
Packet p;
psyc_packet_init(&p, &routing, &entity, cast(ubyte[]*)&method, &data, flag); // FIXME
return p;
}
static Packet opCall (Modifier[] routing, ubyte[] content,
PacketFlag flag = PacketFlag.CHECK_LENGTH)
{
return psyc_packet_new_raw(&routing, &content, flag); // FIXME
Packet p;
psyc_packet_init_raw(&routing, &content, flag); // FIXME
return p;
}
size_t length ( )
@ -215,17 +219,19 @@ PacketFlag psyc_packet_length_check (Packet *p);
private size_t psyc_packet_length_set (Packet *p);
/** Create new list. */
List psyc_list_new (String *elems, size_t num_elems, ListFlag flag);
void psyc_list_init (List *list, String *elems, size_t num_elems, ListFlag flag);
/** Create new packet. */
Packet psyc_packet_new (Modifier *routing, size_t routinglen,
Modifier *entity, size_t entitylen,
char *method, size_t methodlen,
char *data, size_t datalen,
PacketFlag flag);
/** Initialize packet. */
void psyc_packet_init (Packet packet,
Modifier *routing, size_t routinglen,
Modifier *entity, size_t entitylen,
char *method, size_t methodlen,
char *data, size_t datalen,
PacketFlag flag);
/** Create new packet with raw content. */
Packet psyc_packet_new_raw (Modifier *routing, size_t routinglen,
char *content, size_t contentlen,
PacketFlag flag);
/** Initialize packet with raw content. */
void psyc_packet_init_raw (Packet packet,
Modifier *routing, size_t routinglen,
char *content, size_t contentlen,
PacketFlag flag);

View file

@ -45,9 +45,9 @@ enum RenderListRC
* This function renders packet->length bytes to the buffer,
* if buflen is less than that an error is returned.
*
* @see psyc_packet_new
* @see psyc_packet_new_raw
* @see psyc_packet_length_set
* @see psyc_packet_init()
* @see psyc_packet_init_raw()
* @see psyc_packet_length_set()
*/
RenderRC psyc_render (Packet *packet, ubyte *buffer, size_t buflen);

View file

@ -20,7 +20,8 @@
#include <string.h>
#include <sys/types.h>
#define PSYC_EPOCH 1440444041 // 2015-08-24 21:20:41 CET (Monday)
#define PSYC_VERSION 1
#define PSYC_EPOCH 1440444041 // 2015-08-24 21:20:41 CET (Monday)
#define PSYC_C2STR(str) {sizeof(str)-1, str}
#define PSYC_C2ARG(str) str, sizeof(str)-1
@ -118,28 +119,6 @@ typedef struct
intptr_t value;
} PsycDictInt;
/**
* Shortcut for creating a PsycString.
*
* @param str Pointer to the buffer.
* @param len Length of that buffer.
*
* @return An instance of the PsycString struct.
*/
static inline
PsycString psyc_string_new (char *str, size_t len)
{
PsycString s = {len, str};
return s;
}
static inline
unsigned int psyc_version ()
{
return 1;
}
/**
* Checks if long keyword string inherits from short keyword string.
*/

View file

@ -121,19 +121,17 @@ PsycModifierFlag psyc_modifier_length_check (PsycModifier *m)
return flag;
}
/** Create new modifier */
/** Initialize modifier */
static inline
PsycModifier psyc_modifier_new (char oper,
char *name, size_t namelen,
char *value, size_t valuelen,
PsycModifierFlag flag)
void psyc_modifier_init (PsycModifier *m, char oper,
char *name, size_t namelen,
char *value, size_t valuelen,
PsycModifierFlag flag)
{
PsycModifier m = {oper, {namelen, name}, {valuelen, value}, flag};
*m = (PsycModifier) {oper, {namelen, name}, {valuelen, value}, flag};
if (flag == PSYC_MODIFIER_CHECK_LENGTH) // find out if it needs a length
m.flag = psyc_modifier_length_check(&m);
return m;
m->flag = psyc_modifier_length_check(m);
}
/**
@ -165,20 +163,23 @@ PsycPacketFlag psyc_packet_length_check (PsycPacket *p);
*/
size_t psyc_packet_length_set (PsycPacket *p);
/** Create new list. */
PsycList psyc_list_new (PsycString *elems, size_t num_elems, PsycListFlag flag);
/** Initialize list. */
void psyc_list_init (PsycList *list, PsycString *elems, size_t num_elems,
PsycListFlag flag);
/** Create new packet. */
PsycPacket psyc_packet_new (PsycModifier *routing, size_t routinglen,
PsycModifier *entity, size_t entitylen,
char *method, size_t methodlen,
char *data, size_t datalen,
PsycPacketFlag flag);
/** Initialize packet. */
void psyc_packet_init (PsycPacket *packet,
PsycModifier *routing, size_t routinglen,
PsycModifier *entity, size_t entitylen,
char *method, size_t methodlen,
char *data, size_t datalen,
PsycPacketFlag flag);
/** Create new packet with raw content. */
PsycPacket psyc_packet_new_raw (PsycModifier *routing, size_t routinglen,
char *content, size_t contentlen,
PsycPacketFlag flag);
/** Initialize packet with raw content. */
void psyc_packet_init_raw (PsycPacket *packet,
PsycModifier *routing, size_t routinglen,
char *content, size_t contentlen,
PsycPacketFlag flag);
/** @} */ // end of packet group

View file

@ -52,9 +52,9 @@ typedef enum
* This function renders packet->length bytes to the buffer,
* if buflen is less than that an error is returned.
*
* @see psyc_packet_new
* @see psyc_packet_new_raw
* @see psyc_packet_length_set
* @see psyc_packet_init()
* @see psyc_packet_init_raw()
* @see psyc_packet_length_set()
*/
#ifdef __INLINE_PSYC_RENDER
static inline

View file

@ -130,12 +130,10 @@ PIKEFUN string render(mapping rvars, mapping evars, string method, string|void b
if (k->ind.type == PIKE_T_STRING) {
switch(k->val.type) {
case PIKE_T_STRING:
rheaders.modifiers[rheaders.lines++] = psyc_modifier_new(oper,
(char *)STR0(k->ind.u.string),
k->ind.u.string->len,
(char *)STR0(k->val.u.string),
k->val.u.string->len,
PSYC_MODIFIER_ROUTING);
psyc_modifier_init(&rheaders.modifiers[rheaders.lines++], oper,
(char *)STR0(k->ind.u.string), k->ind.u.string->len,
(char *)STR0(k->val.u.string), k->val.u.string->len,
PSYC_MODIFIER_ROUTING);
break;
default:
Pike_error("psyc render: unsupported non-string value in rvars\n");
@ -197,7 +195,7 @@ PIKEFUN string render(mapping rvars, mapping evars, string method, string|void b
Pike_error("psyc_render: unsupported data type in list\n");
}
}
list = psyc_list_new(elems, k->val.u.array->size, PSYC_LIST_CHECK_LENGTH);
psyc_list_init(&list, elems, k->val.u.array->size, PSYC_LIST_CHECK_LENGTH);
struct pike_string *listbuf = begin_shared_string(list.length);
psyc_render_list(&list, (char *) STR0(listbuf), listbuf->len);
@ -211,31 +209,26 @@ PIKEFUN string render(mapping rvars, mapping evars, string method, string|void b
Pike_error("psyc_render: unsupported value in evars\n");
break;
}
eheaders.modifiers[eheaders.lines++] = psyc_modifier_new(oper,
key, keylen,
val, vallen,
PSYC_MODIFIER_CHECK_LENGTH);
psyc_modifier_init(&eheaders.modifiers[eheaders.lines++], oper,
key, keylen,
val, vallen,
PSYC_MODIFIER_CHECK_LENGTH);
} else {
Pike_error("psyc render: unsupported non-string key in evars\n");
}
}
if (body != NULL) {
packet = psyc_packet_new(rheaders.modifiers,
rheaders.lines,
eheaders.modifiers,
eheaders.lines,
(const char *)STR0(method), method->len,
(const char *)STR0(body), body->len,
PSYC_PACKET_CHECK_LENGTH);
psyc_packet_init(&packet, rheaders.modifiers, rheaders.lines,
eheaders.modifiers, eheaders.lines,
(const char *)STR0(method), method->len,
(const char *)STR0(body), body->len,
PSYC_PACKET_CHECK_LENGTH);
} else { // body arg was not given
packet = psyc_packet_new(rheaders.modifiers,
rheaders.lines,
eheaders.modifiers,
eheaders.lines,
(const char *)STR0(method), method->len,
NULL, 0,
PSYC_PACKET_CHECK_LENGTH);
psyc_packet_init(&packet, rheaders.modifiers, rheaders.lines,
eheaders.modifiers, eheaders.lines,
(const char *)STR0(method), method->len,
NULL, 0, PSYC_PACKET_CHECK_LENGTH);
}
struct pike_string *s = begin_shared_string(packet.length);

View file

@ -48,15 +48,15 @@ PsycListFlag psyc_list_length (PsycList *list)
}
inline
PsycList psyc_list_new (PsycString *elems, size_t num_elems, PsycListFlag flag)
void psyc_list_init (PsycList *list, PsycString *elems, size_t num_elems,
PsycListFlag flag)
{
PsycList list = {num_elems, elems, 0, flag};
*list = (PsycList) {num_elems, elems, 0, flag};
if (flag == PSYC_LIST_CHECK_LENGTH) // check if list elements need length
list.flag = psyc_list_length_check(&list);
list->flag = psyc_list_length_check(list);
list.length = psyc_list_length(&list);
return list;
list->length = psyc_list_length(list);
}
@ -134,33 +134,33 @@ size_t psyc_packet_length_set (PsycPacket *p)
}
inline
PsycPacket psyc_packet_new (PsycModifier *routing, size_t routinglen,
PsycModifier *entity, size_t entitylen,
char *method, size_t methodlen,
char *data, size_t datalen,
PsycPacketFlag flag)
void psyc_packet_init (PsycPacket *p,
PsycModifier *routing, size_t routinglen,
PsycModifier *entity, size_t entitylen,
char *method, size_t methodlen,
char *data, size_t datalen,
PsycPacketFlag flag)
{
PsycPacket p = {{routinglen, routing}, {entitylen, entity},
{methodlen, method}, {datalen, data}, {0,0}, 0, 0, flag};
*p = (PsycPacket) {{routinglen, routing}, {entitylen, entity},
{methodlen, method}, {datalen, data}, {0,0}, 0, 0, flag};
if (flag == PSYC_PACKET_CHECK_LENGTH) // find out if it needs a length
p.flag = psyc_packet_length_check(&p);
if (flag == PSYC_PACKET_CHECK_LENGTH) // find out if it needs length
p->flag = psyc_packet_length_check(p);
psyc_packet_length_set(&p);
return p;
psyc_packet_length_set(p);
}
inline
PsycPacket psyc_packet_new_raw (PsycModifier *routing, size_t routinglen,
char *content, size_t contentlen,
PsycPacketFlag flag)
void psyc_packet_init_raw (PsycPacket *p,
PsycModifier *routing, size_t routinglen,
char *content, size_t contentlen,
PsycPacketFlag flag)
{
PsycPacket p = {{routinglen, routing}, {0,0}, {0,0}, {0,0},
{contentlen, content}, 0, 0, flag};
*p = (PsycPacket) {{routinglen, routing}, {0,0}, {0,0}, {0,0},
{contentlen, content}, 0, 0, flag};
if (flag == PSYC_PACKET_CHECK_LENGTH) // find out if it needs a length
p.flag = psyc_packet_length_check(&p);
if (flag == PSYC_PACKET_CHECK_LENGTH) // find out if it needs length
p->flag = psyc_packet_length_check(p);
psyc_packet_length_set(&p);
return p;
psyc_packet_length_set(p);
}

View file

@ -26,8 +26,8 @@ int main (int argc, char **argv) {
for (i=0; i<NELEMS; i++)
elems_bin[i] = (PsycString) PSYC_C2STR("1234567890|abcdefghijklmnopqrstuvwxyz|_\n1234567890|abcdefghijklmnopqrstuvwxyz|_\n1234567890|abcdefghijklmnopqrstuvwxyz|_\n1234567890");
list_text = psyc_list_new(elems_text, PSYC_NUM_ELEM(elems_text), PSYC_LIST_NO_LENGTH);
list_bin = psyc_list_new(elems_bin, PSYC_NUM_ELEM(elems_bin), PSYC_LIST_CHECK_LENGTH);
psyc_list_init(&list_text, elems_text, PSYC_NUM_ELEM(elems_text), PSYC_LIST_NO_LENGTH);
psyc_list_init(&list_bin, elems_bin, PSYC_NUM_ELEM(elems_bin), PSYC_LIST_CHECK_LENGTH);
psyc_render_list(&list_text, buf_text, sizeof(buf_text));
psyc_render_list(&list_bin, buf_bin, sizeof(buf_bin));

View file

@ -11,24 +11,26 @@ int testPresence (const char *avail, int availlen,
const char *desc, int desclen,
const char *rendered, uint8_t verbose)
{
PsycModifier routing[] = {
psyc_modifier_new(C_GLYPH_OPERATOR_SET, PSYC_C2ARG("_context"), PSYC_C2ARG(myUNI),
PSYC_MODIFIER_ROUTING),
};
PsycModifier routing[1];
psyc_modifier_init(&routing[0], C_GLYPH_OPERATOR_SET,
PSYC_C2ARG("_context"),
PSYC_C2ARG(myUNI), PSYC_MODIFIER_ROUTING);
PsycModifier entity[] = {
// presence is to be assigned permanently in distributed state
psyc_modifier_new(C_GLYPH_OPERATOR_ASSIGN, PSYC_C2ARG("_degree_availability"),
avail, availlen, PSYC_MODIFIER_CHECK_LENGTH),
psyc_modifier_new(C_GLYPH_OPERATOR_ASSIGN, PSYC_C2ARG("_description_presence"),
desc, desclen, PSYC_MODIFIER_CHECK_LENGTH),
};
PsycModifier entity[2];
// presence is to be assigned permanently in distributed state
psyc_modifier_init(&entity[0], C_GLYPH_OPERATOR_ASSIGN,
PSYC_C2ARG("_degree_availability"),
avail, availlen, PSYC_MODIFIER_CHECK_LENGTH);
psyc_modifier_init(&entity[1], C_GLYPH_OPERATOR_ASSIGN,
PSYC_C2ARG("_description_presence"),
desc, desclen, PSYC_MODIFIER_CHECK_LENGTH);
PsycPacket packet = psyc_packet_new(routing, PSYC_NUM_ELEM(routing),
entity, PSYC_NUM_ELEM(entity),
PSYC_C2ARG("_notice_presence"),
NULL, 0,
PSYC_PACKET_CHECK_LENGTH);
PsycPacket packet;
psyc_packet_init(&packet, routing, PSYC_NUM_ELEM(routing),
entity, PSYC_NUM_ELEM(entity),
PSYC_C2ARG("_notice_presence"),
NULL, 0,
PSYC_PACKET_CHECK_LENGTH);
char buffer[512];
psyc_render(&packet, buffer, sizeof(buffer));
@ -39,12 +41,13 @@ int testPresence (const char *avail, int availlen,
int testList (const char *rendered, uint8_t verbose)
{
PsycModifier routing[] = {
psyc_modifier_new(C_GLYPH_OPERATOR_SET, PSYC_C2ARG("_source"), PSYC_C2ARG(myUNI),
PSYC_MODIFIER_ROUTING),
psyc_modifier_new(C_GLYPH_OPERATOR_SET, PSYC_C2ARG("_context"), PSYC_C2ARG(myUNI),
PSYC_MODIFIER_ROUTING),
};
PsycModifier routing[2];
psyc_modifier_init(&routing[0], C_GLYPH_OPERATOR_SET,
PSYC_C2ARG("_source"),
PSYC_C2ARG(myUNI), PSYC_MODIFIER_ROUTING);
psyc_modifier_init(&routing[1], C_GLYPH_OPERATOR_SET,
PSYC_C2ARG("_context"),
PSYC_C2ARG(myUNI), PSYC_MODIFIER_ROUTING);
PsycString elems_text[] = {
PSYC_C2STR("foo"),
@ -59,25 +62,27 @@ int testList (const char *rendered, uint8_t verbose)
};
PsycList list_text, list_bin;
list_text = psyc_list_new(elems_text, PSYC_NUM_ELEM(elems_text), PSYC_LIST_CHECK_LENGTH);
list_bin = psyc_list_new(elems_bin, PSYC_NUM_ELEM(elems_bin), PSYC_LIST_CHECK_LENGTH);
psyc_list_init(&list_text, elems_text, PSYC_NUM_ELEM(elems_text), PSYC_LIST_CHECK_LENGTH);
psyc_list_init(&list_bin, elems_bin, PSYC_NUM_ELEM(elems_bin), PSYC_LIST_CHECK_LENGTH);
char buf_text[32], buf_bin[32];
psyc_render_list(&list_text, buf_text, sizeof(buf_text));
psyc_render_list(&list_bin, buf_bin, sizeof(buf_bin));
PsycModifier entity[] = {
psyc_modifier_new(C_GLYPH_OPERATOR_SET, PSYC_C2ARG("_list_text"),
buf_text, list_text.length, list_text.flag),
psyc_modifier_new(C_GLYPH_OPERATOR_SET, PSYC_C2ARG("_list_binary"),
buf_bin, list_bin.length, list_bin.flag),
};
PsycModifier entity[2];
psyc_modifier_init(&entity[0], C_GLYPH_OPERATOR_SET,
PSYC_C2ARG("_list_text"),
buf_text, list_text.length, list_text.flag);
psyc_modifier_init(&entity[1], C_GLYPH_OPERATOR_SET,
PSYC_C2ARG("_list_binary"),
buf_bin, list_bin.length, list_bin.flag);
PsycPacket packet = psyc_packet_new(routing, PSYC_NUM_ELEM(routing),
entity, PSYC_NUM_ELEM(entity),
PSYC_C2ARG("_test_list"),
PSYC_C2ARG("list test"),
PSYC_PACKET_CHECK_LENGTH);
PsycPacket packet;
psyc_packet_init(&packet, routing, PSYC_NUM_ELEM(routing),
entity, PSYC_NUM_ELEM(entity),
PSYC_C2ARG("_test_list"),
PSYC_C2ARG("list test"),
PSYC_PACKET_CHECK_LENGTH);
char buffer[512];
psyc_render(&packet, buffer, sizeof(buffer));