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

getVarType

This commit is contained in:
Gabor Adam Toth 2011-04-29 22:58:19 +02:00
parent e72d576528
commit ea3e40d575
5 changed files with 112 additions and 28 deletions

1
.gitignore vendored
View file

@ -7,6 +7,7 @@ test/testParser
test/testRender
test/testServer
test/isRoutingVar
test/getVarType
.config
~$*

View file

@ -107,6 +107,12 @@ typedef struct
const char *ptr;
} psycString;
typedef struct
{
psycString name;
int value;
} psycMatchVar;
/**
* Shortcut for creating a psycString.
*
@ -204,8 +210,7 @@ inline psycPacket psyc_newPacket2(psycModifier *routing, size_t routinglen,
/// Routing vars in alphabetical order.
extern const psycString PSYC_routingVars[];
/// Number of routing vars.
extern const size_t PSYC_routingVarsNum;
extern const psycMatchVar PSYC_varTypes[];
/**
* Get the type of variable name.
@ -215,7 +220,7 @@ psycBool psyc_isRoutingVar(const char *name, size_t len);
/**
* Get the type of variable name.
*/
psycType psyc_getVarType(char *name, size_t len);
psycType psyc_getVarType(const char *name, size_t len);
/**
* Checks if long keyword string inherits from short keyword string.
@ -238,8 +243,8 @@ int psyc_matches(char *sho, size_t slen,
* number of bytes written. 0 is a legal return value. Should the
* callback return -1, psyc_text leaves the original template text as is.
*/
typedef int (*psyctextCB)(uint8_t *match, size_t mlen,
uint8_t **buffer, size_t *blen);
typedef int (*psyctextCB)(char *match, size_t mlen,
char **buffer, size_t *blen);
/**
* Fills out text templates by asking a callback for content.
@ -254,8 +259,8 @@ typedef int (*psyctextCB)(uint8_t *match, size_t mlen,
*
* See also http://about.psyc.eu/psyctext
*/
int psyc_text(uint8_t *template, size_t tlen,
uint8_t **buffer, size_t *blen,
int psyc_text(char *template, size_t tlen,
char **buffer, size_t *blen,
psyctextCB lookupValue,
char *braceOpen, char *braceClose);

View file

@ -1,62 +1,83 @@
#include <psyc.h>
#include <psyc/lib.h>
#include <stdint.h>
/// Routing variables in alphabetical order.
const psycString PSYC_routingVars[] =
const psycString psyc_routingVars[] =
{
PSYC_C2STR("_amount_fragments"),
PSYC_C2STR("_context"),
//PSYC_C2STR("_count"), // older PSYC
PSYC_C2STR("_counter"), // the name for this is supposed to be _count, not _counter
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"),
PSYC_C2STR("_source_identification"),
//PSYC_C2STR("_source_identification"), // older PSYC
PSYC_C2STR("_source_identity"),
PSYC_C2STR("_source_relay"),
PSYC_C2STR("_source_relay_relay"), // until you have a better idea.. is this really in use?
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"),
//PSYC_C2STR(19, "_understand_modules"), // older PSYC
//PSYC_C2STR(14, "_using_modules"), // older PSYC
//PSYC_C2STR("_understand_modules"), // older PSYC
//PSYC_C2STR("_using_modules"), // older PSYC
};
const size_t PSYC_routingVarsNum = sizeof(PSYC_routingVars) / sizeof(*PSYC_routingVars);
const psycMatchVar psyc_varTypes[] =
{
{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},
};
const size_t psyc_routingVarsNum = PSYC_NUM_ELEM(psyc_routingVars);
const size_t psyc_varTypesNum = PSYC_NUM_ELEM(psyc_varTypes);
/**
* Get the type of variable name.
*/
psycBool psyc_isRoutingVar(const char *name, size_t len)
{
//return psyc_matchArray(psyc_routingVars, PSYC_NUM_ELEM(psyc_routingVars), name, len, 0);
size_t cursor = 1;
int8_t matching[PSYC_routingVarsNum]; // indexes of matching vars
memset(&matching, -1, sizeof(matching));
uint8_t i, m = 0;
int8_t matching[psyc_routingVarsNum]; // indexes of matching vars
if (len < 2 || name[0] != '_')
return PSYC_FALSE;
// first find the vars with matching length
for (i=0; i<PSYC_routingVarsNum; i++)
if (len == PSYC_routingVars[i].length)
for (i=0; i<psyc_routingVarsNum; i++)
if (len == psyc_routingVars[i].length)
matching[m++] = i;
matching[m] = -1; // mark the end of matching indexes
while (cursor < len && matching[0] >= 0)
{
for (i = m = 0; i < PSYC_routingVarsNum; i++)
for (i = m = 0; i < psyc_routingVarsNum; i++)
{
if (matching[i] < 0)
break;
if (PSYC_routingVars[matching[i]].ptr[cursor] == name[cursor])
break; // reached the end of possible matches
if (psyc_routingVars[matching[i]].ptr[cursor] == name[cursor])
matching[m++] = matching[i]; // found a match, update matching indexes
else if (PSYC_routingVars[matching[i]].ptr[cursor] > name[cursor])
break; // passed the possible matches in alphabetical order
else if (psyc_routingVars[matching[i]].ptr[cursor] > name[cursor])
break; // passed the possible matches in alphabetical order in the array
}
if (m < PSYC_routingVarsNum)
if (m < psyc_routingVarsNum)
matching[m] = -1; // mark the end of matching indexes
cursor++;
@ -68,7 +89,43 @@ psycBool psyc_isRoutingVar(const char *name, size_t len)
/**
* Get the type of variable name.
*/
psycType psyc_getVarType(char *name, size_t len)
psycType psyc_getVarType(const char *name, size_t len)
{
return PSYC_TYPE_UNKNOWN;
//return psyc_matchArray(psyc_varTypes, PSYC_NUM_ELEM(psyc_varTypes), name, len, 1);
size_t cursor = 1;
uint8_t i, m = 0;
int8_t matching[psyc_varTypesNum]; // indexes of matching vars
if (len < 2 || name[0] != '_')
return 0;
// first find the vars with matching length
for (i=0; i<psyc_varTypesNum; i++)
if (len == psyc_varTypes[i].name.length || (len > psyc_varTypes[i].name.length && name[psyc_varTypes[i].name.length] == '_'))
matching[m++] = i;
matching[m] = -1; // mark the end of matching indexes
while (cursor < len && matching[0] >= 0)
{
for (i = m = 0; i < psyc_varTypesNum; i++)
{
if (matching[i] < 0)
break; // reached the end of possible matches
if (cursor < psyc_varTypes[matching[i]].name.length && psyc_varTypes[matching[i]].name.ptr[cursor] == name[cursor])
matching[m++] = matching[i]; // found a match, update matching indexes
else if (cursor == psyc_varTypes[matching[i]].name.length && name[cursor] == '_')
return psyc_varTypes[matching[0]].value; // _ after the end of a matching prefix
else if (psyc_varTypes[matching[i]].name.ptr[cursor] > name[cursor])
break; // passed the possible matches in alphabetical order in the array
}
if (m < psyc_varTypesNum)
matching[m] = -1; // mark the end of matching indexes
cursor++;
}
// return first match if found
return matching[0] >= 0 ? psyc_varTypes[matching[0]].value : 0;
}

View file

@ -1,7 +1,7 @@
CFLAGS=-I../include -DDEBUG -g -O0 -Wall
LDFLAGS=-L../src
LOADLIBES=-lpsyc -lm
TARGETS=testServer testParser testMatch testRender isRoutingVar
TARGETS=testServer testParser testMatch testRender isRoutingVar getVarType
PORT=4440
all: $(TARGETS)

21
test/getVarType.c Normal file
View file

@ -0,0 +1,21 @@
#include <psyc.h>
#include <unistd.h>
#include <stdio.h>
#include <fcntl.h>
#include "../include/psyc/lib.h"
int main() {
unless (psyc_getVarType(PSYC_C2ARG("_list"))) return 1;
unless (psyc_getVarType(PSYC_C2ARG("_list_foo"))) return 2;
unless (psyc_getVarType(PSYC_C2ARG("_color_red"))) return 3;
if (psyc_getVarType(PSYC_C2ARG("_last"))) return 4;
if (psyc_getVarType(PSYC_C2ARG("_lost_foo"))) return 5;
if (psyc_getVarType(PSYC_C2ARG("_colorful"))) return 6;
if (psyc_getVarType(PSYC_C2ARG("_foo"))) return 7;
if (psyc_getVarType(PSYC_C2ARG("bar"))) return 8;
if (psyc_getVarType(PSYC_C2ARG("______"))) return 9;
if (psyc_getVarType(PSYC_C2ARG("_"))) return 10;
puts("psyc_getVarType passed all tests.");
return 0; // passed all tests
}