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 *.rej
*.log *.log
*.pem *.pem
*old *.old
*bak *.bak
*diff *.orig
*orig *.diff
*.patch

View file

@ -142,13 +142,17 @@ struct Packet
char[] method, ubyte[] data, char[] method, ubyte[] data,
PacketFlag flag = PacketFlag.CHECK_LENGTH) 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, static Packet opCall (Modifier[] routing, ubyte[] content,
PacketFlag flag = PacketFlag.CHECK_LENGTH) 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 ( ) size_t length ( )
@ -215,17 +219,19 @@ PacketFlag psyc_packet_length_check (Packet *p);
private size_t psyc_packet_length_set (Packet *p); private size_t psyc_packet_length_set (Packet *p);
/** Create new list. */ /** 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. */ /** Initialize packet. */
Packet psyc_packet_new (Modifier *routing, size_t routinglen, void psyc_packet_init (Packet packet,
Modifier *routing, size_t routinglen,
Modifier *entity, size_t entitylen, Modifier *entity, size_t entitylen,
char *method, size_t methodlen, char *method, size_t methodlen,
char *data, size_t datalen, char *data, size_t datalen,
PacketFlag flag); PacketFlag flag);
/** Create new packet with raw content. */ /** Initialize packet with raw content. */
Packet psyc_packet_new_raw (Modifier *routing, size_t routinglen, void psyc_packet_init_raw (Packet packet,
Modifier *routing, size_t routinglen,
char *content, size_t contentlen, char *content, size_t contentlen,
PacketFlag flag); PacketFlag flag);

View file

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

View file

@ -20,6 +20,7 @@
#include <string.h> #include <string.h>
#include <sys/types.h> #include <sys/types.h>
#define PSYC_VERSION 1
#define PSYC_EPOCH 1440444041 // 2015-08-24 21:20:41 CET (Monday) #define PSYC_EPOCH 1440444041 // 2015-08-24 21:20:41 CET (Monday)
#define PSYC_C2STR(str) {sizeof(str)-1, str} #define PSYC_C2STR(str) {sizeof(str)-1, str}
@ -118,28 +119,6 @@ typedef struct
intptr_t value; intptr_t value;
} PsycDictInt; } 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. * 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; return flag;
} }
/** Create new modifier */ /** Initialize modifier */
static inline static inline
PsycModifier psyc_modifier_new (char oper, void psyc_modifier_init (PsycModifier *m, char oper,
char *name, size_t namelen, char *name, size_t namelen,
char *value, size_t valuelen, char *value, size_t valuelen,
PsycModifierFlag flag) 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 if (flag == PSYC_MODIFIER_CHECK_LENGTH) // find out if it needs a length
m.flag = psyc_modifier_length_check(&m); m->flag = psyc_modifier_length_check(m);
return m;
} }
/** /**
@ -165,18 +163,21 @@ PsycPacketFlag psyc_packet_length_check (PsycPacket *p);
*/ */
size_t psyc_packet_length_set (PsycPacket *p); size_t psyc_packet_length_set (PsycPacket *p);
/** Create new list. */ /** Initialize list. */
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);
/** Create new packet. */ /** Initialize packet. */
PsycPacket psyc_packet_new (PsycModifier *routing, size_t routinglen, void psyc_packet_init (PsycPacket *packet,
PsycModifier *routing, size_t routinglen,
PsycModifier *entity, size_t entitylen, PsycModifier *entity, size_t entitylen,
char *method, size_t methodlen, char *method, size_t methodlen,
char *data, size_t datalen, char *data, size_t datalen,
PsycPacketFlag flag); PsycPacketFlag flag);
/** Create new packet with raw content. */ /** Initialize packet with raw content. */
PsycPacket psyc_packet_new_raw (PsycModifier *routing, size_t routinglen, void psyc_packet_init_raw (PsycPacket *packet,
PsycModifier *routing, size_t routinglen,
char *content, size_t contentlen, char *content, size_t contentlen,
PsycPacketFlag flag); PsycPacketFlag flag);

View file

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

View file

