-
Notifications
You must be signed in to change notification settings - Fork 22
Expand file tree
/
Copy pathxpc_array.c
More file actions
324 lines (260 loc) · 6.83 KB
/
xpc_array.c
File metadata and controls
324 lines (260 loc) · 6.83 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
/*
* Copyright 2014-2015 iXsystems, Inc.
* All rights reserved
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted providing that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
* DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
* STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
* IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*
*/
#include <sys/types.h>
#include <xpc/xpc.h>
#include "xpc_internal.h"
xpc_object_t
xpc_array_create(const xpc_object_t *objects, size_t count)
{
struct xpc_object *xo;
size_t i;
xpc_u val;
xo = _xpc_prim_create(_XPC_TYPE_ARRAY, val, 0);
for (i = 0; i < count; i++)
xpc_array_append_value(xo, objects[i]);
return (xo);
}
void
xpc_array_set_value(xpc_object_t xarray, size_t index, xpc_object_t value)
{
struct xpc_object *xo, *xotmp, *xotmp2;
struct xpc_array_head *arr;
size_t i;
xo = xarray;
arr = &xo->xo_array;
i = 0;
if (index == XPC_ARRAY_APPEND)
return xpc_array_append_value(xarray, value);
if (index >= (size_t)xo->xo_size)
return;
TAILQ_FOREACH_SAFE(xotmp, arr, xo_link, xotmp2) {
if (i++ == index) {
TAILQ_INSERT_AFTER(arr, (struct xpc_object *)value,
xotmp, xo_link);
TAILQ_REMOVE(arr, xotmp, xo_link);
xpc_retain(value);
free(xotmp);
break;
}
}
}
void
xpc_array_append_value(xpc_object_t xarray, xpc_object_t value)
{
struct xpc_object *xo;
struct xpc_array_head *arr;
xo = xarray;
arr = &xo->xo_array;
TAILQ_INSERT_TAIL(arr, (struct xpc_object *)value, xo_link);
xpc_retain(value);
}
xpc_object_t
xpc_array_get_value(xpc_object_t xarray, size_t index)
{
struct xpc_object *xo, *xotmp;
struct xpc_array_head *arr;
size_t i;
xo = xarray;
arr = &xo->xo_array;
i = 0;
if (index > xo->xo_size)
return (NULL);
TAILQ_FOREACH(xotmp, arr, xo_link) {
if (i++ == index)
return (xotmp);
}
return (NULL);
}
size_t
xpc_array_get_count(xpc_object_t xarray)
{
struct xpc_object *xo;
xo = xarray;
return (xo->xo_size);
}
void
xpc_array_set_bool(xpc_object_t xarray, size_t index, bool value)
{
struct xpc_object *xo, *xotmp;
xo = xarray;
xotmp = xpc_bool_create(value);
return (xpc_array_set_value(xarray, index, xotmp));
}
void
xpc_array_set_int64(xpc_object_t xarray, size_t index, int64_t value)
{
struct xpc_object *xo, *xotmp;
xo = xarray;
xotmp = xpc_int64_create(value);
return (xpc_array_set_value(xarray, index, xotmp));
}
void
xpc_array_set_uint64(xpc_object_t xarray, size_t index, uint64_t value)
{
struct xpc_object *xo, *xotmp;
xo = xarray;
xotmp = xpc_uint64_create(value);
return (xpc_array_set_value(xarray, index, xotmp));
}
void
xpc_array_set_double(xpc_object_t xarray, size_t index, double value)
{
struct xpc_object *xo, *xotmp;
xo = xarray;
xotmp = xpc_double_create(value);
return (xpc_array_set_value(xarray, index, xotmp));
}
void
xpc_array_set_date(xpc_object_t xarray, size_t index, int64_t value)
{
struct xpc_object *xo, *xotmp;
xo = xarray;
xotmp = xpc_date_create(value);
return (xpc_array_set_value(xarray, index, xotmp));
}
void
xpc_array_set_data(xpc_object_t xarray, size_t index, const void *data,
size_t length)
{
struct xpc_object *xo, *xotmp;
xo = xarray;
xotmp = xpc_data_create(data, length);
return (xpc_array_set_value(xarray, index, xotmp));
}
void
xpc_array_set_string(xpc_object_t xarray, size_t index, const char *string)
{
struct xpc_object *xo, *xotmp;
xo = xarray;
xotmp = xpc_string_create(string);
return (xpc_array_set_value(xarray, index, xotmp));
}
void
xpc_array_set_uuid(xpc_object_t xarray, size_t index, const uuid_t value)
{
struct xpc_object *xo, *xotmp;
xo = xarray;
xotmp = xpc_uuid_create(value);
return (xpc_array_set_value(xarray, index, xotmp));
}
void
xpc_array_set_fd(xpc_object_t xarray, size_t index, int value)
{
struct xpc_object *xo, *xotmp;
xo = xarray;
//xotmp = xpc_fd_create(value);
return (xpc_array_set_value(xarray, index, xotmp));
}
void
xpc_array_set_connection(xpc_object_t xarray, size_t index,
xpc_connection_t value)
{
}
bool
xpc_array_get_bool(xpc_object_t xarray, size_t index)
{
struct xpc_object *xotmp;
xotmp = xpc_array_get_value(xarray, index);
return (xpc_bool_get_value(xotmp));
}
int64_t
xpc_array_get_int64(xpc_object_t xarray, size_t index)
{
struct xpc_object *xotmp;
xotmp = xpc_array_get_value(xarray, index);
return (xpc_int64_get_value(xotmp));
}
uint64_t
xpc_array_get_uint64(xpc_object_t xarray, size_t index)
{
struct xpc_object *xotmp;
xotmp = xpc_array_get_value(xarray, index);
return (xpc_uint64_get_value(xotmp));
}
double
xpc_array_get_double(xpc_object_t xarray, size_t index)
{
struct xpc_object *xotmp;
xotmp = xpc_array_get_value(xarray, index);
return (xpc_double_get_value(xotmp));
}
int64_t
xpc_array_get_date(xpc_object_t xarray, size_t index)
{
struct xpc_object *xotmp;
xotmp = xpc_array_get_value(xarray, index);
return (xpc_date_get_value(xotmp));
}
const void *
xpc_array_get_data(xpc_object_t xarray, size_t index, size_t *length)
{
struct xpc_object *xotmp;
xotmp = xpc_array_get_value(xarray, index);
*length = xpc_data_get_length(xotmp);
return (xpc_data_get_bytes_ptr(xotmp));
}
const uint8_t *
xpc_array_get_uuid(xpc_object_t xarray, size_t index)
{
struct xpc_object *xotmp;
xotmp = xpc_array_get_value(xarray, index);
return (xpc_uuid_get_bytes(xotmp));
}
const char *
xpc_array_get_string(xpc_object_t xarray, size_t index)
{
struct xpc_object *xotmp;
xotmp = xpc_array_get_value(xarray, index);
return (xpc_string_get_string_ptr(xotmp));
}
int
xpc_array_dup_fd(xpc_object_t array, size_t index)
{
/* XXX */
return (-1);
}
xpc_connection_t
xpc_array_get_connection(xpc_object_t array, size_t index)
{
/* XXX */
return (NULL);
}
bool
xpc_array_apply(xpc_object_t xarray, xpc_array_applier_t applier)
{
struct xpc_object *xo, *xotmp;
struct xpc_array_head *arr;
size_t i;
i = 0;
xo = xarray;
arr = &xo->xo_array;
TAILQ_FOREACH(xotmp, arr, xo_link) {
if (!applier(i++, xotmp))
return (false);
}
return (true);
}