--- libaitio/src/Attic/vars.c 2012/03/29 09:37:19 1.5.2.1 +++ libaitio/src/Attic/vars.c 2012/04/02 09:10:31 1.5.2.4 @@ -3,7 +3,7 @@ * by Michael Pounov * * $Author: misho $ -* $Id: vars.c,v 1.5.2.1 2012/03/29 09:37:19 misho Exp $ +* $Id: vars.c,v 1.5.2.4 2012/04/02 09:10:31 misho Exp $ * ************************************************************************** The ELWIX and AITNET software is distributed under the following @@ -47,7 +47,7 @@ SUCH DAMAGE. /* - * io_vars2buffer() Marshaling data from array with variables to buffer + * io_vars2buffer() - Marshaling data from array with variables to buffer * * @buf = Buffer * @buflen = Size of buffer @@ -141,7 +141,7 @@ io_vars2buffer(u_char *buf, int buflen, array_t *vars) } /* - * io_buffer2vars() De-marshaling data from buffer to array with variables + * io_buffer2vars() - De-marshaling data from buffer to array with variables * * @buf = Buffer * @buflen = Size of buffer @@ -261,7 +261,7 @@ io_buffer2vars(u_char *buf, int buflen, int vnum, int /* buffer marshaling without swapping bytes to network order */ /* - * io_vars2map() Marshaling data from array with variables to memory map + * io_vars2map() - Marshaling data from array with variables to memory map * * @buf = Buffer * @buflen = Size of buffer @@ -355,7 +355,7 @@ io_vars2map(u_char *buf, int buflen, array_t *vars) } /* - * io_map2vars() De-marshaling data from memory map to array with variables + * io_map2vars() - De-marshaling data from memory map to array with variables * * @buf = Buffer * @buflen = Size of buffer @@ -465,7 +465,7 @@ io_map2vars(u_char *buf, int buflen, int vnum, int zcp /* - * io_allocVars() Allocate ait_val_t array + * io_allocVars() - Allocate ait_val_t array * * @varnum = Number of variables * return: =NULL error or !=NULL allocated array @@ -496,7 +496,7 @@ io_allocVars(int varnum) } /* - * io_clrVars() Clear ait_val_t elements from array + * io_clrVars() - Clear ait_val_t elements from array * * @vars = Variable array * return: -1 error or size of array @@ -518,7 +518,7 @@ io_clrVars(array_t * __restrict vars) } /* - * io_freeVars() Free ait_val_t array + * io_freeVars() - Free ait_val_t array * * @vars = Variable array * return: none @@ -535,7 +535,7 @@ io_freeVars(array_t ** __restrict vars) } /* - * io_allocVar() Allocate memory for variable + * io_allocVar() - Allocate memory for variable * * return: NULL error or new variable, after use free variable with io_freeVar() */ @@ -556,7 +556,7 @@ io_allocVar(void) } /* - * io_freeVar() Free allocated memory for variable + * io_freeVar() - Free allocated memory for variable * * @val = Variable * return: none @@ -603,4 +603,44 @@ io_sortVars(array_t * __restrict vars, int order, int qsort(vars->arr_data, vars->arr_num, sizeof(void*), _cmp_arr_key_desc); else qsort(vars->arr_data, vars->arr_num, sizeof(void*), _cmp_arr_key_asc); +} + +/* + * io_findKeyVars() - Find variable by key from array + * + * @vars = Variables + * @key = Search key + * return: NULL error or not found, !=NULL valid element + */ +ait_val_t * +io_findKeyVars(array_t * __restrict vars, u_short key) +{ + array_t *tmp; + ait_val_t **vv, *v = NULL; + register int i; + const u_char *p; + + if (!vars) + return NULL; + + if (io_arrayCopy(&tmp, vars) == -1) + return NULL; + else + qsort(tmp->arr_data, tmp->arr_num, sizeof(void*), _cmp_arr_key_asc); + + /* binary search */ + for (p = (const u_char*) tmp->arr_data, i = io_arraySize(tmp); i; i >>= 1) { + vv = (ait_val_t**) (p + (i >> 1) * sizeof(void*)); + if (!(key - AIT_KEY(*vv))) { /* found! */ + v = *vv; + break; + } + if ((key - AIT_KEY(*vv)) > 0) { /* move right key > current */ + p = (const u_char*) vv + sizeof(void*); + i--; + } /* else move left */ + } + + io_arrayDestroy(&tmp); + return v; }