Improve some sieve-related translations
[claws.git] / src / plugins / mailmbox / carray.c
blobeb6963f1e557076a9df1b665b1c5f2fc5ba19b3d
2 /*
3 * libEtPan! -- a mail stuff library
5 * carray - Implements simple dynamic pointer arrays
7 * Copyright (c) 1999-2000, Gaƫl Roualland <gael.roualland@iname.com>
8 * interface changes - 2002 - DINH Viet Hoa
9 * All rights reserved.
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.
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.
37 * $Id$
40 #include "config.h"
42 #include <stdlib.h>
43 #include <string.h>
44 #include "carray.h"
46 carray * carray_new(unsigned int initsize) {
47 carray * array;
49 array = (carray *) malloc(sizeof(carray));
50 if (!array) return NULL;
52 array->len = 0;
53 array->max = initsize;
54 array->array = (void **) malloc(sizeof(void *) * initsize);
55 if (!array->array) {
56 free(array);
57 return NULL;
59 return array;
62 int carray_add(carray * array, void * data, unsigned int * index) {
63 int r;
65 r = carray_set_size(array, array->len + 1);
66 if (r < 0)
67 return r;
69 array->array[array->len - 1] = data;
70 if (index != NULL)
71 * index = array->len - 1;
73 return 0;
76 int carray_set_size(carray * array, unsigned int new_size)
78 if (new_size > array->max) {
79 unsigned int n = array->max * 2;
80 void * new;
82 while (n <= new_size)
83 n *= 2;
85 new = (void **) realloc(array->array, sizeof(void *) * n);
86 if (!new)
87 return -1;
88 array->array = new;
89 array->max = n;
91 array->len = new_size;
93 return 0;
96 int carray_delete_fast(carray * array, unsigned int indx) {
97 if (indx >= array->len)
98 return -1;
100 array->array[indx] = NULL;
102 return 0;
105 int carray_delete(carray * array, unsigned int indx) {
106 if (indx >= array->len)
107 return -1;
109 if (indx != --array->len)
110 array->array[indx] = array->array[array->len];
111 return 0;
114 int carray_delete_slow(carray * array, unsigned int indx) {
115 if (indx >= array->len)
116 return -1;
118 if (indx != --array->len)
119 memmove(array->array + indx, array->array + indx + 1,
120 (array->len - indx) * sizeof(void *));
121 return 0;
124 #ifdef NO_MACROS
125 void ** carray_data(carray * array) {
126 return array->array;
129 unsigned int carray_count(carray * array) {
130 return array->len;
133 void * carray_get(carray * array, unsigned int indx) {
134 return array->array[indx];
137 void carray_set(carray * array, unsigned int indx, void * value) {
138 array->array[indx] = value;
140 #endif
142 void carray_free(carray * array) {
143 free(array->array);
144 free(array);