version 1.11, 2012/07/22 20:39:45
|
version 1.13.2.6, 2012/09/17 14:05:43
|
Line 463 io_freeVar(ait_val_t ** __restrict val)
|
Line 463 io_freeVar(ait_val_t ** __restrict val)
|
} |
} |
} |
} |
|
|
|
/* |
|
* io_makeVar() - Allocate memory and fill variable |
|
* |
|
* @type = type of variable |
|
* @... = arg1 is value of variable |
|
* @... = arg2 is length of variabla. Not required for numbers and strings! |
|
* return: NULL error or new variable, after use free variable with io_freeVar() |
|
*/ |
|
ait_val_t * |
|
io_makeVar(ait_type_t type, ...) |
|
{ |
|
ait_val_t *v = NULL; |
|
va_list lst; |
|
void *p = NULL; |
|
uint32_t len = 0; |
|
uint64_t n = 0LL; |
|
|
|
v = io_allocVar(); |
|
if (!v) |
|
return NULL; |
|
|
|
va_start(lst, type); |
|
switch (type) { |
|
case empty: |
|
v->val_type = (uint8_t) empty; |
|
break; |
|
case ptr: |
|
p = va_arg(lst, void*); |
|
len = va_arg(lst, uint32_t); |
|
AIT_SET_PTR(v, p, len); |
|
break; |
|
case data: |
|
p = va_arg(lst, void*); |
|
len = va_arg(lst, uint32_t); |
|
AIT_SET_DATA(v, p, len); |
|
break; |
|
case buffer: |
|
p = va_arg(lst, void*); |
|
len = va_arg(lst, uint32_t); |
|
AIT_SET_BUF(v, p, len); |
|
break; |
|
case string: |
|
p = va_arg(lst, char*); |
|
AIT_SET_STR(v, (char*) p); |
|
break; |
|
case blob: |
|
n = va_arg(lst, uint32_t); |
|
len = va_arg(lst, uint32_t); |
|
AIT_SET_BLOB(v, n, len); |
|
break; |
|
case f32: |
|
AIT_SET_F32(v, (float) va_arg(lst, double)); |
|
break; |
|
case f64: |
|
AIT_SET_F64(v, va_arg(lst, double)); |
|
break; |
|
case u8: |
|
AIT_SET_U8(v, (uint8_t) va_arg(lst, int)); |
|
break; |
|
case u16: |
|
AIT_SET_U16(v, (uint16_t) va_arg(lst, int)); |
|
break; |
|
case u32: |
|
AIT_SET_U32(v, va_arg(lst, uint32_t)); |
|
break; |
|
case u64: |
|
AIT_SET_U64(v, va_arg(lst, uint64_t)); |
|
break; |
|
case i8: |
|
AIT_SET_I8(v, (int8_t) va_arg(lst, int)); |
|
break; |
|
case i16: |
|
AIT_SET_I16(v, (int16_t) va_arg(lst, int)); |
|
break; |
|
case i32: |
|
AIT_SET_I32(v, va_arg(lst, int32_t)); |
|
break; |
|
case i64: |
|
AIT_SET_I64(v, va_arg(lst, int64_t)); |
|
break; |
|
} |
|
va_end(lst); |
|
|
|
return v; |
|
} |
|
|
static int |
static int |
_cmp_arr_key_asc(const void *a, const void *b) |
_cmp_arr_key_asc(const void *a, const void *b) |
{ |
{ |
Line 566 io_findKeyVars(array_t * __restrict vars, u_short key)
|
Line 652 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; |
} |
} |