summaryrefslogtreecommitdiffabout
path: root/kmicromail/libetpan/tools/carray.c
authorzautrix <zautrix>2004-07-03 16:33:12 (UTC)
committer zautrix <zautrix>2004-07-03 16:33:12 (UTC)
commite3b89230f065c48c84b48c88edb6eb088374c487 (patch) (unidiff)
tree162ea2ef909a6f82ccfcedf45d80d6c821174912 /kmicromail/libetpan/tools/carray.c
parent2dd6ac0b2d24c91d35ce674a6c26351352df2b15 (diff)
downloadkdepimpi-e3b89230f065c48c84b48c88edb6eb088374c487.zip
kdepimpi-e3b89230f065c48c84b48c88edb6eb088374c487.tar.gz
kdepimpi-e3b89230f065c48c84b48c88edb6eb088374c487.tar.bz2
Initial revision
Diffstat (limited to 'kmicromail/libetpan/tools/carray.c') (more/less context) (ignore whitespace changes)
-rw-r--r--kmicromail/libetpan/tools/carray.c143
1 files changed, 143 insertions, 0 deletions
diff --git a/kmicromail/libetpan/tools/carray.c b/kmicromail/libetpan/tools/carray.c
new file mode 100644
index 0000000..a8e78c9
--- a/dev/null
+++ b/kmicromail/libetpan/tools/carray.c
@@ -0,0 +1,143 @@
1
2/*
3 * libEtPan! -- a mail stuff library
4 *
5 * carray - Implements simple dynamic pointer arrays
6 *
7 * Copyright (c) 1999-2000, Gaël Roualland <gael.roualland@iname.com>
8 * interface changes - 2002 - DINH Viet Hoa
9 * All rights reserved.
10 *
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
13 * are met:
14 * 1. Redistributions of source code must retain the above copyright
15 * notice, this list of conditions and the following disclaimer.
16 * 2. Redistributions in binary form must reproduce the above copyright
17 * notice, this list of conditions and the following disclaimer in the
18 * documentation and/or other materials provided with the distribution.
19 * 3. Neither the name of the libEtPan! project nor the names of its
20 * contributors may be used to endorse or promote products derived
21 * from this software without specific prior written permission.
22 *
23 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
24 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
27 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33 * SUCH DAMAGE.
34 */
35
36/*
37 * $Id$
38 */
39
40#include <stdlib.h>
41#include <string.h>
42#include "carray.h"
43
44carray * carray_new(unsigned int initsize) {
45 carray * array;
46
47 array = (carray *) malloc(sizeof(carray));
48 if (!array) return NULL;
49
50 array->len = 0;
51 array->max = initsize;
52 array->array = (void **) malloc(sizeof(void *) * initsize);
53 if (!array->array) {
54 free(array);
55 return NULL;
56 }
57 return array;
58}
59
60int carray_add(carray * array, void * data, unsigned int * index) {
61 int r;
62
63 r = carray_set_size(array, array->len + 1);
64 if (r < 0)
65 return r;
66
67 array->array[array->len - 1] = data;
68 if (index != NULL)
69 * index = array->len - 1;
70
71 return 0;
72}
73
74int carray_set_size(carray * array, unsigned int new_size)
75{
76 if (new_size > array->max) {
77 unsigned int n = array->max * 2;
78 void * new;
79
80 while (n <= new_size)
81 n *= 2;
82
83 new = (void **) realloc(array->array, sizeof(void *) * n);
84 if (!new)
85 return -1;
86 array->array = new;
87 array->max = n;
88 }
89 array->len = new_size;
90
91 return 0;
92}
93
94int carray_delete_fast(carray * array, unsigned int indx) {
95 if (indx >= array->len)
96 return -1;
97
98 array->array[indx] = NULL;
99
100 return 0;
101}
102
103int carray_delete(carray * array, unsigned int indx) {
104 if (indx >= array->len)
105 return -1;
106
107 if (indx != --array->len)
108 array->array[indx] = array->array[array->len];
109 return 0;
110}
111
112int carray_delete_slow(carray * array, unsigned int indx) {
113 if (indx >= array->len)
114 return -1;
115
116 if (indx != --array->len)
117 memmove(array->array + indx, array->array + indx + 1,
118 (array->len - indx) * sizeof(void *));
119 return 0;
120}
121
122#ifdef NO_MACROS
123void ** carray_data(carray * array) {
124 return array->array;
125}
126
127unsigned int carray_count(carray * array) {
128 return array->len;
129}
130
131void * carray_get(carray * array, unsigned int indx) {
132 return array->array[indx];
133}
134
135void carray_set(carray * array, unsigned int indx, void * value) {
136 array->array[indx] = value;
137}
138#endif
139
140void carray_free(carray * array) {
141 free(array->array);
142 free(array);
143}