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

100 lines
3 KiB
C
Raw Normal View History

2011-05-08 21:40:26 +00:00
#include "lib.h"
2011-04-22 15:09:32 +00:00
#include <stdint.h>
2011-04-29 20:58:19 +00:00
/// Routing variables in alphabetical order.
2011-10-31 19:26:47 +00:00
const PsycString psyc_routing_vars[] =
2011-04-22 15:09:32 +00:00
{
2011-11-03 13:44:04 +00:00
PSYC_C2STRI("_amount_fragments"),
PSYC_C2STRI("_context"),
//PSYC_C2STRI("_count"), // older PSYC
PSYC_C2STRI("_counter"), // the name for this is supposed to be _count, not _counter
PSYC_C2STRI("_fragment"),
//PSYC_C2STRI("_length"), // older PSYC
PSYC_C2STRI("_source"),
//PSYC_C2STRI("_source_identification"), // older PSYC
PSYC_C2STRI("_source_identity"),
PSYC_C2STRI("_source_relay"),
PSYC_C2STRI("_source_relay_relay"), // until you have a better idea.. is this really in use?
PSYC_C2STRI("_tag"),
PSYC_C2STRI("_tag_relay"),
//PSYC_C2STRI("_tag_reply"), // older PSYC
PSYC_C2STRI("_target"),
PSYC_C2STRI("_target_forward"),
PSYC_C2STRI("_target_relay"),
//PSYC_C2STRI("_understand_modules"), // older PSYC
//PSYC_C2STRI("_using_modules"), // older PSYC
2011-04-29 20:58:19 +00:00
};
// Variable types in alphabetical order.
2011-11-01 11:06:58 +00:00
const PsycDictInt psyc_var_types[] =
2011-04-29 20:58:19 +00:00
{
2011-11-03 13:44:04 +00:00
{PSYC_C2STRI("_amount"), PSYC_TYPE_AMOUNT},
{PSYC_C2STRI("_color"), PSYC_TYPE_COLOR},
{PSYC_C2STRI("_date"), PSYC_TYPE_DATE},
{PSYC_C2STRI("_degree"), PSYC_TYPE_DEGREE},
{PSYC_C2STRI("_entity"), PSYC_TYPE_ENTITY},
{PSYC_C2STRI("_flag"), PSYC_TYPE_FLAG},
{PSYC_C2STRI("_language"), PSYC_TYPE_LANGUAGE},
{PSYC_C2STRI("_list"), PSYC_TYPE_LIST},
{PSYC_C2STRI("_nick"), PSYC_TYPE_NICK},
{PSYC_C2STRI("_page"), PSYC_TYPE_PAGE},
{PSYC_C2STRI("_uniform"), PSYC_TYPE_UNIFORM},
{PSYC_C2STRI("_time"), PSYC_TYPE_TIME},
2011-04-22 15:09:32 +00:00
};
2011-10-31 19:04:16 +00:00
const size_t psyc_routing_vars_num = PSYC_NUM_ELEM(psyc_routing_vars);
const size_t psyc_var_types_num = PSYC_NUM_ELEM(psyc_var_types);
2011-04-22 15:09:32 +00:00
/**
* Get the type of variable name.
*/
inline
2011-10-31 19:26:47 +00:00
PsycBool psyc_var_is_routing (const char *name, size_t len)
2011-04-22 15:09:32 +00:00
{
size_t cursor = 1;
uint8_t i, m = 0;
2011-10-31 19:04:16 +00:00
int8_t matching[psyc_routing_vars_num]; // indexes of matching vars
2011-04-22 15:09:32 +00:00
if (len < 2 || name[0] != '_')
return PSYC_FALSE;
// first find the vars with matching length
2011-10-31 19:04:16 +00:00
for (i=0; i<psyc_routing_vars_num; i++)
if (len == psyc_routing_vars[i].length)
2011-04-22 15:09:32 +00:00
matching[m++] = i;
2011-04-29 20:58:19 +00:00
matching[m] = -1; // mark the end of matching indexes
2011-04-22 15:09:32 +00:00
while (cursor < len && matching[0] >= 0)
{
2011-10-31 19:04:16 +00:00
for (i = m = 0; i < psyc_routing_vars_num; i++)
2011-04-22 15:09:32 +00:00
{
if (matching[i] < 0)
2011-04-29 20:58:19 +00:00
break; // reached the end of possible matches
2011-11-01 11:06:58 +00:00
if (psyc_routing_vars[matching[i]].data[cursor] == name[cursor])
2011-04-22 15:09:32 +00:00
matching[m++] = matching[i]; // found a match, update matching indexes
2011-11-01 11:06:58 +00:00
else if (psyc_routing_vars[matching[i]].data[cursor] > name[cursor])
2011-04-29 20:58:19 +00:00
break; // passed the possible matches in alphabetical order in the array
2011-04-22 15:09:32 +00:00
}
2011-10-31 19:04:16 +00:00
if (m < psyc_routing_vars_num)
2011-04-22 15:09:32 +00:00
matching[m] = -1; // mark the end of matching indexes
cursor++;
}
return matching[0] >= 0 ? PSYC_TRUE : PSYC_FALSE;
}
/**
* Get the type of variable name.
*/
inline
2011-10-31 19:26:47 +00:00
PsycType psyc_var_type (const char *name, size_t len)
2011-04-22 15:09:32 +00:00
{
2011-10-31 19:04:16 +00:00
int8_t m[psyc_var_types_num];
2011-11-01 11:06:58 +00:00
return (PsycType) psyc_dict_lookup((PsycDict*)psyc_var_types, psyc_var_types_num,
name, len, PSYC_YES, (int8_t*)&m);
2011-10-30 15:04:54 +00:00
}