Diffstat (limited to 'libetpan/include/libetpan/carray.h') (more/less context) (ignore whitespace changes)
-rw-r--r-- | libetpan/include/libetpan/carray.h | 123 |
1 files changed, 123 insertions, 0 deletions
diff --git a/libetpan/include/libetpan/carray.h b/libetpan/include/libetpan/carray.h new file mode 100644 index 0000000..f906c57 --- a/dev/null +++ b/libetpan/include/libetpan/carray.h | |||
@@ -0,0 +1,123 @@ | |||
1 | /* | ||
2 | * libEtPan! -- a mail stuff library | ||
3 | * | ||
4 | * carray - Implements simple dynamic pointer arrays | ||
5 | * | ||
6 | * Copyright (c) 1999-2005, Gaël Roualland <gael.roualland@iname.com> | ||
7 | * interface changes - 2005 - DINH Viet Hoa | ||
8 | * All rights reserved. | ||
9 | * | ||
10 | * Redistribution and use in source and binary forms, with or without | ||
11 | * modification, are permitted provided that the following conditions | ||
12 | * are met: | ||
13 | * 1. Redistributions of source code must retain the above copyright | ||
14 | * notice, this list of conditions and the following disclaimer. | ||
15 | * 2. Redistributions in binary form must reproduce the above copyright | ||
16 | * notice, this list of conditions and the following disclaimer in the | ||
17 | * documentation and/or other materials provided with the distribution. | ||
18 | * 3. Neither the name of the libEtPan! project nor the names of its | ||
19 | * contributors may be used to endorse or promote products derived | ||
20 | * from this software without specific prior written permission. | ||
21 | * | ||
22 | * THIS SOFTWARE IS PROVIDED BY THE AUTHORS AND CONTRIBUTORS ``AS IS'' AND | ||
23 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | ||
24 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | ||
25 | * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHORS OR CONTRIBUTORS BE LIABLE | ||
26 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | ||
27 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | ||
28 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | ||
29 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | ||
30 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | ||
31 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | ||
32 | * SUCH DAMAGE. | ||
33 | */ | ||
34 | |||
35 | /* | ||
36 | * $Id$ | ||
37 | */ | ||
38 | |||
39 | #ifndef CARRAY_H | ||
40 | #define CARRAY_H | ||
41 | |||
42 | #ifdef __cplusplus | ||
43 | extern "C" { | ||
44 | #endif | ||
45 | |||
46 | struct carray_s { | ||
47 | void ** array; | ||
48 | unsigned int len; | ||
49 | unsigned int max; | ||
50 | }; | ||
51 | |||
52 | typedef struct carray_s carray; | ||
53 | |||
54 | /* Creates a new array of pointers, with initsize preallocated cells */ | ||
55 | carray * carray_new(unsigned int initsize); | ||
56 | |||
57 | /* Adds the pointer to data in the array. | ||
58 | Returns the index of the pointer in the array or -1 on error */ | ||
59 | int carray_add(carray * array, void * data, unsigned int * index); | ||
60 | |||
61 | int carray_set_size(carray * array, unsigned int new_size); | ||
62 | |||
63 | /* Removes the cell at this index position. Returns TRUE on success. | ||
64 | Order of elements in the array IS changed. */ | ||
65 | int carray_delete(carray * array, unsigned int indx); | ||
66 | |||
67 | /* Removes the cell at this index position. Returns TRUE on success. | ||
68 | Order of elements in the array IS not changed. */ | ||
69 | int carray_delete_slow(carray * array, unsigned int indx); | ||
70 | |||
71 | /* remove without decreasing the size of the array */ | ||
72 | int carray_delete_fast(carray * array, unsigned int indx); | ||
73 | |||
74 | /* Some of the following routines can be implemented as macros to | ||
75 | be faster. If you don't want it, define NO_MACROS */ | ||
76 | #ifdef NO_MACROS | ||
77 | |||
78 | /* Returns the array itself */ | ||
79 | void ** carray_data(carray *); | ||
80 | |||
81 | /* Returns the number of elements in the array */ | ||
82 | int carray_count(carray *); | ||
83 | |||
84 | /* Returns the contents of one cell */ | ||
85 | void * carray_get(carray * array, unsigned int indx); | ||
86 | |||
87 | /* Sets the contents of one cell */ | ||
88 | void carray_set(carray * array, unsigned int indx, void * value); | ||
89 | |||
90 | #else | ||
91 | |||
92 | #if 0 | ||
93 | #define carray_data(a) (a->array) | ||
94 | #define carray_count(a) (a->len) | ||
95 | #define carray_get(a, indx) (a->array[indx]) | ||
96 | #define carray_set(a, indx, v) do { a->array[indx]=v; } while(0) | ||
97 | #endif | ||
98 | |||
99 | static inline void ** carray_data(carray * array) { | ||
100 | return array->array; | ||
101 | } | ||
102 | |||
103 | static inline unsigned int carray_count(carray * array) { | ||
104 | return array->len; | ||
105 | } | ||
106 | |||
107 | static inline void * carray_get(carray * array, unsigned int indx) { | ||
108 | return array->array[indx]; | ||
109 | } | ||
110 | |||
111 | static inline void carray_set(carray * array, | ||
112 | unsigned int indx, void * value) { | ||
113 | array->array[indx] = value; | ||
114 | } | ||
115 | #endif | ||
116 | |||
117 | void carray_free(carray * array); | ||
118 | |||
119 | #ifdef __cplusplus | ||
120 | } | ||
121 | #endif | ||
122 | |||
123 | #endif | ||