]> arthur.barton.de Git - ngircd-alex.git/blob - src/ngircd/array.h
add new buffer abstraction layer
[ngircd-alex.git] / src / ngircd / array.h
1 /*
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.
7  *
8  * libarray - dynamically allocate arrays.
9  * Copyright (c) 2005 Florian Westphal (westphal@foo.fh-furtwangen.de)
10  *
11  * $Id: array.h,v 1.1 2005/07/07 18:38:14 fw Exp $
12  */
13
14 #ifndef array_h_included
15 #define array_h_included
16
17 #include "portab.h"
18
19 typedef struct {
20         char * mem;
21         unsigned int allocated;
22         unsigned int used;
23 } array;
24
25 /* allocated: mem != NULL, used >= 0 && used <= allocated, allocated > 0
26    unallocated: mem == NULL, allocated == 0, used == 0 */
27
28
29 #define array_unallocated(x)    (array_bytes(x)==0)
30 #define INIT_ARRAY              { NULL, 0, 0 }
31
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));
35
36 /* returns the number of initialized BYTES in a. */
37 #define array_bytes(array)      ( (array)->used )
38
39 /* returns the number of initialized ELEMS in a. */
40 extern unsigned int array_length PARAMS((const array* const a, unsigned int elemsize));
41
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. */
45
46 /* copy array src to dest */
47 extern bool array_copy PARAMS((array* dest, const array* const src));
48
49 /* copy len bytes from src to array dest. */
50 extern bool array_copyb PARAMS((array* dest, const char* src, unsigned int len));
51
52 /* copy string to dest */
53 extern bool array_copys PARAMS((array* dest, const char* src));
54
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. */
58
59 /* append len bytes from src to array dest. */
60 extern bool array_catb PARAMS((array* dest, const char* src, unsigned int len));
61
62 /* append string to dest */
63 extern bool array_cats PARAMS((array* dest, const char* src));
64
65 /* append NUL byte to dest */
66 extern bool array_cat0 PARAMS((array* dest));
67
68 /* append contents of array src to array dest. */
69 extern bool array_cat PARAMS((array* dest, const array* const src));
70
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));
75
76 /* free the contents of this array. */
77 extern void array_free PARAMS((array* a));
78
79 /* overwrite array with zeroes before free */
80 extern void array_free_wipe PARAMS((array* a));
81
82 /* return pointer to first element in this array */
83 extern void* array_start PARAMS((const array* const a));
84
85 /* reset this array (the memory is not free'd */
86 extern void array_trunc PARAMS((array* a));
87
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));
90
91 /* move elements starting at pos to beginning of array */
92 extern void array_moveleft PARAMS((array* a, unsigned int membersize, unsigned int pos));
93
94 #endif
95 /* -eof- */