2 * This program is free software; you can redistribute it and/or modify
3 * it under the terms of the GNU General Public License as published by
4 * the Free Software Foundation; either version 2 of the License, or
5 * (at your option) any later version.
6 * Please read the file COPYING, README and AUTHORS for more information.
8 * libarray - dynamically allocate arrays.
9 * Copyright (c) 2005 Florian Westphal (westphal@foo.fh-furtwangen.de)
11 * $Id: array.h,v 1.2 2005/07/14 09:11:38 alex Exp $
14 #ifndef array_h_included
15 #define array_h_included
21 unsigned int allocated;
25 /* allocated: mem != NULL, used >= 0 && used <= allocated, allocated > 0
26 unallocated: mem == NULL, allocated == 0, used == 0 */
28 #define array_unallocated(x) (array_bytes(x)==0)
29 #define INIT_ARRAY { NULL, 0, 0 }
31 /* allocates space for at least nmemb+1 elements of size bytes each.
32 return pointer to elem at pos, or NULL if realloc() fails */
33 extern void * array_alloc PARAMS((array *a, unsigned int size, unsigned int pos));
35 /* returns the number of initialized BYTES in a. */
36 #define array_bytes(array) ( (array)->used )
38 /* returns the number of initialized ELEMS in a. */
39 extern unsigned int array_length PARAMS((const array* const a, unsigned int elemsize));
41 /* _copy functions: copy src to dest.
42 return true if OK, else false (e. g. realloc failure, invalid src/dest
43 array, ...). In that case dest is left unchanged. */
45 /* copy array src to dest */
46 extern bool array_copy PARAMS((array* dest, const array* const src));
48 /* copy len bytes from src to array dest. */
49 extern bool array_copyb PARAMS((array* dest, const char* src, unsigned int len));
51 /* copy string to dest */
52 extern bool array_copys PARAMS((array* dest, const char* src));
54 /* _cat functions: append src to dest.
55 return true if OK, else false (e. g. realloc failure, invalid src/dest
56 array, ...). In that case dest is left unchanged. */
58 /* append len bytes from src to array dest. */
59 extern bool array_catb PARAMS((array* dest, const char* src, unsigned int len));
61 /* append string to dest */
62 extern bool array_cats PARAMS((array* dest, const char* src));
64 /* append NUL byte to dest */
65 extern bool array_cat0 PARAMS((array* dest));
67 /* append contents of array src to array dest. */
68 extern bool array_cat PARAMS((array* dest, const array* const src));
70 /* return pointer to element at pos.
71 return NULL if the array is unallocated or if pos is larger than the number
72 of elements stored int the array. */
73 extern void* array_get PARAMS((array* a, unsigned int membersize, unsigned int pos));
75 /* free the contents of this array. */
76 extern void array_free PARAMS((array* a));
78 /* overwrite array with zeroes before free */
79 extern void array_free_wipe PARAMS((array* a));
81 /* return pointer to first element in this array */
82 extern void* array_start PARAMS((const array* const a));
84 /* reset this array (the memory is not free'd */
85 extern void array_trunc PARAMS((array* a));
87 /* set number of used elements in this array to len */
88 extern void array_truncate PARAMS((array* a, unsigned int membersize, unsigned int len));
90 /* move elements starting at pos to beginning of array */
91 extern void array_moveleft PARAMS((array* a, unsigned int membersize, unsigned int pos));