Diffstat (limited to 'libical/src/libical/icalarray.c') (more/less context) (ignore whitespace changes)
-rw-r--r-- | libical/src/libical/icalarray.c | 163 |
1 files changed, 163 insertions, 0 deletions
diff --git a/libical/src/libical/icalarray.c b/libical/src/libical/icalarray.c new file mode 100644 index 0000000..78e1ada --- a/dev/null +++ b/libical/src/libical/icalarray.c | |||
@@ -0,0 +1,163 @@ | |||
1 | /* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 4 -*- | ||
2 | ====================================================================== | ||
3 | FILE: icalarray.c | ||
4 | CREATOR: Damon Chaplin 07 March 2001 | ||
5 | |||
6 | $Id$ | ||
7 | $Locker$ | ||
8 | |||
9 | (C) COPYRIGHT 2001, Ximian, Inc. | ||
10 | |||
11 | This program is free software; you can redistribute it and/or modify | ||
12 | it under the terms of either: | ||
13 | |||
14 | The LGPL as published by the Free Software Foundation, version | ||
15 | 2.1, available at: http://www.fsf.org/copyleft/lesser.html | ||
16 | |||
17 | Or: | ||
18 | |||
19 | The Mozilla Public License Version 1.0. You may obtain a copy of | ||
20 | the License at http://www.mozilla.org/MPL/ | ||
21 | |||
22 | |||
23 | ======================================================================*/ | ||
24 | |||
25 | /** @file icalarray.c | ||
26 | * | ||
27 | * @brief An array of arbitrarily-sized elements which grows | ||
28 | * dynamically as elements are added. | ||
29 | */ | ||
30 | |||
31 | #ifdef HAVE_CONFIG_H | ||
32 | #include "config.h" | ||
33 | #endif | ||
34 | |||
35 | #include <stdlib.h> | ||
36 | #include <string.h> | ||
37 | |||
38 | #include "icalarray.h" | ||
39 | #include "icalerror.h" | ||
40 | |||
41 | |||
42 | static void icalarray_expand (icalarray*array, | ||
43 | int space_needed); | ||
44 | |||
45 | /** @brief Constructor | ||
46 | */ | ||
47 | |||
48 | icalarray* | ||
49 | icalarray_new (int element_size, | ||
50 | int increment_size) | ||
51 | { | ||
52 | icalarray *array; | ||
53 | |||
54 | array = (icalarray*) malloc (sizeof (icalarray)); | ||
55 | if (!array) { | ||
56 | icalerror_set_errno(ICAL_NEWFAILED_ERROR); | ||
57 | return NULL; | ||
58 | } | ||
59 | |||
60 | array->element_size = element_size; | ||
61 | array->increment_size = increment_size; | ||
62 | array->num_elements = 0; | ||
63 | array->space_allocated = 0; | ||
64 | array->data = NULL; | ||
65 | |||
66 | return array; | ||
67 | } | ||
68 | |||
69 | /** @brief Destructor | ||
70 | */ | ||
71 | |||
72 | void | ||
73 | icalarray_free (icalarray*array) | ||
74 | { | ||
75 | if (array->data) | ||
76 | free (array->data); | ||
77 | free (array); | ||
78 | } | ||
79 | |||
80 | |||
81 | void | ||
82 | icalarray_append (icalarray*array, | ||
83 | void *element) | ||
84 | { | ||
85 | if (array->num_elements >= array->space_allocated) | ||
86 | icalarray_expand (array, 1); | ||
87 | |||
88 | memcpy ((char *)(array->data) + ( array->num_elements * array->element_size ), element, | ||
89 | array->element_size); | ||
90 | array->num_elements++; | ||
91 | } | ||
92 | |||
93 | |||
94 | void* | ||
95 | icalarray_element_at (icalarray*array, | ||
96 | int position) | ||
97 | { | ||
98 | assert (position >= 0); | ||
99 | assert (position < array->num_elements); | ||
100 | |||
101 | return (char *)(array->data) + (position * array->element_size); | ||
102 | } | ||
103 | |||
104 | |||
105 | void | ||
106 | icalarray_remove_element_at (icalarray*array, | ||
107 | int position) | ||
108 | { | ||
109 | void *dest; | ||
110 | int elements_to_move; | ||
111 | |||
112 | assert (position >= 0); | ||
113 | assert (position < array->num_elements); | ||
114 | |||
115 | dest = (char *)array->data + (position * array->element_size); | ||
116 | elements_to_move = array->num_elements - position - 1; | ||
117 | |||
118 | if (elements_to_move > 0) | ||
119 | memmove (dest, (char *)dest + array->element_size, | ||
120 | elements_to_move * array->element_size); | ||
121 | |||
122 | array->num_elements--; | ||
123 | } | ||
124 | |||
125 | |||
126 | void | ||
127 | icalarray_sort (icalarray*array, | ||
128 | int (*compare) (const void *, | ||
129 | const void *)) | ||
130 | { | ||
131 | qsort (array->data, array->num_elements, array->element_size, compare); | ||
132 | } | ||
133 | |||
134 | |||
135 | static void | ||
136 | icalarray_expand (icalarray*array, | ||
137 | int space_needed) | ||
138 | { | ||
139 | int new_space_allocated; | ||
140 | void *new_data; | ||
141 | |||
142 | new_space_allocated = array->space_allocated + array->increment_size; | ||
143 | |||
144 | if (space_needed > array->increment_size) | ||
145 | new_space_allocated += space_needed; | ||
146 | |||
147 | /* | ||
148 | new_data = realloc (array->data, | ||
149 | new_space_allocated * array->element_size); | ||
150 | */ | ||
151 | new_data = malloc(new_space_allocated * array->element_size); | ||
152 | memcpy(new_data,array->data,array->element_size*array->space_allocated); | ||
153 | free(array->data); | ||
154 | |||
155 | if (new_data) { | ||
156 | array->data = new_data; | ||
157 | array->space_allocated = new_space_allocated; | ||
158 | } else { | ||
159 | icalerror_set_errno(ICAL_ALLOCATION_ERROR); | ||
160 | } | ||
161 | } | ||
162 | |||
163 | |||