add _krb5_key_data
[heimdal.git] / base / array.c
blob7b0d77b1cc11fbc2a7d7d614f134ebe53d60ff7e
1 /*
2 * Copyright (c) 2010 Kungliga Tekniska Högskolan
3 * (Royal Institute of Technology, Stockholm, Sweden).
4 * All rights reserved.
6 * Portions Copyright (c) 2010 Apple Inc. All rights reserved.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
12 * 1. Redistributions of source code must retain the above copyright
13 * 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.
19 * 3. Neither the name of the Institute nor the names of its contributors
20 * may be used to endorse or promote products derived from this software
21 * without specific prior written permission.
23 * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE 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 INSTITUTE 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.
36 #include "baselocl.h"
42 struct heim_array_data {
43 size_t len;
44 heim_object_t *val;
47 static void
48 array_dealloc(heim_object_t ptr)
50 heim_array_t array = ptr;
51 size_t n;
52 for (n = 0; n < array->len; n++)
53 heim_release(array->val[n]);
54 free(array->val);
57 struct heim_type_data array_object = {
58 HEIM_TID_ARRAY,
59 "dict-object",
60 NULL,
61 array_dealloc,
62 NULL,
63 NULL,
64 NULL
67 /**
68 * Allocate an array
70 * @return A new allocated array, free with heim_release()
73 heim_array_t
74 heim_array_create(void)
76 heim_array_t array;
78 array = _heim_alloc_object(&array_object, sizeof(*array));
79 if (array == NULL)
80 return NULL;
82 array->val = NULL;
83 array->len = 0;
85 return array;
88 /**
89 * Get type id of an dict
91 * @return the type id
94 heim_tid_t
95 heim_array_get_type_id(void)
97 return HEIM_TID_ARRAY;
101 * Append object to array
103 * @param array array to add too
104 * @param object the object to add
106 * @return zero if added, errno otherwise
110 heim_array_append_value(heim_array_t array, heim_object_t object)
112 heim_object_t *ptr;
114 ptr = realloc(array->val, (array->len + 1) * sizeof(array->val[0]));
115 if (ptr == NULL)
116 return ENOMEM;
117 array->val = ptr;
118 array->val[array->len++] = heim_retain(object);
120 return 0;
124 * Iterate over all objects in array
126 * @param array array to iterate over
127 * @param fn function to call on each object
128 * @param ctx context passed to fn
131 void
132 heim_array_iterate_f(heim_array_t array, heim_array_iterator_f_t fn, void *ctx)
134 size_t n;
135 for (n = 0; n < array->len; n++)
136 fn(array->val[n], ctx);
139 #ifdef __BLOCKS__
141 * Iterate over all objects in array
143 * @param array array to iterate over
144 * @param fn block to call on each object
147 void
148 heim_array_iterate(heim_array_t array, void (^fn)(heim_object_t))
150 size_t n;
151 for (n = 0; n < array->len; n++)
152 fn(array->val[n]);
154 #endif
157 * Get length of array
159 * @param array array to get length of
161 * @return length of array
164 size_t
165 heim_array_get_length(heim_array_t array)
167 return array->len;
171 * Copy value of array
173 * @param array array copy object from
174 * @param idx index of object, 0 based, must be smaller then
175 * heim_array_get_length()
177 * @return a retained copy of the object
180 heim_object_t
181 heim_array_copy_value(heim_array_t array, size_t idx)
183 if (idx >= array->len)
184 heim_abort("index too large");
185 return heim_retain(array->val[idx]);
189 * Delete value at idx
191 * @param array the array to modify
192 * @param idx the key to delete
195 void
196 heim_array_delete_value(heim_array_t array, size_t idx)
198 heim_object_t obj;
199 if (idx >= array->len)
200 heim_abort("index too large");
201 obj = array->val[idx];
203 array->len--;
205 if (idx < array->len)
206 memmove(&array->val[idx], &array->val[idx + 1],
207 (array->len - idx) * sizeof(array->val[0]));
209 heim_release(obj);
212 #ifdef __BLOCKS__
214 * Get value at idx
216 * @param array the array to modify
217 * @param idx the key to delete
220 void
221 heim_array_filter(heim_array_t array, bool (^block)(heim_object_t))
223 size_t n = 0;
225 while (n < array->len) {
226 if (block(array->val[n])) {
227 heim_array_delete_value(array, n);
228 } else {
229 n++;
234 #endif /* __BLOCKS__ */