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

refactoring - more renames

This commit is contained in:
tg(x) 2011-11-01 12:06:58 +01:00
parent f25e768482
commit 344cdb7996
21 changed files with 215 additions and 199 deletions

View file

@ -101,7 +101,7 @@ Bool psyc_var_is_routing (char* name, size_t len);
bool isRoutingVar (char[] name) bool isRoutingVar (char[] name)
{ {
return psyc_var_is_routing(name.ptr, name.length); //FIXME return psyc_var_is_routing(name.data, name.length); //FIXME
} }
/** /**

View file

@ -105,7 +105,7 @@ struct Modifier
if (value.length > PSYC_MODIFIER_SIZE_THRESHOLD) if (value.length > PSYC_MODIFIER_SIZE_THRESHOLD)
flag = ModifierFlag.NEED_LENGTH; flag = ModifierFlag.NEED_LENGTH;
else if (memchr(value.ptr, cast(int)'\n', value.length)) else if (memchr(value.data, cast(int)'\n', value.length))
flag = ModifierFlag.NEED_LENGTH; flag = ModifierFlag.NEED_LENGTH;
else else
flag = ModifierFlag.NO_LENGTH; flag = ModifierFlag.NO_LENGTH;
@ -162,7 +162,7 @@ struct Packet
psyc_packet_length_set(this); psyc_packet_length_set(this);
with (RenderRC) with (RenderRC)
switch (psyc_render(this, buffer.ptr, buffer.length)) switch (psyc_render(this, buffer.data, buffer.length))
{ {
case ERROR_METHOD_MISSING: case ERROR_METHOD_MISSING:
throw new Exception("Method missing"); throw new Exception("Method missing");

View file

@ -247,13 +247,13 @@ struct ParseState
ubyte* getRemainingBuffer ( ) ubyte* getRemainingBuffer ( )
{ {
return cast(ubyte*)this.buffer.ptr + this.cursor; return cast(ubyte*)this.buffer.data + this.cursor;
} }
void getRemainingBuffer ( ref ubyte[] buf ) void getRemainingBuffer ( ref ubyte[] buf )
{ {
buf = cast(ubyte[])this.buffer.ptr[cursor .. cursor + getRemainingLength()]; buf = cast(ubyte[])this.buffer.data[cursor .. cursor + getRemainingLength()];
} }
} }

View file

@ -24,8 +24,8 @@
#define PSYC_C2STR(str) {sizeof(str)-1, str} #define PSYC_C2STR(str) {sizeof(str)-1, str}
#define PSYC_C2ARG(str) str, sizeof(str)-1 #define PSYC_C2ARG(str) str, sizeof(str)-1
#define PSYC_S2ARG(str) (str).ptr, (str).length #define PSYC_S2ARG(str) (str).data, (str).length
#define PSYC_S2ARG2(str) (str).length, (str).ptr #define PSYC_S2ARG2(str) (str).length, (str).data
#define PSYC_NUM_ELEM(a) (sizeof(a) / sizeof(*(a))) #define PSYC_NUM_ELEM(a) (sizeof(a) / sizeof(*(a)))
@ -37,6 +37,12 @@ typedef enum
PSYC_YES = 1, PSYC_YES = 1,
} PsycBool; } PsycBool;
typedef enum
{
PSYC_OK = 1,
PSYC_ERROR = -1,
} PsycRC;
/** /**
* PSYC packet parts. * PSYC packet parts.
*/ */
@ -96,14 +102,20 @@ typedef struct
/// Length of the data pointed to by ptr /// Length of the data pointed to by ptr
size_t length; size_t length;
/// pointer to the data /// pointer to the data
char *ptr; char *data;
} PsycString; } PsycString;
typedef struct typedef struct
{ {
PsycString key; PsycString key;
int value; void *value;
} PsycMatchVar; } PsycDict;
typedef struct
{
PsycString key;
intptr_t value;
} PsycDictInt;
/** /**
* Shortcut for creating a PsycString. * Shortcut for creating a PsycString.
@ -140,23 +152,37 @@ int psyc_matches (char *sho, size_t slen,
char *lon, size_t llen); char *lon, size_t llen);
/** /**
* Check if keyword is in array. * Look up value associated with a key in a dictionary.
* *
* @param array The array to search, should be ordered alphabetically. * @param dict The dictionary to search, should be ordered alphabetically.
* @param size Size of array. * @param size Size of dict.
* @param kw Keyword to look for. * @param key Key to look for.
* @param kwlen Length of keyword. * @param keylen Length of key.
* @param inherit If true, also look for anything inheriting from kw, * @param inherit If true, also look for anything inheriting from key,
otherwise only exact matches are returned. otherwise only exact matches are returned.
* @param matching A temporary array used for keeping track of results. * @param tmp A temporary array used for keeping track of results.
* Should be the same size as the array we're searching. * Should be the same size as dict.
* *
* @return The value of the matched variable in the array. * @return The value of the entry if found, or NULL if not found.
*/ */
int psyc_in_array (const PsycMatchVar *array, size_t size, void * psyc_dict_lookup (const PsycDict *dict, size_t size,
const char *kw, size_t kwlen, const char *key, size_t keylen,
PsycBool inherit, int8_t *matching); PsycBool inherit, int8_t *tmp);
/**
* Look up value associated with a key in a dictionary of integers.
* @see psyc_dict_lookup
*/
static inline
intptr_t psyc_dict_lookup_int (const PsycDictInt *dict, size_t size,
const char *key, size_t keylen,
PsycBool inherit, int8_t *tmp)
{
return (intptr_t) psyc_dict_lookup((PsycDict *)dict, size, key, keylen, inherit, tmp);
}
#include "psyc/variable.h" #include "psyc/variable.h"

View file

@ -113,7 +113,7 @@ PsycModifierFlag psyc_modifier_length_check (PsycModifier *m)
if (m->value.length > PSYC_MODIFIER_SIZE_THRESHOLD) if (m->value.length > PSYC_MODIFIER_SIZE_THRESHOLD)
flag = PSYC_MODIFIER_NEED_LENGTH; flag = PSYC_MODIFIER_NEED_LENGTH;
else if (memchr(m->value.ptr, (int)'\n', m->value.length)) else if (memchr(m->value.data, (int)'\n', m->value.length))
flag = PSYC_MODIFIER_NEED_LENGTH; flag = PSYC_MODIFIER_NEED_LENGTH;
else else
flag = PSYC_MODIFIER_NO_LENGTH; flag = PSYC_MODIFIER_NO_LENGTH;

View file

