summaryrefslogtreecommitdiff
path: root/common/lib/libprop/prop_array.3
blob: 510d620705148a521391d115ef17307227440d67 (plain)
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
.\"	$NetBSD: prop_array.3,v 1.13 2011/09/30 22:08:18 jym Exp $
.\"
.\" Copyright (c) 2006, 2009 The NetBSD Foundation, Inc.
.\" All rights reserved.
.\"
.\" This code is derived from software contributed to The NetBSD Foundation
.\" by Jason R. Thorpe.
.\"
.\" Redistribution and use in source and binary forms, with or without
.\" modification, are permitted provided 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 NETBSD FOUNDATION, INC. AND CONTRIBUTORS
.\" ``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 FOUNDATION OR CONTRIBUTORS
.\" 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.
.\"
.Dd October 10, 2009
.Dt PROP_ARRAY 3
.Os
.Sh NAME
.Nm prop_array ,
.Nm prop_array_create ,
.Nm prop_array_create_with_capacity ,
.Nm prop_array_copy ,
.Nm prop_array_copy_mutable ,
.Nm prop_array_capacity ,
.Nm prop_array_count ,
.Nm prop_array_ensure_capacity ,
.Nm prop_array_iterator ,
.Nm prop_array_make_immutable ,
.Nm prop_array_mutable ,
.Nm prop_array_get ,
.Nm prop_array_set ,
.Nm prop_array_add ,
.Nm prop_array_remove ,
.Nm prop_array_externalize ,
.Nm prop_array_internalize ,
.Nm prop_array_externalize_to_file ,
.Nm prop_array_internalize_from_file ,
.Nm prop_array_externalize_to_pref ,
.Nm prop_array_internalize_from_pref ,
.Nm prop_array_equals
.Nd array property collection object
.Sh LIBRARY
.Lb libprop
.Sh SYNOPSIS
.In prop/proplib.h
.\"
.Ft prop_array_t
.Fn prop_array_create "void"
.Ft prop_array_t
.Fn prop_array_create_with_capacity "unsigned int capacity"
.\"
.Ft prop_array_t
.Fn prop_array_copy "prop_array_t array"
.Ft prop_array_t
.Fn prop_array_copy_mutable "prop_array_t array"
.\"
.Ft unsigned int
.Fn prop_array_capacity "prop_array_t array"
.Ft unsigned int
.Fn prop_array_count "prop_array_t array"
.Ft bool
.Fn prop_array_ensure_capacity "prop_array_t array" "unsigned int capacity"
.\"
.Ft prop_object_iterator_t
.Fn prop_array_iterator "prop_array_t array"
.\"
.Ft void
.Fn prop_array_make_immutable "prop_array_t array"
.Ft bool
.Fn prop_array_mutable "prop_array_t array"
.\"
.Ft prop_object_t
.Fn prop_array_get "prop_array_t array" "unsigned int index"
.Ft bool
.Fn prop_array_set "prop_array_t array" "unsigned int index" "prop_object_t obj"
.Ft bool
.Fn prop_array_add "prop_array_t array" "prop_object_t obj"
.Ft void
.Fn prop_array_remove "prop_array_t array" "unsigned int index"
.\"
.Ft char *
.Fn prop_array_externalize "prop_array_t array"
.Ft prop_array_t
.Fn prop_array_internalize "const char *xml"
.\"
.Ft bool
.Fn prop_array_externalize_to_file "prop_array_t array" "const char *path"
.Ft prop_array_t
.Fn prop_array_internalize_from_file "const char *path"
.\"
.Ft bool
.Fn prop_array_externalize_to_pref "prop_array_t array" "struct plistref *pref"
.Ft bool
.Fn prop_array_internalize_from_pref "const struct plistref *pref" \
    "prop_array_t *arrayp"