@ -130,11 +130,9 @@ PIKEFUN string render(mapping rvars, mapping evars, string method, string|void b
if (k->ind.type == PIKE_T_STRING) { if (k->ind.type == PIKE_T_STRING) {
switch(k->val.type) { switch(k->val.type) {
case PIKE_T_STRING: case PIKE_T_STRING:
rheaders.modifiers[rheaders.lines++] = psyc_modifier_new(oper, psyc_modifier_init(&rheaders.modifiers[rheaders.lines++], oper,
(char *)STR0(k->ind.u.string), (char *)STR0(k->ind.u.string), k->ind.u.string->len,
k->ind.u.string->len, (char *)STR0(k->val.u.string), k->val.u.string->len,
(char *)STR0(k->val.u.string),
k->val.u.string->len,
PSYC_MODIFIER_ROUTING); PSYC_MODIFIER_ROUTING);
break; break;
default: default:
@ -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"); 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); struct pike_string *listbuf = begin_shared_string(list.length);
psyc_render_list(&list, (char *) STR0(listbuf), listbuf->len); psyc_render_list(&list, (char *) STR0(listbuf), listbuf->len);
@ -211,7 +209,7 @@ PIKEFUN string render(mapping rvars, mapping evars, string method, string|void b
Pike_error("psyc_render: unsupported value in evars\n"); Pike_error("psyc_render: unsupported value in evars\n");
break; break;
} }
eheaders.modifiers[eheaders.lines++] = psyc_modifier_new(oper, psyc_modifier_init(&eheaders.modifiers[eheaders.lines++], oper,
key, keylen, key, keylen,
val, vallen, val, vallen,
PSYC_MODIFIER_CHECK_LENGTH); PSYC_MODIFIER_CHECK_LENGTH);
@ -221,21 +219,16 @@ PIKEFUN string render(mapping rvars, mapping evars, string method, string|void b
} }
if (body != NULL) { if (body != NULL) {
packet = psyc_packet_new(rheaders.modifiers, psyc_packet_init(&packet, rheaders.modifiers, rheaders.lines,
rheaders.lines, eheaders.modifiers, eheaders.lines,
eheaders.modifiers,
eheaders.lines,
(const char *)STR0(method), method->len, (const char *)STR0(method), method->len,
(const char *)STR0(body), body->len, (const char *)STR0(body), body->len,
PSYC_PACKET_CHECK_LENGTH); PSYC_PACKET_CHECK_LENGTH);
} else { // body arg was not given } else { // body arg was not given
packet = psyc_packet_new(rheaders.modifiers, psyc_packet_init(&packet, rheaders.modifiers, rheaders.lines,
rheaders.lines, eheaders.modifiers, eheaders.lines,
eheaders.modifiers,
eheaders.lines,
(const char *)STR0(method), method->len, (const char *)STR0(method), method->len,
NULL, 0, NULL, 0, PSYC_PACKET_CHECK_LENGTH);
PSYC_PACKET_CHECK_LENGTH);
} }
struct pike_string *s = begin_shared_string(packet.length); struct pike_string *s = begin_shared_string(packet.length);

View file

@ -48,15 +48,15 @@ PsycListFlag psyc_list_length (PsycList *list)
} }
inline 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 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); list->length = psyc_list_length(list);
return list;
} }
@ -134,33 +134,33 @@ size_t psyc_packet_length_set (PsycPacket *p)
} }
inline inline
PsycPacket psyc_packet_new (PsycModifier *routing, size_t routinglen, void psyc_packet_init (PsycPacket *p,
PsycModifier *routing, size_t routinglen,
PsycModifier *entity, size_t entitylen, PsycModifier *entity, size_t entitylen,
char *method, size_t methodlen, char *method, size_t methodlen,
char *data, size_t datalen, char *data, size_t datalen,
PsycPacketFlag flag) PsycPacketFlag flag)
{ {
PsycPacket p = {{routinglen, routing}, {entitylen, entity}, *p = (PsycPacket) {{routinglen, routing}, {entitylen, entity},
{methodlen, method}, {datalen, data}, {0,0}, 0, 0, flag}; {methodlen, method}, {datalen, data}, {0,0}, 0, 0, flag};
if (flag == PSYC_PACKET_CHECK_LENGTH) // find out if it needs a length if (flag == PSYC_PACKET_CHECK_LENGTH) // find out if it needs length
p.flag = psyc_packet_length_check(&p); p->flag = psyc_packet_length_check(p);
psyc_packet_length_set(&p); psyc_packet_length_set(p);
return p;
} }
inline inline
PsycPacket psyc_packet_new_raw (PsycModifier *routing, size_t routinglen, void psyc_packet_init_raw (PsycPacket *p,
PsycModifier *routing, size_t routinglen,
char *content, size_t contentlen, char *content, size_t contentlen,
PsycPacketFlag flag) PsycPacketFlag flag)
{ {
PsycPacket p = {{routinglen, routing}, {0,0}, {0,0}, {0,0}, *p = (PsycPacket) {{routinglen, routing}, {0,0}, {0,0}, {0,0},
{contentlen, content}, 0, 0, flag}; {contentlen, content}, 0, 0, flag};
if (flag == PSYC_PACKET_CHECK_LENGTH) // find out if it needs a length if (flag == PSYC_PACKET_CHECK_LENGTH) // find out if it needs length
p.flag = psyc_packet_length_check(&p); p->flag = psyc_packet_length_check(p);
psyc_packet_length_set(&p); psyc_packet_length_set(p);
return p;
} }

View file

@ -26,8 +26,8 @@ int main (int argc, char **argv) {
for (i=0; i<NELEMS; i++) for (i=0; i<NELEMS; i++)
elems_bin[i] = (PsycString) PSYC_C2STR("1234567890|abcdefghijklmnopqrstuvwxyz|_\n1234567890|abcdefghijklmnopqrstuvwxyz|_\n1234567890|abcdefghijklmnopqrstuvwxyz|_\n1234567890"); 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); psyc_list_init(&list_text, 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_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_text, buf_text, sizeof(buf_text));
psyc_render_list(&list_bin, buf_bin, sizeof(buf_bin)); psyc_render_list(&list_bin, buf_bin, sizeof(buf_bin));

View file

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