mirror of
git://git.psyc.eu/libpsyc
synced 2024-08-15 03:19:02 +00:00
number, date & time parsing functions; psycString variant for isRoutingVar & getVarType
This commit is contained in:
parent
52c9159b2e
commit
653fbcc156
5 changed files with 113 additions and 33 deletions
|
@ -18,6 +18,7 @@
|
|||
|
||||
#include <stdint.h>
|
||||
#include <string.h>
|
||||
#include <sys/types.h>
|
||||
|
||||
#define PSYC_EPOCH 1440444041 // 2015-08-24 21:20:41 CET (Monday)
|
||||
|
||||
|
@ -124,24 +125,22 @@ extern const psycString PSYC_routingVars[];
|
|||
extern const psycMatchVar PSYC_varTypes[];
|
||||
|
||||
/**
|
||||
* Get the type of variable name.
|
||||
* Is this a routing variable name?
|
||||
*/
|
||||
psycBool psyc_isRoutingVar(const char *name, size_t len);
|
||||
psycBool psyc_isRoutingVar(psycString *name);
|
||||
/**
|
||||
* Is this a routing variable name?
|
||||
*/
|
||||
psycBool psyc_isRoutingVar2(const char *name, size_t len);
|
||||
|
||||
/**
|
||||
* Get the type of variable name.
|
||||
*/
|
||||
psycType psyc_getVarType(const char *name, size_t len);
|
||||
|
||||
psycType psyc_getVarType(psycString *name);
|
||||
/**
|
||||
* Is this a list variable name?
|
||||
* Get the type of variable name.
|
||||
*/
|
||||
static inline
|
||||
psycBool psyc_isListVar(psycString *name)
|
||||
{
|
||||
return name->length < 5 || memcmp(name->ptr, "_list", 5) != 0 ||
|
||||
(name->length > 5 && name->ptr[5] != '_') ? PSYC_FALSE : PSYC_TRUE;
|
||||
}
|
||||
psycType psyc_getVarType2(const char *name, size_t len);
|
||||
|
||||
/**
|
||||
* Is this a list variable name?
|
||||
|
@ -149,7 +148,17 @@ psycBool psyc_isListVar(psycString *name)
|
|||
static inline
|
||||
psycBool psyc_isListVar2(const char *name, size_t len)
|
||||
{
|
||||
return psyc_isListVar(&(psycString){len, name});
|
||||
return len < 5 || memcmp(name, "_list", 5) != 0 ||
|
||||
(len > 5 && name[5] != '_') ? PSYC_FALSE : PSYC_TRUE;
|
||||
}
|
||||
|
||||
/**
|
||||
* Is this a list variable name?
|
||||
*/
|
||||
static inline
|
||||
psycBool psyc_isListVar(psycString *name)
|
||||
{
|
||||
return psyc_isListVar2(name->ptr, name->length);
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
|
@ -451,6 +451,65 @@ static inline
|
|||
#endif
|
||||
psycParseListRC psyc_parseList (psycParseListState *state, psycString *elem);
|
||||
|
||||
static inline
|
||||
psycBool psyc_parseNumber2 (const char *value, size_t len, ssize_t *n)
|
||||
{
|
||||
size_t c = 0;
|
||||
uint8_t neg = 0;
|
||||
|
||||
if (!value)
|
||||
return PSYC_FALSE;
|
||||
|
||||
if (value[0] == '-')
|
||||
neg = ++c;
|
||||
|
||||
*n = 0;
|
||||
while (c < len && value[c] >= '0' && value[c] <= '9')
|
||||
*n = 10 * *n + (value[c++] - '0');
|
||||
|
||||
if (c != len)
|
||||
return PSYC_FALSE;
|
||||
|
||||
if (neg)
|
||||
*n = 0 - *n;
|
||||
|
||||
return PSYC_TRUE;
|
||||
}
|
||||
|
||||
static inline
|
||||
psycBool psyc_parseNumber (psycString *value, ssize_t *n)
|
||||
{
|
||||
return psyc_parseNumber2(value->ptr, value->length, n);
|
||||
}
|
||||
|
||||
static inline
|
||||
psycBool psyc_parseTime2 (const char *value, size_t len, time_t *t)
|
||||
{
|
||||
return psyc_parseNumber2(value, len, t);
|
||||
}
|
||||
|
||||
static inline
|
||||
psycBool psyc_parseTime (psycString *value, time_t *t)
|
||||
{
|
||||
return psyc_parseNumber2(value->ptr, value->length, t);
|
||||
}
|
||||
|
||||
static inline
|
||||
psycBool psyc_parseDate2 (const char *value, size_t len, time_t *t)
|
||||
{
|
||||
if (psyc_parseNumber2(value, len, t)) {
|
||||
*t += PSYC_EPOCH;
|
||||
return PSYC_TRUE;
|
||||
}
|
||||
return PSYC_FALSE;
|
||||
}
|
||||
|
||||
static inline
|
||||
psycBool psyc_parseDate (psycString *value, time_t *t)
|
||||
{
|
||||
return psyc_parseDate2(value->ptr, value->length, t);
|
||||
}
|
||||
|
||||
/** @} */ // end of parse group
|
||||
|
||||
#define PSYC_PARSE_H
|
||||
|
|
|
@ -48,7 +48,8 @@ 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)
|
||||
inline
|
||||
psycBool psyc_isRoutingVar2(const char *name, size_t len)
|
||||
{
|
||||
//return psyc_matchArray(psyc_routingVars, PSYC_NUM_ELEM(psyc_routingVars), name, len, 0);
|
||||
size_t cursor = 1;
|
||||
|
@ -86,10 +87,16 @@ psycBool psyc_isRoutingVar(const char *name, size_t len)
|
|||
return matching[0] >= 0 ? PSYC_TRUE : PSYC_FALSE;
|
||||
}
|
||||
|
||||
psycBool psyc_isRoutingVar(psycString *name)
|
||||
{
|
||||
return psyc_isRoutingVar2(name->ptr, name->length);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the type of variable name.
|
||||
*/
|
||||
psycType psyc_getVarType(const char *name, size_t len)
|
||||
inline
|
||||
psycType psyc_getVarType2(const char *name, size_t len)
|
||||
{
|
||||
//return psyc_matchArray(psyc_varTypes, PSYC_NUM_ELEM(psyc_varTypes), name, len, 1);
|
||||
size_t cursor = 1;
|
||||
|
@ -129,3 +136,8 @@ psycType psyc_getVarType(const char *name, size_t len)
|
|||
// return first match if found
|
||||
return matching[0] >= 0 ? psyc_varTypes[matching[0]].value : 0;
|
||||
}
|
||||
|
||||
psycType psyc_getVarType(psycString *name)
|
||||
{
|
||||
return psyc_getVarType2(name->ptr, name->length);
|
||||
}
|
||||
|
|
|
@ -5,16 +5,16 @@
|
|||
#include <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;
|
||||
unless (psyc_getVarType2(PSYC_C2ARG("_list"))) return 1;
|
||||
unless (psyc_getVarType2(PSYC_C2ARG("_list_foo"))) return 2;
|
||||
unless (psyc_getVarType2(PSYC_C2ARG("_color_red"))) return 3;
|
||||
if (psyc_getVarType2(PSYC_C2ARG("_last"))) return 4;
|
||||
if (psyc_getVarType2(PSYC_C2ARG("_lost_foo"))) return 5;
|
||||
if (psyc_getVarType2(PSYC_C2ARG("_colorful"))) return 6;
|
||||
if (psyc_getVarType2(PSYC_C2ARG("_foo"))) return 7;
|
||||
if (psyc_getVarType2(PSYC_C2ARG("bar"))) return 8;
|
||||
if (psyc_getVarType2(PSYC_C2ARG("______"))) return 9;
|
||||
if (psyc_getVarType2(PSYC_C2ARG("_"))) return 10;
|
||||
|
||||
puts("psyc_getVarType passed all tests.");
|
||||
return 0; // passed all tests
|
||||
|
|
|
@ -21,17 +21,17 @@ int main() {
|
|||
for (i = 0; i < sizeof(vars) / sizeof(*vars); i++)
|
||||
{
|
||||
printf(">> %s: %d %d\n", vars[i], sizeof(vars[i]), sizeof(*vars[i]));
|
||||
printf("%s: %d\n", vars[i], psyc_isRoutingVar(vars[i], strlen(vars[i])));
|
||||
printf("%s: %d\n", vars[i], psyc_isRoutingVar2(vars[i], strlen(vars[i])));
|
||||
}
|
||||
#else
|
||||
unless (psyc_isRoutingVar(PSYC_C2ARG("_source"))) return 1;
|
||||
unless (psyc_isRoutingVar(PSYC_C2ARG("_source_relay"))) return 2;
|
||||
if (psyc_isRoutingVar(PSYC_C2ARG("_source_foo"))) return 3;
|
||||
if (psyc_isRoutingVar(PSYC_C2ARG("_sourcherry"))) return 4;
|
||||
if (psyc_isRoutingVar(PSYC_C2ARG("_sour"))) return 5;
|
||||
if (psyc_isRoutingVar(PSYC_C2ARG("_foo"))) return 6;
|
||||
if (psyc_isRoutingVar(PSYC_C2ARG("bar"))) return 7;
|
||||
if (psyc_isRoutingVar(PSYC_C2ARG("_"))) return 8;
|
||||
unless (psyc_isRoutingVar2(PSYC_C2ARG("_source"))) return 1;
|
||||
unless (psyc_isRoutingVar2(PSYC_C2ARG("_source_relay"))) return 2;
|
||||
if (psyc_isRoutingVar2(PSYC_C2ARG("_source_foo"))) return 3;
|
||||
if (psyc_isRoutingVar2(PSYC_C2ARG("_sourcherry"))) return 4;
|
||||
if (psyc_isRoutingVar2(PSYC_C2ARG("_sour"))) return 5;
|
||||
if (psyc_isRoutingVar2(PSYC_C2ARG("_foo"))) return 6;
|
||||
if (psyc_isRoutingVar2(PSYC_C2ARG("bar"))) return 7;
|
||||
if (psyc_isRoutingVar2(PSYC_C2ARG("_"))) return 8;
|
||||
|
||||
puts("psyc_isRoutingVar passed all tests.");
|
||||
#endif
|
||||
|
|
Loading…
Reference in a new issue