version 1.12, 2012/08/01 00:37:08
|
version 1.13.2.6, 2012/09/17 14:05:43
|
Line 653 io_findKeyVars(array_t * __restrict vars, u_short key)
|
Line 653 io_findKeyVars(array_t * __restrict vars, u_short key)
|
io_arrayDestroy(&tmp); |
io_arrayDestroy(&tmp); |
return v; |
return v; |
} |
} |
|
|
|
/* |
|
* io_hashVar() - Generate hash key for variable from string or value |
|
* |
|
* @v = variable |
|
* @key = key string for hash, if =NULL hash will built from variable |
|
* return: hash key |
|
*/ |
|
u_short |
|
io_hashVar(ait_val_t * __restrict v, const char * __restrict key) |
|
{ |
|
void *p; |
|
u_short cksum; |
|
int l; |
|
|
|
if (!v) |
|
return 0; |
|
|
|
if (key) { |
|
p = (void*) key; |
|
l = (strlen(key) + 1) / 2; |
|
} else { |
|
switch (AIT_TYPE(v)) { |
|
case empty: |
|
AIT_KEY(v) = 0; |
|
return 0; |
|
case string: |
|
case buffer: |
|
p = AIT_ADDR(v); |
|
l = AIT_LEN(v) / 2; |
|
break; |
|
case data: |
|
p = v->val_data; |
|
l = AIT_LEN(v) / 2; |
|
break; |
|
default: |
|
p = &AIT_RAW(v); |
|
l = sizeof AIT_RAW(v) / 2; |
|
break; |
|
} |
|
} |
|
|
|
cksum = crcFletcher16((u_short*) p, l); |
|
|
|
if (AIT_BE(v)) |
|
AIT_KEY(v) = htobe16(cksum); |
|
else if (AIT_LE(v)) |
|
AIT_KEY(v) = htole16(cksum); |
|
else |
|
AIT_KEY(v) = cksum; |
|
|
|
return AIT_KEY(v); |
|
} |
|
|
|
/* |
|
* io_hashKeyVars() - Generate hash keys for variables |
|
* |
|
* @vars = Variables |
|
* return -1 error or 0 ok |
|
*/ |
|
inline int |
|
io_hashKeyVars(array_t * __restrict vars) |
|
{ |
|
register int i; |
|
|
|
if (!vars) |
|
return -1; |
|
|
|
for (i = 0; i < io_arraySize(vars); i++) |
|
io_hashVar(io_array(vars, i, ait_val_t*), NULL); |
|
|
|
return 0; |
|
} |
|
|
|
/* |
|
* io_findKeyHash() - Find variable by hash string from array |
|
* |
|
* @vars = Variables |
|
* @key = Search string |
|
* return: NULL error or not found, !=NULL valid element |
|
*/ |
|
inline ait_val_t * |
|
io_findKeyHash(array_t * __restrict vars, const char * __restrict key) |
|
{ |
|
u_short k = 0; |
|
|
|
if (!vars || !key) |
|
return NULL; |
|
|
|
k = crcFletcher16((u_short*) key, (strlen(key) + 1) / 2); |
|
return io_findKeyVars(vars, k); |
|
} |
|
|
|
/* |
|
* io_sprintfVar() - Builtin string variable from formatted input |
|
* |
|
* @v = variable |
|
* @fmt = format string |
|
* @... = argument(s) |
|
* return: -1 error or >0 copied bytes to variable |
|
*/ |
|
int |
|
io_sprintfVar(ait_val_t * __restrict v, const char *fmt, ...) |
|
{ |
|
int ret = 0; |
|
va_list lst; |
|
char *str = NULL; |
|
|
|
if (!v || !fmt) |
|
return -1; |
|
|
|
va_start(lst, fmt); |
|
ret = vasprintf(&str, fmt, lst); |
|
va_end(lst); |
|
|
|
if (str && ret > -1) { |
|
AIT_FREE_VAL(v); |
|
AIT_SET_STR(v, str); |
|
} else |
|
LOGERR; |
|
|
|
if (str) |
|
free(str); |
|
return ret; |
|
} |
|
|
|
/* |
|
* io_setlikeVar() - Set variable like ... |
|
* |
|
* @v = variable |
|
* @t = type of data |
|
* @l = length of data |
|
* @... = data |
|
* return: -1 error or 0 ok |
|
*/ |
|
inline int |
|
io_setlikeVar(ait_val_t * __restrict v, ait_type_t t, u_int l, ...) |
|
{ |
|
va_list lst; |
|
|
|
if (!v) |
|
return -1; |
|
|
|
AIT_FREE_VAL(v); |
|
AIT_INIT_VAL2(v, t); |
|
AIT_LEN(v) = l; |
|
AIT_IN(v) = 1; |
|
|
|
va_start(lst, l); |
|
switch (AIT_TYPE(v)) { |
|
case ptr: |
|
case buffer: |
|
case string: |
|
AIT_ADDR(v) = va_arg(lst, void*); |
|
break; |
|
default: |
|
AIT_RAW(v) = va_arg(lst, uint64_t); |
|
break; |
|
} |
|
va_end(lst); |
|
|
|
return 0; |
|
} |
|
|
|
/* |
|
* io_cmpVar() - Compare two variables |
|
* |
|
* @a = 1st variable |
|
* @b = 2nd variable |
|
* return: 0 is equal or !=0 is different |
|
*/ |
|
inline int |
|
io_cmpVar(ait_val_t * __restrict a, ait_val_t * __restrict b) |
|
{ |
|
intptr_t ret; |
|
|
|
if (!(ret = (a - b))) |
|
return ret; |
|
if ((ret = AIT_TYPE(a) - AIT_TYPE(b))) |
|
return ret; |
|
if ((ret = AIT_LEN(a) - AIT_LEN(b))) |
|
return ret; |
|
|
|
switch (AIT_TYPE(a)) { |
|
case buffer: |
|
ret = memcmp(AIT_GET_BUF(a), AIT_GET_BUF(b), AIT_LEN(a)); |
|
break; |
|
case string: |
|
ret = strncmp(AIT_GET_STR(a), AIT_GET_STR(b), AIT_LEN(a)); |
|
break; |
|
case data: |
|
ret = memcmp(AIT_GET_DATA(a), AIT_GET_DATA(b), AIT_LEN(a)); |
|
break; |
|
case ptr: |
|
ret = AIT_ADDR(a) - AIT_ADDR(b); |
|
break; |
|
default: |
|
ret = AIT_RAW(a) - AIT_RAW(b); |
|
break; |
|
} |
|
|
|
return (int) ret; |
|
} |