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.1 2005/07/07 18:38:14 fw 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 */
29 #define array_unallocated(x) (array_bytes(x)==0)
30 #define INIT_ARRAY { NULL, 0, 0 }
32 /* allocates space for at least nmemb+1 elements of size bytes each.
33 return pointer to elem at pos, or NULL if realloc() fails */
34 extern void * array_alloc PARAMS((array *a, unsigned int size, unsigned int pos));
36 /* returns the number of initialized BYTES in a. */
37 #define array_bytes(array) ( (array)->used )
39 /* returns the number of initialized ELEMS in a. */
40 extern unsigned int array_length PARAMS((const array* const a, unsigned int elemsize));
42 /* _copy functions: copy src to dest.
43 return true if OK, else false. (e.g. realloc failure, invalid src/dest array, ..)
44 In that case dest is left unchanged. */
46 /* copy array src to dest */
47 extern bool array_copy PARAMS((array* dest, const array* const src));
49 /* copy len bytes from src to array dest. */
50 extern bool array_copyb PARAMS((array* dest, const char* src, unsigned int len));
52 /* copy string to dest */
53 extern bool array_copys PARAMS((array* dest, const char* src));
55 /* _cat functions: append src to dest.
56 return true if OK, else false. (e.g. realloc failure, invalid src/dest array, ..)
57 In that case dest is left unchanged. */
59 /* append len bytes from src to array dest. */
60 extern bool array_catb PARAMS((array* dest, const char* src, unsigned int len));
62 /* append string to dest */
63 extern bool array_cats PARAMS((array* dest, const char* src));
65 /* append NUL byte to dest */
66 extern bool array_cat0 PARAMS((array* dest));
68 /* append contents of array src to array dest. */
69 extern bool array_cat PARAMS((array* dest, const array* const src));
71 /* return pointer to element at pos.
72 return NULL if the array is unallocated or if pos is larger than the number
73 of elements stored int the array. */
74 extern void* array_get PARAMS((array* a, unsigned int membersize, unsigned int pos));
76 /* free the contents of this array. */
77 extern void array_free PARAMS((array* a));
79 /* overwrite array with zeroes before free */
80 extern void array_free_wipe PARAMS((array* a));
82 /* return pointer to first element in this array */
83 extern void* array_start PARAMS((const array* const a));
85 /* reset this array (the memory is not free'd */
86 extern void array_trunc PARAMS((array* a));
88 /* set number of used elements in this array to len */
89 extern void array_truncate PARAMS((array* a, unsigned int membersize, unsigned int len));
91 /* move elements starting at pos to beginning of array */
92 extern void array_moveleft PARAMS((array* a, unsigned int membersize, unsigned int pos));