@ -77,17 +77,17 @@
* // Name, value and operator of the variable can now be found in the * // Name, value and operator of the variable can now be found in the
* // respective variables: * // respective variables:
* printf("Variable: %.*s Value: %.*s Operator: %c\n", * printf("Variable: %.*s Value: %.*s Operator: %c\n",
* name.length, name.ptr, * name.length, name.data,
* value.length, value.ptr, * value.length, value.data,
* oper); * oper);
* // Note that the .ptr member still points at your original buffer. If * // Note that the .data member still points at your original buffer. If
* // you want to reuse that buffer for the next packet, you better copy it * // you want to reuse that buffer for the next packet, you better copy it
* // before passing it to the parser or you copy each variable now. * // before passing it to the parser or you copy each variable now.
* break; * break;
* case PSYC_PARSE_BODY: // it is the method and the body of the packet. * case PSYC_PARSE_BODY: // it is the method and the body of the packet.
* printf("Method Name: %.*s Body: %.*s\n", * printf("Method Name: %.*s Body: %.*s\n",
* name.length, name.ptr, // name of the method * name.length, name.data, // name of the method
* value.length, value.ptr); // value of the body * value.length, value.data); // value of the body
* break; * break;
* case PSYC_PARSE_COMPLETE: // parsing of this packet is complete * case PSYC_PARSE_COMPLETE: // parsing of this packet is complete
* // You can simply continue parsing till you get the * // You can simply continue parsing till you get the
@ -345,7 +345,7 @@ size_t psyc_parse_remaining_length (PsycParseState *state)
static inline static inline
const char * psyc_parse_remaining_buffer (PsycParseState *state) const char * psyc_parse_remaining_buffer (PsycParseState *state)
{ {
return state->buffer.ptr + state->cursor; return state->buffer.data + state->cursor;
} }
/** /**

View file

@ -58,7 +58,7 @@ typedef struct
* Callback for psyc_text() that produces a value for a match. * Callback for psyc_text() that produces a value for a match.
* *
* The application looks up a match such as _fruit from [_fruit] and * The application looks up a match such as _fruit from [_fruit] and
* if found sets value->ptr & value->length to point to the found value, * if found sets value->data & value->length to point to the found value,
* "Apple" for example. 0 is a legal value for the length. * "Apple" for example. 0 is a legal value for the length.
* The callbacks returns either PSYC_TEXT_VALUE_FOUND or * The callbacks returns either PSYC_TEXT_VALUE_FOUND or
* PSYC_TEXT_VALUE_NOT_FOUND if no match found in which case psyc_text * PSYC_TEXT_VALUE_NOT_FOUND if no match found in which case psyc_text

View file

@ -68,6 +68,7 @@ typedef enum {
} PsycTransport; } PsycTransport;
typedef enum { typedef enum {
PSYC_ENTITY_ROOT = 0,
PSYC_ENTITY_PERSON = '~', PSYC_ENTITY_PERSON = '~',
PSYC_ENTITY_PLACE = '@', PSYC_ENTITY_PLACE = '@',
PSYC_ENTITY_SERVICE = '$', PSYC_ENTITY_SERVICE = '$',

View file

@ -8,7 +8,7 @@
extern const PsycString psyc_routing_vars[]; extern const PsycString psyc_routing_vars[];
// Variable types in alphabetical order. // Variable types in alphabetical order.
extern const PsycMatchVar psyc_var_types[]; extern const PsycDictInt psyc_var_types[];
extern const size_t psyc_routing_vars_num; extern const size_t psyc_routing_vars_num;
extern const size_t psyc_var_types_num; extern const size_t psyc_var_types_num;

View file

@ -47,13 +47,13 @@ PsycTextValueRC lookup_value_mapping(const char *name, size_t len, PsycString *v
switch(s->type) { switch(s->type) {
case PIKE_T_STRING: case PIKE_T_STRING:
//printf("lookup returned %.*s\n", (int) s->u.string->len, STR0(s->u.string)); //printf("lookup returned %.*s\n", (int) s->u.string->len, STR0(s->u.string));
value->ptr = (char *) STR0(s->u.string); value->data = (char *) STR0(s->u.string);
value->length = s->u.string->len; value->length = s->u.string->len;
break; break;
default: default:
printf("lookup did return !string\n"); printf("lookup did return !string\n");
// FIXME: we need the automagic value conversion // FIXME: we need the automagic value conversion
value->ptr = ""; value->data = "";
value->length = 0; value->length = 0;
} }
return PSYC_TEXT_VALUE_FOUND; return PSYC_TEXT_VALUE_FOUND;
@ -302,29 +302,29 @@ PIKECLASS Parser {
ret = psyc_parse(&THIS->parser, &oper, &name, &value); ret = psyc_parse(&THIS->parser, &oper, &name, &value);
switch(ret) { switch(ret) {
case PSYC_PARSE_ROUTING: case PSYC_PARSE_ROUTING:
// printf("R %.*s -> %.*s\n", (int)name.length, name.ptr, (int)value.length, value.ptr); // printf("R %.*s -> %.*s\n", (int)name.length, name.data, (int)value.length, value.data);
mapping_string_insert_string(THIS->rvars, mapping_string_insert_string(THIS->rvars,
make_shared_binary_string(name.ptr, name.length), make_shared_binary_string(name.data, name.length),
make_shared_binary_string(value.ptr, value.length)); make_shared_binary_string(value.data, value.length));
break; break;
case PSYC_PARSE_ENTITY_START: // entity var with length case PSYC_PARSE_ENTITY_START: // entity var with length
init_string_builder_alloc(&THIS->incomplete, psyc_parse_value_length(&THIS->parser), 0); init_string_builder_alloc(&THIS->incomplete, psyc_parse_value_length(&THIS->parser), 0);
// fall thru // fall thru
case PSYC_PARSE_ENTITY_CONT: case PSYC_PARSE_ENTITY_CONT:
string_builder_append(&THIS->incomplete, MKPCHARP(value.ptr, 0), value.length); string_builder_append(&THIS->incomplete, MKPCHARP(value.data, 0), value.length);
break; break;
case PSYC_PARSE_ENTITY_END: case PSYC_PARSE_ENTITY_END:
string_builder_append(&THIS->incomplete, MKPCHARP(value.ptr, 0), value.length); string_builder_append(&THIS->incomplete, MKPCHARP(value.data, 0), value.length);
do { do {
struct pike_string *tmp = finish_string_builder(&THIS->incomplete); struct pike_string *tmp = finish_string_builder(&THIS->incomplete);
value.length = tmp->len; value.length = tmp->len;
value.ptr = (char *) STR0(tmp); value.data = (char *) STR0(tmp);
// FIXME: not sure if this is really safe // FIXME: not sure if this is really safe
free_string(tmp); free_string(tmp);
} while (0); } while (0);
// fall thru // fall thru
case PSYC_PARSE_ENTITY: case PSYC_PARSE_ENTITY:
//printf("E %.*s -> %.*s\n", (int)name.length, name.ptr, (int)value.length, value.ptr); //printf("E %.*s -> %.*s\n", (int)name.length, name.data, (int)value.length, value.data);
do { do {
err = 0; err = 0;
int type = psyc_var_type(PSYC_S2ARG(&name)); int type = psyc_var_type(PSYC_S2ARG(&name));
@ -335,7 +335,7 @@ PIKECLASS Parser {
if (psyc_parse_date(&value, &timmy)) { if (psyc_parse_date(&value, &timmy)) {
sv.type = PIKE_T_INT; sv.u.integer = timmy; sv.type = PIKE_T_INT; sv.u.integer = timmy;
mapping_string_insert(THIS->evars, mapping_string_insert(THIS->evars,
make_shared_binary_string(name.ptr, name.length), make_shared_binary_string(name.data, name.length),
&sv); &sv);
} else { } else {
err = 1; err = 1;
@ -345,7 +345,7 @@ PIKECLASS Parser {
if (psyc_parse_time(&value, &timmy)) { if (psyc_parse_time(&value, &timmy)) {
sv.type = PIKE_T_INT; sv.u.integer = timmy; sv.type = PIKE_T_INT; sv.u.integer = timmy;
mapping_string_insert(THIS->evars, mapping_string_insert(THIS->evars,
make_shared_binary_string(name.ptr, name.length), make_shared_binary_string(name.data, name.length),
&sv); &sv);
} else { } else {
err = 2; err = 2;
@ -354,20 +354,20 @@ PIKECLASS Parser {
case PSYC_TYPE_AMOUNT: case PSYC_TYPE_AMOUNT:
break; break;
case PSYC_TYPE_DEGREE: case PSYC_TYPE_DEGREE:
if (value.length && value.ptr[0] >= '0' && value.ptr[0] <= '9') { if (value.length && value.data[0] >= '0' && value.data[0] <= '9') {
sv.type = PIKE_T_FLOAT; sv.u.float_number = (float) (value.ptr[0] - '0') / 10.0; sv.type = PIKE_T_FLOAT; sv.u.float_number = (float) (value.data[0] - '0') / 10.0;
mapping_string_insert(THIS->evars, mapping_string_insert(THIS->evars,
make_shared_binary_string(name.ptr, name.length), make_shared_binary_string(name.data, name.length),
&sv); &sv);
} else { } else {
err = 3; err = 3;
} }
break; break;
case PSYC_TYPE_FLAG: case PSYC_TYPE_FLAG:
if (value.length && value.ptr[0] >= '0' && value.ptr[0] <= '1') { if (value.length && value.data[0] >= '0' && value.data[0] <= '1') {
sv.type = PIKE_T_INT; sv.u.integer = value.ptr[0] - '0'; sv.type = PIKE_T_INT; sv.u.integer = value.data[0] - '0';
mapping_string_insert(THIS->evars, mapping_string_insert(THIS->evars,
make_shared_binary_string(name.ptr, name.length), make_shared_binary_string(name.data, name.length),
&sv); &sv);
} else { } else {
err = 4; err = 4;
@ -390,7 +390,7 @@ PIKECLASS Parser {
case PSYC_PARSE_LIST_END: // last element case PSYC_PARSE_LIST_END: // last element
retl = 0; retl = 0;
case PSYC_PARSE_LIST_ELEM: case PSYC_PARSE_LIST_ELEM:
sv.type = PIKE_T_STRING; sv.u.string = make_shared_binary_string(elem.ptr, elem.length); sv.type = PIKE_T_STRING; sv.u.string = make_shared_binary_string(elem.data, elem.length);
elems = array_insert(elems, &sv, count++); elems = array_insert(elems, &sv, count++);
break; break;
default: default:
@ -403,7 +403,7 @@ PIKECLASS Parser {
sv.type = PIKE_T_ARRAY; sv.type = PIKE_T_ARRAY;
sv.u.array = elems; sv.u.array = elems;
mapping_string_insert(THIS->evars, mapping_string_insert(THIS->evars,
make_shared_binary_string(name.ptr, name.length), make_shared_binary_string(name.data, name.length),
&sv); &sv);
} }
free_array(elems); free_array(elems);
@ -411,8 +411,8 @@ PIKECLASS Parser {
break; break;
default: // string default: // string
mapping_string_insert_string(THIS->evars, mapping_string_insert_string(THIS->evars,
make_shared_binary_string(name.ptr, name.length), make_shared_binary_string(name.data, name.length),
make_shared_binary_string(value.ptr, value.length)); make_shared_binary_string(value.data, value.length));
} }
} while (0); } while (0);
if (err) { // there was an error while if (err) { // there was an error while
@ -423,21 +423,21 @@ PIKECLASS Parser {
case PSYC_PARSE_BODY_START: // if length was given this is used for body case PSYC_PARSE_BODY_START: // if length was given this is used for body
init_string_builder_alloc(&THIS->incomplete, psyc_parse_value_length(&THIS->parser), 0); init_string_builder_alloc(&THIS->incomplete, psyc_parse_value_length(&THIS->parser), 0);
case PSYC_PARSE_BODY_CONT: case PSYC_PARSE_BODY_CONT:
string_builder_append(&THIS->incomplete, MKPCHARP(value.ptr, 0), value.length); string_builder_append(&THIS->incomplete, MKPCHARP(value.data, 0), value.length);
break; break;
case PSYC_PARSE_BODY_END: case PSYC_PARSE_BODY_END:
string_builder_append(&THIS->incomplete, MKPCHARP(value.ptr, 0), value.length); string_builder_append(&THIS->incomplete, MKPCHARP(value.data, 0), value.length);
do { do {
struct pike_string *tmp = finish_string_builder(&THIS->incomplete); struct pike_string *tmp = finish_string_builder(&THIS->incomplete);
value.length = tmp->len; value.length = tmp->len;
value.ptr = (char *) STR0(tmp); value.data = (char *) STR0(tmp);
// FIXME: not sure if this is really safe // FIXME: not sure if this is really safe
free_string(tmp); free_string(tmp);
} while (0); } while (0);
// fall thru // fall thru
case PSYC_PARSE_BODY: case PSYC_PARSE_BODY:
THIS->method = make_shared_binary_string(name.ptr, name.length); THIS->method = make_shared_binary_string(name.data, name.length);
THIS->body = make_shared_binary_string(value.ptr, value.length); THIS->body = make_shared_binary_string(value.data, value.length);
break; break;
case PSYC_PARSE_COMPLETE: // apply the callback case PSYC_PARSE_COMPLETE: // apply the callback
push_mapping(THIS->rvars); push_mapping(THIS->rvars);

View file

@ -101,49 +101,38 @@ failed:
} }
/** /**
* Check if keyword is in array. * Look up value associated with a key in a dictionary.
*
* @param array The array to search, should be ordered alphabetically.
* @param size Size of array.
* @param kw Keyword to look for.
* @param kwlen Length of keyword.
* @param inherit If true, look for any keyword inheriting from name,
otherwise only exact matches are returned.
* @param matching A temporary array used for keeping track of results.
* Should be the same size as the array we're searching.
*
* @return The value of the matched variable in the array.
*/ */
int psyc_in_array (const PsycMatchVar *array, size_t size, void * psyc_dict_lookup (const PsycDict *dict, size_t size,
const char *kw, size_t kwlen, const char *key, size_t keylen,
PsycBool inherit, int8_t *matching) PsycBool inherit, int8_t *matching)
{ {
size_t cursor = 1; size_t cursor = 1;
uint8_t i, m = 0; uint8_t i, m = 0;
//memset(&matching, -1, sizeof matching); //memset(&matching, -1, sizeof matching);
if (kwlen < 2 || kw[0] != '_') if (keylen < 2 || key[0] != '_')
return 0; return 0;
// first find the keywords with matching length // first find the keywords with matching length
for (i=0; i<size; i++) for (i=0; i<size; i++)
if (kwlen == array[i].key.length || if (keylen == dict[i].key.length ||
(inherit && kwlen > array[i].key.length && kw[array[i].key.length] == '_')) (inherit && keylen > dict[i].key.length && key[dict[i].key.length] == '_'))
matching[m++] = i; matching[m++] = i;
matching[m] = -1; // mark the end of matching indexes matching[m] = -1; // mark the end of matching indexes
while (cursor < kwlen && matching[0] >= 0) { while (cursor < keylen && matching[0] >= 0) {
for (i = m = 0; i < size; i++) { for (i = m = 0; i < size; i++) {
if (matching[i] < 0) if (matching[i] < 0)
break; // reached the end of possible matches break; // reached the end of possible matches
if (cursor < array[matching[i]].key.length && if (cursor < dict[matching[i]].key.length &&
array[matching[i]].key.ptr[cursor] == kw[cursor]) dict[matching[i]].key.data[cursor] == key[cursor])
matching[m++] = matching[i]; // found a match, update matching indexes matching[m++] = matching[i]; // found a match, update matching indexes
else if (cursor == array[matching[i]].key.length && kw[cursor] == '_') else if (cursor == dict[matching[i]].key.length && key[cursor] == '_')
return array[matching[0]].value; // _ after the end of a matching prefix return dict[matching[0]].value; // _ after the end of a matching prefix
else if (array[matching[i]].key.ptr[cursor] > kw[cursor]) else if (dict[matching[i]].key.data[cursor] > key[cursor])
break; // passed the possible matches in alphabetical order in the array break; // passed the possible matches in alphabetical order in the dict
} }
if (m < size) if (m < size)
@ -153,7 +142,7 @@ int psyc_in_array (const PsycMatchVar *array, size_t size,
} }
// return first match if found // return first match if found
return matching[0] >= 0 ? array[matching[0]].value : 0; return matching[0] >= 0 ? dict[matching[0]].value : 0;
} }
#ifdef CMDTOOL #ifdef CMDTOOL

View file

@ -13,8 +13,8 @@ PsycListFlag psyc_list_length_check (PsycList *list)
PsycString *elem = &list->elems[i]; PsycString *elem = &list->elems[i];
length += 1 + elem->length; // |elem length += 1 + elem->length; // |elem
if (length > PSYC_MODIFIER_SIZE_THRESHOLD || if (length > PSYC_MODIFIER_SIZE_THRESHOLD ||
memchr(elem->ptr, (int)'|', elem->length) || memchr(elem->data, (int)'|', elem->length) ||
memchr(elem->ptr, (int)'\n', elem->length)) memchr(elem->data, (int)'\n', elem->length))
{ {
flag = PSYC_LIST_NEED_LENGTH; flag = PSYC_LIST_NEED_LENGTH;
break; break;
@ -76,7 +76,7 @@ size_t psyc_modifier_length (PsycModifier *m)
inline inline
PsycPacketFlag psyc_packet_length_check (PsycPacket *p) PsycPacketFlag psyc_packet_length_check (PsycPacket *p)
{ {
if (p->data.length == 1 && p->data.ptr[0] == C_GLYPH_PACKET_DELIMITER) if (p->data.length == 1 && p->data.data[0] == C_GLYPH_PACKET_DELIMITER)
return PSYC_PACKET_NEED_LENGTH; return PSYC_PACKET_NEED_LENGTH;
if (p->data.length > PSYC_CONTENT_SIZE_THRESHOLD) if (p->data.length > PSYC_CONTENT_SIZE_THRESHOLD)
@ -89,7 +89,7 @@ PsycPacketFlag psyc_packet_length_check (PsycPacket *p)
if (p->entity.modifiers[i].flag == PSYC_MODIFIER_NEED_LENGTH) if (p->entity.modifiers[i].flag == PSYC_MODIFIER_NEED_LENGTH)
return PSYC_PACKET_NEED_LENGTH; return PSYC_PACKET_NEED_LENGTH;
if (memmem(p->data.ptr, p->data.length, PSYC_C2ARG(PSYC_PACKET_DELIMITER))) if (memmem(p->data.data, p->data.length, PSYC_C2ARG(PSYC_PACKET_DELIMITER)))
return PSYC_PACKET_NEED_LENGTH; return PSYC_PACKET_NEED_LENGTH;
return PSYC_PACKET_NO_LENGTH; return PSYC_PACKET_NO_LENGTH;

View file

@ -31,10 +31,10 @@ typedef enum {
static inline static inline
parseRC psyc_parse_keyword (PsycParseState *state, PsycString *name) parseRC psyc_parse_keyword (PsycParseState *state, PsycString *name)
{ {
name->ptr = state->buffer.ptr + state->cursor; name->data = state->buffer.data + state->cursor;
name->length = 0; name->length = 0;
while (psyc_is_kw_char(state->buffer.ptr[state->cursor])) while (psyc_is_kw_char(state->buffer.data[state->cursor]))
{ {
name->length++; // was a valid char, increase length name->length++; // was a valid char, increase length
ADVANCE_CURSOR_OR_RETURN(PSYC_PARSE_INSUFFICIENT); ADVANCE_CURSOR_OR_RETURN(PSYC_PARSE_INSUFFICIENT);
@ -58,7 +58,7 @@ parseRC psyc_parse_binary_value (PsycParseState *state, PsycString *value,
size_t *length, size_t *parsed) size_t *length, size_t *parsed)
{ {
size_t remaining = *length - *parsed; size_t remaining = *length - *parsed;
value->ptr = state->buffer.ptr + state->cursor; value->data = state->buffer.data + state->cursor;
if (state->cursor + remaining > state->buffer.length) if (state->cursor + remaining > state->buffer.length)
{ // value doesn't fit in the buffer completely { // value doesn't fit in the buffer completely
@ -84,7 +84,7 @@ static inline
parseRC psyc_parse_modifier (PsycParseState *state, char *oper, parseRC psyc_parse_modifier (PsycParseState *state, char *oper,
PsycString *name, PsycString *value) PsycString *name, PsycString *value)
{ {
*oper = *(state->buffer.ptr + state->cursor); *oper = *(state->buffer.data + state->cursor);
ADVANCE_CURSOR_OR_RETURN(PSYC_PARSE_INSUFFICIENT); ADVANCE_CURSOR_OR_RETURN(PSYC_PARSE_INSUFFICIENT);
parseRC ret = psyc_parse_keyword(state, name); parseRC ret = psyc_parse_keyword(state, name);
@ -101,26 +101,26 @@ parseRC psyc_parse_modifier (PsycParseState *state, char *oper,
// Parse the value. // Parse the value.
// If we're in the content part check if it's a binary var. // If we're in the content part check if it's a binary var.
if (state->part == PSYC_PART_CONTENT && state->buffer.ptr[state->cursor] == ' ') // binary arg if (state->part == PSYC_PART_CONTENT && state->buffer.data[state->cursor] == ' ') // binary arg
{ // After SP the length follows. { // After SP the length follows.
ADVANCE_CURSOR_OR_RETURN(PSYC_PARSE_INSUFFICIENT); ADVANCE_CURSOR_OR_RETURN(PSYC_PARSE_INSUFFICIENT);
if (psyc_is_numeric(state->buffer.ptr[state->cursor])) if (psyc_is_numeric(state->buffer.data[state->cursor]))
{ {
state->valueLengthFound = 1; state->valueLengthFound = 1;
do do
{ {
length = 10 * length + state->buffer.ptr[state->cursor] - '0'; length = 10 * length + state->buffer.data[state->cursor] - '0';
ADVANCE_CURSOR_OR_RETURN(PSYC_PARSE_INSUFFICIENT); ADVANCE_CURSOR_OR_RETURN(PSYC_PARSE_INSUFFICIENT);
} }
while (psyc_is_numeric(state->buffer.ptr[state->cursor])); while (psyc_is_numeric(state->buffer.data[state->cursor]));
state->valueLength = length; state->valueLength = length;
} }
else else
return PSYC_PARSE_ERROR_MOD_LEN; return PSYC_PARSE_ERROR_MOD_LEN;
// After the length a TAB follows. // After the length a TAB follows.
if (state->buffer.ptr[state->cursor] != '\t') if (state->buffer.data[state->cursor] != '\t')
return PSYC_PARSE_ERROR_MOD_TAB; return PSYC_PARSE_ERROR_MOD_TAB;
if (++(state->cursor) >= state->buffer.length) if (++(state->cursor) >= state->buffer.length)
@ -132,12 +132,12 @@ parseRC psyc_parse_modifier (PsycParseState *state, char *oper,
return PARSE_SUCCESS; return PARSE_SUCCESS;
} }
else if (state->buffer.ptr[state->cursor] == '\t') // simple arg else if (state->buffer.data[state->cursor] == '\t') // simple arg
{ {
ADVANCE_CURSOR_OR_RETURN(PSYC_PARSE_INSUFFICIENT); ADVANCE_CURSOR_OR_RETURN(PSYC_PARSE_INSUFFICIENT);
value->ptr = state->buffer.ptr + state->cursor; value->data = state->buffer.data + state->cursor;
while (state->buffer.ptr[state->cursor] != '\n') while (state->buffer.data[state->cursor] != '\n')
{ {
value->length++; value->length++;
ADVANCE_CURSOR_OR_RETURN(PSYC_PARSE_INSUFFICIENT); ADVANCE_CURSOR_OR_RETURN(PSYC_PARSE_INSUFFICIENT);
@ -189,7 +189,7 @@ PsycParseRC psyc_parse (PsycParseState *state, char *oper,
case PSYC_PART_ROUTING: case PSYC_PART_ROUTING:
if (state->routingLength > 0) if (state->routingLength > 0)
{ {
if (state->buffer.ptr[state->cursor] != '\n') if (state->buffer.data[state->cursor] != '\n')
return PSYC_PARSE_ERROR_MOD_NL; return PSYC_PARSE_ERROR_MOD_NL;
ADVANCE_CURSOR_OR_RETURN(PSYC_PARSE_INSUFFICIENT); ADVANCE_CURSOR_OR_RETURN(PSYC_PARSE_INSUFFICIENT);
} }
@ -197,7 +197,7 @@ PsycParseRC psyc_parse (PsycParseState *state, char *oper,
// Each line of the header starts with a glyph, // Each line of the header starts with a glyph,
// i.e. :_name, -_name +_name etc, // i.e. :_name, -_name +_name etc,
// so just test if the first char is a glyph. // so just test if the first char is a glyph.
if (psyc_is_glyph(state->buffer.ptr[state->cursor])) // is the first char a glyph? if (psyc_is_glyph(state->buffer.data[state->cursor])) // is the first char a glyph?
{ // it is a glyph, so a variable starts here { // it is a glyph, so a variable starts here
ret = psyc_parse_modifier(state, oper, name, value); ret = psyc_parse_modifier(state, oper, name, value);
state->routingLength += state->cursor - pos; state->routingLength += state->cursor - pos;
@ -212,20 +212,20 @@ PsycParseRC psyc_parse (PsycParseState *state, char *oper,
case PSYC_PART_LENGTH: case PSYC_PART_LENGTH:
// End of header, content starts with an optional length then a NL // End of header, content starts with an optional length then a NL
if (psyc_is_numeric(state->buffer.ptr[state->cursor])) if (psyc_is_numeric(state->buffer.data[state->cursor]))
{ {
state->contentLengthFound = 1; state->contentLengthFound = 1;
state->contentLength = 0; state->contentLength = 0;
do do
{ {
state->contentLength = 10 * state->contentLength + state->buffer.ptr[state->cursor] - '0'; state->contentLength = 10 * state->contentLength + state->buffer.data[state->cursor] - '0';
ADVANCE_CURSOR_OR_RETURN(PSYC_PARSE_INSUFFICIENT); ADVANCE_CURSOR_OR_RETURN(PSYC_PARSE_INSUFFICIENT);
} }
while (psyc_is_numeric(state->buffer.ptr[state->cursor])); while (psyc_is_numeric(state->buffer.data[state->cursor]));
} }
if (state->buffer.ptr[state->cursor] == '\n') // start of content if (state->buffer.data[state->cursor] == '\n') // start of content
{ {
// If we need to parse the header only and we know the content length, // If we need to parse the header only and we know the content length,
// then skip content parsing. // then skip content parsing.
@ -270,7 +270,7 @@ PsycParseRC psyc_parse (PsycParseState *state, char *oper,
if (state->contentParsed > 0) if (state->contentParsed > 0)
{ {
if (state->buffer.ptr[state->cursor] != '\n') if (state->buffer.data[state->cursor] != '\n')
return PSYC_PARSE_ERROR_MOD_NL; return PSYC_PARSE_ERROR_MOD_NL;
ADVANCE_CURSOR_OR_RETURN(PSYC_PARSE_INSUFFICIENT); ADVANCE_CURSOR_OR_RETURN(PSYC_PARSE_INSUFFICIENT);
} }
@ -280,7 +280,7 @@ PsycParseRC psyc_parse (PsycParseState *state, char *oper,
// So just test if the first char is a glyph. // So just test if the first char is a glyph.
// In the body, the same applies, only that the // In the body, the same applies, only that the
// method does not start with a glyph. // method does not start with a glyph.
if (psyc_is_glyph(state->buffer.ptr[state->cursor])) if (psyc_is_glyph(state->buffer.data[state->cursor]))
{ {
ret = psyc_parse_modifier(state, oper, name, value); ret = psyc_parse_modifier(state, oper, name, value);
state->contentParsed += state->cursor - pos; state->contentParsed += state->cursor - pos;
@ -308,7 +308,7 @@ PsycParseRC psyc_parse (PsycParseState *state, char *oper,
return ret; return ret;
else if (ret == PARSE_SUCCESS) else if (ret == PARSE_SUCCESS)
{ // the method ends with a \n then the data follows { // the method ends with a \n then the data follows
if (state->buffer.ptr[state->cursor] != '\n') if (state->buffer.data[state->cursor] != '\n')
return PSYC_PARSE_ERROR_METHOD; return PSYC_PARSE_ERROR_METHOD;
state->valueLengthFound = 0; state->valueLengthFound = 0;
@ -338,7 +338,7 @@ PsycParseRC psyc_parse (PsycParseState *state, char *oper,
case PSYC_PART_DATA: case PSYC_PART_DATA:
PSYC_PART_DATA: PSYC_PART_DATA:
value->ptr = state->buffer.ptr + state->cursor; value->data = state->buffer.data + state->cursor;
value->length = 0; value->length = 0;
if (state->contentLengthFound) // We know the length of the packet. if (state->contentLengthFound) // We know the length of the packet.
@ -370,7 +370,7 @@ PsycParseRC psyc_parse (PsycParseState *state, char *oper,
while (1) while (1)
{ {
uint8_t nl = state->buffer.ptr[state->cursor] == '\n'; uint8_t nl = state->buffer.data[state->cursor] == '\n';
// check for |\n if we're at the start of data or we have found a \n // check for |\n if we're at the start of data or we have found a \n
if (state->cursor == datac || nl) if (state->cursor == datac || nl)
{ {
@ -380,8 +380,8 @@ PsycParseRC psyc_parse (PsycParseState *state, char *oper,
return PSYC_PARSE_INSUFFICIENT; return PSYC_PARSE_INSUFFICIENT;
} }
if (state->buffer.ptr[state->cursor+nl] == '|' && if (state->buffer.data[state->cursor+nl] == '|' &&
state->buffer.ptr[state->cursor+1+nl] == '\n') // packet ends here state->buffer.data[state->cursor+1+nl] == '\n') // packet ends here
{ {
if (state->flags & PSYC_PARSE_ROUTING_ONLY) if (state->flags & PSYC_PARSE_ROUTING_ONLY)
value->length++; value->length++;
@ -405,7 +405,7 @@ PsycParseRC psyc_parse (PsycParseState *state, char *oper,
state->valueLength = 0; state->valueLength = 0;
state->valueLengthFound = 0; state->valueLengthFound = 0;
if (state->buffer.ptr[state->cursor] != '\n') if (state->buffer.data[state->cursor] != '\n')
return PSYC_PARSE_ERROR_END; return PSYC_PARSE_ERROR_END;
state->contentParsed++; state->contentParsed++;
@ -417,8 +417,8 @@ PsycParseRC psyc_parse (PsycParseState *state, char *oper,
if (state->cursor+1 >= state->buffer.length) // incremented cursor inside length? if (state->cursor+1 >= state->buffer.length) // incremented cursor inside length?
return PSYC_PARSE_INSUFFICIENT; return PSYC_PARSE_INSUFFICIENT;
if (state->buffer.ptr[state->cursor] == '|' && if (state->buffer.data[state->cursor] == '|' &&
state->buffer.ptr[state->cursor+1] == '\n') // packet ends here state->buffer.data[state->cursor+1] == '\n') // packet ends here
{ {
state->cursor += 2; state->cursor += 2;
state->part = PSYC_PART_RESET; state->part = PSYC_PART_RESET;
@ -447,12 +447,12 @@ PsycParseListRC psyc_parse_list (PsycParseListState *state, PsycString *elem)
if (!state->type) // If type is not set we're at the start if (!state->type) // If type is not set we're at the start
{ {
// First character is either | for text lists, or a number for binary lists // First character is either | for text lists, or a number for binary lists
if (state->buffer.ptr[state->cursor] == '|') if (state->buffer.data[state->cursor] == '|')
{ {
state->type = PSYC_LIST_TEXT; state->type = PSYC_LIST_TEXT;
state->cursor++; state->cursor++;
} }
else if (psyc_is_numeric(state->buffer.ptr[state->cursor])) else if (psyc_is_numeric(state->buffer.data[state->cursor]))
state->type = PSYC_LIST_BINARY; state->type = PSYC_LIST_BINARY;
else else
return PSYC_PARSE_LIST_ERROR_TYPE; return PSYC_PARSE_LIST_ERROR_TYPE;
@ -460,13 +460,13 @@ PsycParseListRC psyc_parse_list (PsycParseListState *state, PsycString *elem)
if (state->type == PSYC_LIST_TEXT) if (state->type == PSYC_LIST_TEXT)
{ {
elem->ptr = state->buffer.ptr + state->cursor; elem->data = state->buffer.data + state->cursor;
elem->length = 0; elem->length = 0;
if (state->cursor >= state->buffer.length) if (state->cursor >= state->buffer.length)
return PSYC_PARSE_LIST_END; return PSYC_PARSE_LIST_END;
while (state->buffer.ptr[state->cursor] != '|') while (state->buffer.data[state->cursor] != '|')
{ {
elem->length++; elem->length++;
if (++(state->cursor) >= state->buffer.length) if (++(state->cursor) >= state->buffer.length)
@ -480,23 +480,23 @@ PsycParseListRC psyc_parse_list (PsycParseListState *state, PsycString *elem)
if (!(state->elemParsed < state->elemLength)) if (!(state->elemParsed < state->elemLength))
{ {
// Element starts with a number. // Element starts with a number.
if (psyc_is_numeric(state->buffer.ptr[state->cursor])) if (psyc_is_numeric(state->buffer.data[state->cursor]))
{ {
do do
{ {
state->elemLength = 10 * state->elemLength + state->buffer.ptr[state->cursor] - '0'; state->elemLength = 10 * state->elemLength + state->buffer.data[state->cursor] - '0';
ADVANCE_CURSOR_OR_RETURN(PSYC_PARSE_LIST_INCOMPLETE); ADVANCE_CURSOR_OR_RETURN(PSYC_PARSE_LIST_INCOMPLETE);
} }
while (psyc_is_numeric(state->buffer.ptr[state->cursor])); while (psyc_is_numeric(state->buffer.data[state->cursor]));
} }
else else
return PSYC_PARSE_LIST_ERROR_LEN; return PSYC_PARSE_LIST_ERROR_LEN;
if (state->buffer.ptr[state->cursor] != ' ') if (state->buffer.data[state->cursor] != ' ')
return PSYC_PARSE_LIST_ERROR_LEN; return PSYC_PARSE_LIST_ERROR_LEN;
state->cursor++; state->cursor++;
elem->ptr = state->buffer.ptr + state->cursor; elem->data = state->buffer.data + state->cursor;
elem->length = 0; elem->length = 0;
state->elemParsed = 0; state->elemParsed = 0;
} }
@ -513,7 +513,7 @@ PsycParseListRC psyc_parse_list (PsycParseListState *state, PsycString *elem)
if (state->cursor >= state->buffer.length) if (state->cursor >= state->buffer.length)
return PSYC_PARSE_LIST_END; return PSYC_PARSE_LIST_END;
if (state->buffer.ptr[state->cursor] != '|') if (state->buffer.data[state->cursor] != '|')
return PSYC_PARSE_LIST_ERROR_DELIM; return PSYC_PARSE_LIST_ERROR_DELIM;
state->cursor++; state->cursor++;

View file

@ -22,7 +22,7 @@ PsycRenderListRC psyc_render_list (PsycList *list, char *buffer, size_t buflen)
buffer[cur++] = '|'; buffer[cur++] = '|';
cur += itoa(elem->length, buffer + cur, 10); cur += itoa(elem->length, buffer + cur, 10);
buffer[cur++] = ' '; buffer[cur++] = ' ';
memcpy(buffer + cur, elem->ptr, elem->length); memcpy(buffer + cur, elem->data, elem->length);
cur += elem->length; cur += elem->length;
} }
} }
@ -32,7 +32,7 @@ PsycRenderListRC psyc_render_list (PsycList *list, char *buffer, size_t buflen)
{ {
elem = &list->elems[i]; elem = &list->elems[i];
buffer[cur++] = '|'; buffer[cur++] = '|';
memcpy(buffer + cur, elem->ptr, elem->length); memcpy(buffer + cur, elem->data, elem->length);
cur += elem->length; cur += elem->length;
} }
} }
@ -48,7 +48,7 @@ size_t psyc_render_modifier (PsycModifier *mod, char *buffer)
size_t cur = 0; size_t cur = 0;
buffer[cur++] = mod->oper; buffer[cur++] = mod->oper;
memcpy(buffer + cur, mod->name.ptr, mod->name.length); memcpy(buffer + cur, mod->name.data, mod->name.length);
cur += mod->name.length; cur += mod->name.length;
if (cur <= 1) if (cur <= 1)
return cur; // error, name can't be empty return cur; // error, name can't be empty
@ -60,7 +60,7 @@ size_t psyc_render_modifier (PsycModifier *mod, char *buffer)
} }
buffer[cur++] = '\t'; buffer[cur++] = '\t';
memcpy(buffer + cur, mod->value.ptr, mod->value.length); memcpy(buffer + cur, mod->value.data, mod->value.length);
cur += mod->value.length; cur += mod->value.length;
buffer[cur++] = '\n'; buffer[cur++] = '\n';
@ -96,7 +96,7 @@ PsycRenderRC psyc_render (PsycPacket *packet, char *buffer, size_t buflen)
if (packet->content.length) // render raw content if present if (packet->content.length) // render raw content if present
{ {
memcpy(buffer + cur, packet->content.ptr, packet->content.length); memcpy(buffer + cur, packet->content.data, packet->content.length);
cur += packet->content.length; cur += packet->content.length;
} }
else else
@ -107,13 +107,13 @@ PsycRenderRC psyc_render (PsycPacket *packet, char *buffer, size_t buflen)
if (packet->method.length) // add method\n if (packet->method.length) // add method\n
{ {
memcpy(buffer + cur, packet->method.ptr, packet->method.length); memcpy(buffer + cur, packet->method.data, packet->method.length);
cur += packet->method.length; cur += packet->method.length;
buffer[cur++] = '\n'; buffer[cur++] = '\n';
if (packet->data.length) // add data\n if (packet->data.length) // add data\n
{ {
memcpy(buffer + cur, packet->data.ptr, packet->data.length); memcpy(buffer + cur, packet->data.data, packet->data.length);
cur += packet->data.length; cur += packet->data.length;
buffer[cur++] = '\n'; buffer[cur++] = '\n';
} }

View file

@ -3,8 +3,8 @@
PsycTextRC psyc_text (PsycTextState *state, PsycTextCB getValue, void* extra) PsycTextRC psyc_text (PsycTextState *state, PsycTextCB getValue, void* extra)
{ {
const char *start = state->tmpl.ptr, *end; // start & end of variable name const char *start = state->tmpl.data, *end; // start & end of variable name
const char *prev = state->tmpl.ptr + state->cursor; const char *prev = state->tmpl.data + state->cursor;
PsycString value; PsycString value;
int ret; int ret;
size_t len; size_t len;
@ -12,20 +12,20 @@ PsycTextRC psyc_text (PsycTextState *state, PsycTextCB getValue, void* extra)
while (state->cursor < state->tmpl.length) while (state->cursor < state->tmpl.length)
{ {
start = memmem(state->tmpl.ptr + state->cursor, start = memmem(state->tmpl.data + state->cursor,
state->tmpl.length - state->cursor, state->tmpl.length - state->cursor,
state->open.ptr, state->open.length); state->open.data, state->open.length);
if (!start) if (!start)
break; break;
state->cursor = (start - state->tmpl.ptr) + state->open.length; state->cursor = (start - state->tmpl.data) + state->open.length;
if (state->cursor >= state->tmpl.length) if (state->cursor >= state->tmpl.length)
break; // [ at the end break; // [ at the end
end = memmem(state->tmpl.ptr + state->cursor, end = memmem(state->tmpl.data + state->cursor,
state->tmpl.length - state->cursor, state->tmpl.length - state->cursor,
state->close.ptr, state->close.length); state->close.data, state->close.length);
state->cursor = (end - state->tmpl.ptr) + state->close.length; state->cursor = (end - state->tmpl.data) + state->close.length;
if (!end) if (!end)
break; // ] not found break; // ] not found
@ -45,25 +45,25 @@ PsycTextRC psyc_text (PsycTextState *state, PsycTextCB getValue, void* extra)
len = start - prev; len = start - prev;
if (state->written + len > state->buffer.length) if (state->written + len > state->buffer.length)
{ {
state->cursor = prev - state->tmpl.ptr; state->cursor = prev - state->tmpl.data;
return PSYC_TEXT_INCOMPLETE; return PSYC_TEXT_INCOMPLETE;
} }
memcpy((void *)(state->buffer.ptr + state->written), prev, len); memcpy((void *)(state->buffer.data + state->written), prev, len);
state->written += len; state->written += len;
// now substitute the value if there's enough buffer space // now substitute the value if there's enough buffer space
if (state->written + value.length > state->buffer.length) if (state->written + value.length > state->buffer.length)
{ {
state->cursor = start - state->tmpl.ptr; state->cursor = start - state->tmpl.data;
return PSYC_TEXT_INCOMPLETE; return PSYC_TEXT_INCOMPLETE;
} }
memcpy((void *)(state->buffer.ptr + state->written), value.ptr, value.length); memcpy((void *)(state->buffer.data + state->written), value.data, value.length);
state->written += value.length; state->written += value.length;
// mark the start of the next chunk of text in the template // mark the start of the next chunk of text in the template
prev = state->tmpl.ptr + state->cursor; prev = state->tmpl.data + state->cursor;
no_subst = 0; no_subst = 0;
} }
@ -71,11 +71,11 @@ PsycTextRC psyc_text (PsycTextState *state, PsycTextCB getValue, void* extra)
return PSYC_TEXT_NO_SUBST; return PSYC_TEXT_NO_SUBST;
// copy the rest of the template after the last var // copy the rest of the template after the last var
len = state->tmpl.length - (prev - state->tmpl.ptr); len = state->tmpl.length - (prev - state->tmpl.data);
if (state->written + len > state->buffer.length) if (state->written + len > state->buffer.length)
return PSYC_TEXT_INCOMPLETE; return PSYC_TEXT_INCOMPLETE;
memcpy((void *)(state->buffer.ptr + state->written), prev, len); memcpy((void *)(state->buffer.data + state->written), prev, len);
state->written += len; state->written += len;
return PSYC_TEXT_COMPLETE; return PSYC_TEXT_COMPLETE;

View file

@ -10,13 +10,13 @@ int psyc_uniform_parse (PsycUniform *uni, char *str, size_t length)
size_t pos = 0, part = PSYC_UNIFORM_SCHEME; size_t pos = 0, part = PSYC_UNIFORM_SCHEME;
uni->valid = 0; uni->valid = 0;
uni->full.ptr = str; uni->full.data = str;
uni->full.length = length; uni->full.length = length;
while (pos < length) { while (pos < length) {
c = str[pos]; c = str[pos];
if (c == ':') { if (c == ':') {
uni->scheme.ptr = str; uni->scheme.data = str;
uni->scheme.length = pos++; uni->scheme.length = pos++;
break; break;
} else if (!psyc_is_host_char(c)) } else if (!psyc_is_host_char(c))
@ -26,14 +26,14 @@ int psyc_uniform_parse (PsycUniform *uni, char *str, size_t length)
p = &uni->scheme; p = &uni->scheme;
if (p->length == 4 && if (p->length == 4 &&
tolower(p->ptr[0]) == 'p' && tolower(p->data[0]) == 'p' &&
tolower(p->ptr[1]) == 's' && tolower(p->data[1]) == 's' &&
tolower(p->ptr[2]) == 'y' && tolower(p->data[2]) == 'y' &&
tolower(p->ptr[3]) == 'c') { tolower(p->data[3]) == 'c') {
uni->type = PSYC_SCHEME_PSYC; uni->type = PSYC_SCHEME_PSYC;
part = PSYC_UNIFORM_SLASHES; part = PSYC_UNIFORM_SLASHES;
uni->slashes.ptr = str + pos; uni->slashes.data = str + pos;
uni->slashes.length = 0; uni->slashes.length = 0;
while (pos < length) { while (pos < length) {
@ -46,7 +46,7 @@ int psyc_uniform_parse (PsycUniform *uni, char *str, size_t length)
if (uni->slashes.length == 2) { if (uni->slashes.length == 2) {
part = PSYC_UNIFORM_HOST; part = PSYC_UNIFORM_HOST;
uni->host.ptr = str + pos + 1; uni->host.data = str + pos + 1;
uni->host.length = 0; uni->host.length = 0;
} }
break; break;
@ -69,7 +69,7 @@ int psyc_uniform_parse (PsycUniform *uni, char *str, size_t length)
} }
else return PSYC_PARSE_UNIFORM_INVALID_HOST; else return PSYC_PARSE_UNIFORM_INVALID_HOST;
p->ptr = str + pos + 1; p->data = str + pos + 1;
p->length = 0; p->length = 0;
break; break;
@ -84,13 +84,13 @@ int psyc_uniform_parse (PsycUniform *uni, char *str, size_t length)
if (c == '/') { if (c == '/') {
part = PSYC_UNIFORM_RESOURCE; part = PSYC_UNIFORM_RESOURCE;
uni->resource.ptr = str + pos + 1; uni->resource.data = str + pos + 1;
uni->resource.length = 0; uni->resource.length = 0;
break; break;
} }
else { else {
part = PSYC_UNIFORM_TRANSPORT; part = PSYC_UNIFORM_TRANSPORT;
uni->transport.ptr = str + pos; uni->transport.data = str + pos;
uni->transport.length = 0; uni->transport.length = 0;
} }
// fall thru // fall thru
@ -109,7 +109,7 @@ int psyc_uniform_parse (PsycUniform *uni, char *str, size_t length)
break; break;
case '/': case '/':
part = PSYC_UNIFORM_RESOURCE; part = PSYC_UNIFORM_RESOURCE;
uni->resource.ptr = str + pos + 1; uni->resource.data = str + pos + 1;
uni->resource.length = 0; uni->resource.length = 0;
break; break;
default: default:
@ -123,7 +123,7 @@ int psyc_uniform_parse (PsycUniform *uni, char *str, size_t length)
break; break;
} else if (c == '#') { } else if (c == '#') {
part = PSYC_UNIFORM_CHANNEL; part = PSYC_UNIFORM_CHANNEL;
uni->channel.ptr = str + pos + 1; uni->channel.data = str + pos + 1;
uni->channel.length = 0; uni->channel.length = 0;
break; break;
} else return PSYC_PARSE_UNIFORM_INVALID_RESOURCE; } else return PSYC_PARSE_UNIFORM_INVALID_RESOURCE;
@ -140,20 +140,20 @@ int psyc_uniform_parse (PsycUniform *uni, char *str, size_t length)
if (uni->host.length == 0) if (uni->host.length == 0)
return PSYC_PARSE_UNIFORM_INVALID_HOST; return PSYC_PARSE_UNIFORM_INVALID_HOST;
uni->host_port.ptr = uni->host.ptr; uni->host_port.data = uni->host.data;
uni->host_port.length = uni->host.length + uni->port.length + uni->transport.length; uni->host_port.length = uni->host.length + uni->port.length + uni->transport.length;
if (uni->port.length > 0 || uni->transport.length > 0) if (uni->port.length > 0 || uni->transport.length > 0)
uni->host_port.length++; uni->host_port.length++;
uni->root.ptr = str; uni->root.data = str;
uni->root.length = uni->scheme.length + 1 + uni->slashes.length + uni->root.length = uni->scheme.length + 1 + uni->slashes.length +
uni->host_port.length; uni->host_port.length;
uni->body.ptr = uni->host.ptr; uni->body.data = uni->host.data;
uni->body.length = length - uni->scheme.length - 1 - uni->slashes.length; uni->body.length = length - uni->scheme.length - 1 - uni->slashes.length;
if (uni->resource.length) { if (uni->resource.length) {
uni->nick.ptr = uni->resource.ptr + 1; uni->nick.data = uni->resource.data + 1;
uni->nick.length = uni->resource.length; uni->nick.length = uni->resource.length;
} }

View file

@ -27,7 +27,7 @@ const PsycString psyc_routing_vars[] =
}; };
// Variable types in alphabetical order. // Variable types in alphabetical order.
const PsycMatchVar psyc_var_types[] = const PsycDictInt psyc_var_types[] =
{ {
{PSYC_C2STR("_amount"), PSYC_TYPE_AMOUNT}, {PSYC_C2STR("_amount"), PSYC_TYPE_AMOUNT},
{PSYC_C2STR("_color"), PSYC_TYPE_COLOR}, {PSYC_C2STR("_color"), PSYC_TYPE_COLOR},
@ -72,9 +72,9 @@ PsycBool psyc_var_is_routing (const char *name, size_t len)
{ {
if (matching[i] < 0) if (matching[i] < 0)
break; // reached the end of possible matches break; // reached the end of possible matches
if (psyc_routing_vars[matching[i]].ptr[cursor] == name[cursor]) if (psyc_routing_vars[matching[i]].data[cursor] == name[cursor])
matching[m++] = matching[i]; // found a match, update matching indexes matching[m++] = matching[i]; // found a match, update matching indexes
else if (psyc_routing_vars[matching[i]].ptr[cursor] > name[cursor]) else if (psyc_routing_vars[matching[i]].data[cursor] > name[cursor])
break; // passed the possible matches in alphabetical order in the array break; // passed the possible matches in alphabetical order in the array
} }
@ -94,6 +94,6 @@ inline
PsycType psyc_var_type (const char *name, size_t len) PsycType psyc_var_type (const char *name, size_t len)
{ {
int8_t m[psyc_var_types_num]; int8_t m[psyc_var_types_num];
return psyc_in_array(psyc_var_types, psyc_var_types_num, return (PsycType) psyc_dict_lookup((PsycDict*)psyc_var_types, psyc_var_types_num,
name, len, PSYC_YES, (int8_t*)&m); name, len, PSYC_YES, (int8_t*)&m);
} }

View file

@ -48,9 +48,9 @@ int main (int argc, char **argv) {
case PSYC_PARSE_LIST_END: case PSYC_PARSE_LIST_END:
ret = 0; ret = 0;
case PSYC_PARSE_LIST_ELEM: case PSYC_PARSE_LIST_ELEM:
if (verbose) printf("|%d: %.*s... (%ld)\n", i, 10, elem.ptr, elem.length); if (verbose) printf("|%d: %.*s... (%ld)\n", i, 10, elem.data, elem.length);
//elems[i] = malloc(elem.length); //elems[i] = malloc(elem.length);
//memcpy(&elems[i++], elem.ptr, elem.length); //memcpy(&elems[i++], elem.data, elem.length);
break; break;
default: default:
printf("# Error while parsing list: %i\n", ret); printf("# Error while parsing list: %i\n", ret);
@ -76,10 +76,10 @@ int main (int argc, char **argv) {
case PSYC_PARSE_LIST_END: case PSYC_PARSE_LIST_END:
ret = 0; ret = 0;
case PSYC_PARSE_LIST_ELEM: case PSYC_PARSE_LIST_ELEM:
//if (verbose) printf("|%.*s\n", (int)elem.length, elem.ptr); //if (verbose) printf("|%.*s\n", (int)elem.length, elem.data);
if (verbose) printf("|%d: %.*s... (%ld)\n", i, 10, elem.ptr, elem.length); if (verbose) printf("|%d: %.*s... (%ld)\n", i, 10, elem.data, elem.length);
elems[i] = malloc(elem.length); elems[i] = malloc(elem.length);
memcpy(elems[i++], elem.ptr, elem.length); memcpy(elems[i++], elem.data, elem.length);
break; break;
default: default:
printf("# Error while parsing list: %i\n", ret); printf("# Error while parsing list: %i\n", ret);
@ -111,7 +111,7 @@ int main (int argc, char **argv) {
elems2 = malloc((i+1) * sizeof(char*)); elems2 = malloc((i+1) * sizeof(char*));
elems2[i] = malloc(elem.length); elems2[i] = malloc(elem.length);
memcpy(elems2[i++], elem.ptr, elem.length); memcpy(elems2[i++], elem.data, elem.length);
break; break;
default: default:
printf("# Error while parsing list: %i\n", ret); printf("# Error while parsing list: %i\n", ret);
@ -145,7 +145,7 @@ int main (int argc, char **argv) {
*/ */
elems2 = malloc(sizeof(char*)); elems2 = malloc(sizeof(char*));
elems2[i] = malloc(elem.length); elems2[i] = malloc(elem.length);
memcpy(elems2[i], elem.ptr, elem.length); memcpy(elems2[i], elem.data, elem.length);
break; break;
default: default:
printf("# Error while parsing list: %i\n", ret); printf("# Error while parsing list: %i\n", ret);
@ -191,7 +191,7 @@ int main (int argc, char **argv) {
ret = 0; ret = 0;
case PSYC_PARSE_LIST_ELEM: case PSYC_PARSE_LIST_ELEM:
elems2[i] = malloc(elem.length); elems2[i] = malloc(elem.length);
memcpy(elems2[i++], elem.ptr, elem.length); memcpy(elems2[i++], elem.data, elem.length);
break; break;
default: default:
printf("# Error while parsing list: %i\n", ret); printf("# Error while parsing list: %i\n", ret);

View file

@ -53,8 +53,8 @@ int main (int argc, char **argv)
// printf("the string is '%.*s'\n", name); // printf("the string is '%.*s'\n", name);
if (verbose) if (verbose)
printf("%.*s = %.*s\n", printf("%.*s = %.*s\n",
(int)name.length, name.ptr, (int)name.length, name.data,
(int)value.length, value.ptr); (int)value.length, value.data);
if (psyc_var_is_list(PSYC_S2ARG(name))) if (psyc_var_is_list(PSYC_S2ARG(name)))
{ {
@ -71,7 +71,7 @@ int main (int argc, char **argv)
case PSYC_PARSE_LIST_END: case PSYC_PARSE_LIST_END:
case PSYC_PARSE_LIST_ELEM: case PSYC_PARSE_LIST_ELEM:
if (verbose) if (verbose)
printf("|%.*s\n", (int)elem.length, elem.ptr); printf("|%.*s\n", (int)elem.length, elem.data);
break; break;
default: default:
printf("Error while parsing list: %i\n", ret); printf("Error while parsing list: %i\n", ret);

View file

@ -82,7 +82,7 @@ int test_input (int i, char *recvbuf, size_t nbytes) {
do { do {
if (verbose >= 3) if (verbose >= 3)
printf("\n# buffer = [%.*s]\n# part = %d\n", (int)parser->buffer.length, parser->buffer.ptr, parser->part); printf("\n# buffer = [%.*s]\n# part = %d\n", (int)parser->buffer.length, parser->buffer.data, parser->part);
// Parse the next part of the packet (a routing/entity modifier or the body) // Parse the next part of the packet (a routing/entity modifier or the body)
ret = exit_code = psyc_parse(parser, &oper, &name, &value); ret = exit_code = psyc_parse(parser, &oper, &name, &value);
if (verbose >= 2) if (verbose >= 2)
@ -222,15 +222,15 @@ int test_input (int i, char *recvbuf, size_t nbytes) {
} }
if (name.length) { if (name.length) {
pname->ptr = malloc(name.length); pname->data = malloc(name.length);
pname->length = name.length; pname->length = name.length;
assert(pname->ptr != NULL); assert(pname->data != NULL);
memcpy((void*)pname->ptr, name.ptr, name.length); memcpy((void*)pname->data, name.data, name.length);
name.length = 0; name.length = 0;
if (verbose >= 2) if (verbose >= 2)
printf("%.*s = ", (int)pname->length, pname->ptr); printf("%.*s = ", (int)pname->length, pname->data);
} }
if (value.length) { if (value.length) {
@ -239,15 +239,15 @@ int test_input (int i, char *recvbuf, size_t nbytes) {
len = psyc_parse_value_length(parser); len = psyc_parse_value_length(parser);
else else
len = value.length; len = value.length;
pvalue->ptr = malloc(len); pvalue->data = malloc(len);
} }
assert(pvalue->ptr != NULL); assert(pvalue->data != NULL);
memcpy((void*)pvalue->ptr + pvalue->length, value.ptr, value.length); memcpy((void*)pvalue->data + pvalue->length, value.data, value.length);
pvalue->length += value.length; pvalue->length += value.length;
value.length = 0; value.length = 0;
if (verbose >= 2) { if (verbose >= 2) {
printf("[%.*s]", (int)pvalue->length, pvalue->ptr); printf("[%.*s]", (int)pvalue->length, pvalue->data);
if (parser->valueLength > pvalue->length) if (parser->valueLength > pvalue->length)
printf("..."); printf("...");
printf("\n"); printf("\n");
@ -284,7 +284,7 @@ int test_input (int i, char *recvbuf, size_t nbytes) {
retl = 0; retl = 0;
case PSYC_PARSE_LIST_ELEM: case PSYC_PARSE_LIST_ELEM:
if (verbose >= 2) { if (verbose >= 2) {
printf("|%.*s\n", (int)elem.length, elem.ptr); printf("|%.*s\n", (int)elem.length, elem.data);
if (ret == PSYC_PARSE_LIST_END) if (ret == PSYC_PARSE_LIST_END)
printf("## LIST END"); printf("## LIST END");
} }
@ -311,9 +311,9 @@ static inline
void resetString (PsycString *s, uint8_t freeptr) void resetString (PsycString *s, uint8_t freeptr)
{ {
if (freeptr && s->length) if (freeptr && s->length)
free((void*)s->ptr); free((void*)s->data);
s->ptr = NULL; s->data = NULL;
s->length = 0; s->length = 0;
} }

View file

@ -11,7 +11,7 @@ PsycTextValueRC getValueFooBar (const char *name, size_t len, PsycString *value,
{ {
if (verbose) if (verbose)
printf("> getValue: %.*s\n", (int)len, name); printf("> getValue: %.*s\n", (int)len, name);
value->ptr = "Foo Bar"; value->data = "Foo Bar";
value->length = 7; value->length = 7;
return PSYC_TEXT_VALUE_FOUND; return PSYC_TEXT_VALUE_FOUND;
} }
@ -20,7 +20,7 @@ PsycTextValueRC getValueEmpty (const char *name, size_t len, PsycString *value,
{ {
if (verbose) if (verbose)
printf("> getValue: %.*s\n", (int)len, name); printf("> getValue: %.*s\n", (int)len, name);
value->ptr = ""; value->data = "";
value->length = 0; value->length = 0;
return PSYC_TEXT_VALUE_FOUND; return PSYC_TEXT_VALUE_FOUND;
} }
@ -54,7 +54,7 @@ int testText (char *template, size_t tmplen, char *buffer, size_t buflen, PsycSt
if (verbose) if (verbose)
printf("%.*s\n", (int)length, buffer); printf("%.*s\n", (int)length, buffer);
result->length = length; result->length = length;
result->ptr = buffer; result->data = buffer;
return ret; return ret;
case PSYC_TEXT_NO_SUBST: case PSYC_TEXT_NO_SUBST:
if (verbose) if (verbose)
@ -78,11 +78,11 @@ int main(int argc, char **argv)
int i; int i;
testText(str, len, buffer, BUFSIZE, &result, &getValueFooBar); testText(str, len, buffer, BUFSIZE, &result, &getValueFooBar);
if (memcmp(result.ptr, PSYC_C2ARG("Hello Foo Bar & Foo Bar!"))) if (memcmp(result.data, PSYC_C2ARG("Hello Foo Bar & Foo Bar!")))
return 1; return 1;
testText(str, len, buffer, BUFSIZE, &result, &getValueEmpty); testText(str, len, buffer, BUFSIZE, &result, &getValueEmpty);
if (memcmp(result.ptr, PSYC_C2ARG("Hello & !"))) if (memcmp(result.data, PSYC_C2ARG("Hello & !")))
return 2; return 2;
if (testText(str, len, buffer, BUFSIZE, &result, &getValueNotFound) != PSYC_TEXT_NO_SUBST) if (testText(str, len, buffer, BUFSIZE, &result, &getValueNotFound) != PSYC_TEXT_NO_SUBST)
@ -91,7 +91,7 @@ int main(int argc, char **argv)
for (i = 1; i < 22; i++) for (i = 1; i < 22; i++)
{ {
testText(str, len, buffer, i, &result, &getValueFooBar); testText(str, len, buffer, i, &result, &getValueFooBar);
if (memcmp(result.ptr, PSYC_C2ARG("Hello Foo Bar & Foo Bar!"))) if (memcmp(result.data, PSYC_C2ARG("Hello Foo Bar & Foo Bar!")))
return 10 + i; return 10 + i;
} }