libpsyc/src/variable.c

100 lines
2.9 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:04:16 +00:00
const psycString psyc_routing_vars[] =
2011-04-22 15:09:32 +00:00
{
PSYC_C2STR("_amount_fragments"),
PSYC_C2STR("_context"),
//PSYC_C2STR("_count"), // older PSYC
2011-04-29 20:58:19 +00:00
PSYC_C2STR("_counter"), // the name for this is supposed to be _count, not _counter
PSYC_C2STR("_fragment"),
//PSYC_C2STR("_length"), // older PSYC
PSYC_C2STR("_source"),
2011-04-29 20:58:19 +00:00
//PSYC_C2STR("_source_identification"), // older PSYC
PSYC_C2STR("_source_identity"),
PSYC_C2STR("_source_relay"),
2011-04-29 20:58:19 +00:00
PSYC_C2STR("_source_relay_relay"), // until you have a better idea.. is this really in use?
PSYC_C2STR("_tag"),
PSYC_C2STR("_tag_relay"),
//PSYC_C2STR("_tag_reply"), // older PSYC
PSYC_C2STR("_target"),
PSYC_C2STR("_target_forward"),
PSYC_C2STR("_target_relay"),
2011-04-29 20:58:19 +00:00
//PSYC_C2STR("_understand_modules"), // older PSYC
//PSYC_C2STR("_using_modules"), // older PSYC
};
// Variable types in alphabetical order.
2011-10-31 19:04:16 +00:00
const psycMatchVar psyc_var_types[] =
2011-04-29 20:58:19 +00:00
{
{PSYC_C2STR("_amount"), PSYC_TYPE_AMOUNT},
{PSYC_C2STR("_color"), PSYC_TYPE_COLOR},
{PSYC_C2STR("_date"), PSYC_TYPE_DATE},
{PSYC_C2STR("_degree"), PSYC_TYPE_DEGREE},
{PSYC_C2STR("_entity"), PSYC_TYPE_ENTITY},
{PSYC_C2STR("_flag"), PSYC_TYPE_FLAG},
{PSYC_C2STR("_language"), PSYC_TYPE_LANGUAGE},
{PSYC_C2STR("_list"), PSYC_TYPE_LIST},
{PSYC_C2STR("_nick"), PSYC_TYPE_NICK},
{PSYC_C2STR("_page"), PSYC_TYPE_PAGE},
{PSYC_C2STR("_uniform"), PSYC_TYPE_UNIFORM},
{PSYC_C2STR("_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:04:16 +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-10-31 19:04:16 +00:00
if (psyc_routing_vars[matching[i]].ptr[cursor] == name[cursor])
2011-04-22 15:09:32 +00:00
matching[m++] = matching[i]; // found a match, update matching indexes
2011-10-31 19:04:16 +00:00
else if (psyc_routing_vars[matching[i]].ptr[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:04:16 +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];
return psyc_in_array(psyc_var_types, psyc_var_types_num,
name, len, PSYC_YES, (int8_t*)&m);
2011-10-30 15:04:54 +00:00
}