summaryrefslogtreecommitdiffabout
path: root/libetpan/src/data-types/carray.c
Unidiff
Diffstat (limited to 'libetpan/src/data-types/carray.c') (more/less context) (ignore whitespace changes)
-rw-r--r--libetpan/src/data-types/carray.c142
1 files changed, 142 insertions, 0 deletions
diff --git a/libetpan/src/data-types/carray.c b/libetpan/src/data-types/carray.c
new file mode 100644
index 0000000..f1e618b
--- a/dev/null
+++ b/libetpan/src/data-types/carray.c
@@ -0,0 +1,142 @@
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#include <stdlib.h>
40#include <string.h>
41#include "carray.h"
42
43carray * carray_new(unsigned int initsize) {
44 carray * array;
45
46 array = (carray *) malloc(sizeof(carray));
47 if (!array) return NULL;
48
49 array->len = 0;
50 array->max = initsize;
51 array->array = (void **) malloc(sizeof(void *) * initsize);
52 if (!array->array) {
53 free(array);
54 return NULL;
55 }
56 return array;
57}
58
59int carray_add(carray * array, void * data, unsigned int * index) {
60 int r;
61
62 r = carray_set_size(array, array->len + 1);
63 if (r < 0)
64 return r;
65
66 array->array[array->len - 1] = data;
67 if (index != NULL)
68 * index = array->len - 1;
69
70 return 0;
71}
72
73int carray_set_size(carray * array, unsigned int new_size)
74{
75 if (new_size > array->max) {
76 unsigned int n = array->max * 2;
77 void * new;
78
79 while (n <= new_size)
80 n *= 2;
81
82 new = (void **) realloc(array->array, sizeof(void *) * n);
83 if (!new)
84 return -1;
85 array->array = new;
86 array->max = n;
87 }
88 array->len = new_size;
89
90 return 0;
91}
92
93int carray_delete_fast(carray * array, unsigned int indx) {
94 if (indx >= array->len)
95 return -1;
96
97 array->array[indx] = NULL;
98
99 return 0;
100}
101
102int carray_delete(carray * array, unsigned int indx) {
103 if (indx >= array->len)
104 return -1;
105
106 if (indx != --array->len)
107 array->array[indx] = array->array[array->len];
108 return 0;
109}
110
111int carray_delete_slow(carray * array, unsigned int indx) {
112 if (indx >= array->len)
113 return -1;
114
115 if (indx != --array->len)
116 memmove(array->array + indx, array->array + indx + 1,
117 (array->len - indx) * sizeof(void *));
118 return 0;
119}
120
121#ifdef NO_MACROS
122void ** carray_data(carray * array) {
123 return array->array;
124}
125
126unsigned int carray_count(carray * array) {
127 return array->len;
128}
129
130void * carray_get(carray * array, unsigned int indx) {
131 return array->array[indx];
132}
133
134void carray_set(carray * array, unsigned int indx, void * value) {
135 array->array[indx] = value;
136}
137#endif
138
139void carray_free(carray * array) {
140 free(array->array);
141 free(array);
142}