--- libaitio/src/Attic/vars.c 2012/03/29 12:23:00 1.5.2.2 +++ libaitio/src/Attic/vars.c 2012/05/19 00:11:58 1.9 @@ -3,7 +3,7 @@ * by Michael Pounov * * $Author: misho $ -* $Id: vars.c,v 1.5.2.2 2012/03/29 12:23:00 misho Exp $ +* $Id: vars.c,v 1.9 2012/05/19 00:11:58 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 @@ -71,7 +71,7 @@ io_vars2buffer(u_char *buf, int buflen, array_t *vars) Limit = sizeof(ait_val_t) * io_arraySize(vars); if (Limit > buflen) { - io_SetErr(EMSGSIZE, "Error:: too short buffer buflen=%d needed min %d ...\n", + io_SetErr(EMSGSIZE, "Short buffer buflen=%d needed min %d", buflen, Limit); return -1; } else { @@ -105,8 +105,8 @@ io_vars2buffer(u_char *buf, int buflen, array_t *vars) break; case data: if (AIT_LEN(val) > buflen - Limit) { - io_SetErr(EMSGSIZE, "Error:: too short buffer buflen=%d " - "needed min %d ...\n", buflen, Limit + AIT_LEN(val)); + io_SetErr(EMSGSIZE, "Short buffer buflen=%d " + "needed min %d", buflen, Limit + AIT_LEN(val)); return -1; } else Limit += AIT_LEN(val); @@ -119,8 +119,8 @@ io_vars2buffer(u_char *buf, int buflen, array_t *vars) case buffer: case string: if (AIT_LEN(val) > buflen - Limit) { - io_SetErr(EMSGSIZE, "Error:: too short buffer buflen=%d " - "needed min %d ...\n", buflen, Limit + AIT_LEN(val)); + io_SetErr(EMSGSIZE, "Short buffer buflen=%d " + "needed min %d", buflen, Limit + AIT_LEN(val)); return -1; } else Limit += AIT_LEN(val); @@ -131,7 +131,7 @@ io_vars2buffer(u_char *buf, int buflen, array_t *vars) dat += AIT_LEN(val); break; default: - io_SetErr(EINVAL, "Error:: unsupported variable type=%d at element #%d ...\n", + io_SetErr(EINVAL, "Unsupported variable type=%d at element #%d", AIT_TYPE(val), i); return -1; } @@ -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 @@ -165,7 +165,7 @@ io_buffer2vars(u_char *buf, int buflen, int vnum, int Limit = sizeof(ait_val_t) * vnum; if (Limit > buflen) { - io_SetErr(EMSGSIZE, "Error:: too short buffer buflen=%d needed min %d ...\n", + io_SetErr(EMSGSIZE, "Short buffer buflen=%d needed min %d", buflen, Limit); return NULL; } else { @@ -223,8 +223,8 @@ io_buffer2vars(u_char *buf, int buflen, int vnum, int case buffer: case string: if (AIT_LEN(val) > buflen - Limit) { - io_SetErr(EMSGSIZE, "Error:: too short buffer buflen=%d " - "needed min %d ...\n", buflen, Limit + AIT_LEN(val)); + io_SetErr(EMSGSIZE, "Short buffer buflen=%d " + "needed min %d", buflen, Limit + AIT_LEN(val)); if (!zcpy) io_arrayFree(vars); io_arrayDestroy(&vars); @@ -246,7 +246,7 @@ io_buffer2vars(u_char *buf, int buflen, int vnum, int dat += AIT_LEN(val); break; default: - io_SetErr(EINVAL, "Error:: unsupported variable type=%d at element #%d ...\n", + io_SetErr(EINVAL, "Unsupported variable type=%d at element #%d", AIT_TYPE(val), i); if (!zcpy) io_arrayFree(vars); @@ -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 @@ -285,7 +285,7 @@ io_vars2map(u_char *buf, int buflen, array_t *vars) Limit = sizeof(ait_val_t) * io_arraySize(vars); if (Limit > buflen) { - io_SetErr(EMSGSIZE, "Error:: too short buffer buflen=%d needed min %d ...\n", + io_SetErr(EMSGSIZE, "Short buffer buflen=%d needed min %d", buflen, Limit); return -1; } else { @@ -319,8 +319,8 @@ io_vars2map(u_char *buf, int buflen, array_t *vars) break; case data: if (AIT_LEN(val) > buflen - Limit) { - io_SetErr(EMSGSIZE, "Error:: too short buffer buflen=%d " - "needed min %d ...\n", buflen, Limit + AIT_LEN(val)); + io_SetErr(EMSGSIZE, "Short buffer buflen=%d " + "needed min %d", buflen, Limit + AIT_LEN(val)); return -1; } else Limit += AIT_LEN(val); @@ -333,8 +333,8 @@ io_vars2map(u_char *buf, int buflen, array_t *vars) case buffer: case string: if (AIT_LEN(val) > buflen - Limit) { - io_SetErr(EMSGSIZE, "Error:: too short buffer buflen=%d " - "needed min %d ...\n", buflen, Limit + AIT_LEN(val)); + io_SetErr(EMSGSIZE, "Short buffer buflen=%d " + "needed min %d", buflen, Limit + AIT_LEN(val)); return -1; } else Limit += AIT_LEN(val); @@ -345,7 +345,7 @@ io_vars2map(u_char *buf, int buflen, array_t *vars) dat += AIT_LEN(val); break; default: - io_SetErr(EINVAL, "Error:: unsupported variable type=%d at element #%d ...\n", + io_SetErr(EINVAL, "Unsupported variable type=%d at element #%d", AIT_TYPE(val), i); return -1; } @@ -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 @@ -379,7 +379,7 @@ io_map2vars(u_char *buf, int buflen, int vnum, int zcp Limit = sizeof(ait_val_t) * vnum; if (Limit > buflen) { - io_SetErr(EMSGSIZE, "Error:: too short buffer buflen=%d needed min %d ...\n", + io_SetErr(EMSGSIZE, "Short buffer buflen=%d needed min %d", buflen, Limit); return NULL; } else { @@ -428,8 +428,8 @@ io_map2vars(u_char *buf, int buflen, int vnum, int zcp case buffer: case string: if (AIT_LEN(val) > buflen - Limit) { - io_SetErr(EMSGSIZE, "Error:: too short buffer buflen=%d " - "needed min %d ...\n", buflen, Limit + AIT_LEN(val)); + io_SetErr(EMSGSIZE, "short buffer buflen=%d " + "needed min %d", buflen, Limit + AIT_LEN(val)); if (!zcpy) io_arrayFree(vars); io_arrayDestroy(&vars); @@ -451,7 +451,7 @@ io_map2vars(u_char *buf, int buflen, int vnum, int zcp dat += AIT_LEN(val); break; default: - io_SetErr(EINVAL, "Error:: unsupported variable type=%d at element #%d ...\n", + io_SetErr(EINVAL, "Unsupported variable type=%d at element #%d", AIT_TYPE(val), i); if (!zcpy) io_arrayFree(vars); @@ -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 @@ -477,28 +477,60 @@ io_allocVars(int varnum) register int i; ait_val_t *v; - if (!varnum || !(arr = io_arrayInit(varnum))) + if (!(arr = io_arrayInit(varnum))) return NULL; for (i = 0; i < io_arraySize(arr); i++) { - v = malloc(sizeof(ait_val_t)); - if (!v) { - LOGERR; + if (!(v = io_allocVar())) { io_freeVars(&arr); return NULL; - } else { - memset(v, 0, sizeof(ait_val_t)); + } else io_arraySet(arr, i, v); - } } return arr; } /* - * io_clrVars() Clear ait_val_t elements from array + * io_getVars() - Get ait_val_t element from array and if not exists allocate it * * @vars = Variable array + * @n = index of variable into array + * return: NULL error or !=NULL ait_val_t element + */ +inline ait_val_t * +io_getVars(array_t ** __restrict vars, int n) +{ + register int i; + ait_val_t *v; + + if (!vars) + return NULL; + + if (!*vars) { + if (!(*vars = io_allocVars(n + 1))) + return NULL; + } else { + if (n >= (i = io_arraySize(*vars))) { + if (io_arrayGrow(*vars, n + 1, 0)) + return NULL; + for (; i < io_arraySize(*vars); i++) + if (!io_arrayGet(*vars, i)) { + if (!(v = io_allocVar())) + return NULL; + else + io_arraySet(*vars, n, v); + } + } + } + + return io_array(*vars, n, ait_val_t*); +} + +/* + * io_clrVars() - Clear ait_val_t elements from array + * + * @vars = Variable array * return: -1 error or size of array */ inline int @@ -518,7 +550,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 +567,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,18 +588,18 @@ io_allocVar(void) } /* - * io_freeVar() Free allocated memory for variable + * io_freeVar() - Free allocated memory for variable * * @val = Variable * return: none */ inline void -io_freeVar(ait_val_t * __restrict val) +io_freeVar(ait_val_t ** __restrict val) { - if (val) { - AIT_FREE_VAL(val); - free(val); - val = NULL; + if (val && *val) { + AIT_FREE_VAL(*val); + free(*val); + *val = NULL; } } @@ -584,7 +616,7 @@ _cmp_arr_key_desc(const void *a, const void *b) } /* - * io_sortVars() Sorting array with variables + * io_sortVars() - Sorting array with variables * * @vars = Variable array * @order = Sort order. If =0 ascend or !=0 descend @@ -606,7 +638,7 @@ io_sortVars(array_t * __restrict vars, int order, int } /* - * io_findKeyVars() Find variable by key from array + * io_findKeyVars() - Find variable by key from array * * @vars = Variables * @key = Search key @@ -616,7 +648,7 @@ ait_val_t * io_findKeyVars(array_t * __restrict vars, u_short key) { array_t *tmp; - ait_val_t **v = NULL; + ait_val_t **vv, *v = NULL; register int i; const u_char *p; @@ -630,17 +662,17 @@ io_findKeyVars(array_t * __restrict vars, u_short key) /* binary search */ for (p = (const u_char*) tmp->arr_data, i = io_arraySize(tmp); i; i >>= 1) { - v = (ait_val_t**) (p + (i >> 1) * sizeof(void*)); - if (!(key - AIT_KEY(*v))) { /* found! */ - io_arrayDestroy(&tmp); - return *v; + vv = (ait_val_t**) (p + (i >> 1) * sizeof(void*)); + if (!(key - AIT_KEY(*vv))) { /* found! */ + v = *vv; + break; } - if ((key - AIT_KEY(*v)) > 0) { /* move right key > current */ - p = (const u_char*) v + sizeof(void*); + 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 NULL; + return v; }