.\"
.Ft bool
.Fn prop_array_equals "prop_array_t array1" "prop_array_t array2"
.Sh DESCRIPTION
The
.Nm prop_array
family of functions operate on the array property collection object type.
An array is an ordered set; an iterated array will return objects in the
same order with which they were stored.
.Bl -tag -width "xxxxx"
.It Fn prop_array_create "void"
Create an empty array.
The array initially has no capacity.
Returns
.Dv NULL
on failure.
.It Fn prop_array_create_with_capacity "unsigned int capacity"
Create an array with the capacity to store
.Fa capacity
objects.
Returns
.Dv NULL
on failure.
.It Fn prop_array_copy "prop_array_t array"
Copy an array.
The new array has an initial capacity equal to the number of objects stored
in the array being copied.
The new array contains references to the original array's objects, not
copies of those objects
.Pq i.e. a shallow copy is made .
If the original array is immutable, the resulting array is also immutable.
Returns
.Dv NULL
on failure.
.It Fn prop_array_copy_mutable "prop_array_t array"
Like
.Fn prop_array_copy ,
except the resulting array is always mutable.
.It Fn prop_array_capacity "prop_array_t array"
Returns the total capacity of the array, including objects already stored
in the array.
If the supplied object isn't an array, zero is returned.
.It Fn prop_array_count "prop_array_t array"
Returns the number of objects stored in the array.
If the supplied object isn't an array, zero is returned.
.It Fn prop_array_ensure_capacity "prop_array_t array" "unsigned int capacity"
Ensure that the array has a total capacity of
.Fa capacity ,
including objects already stored in the array.
Returns
.Dv true
if the capacity of the array is greater or equal to
.Fa capacity
or if expansion of the array's capacity was successful
and
.Dv false
otherwise.
.It Fn prop_array_iterator "prop_array_t array"
Create an iterator for the array.
The array is retained by the iterator.
An array iterator returns the object references stored in the array.
Storing to or removing from the array invalidates any active iterators for
the array.
Returns
.Dv NULL
on failure.
.It Fn prop_array_make_immutable "prop_array_t array"
Make
.Fa array
immutable.
.It Fn prop_array_mutable "prop_array_t array"
Returns
.Dv true
if the array is mutable.
.It Fn prop_array_get "prop_array_t array" "unsigned int index"
Return the object stored at the array index
.Fa index .
Returns
.Dv NULL
on failure.
.It Fn prop_array_set "prop_array_t array" "unsigned int index" \
       "prop_object_t obj"
Store a reference to the object
.Fa obj
at the array index
.Fa index .
This function is not allowed to create holes in the array;
the caller must either be setting the object just beyond the existing
count or replacing an already existing object reference.
The object will be retained by the array.
If an existing object reference is being replaced, that object will be
released.
Returns
.Dv true
if storing the object was successful and
.Dv false
otherwise.
.It Fn prop_array_add "prop_array_t array" "prop_object_t obj"
Add a reference to the object
.Fa obj
to the array, appending to the end and growing the array's capacity if
necessary.
The object will be retained by the array.
Returns
.Dv true
if storing the object was successful and
.Dv false
otherwise.
.Pp
During expansion, array's capacity is augmented by the
.Dv EXPAND_STEP
constant, as defined in
.Pa libprop/prop_array.c
file, e.g.
.Pp
.Dl #define	EXPAND_STEP		16
.It Fn prop_array_remove "prop_array_t array" "unsigned int index"
Remove the reference to the object stored at array index
.Fa index .
The object will be released and the array compacted following
the removal.
.It Fn prop_array_externalize "prop_array_t array"
Externalizes an array, returning a NUL-terminated buffer containing
the XML representation of the array.
The caller is responsible for freeing the returned buffer.
If converting to the external representation fails for any reason,
.Dv NULL
is returned.
.Pp
In user space, the buffer is allocated using
.Xr malloc 3 .
In the kernel, the buffer is allocated using
.Xr malloc 9
using the malloc type
.Dv M_TEMP .
.It Fn prop_array_internalize "const char *xml"
Parse the XML representation of a property list in the NUL-terminated
buffer
.Fa xml
and return the corresponding array.
Returns
.Dv NULL
if parsing fails for any reason.
.It Fn prop_array_externalize_to_file "prop_array_t array" "const char *path"
Externalizes an array and writes it to the file specified by
.Fa path .
The file is saved with the mode
.Dv 0666
as modified by the process's file creation mask
.Pq see Xr umask 2
and is written atomically.
Returns
.Dv false
if externalizing or writing the array fails for any reason.
.It Fn prop_array_internalize_from_file "const char *path"
Reads the XML property list contained in the file specified by
.Fa path ,
internalizes it, and returns the corresponding array.
Returns
.Dv NULL
on failure.
.It Fn prop_array_externalize_to_pref "prop_array_t array" \
    "struct plistref *pref"
Externalizes an array and packs it into the plistref specified by
.Fa pref .
Returns
.Dv false
if externalizing the array fails for any reason.
.It Fn prop_array_internalize_from_pref "const struct plistref *pref" \
    "prop_array_t *arrayp"
Reads the plistref specified by
.Fa pref ,
internalizes it, and returns the corresponding array.
Returns
.Dv false
if internalizing or writing the array fails for any reason.
.It Fn prop_array_equals "prop_array_t array1" "prop_array_t array2"
Returns
.Dv true
if the two arrays are equivalent.
If at least one of the supplied objects isn't an array,
.Dv false
is returned.
Note: Objects contained in the array are compared by value, not by reference.
.El
.Sh SEE ALSO
.Xr prop_bool 3 ,
.Xr prop_data 3 ,
.Xr prop_dictionary 3 ,
.Xr prop_number 3 ,
.Xr prop_object 3 ,
.Xr prop_string 3 ,
.Xr proplib 3
.Sh HISTORY
The
.Nm proplib
property container object library first appeared in
.Nx 4.0 .