]> arthur.barton.de Git - ngircd-alex.git/blob - src/ngircd/array.h
Reformatted some comments.
[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.2 2005/07/14 09:11:38 alex 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 #define array_unallocated(x)    (array_bytes(x)==0)
29 #define INIT_ARRAY              { NULL, 0, 0 }
30
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));
34
35 /* returns the number of initialized BYTES in a. */
36 #define array_bytes(array)      ( (array)->used )
37
38 /* returns the number of initialized ELEMS in a. */
39 extern unsigned int array_length PARAMS((const array* const a, unsigned int elemsize));
40
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. */
44
45 /* copy array src to dest */
46 extern bool array_copy PARAMS((array* dest, const array* const src));
47
48 /* copy len bytes from src to array dest. */
49 extern bool array_copyb PARAMS((array* dest, const char* src, unsigned int len));
50
51 /* copy string to dest */
52 extern bool array_copys PARAMS((array* dest, const char* src));
53
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. */
57
58 /* append len bytes from src to array dest. */
59 extern bool array_catb PARAMS((array* dest, const char* src, unsigned int len));
60
61 /* append string to dest */
62 extern bool array_cats PARAMS((array* dest, const char* src));
63
64 /* append NUL byte to dest */
65 extern bool array_cat0 PARAMS((array* dest));
66
67 /* append contents of array src to array dest. */
68 extern bool array_cat PARAMS((array* dest, const array* const src));
69
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));
74
75 /* free the contents of this array. */
76 extern void array_free PARAMS((array* a));
77
78 /* overwrite array with zeroes before free */
79 extern void array_free_wipe PARAMS((array* a));
80
81 /* return pointer to first element in this array */
82 extern void* array_start PARAMS((const array* const a));
83
84 /* reset this array (the memory is not free'd */
85 extern void array_trunc PARAMS((array* a));
86
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));
89
90 /* move elements starting at pos to beginning of array */
91 extern void array_moveleft PARAMS((array* a, unsigned int membersize, unsigned int pos));
92
93 #endif
94
95 /* -eof- */