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
2324f8e6b4
commit
d40fa8c398
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
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue