xref: /netbsd-src/common/lib/libprop/prop_array.3 (revision a792b8435ef0aa524ea0324495e533001a8cb8e6)
1*a792b843Sthorpej.\"	$NetBSD: prop_array.3,v 1.15 2020/06/06 21:25:59 thorpej Exp $
2774eb1a3Sthorpej.\"
36947118aSbad.\" Copyright (c) 2006, 2009 The NetBSD Foundation, Inc.
4774eb1a3Sthorpej.\" All rights reserved.
5774eb1a3Sthorpej.\"
6774eb1a3Sthorpej.\" This code is derived from software contributed to The NetBSD Foundation
7774eb1a3Sthorpej.\" by Jason R. Thorpe.
8774eb1a3Sthorpej.\"
9774eb1a3Sthorpej.\" Redistribution and use in source and binary forms, with or without
10774eb1a3Sthorpej.\" modification, are permitted provided that the following conditions
11774eb1a3Sthorpej.\" are met:
12774eb1a3Sthorpej.\" 1. Redistributions of source code must retain the above copyright
13774eb1a3Sthorpej.\" notice, this list of conditions and the following disclaimer.
14774eb1a3Sthorpej.\" 2. Redistributions in binary form must reproduce the above copyright
15774eb1a3Sthorpej.\" notice, this list of conditions and the following disclaimer in the
16774eb1a3Sthorpej.\" documentation and/or other materials provided with the distribution.
17774eb1a3Sthorpej.\"
18774eb1a3Sthorpej.\" THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
19774eb1a3Sthorpej.\" ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
20774eb1a3Sthorpej.\" TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
21774eb1a3Sthorpej.\" PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
22774eb1a3Sthorpej.\" BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
23774eb1a3Sthorpej.\" CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
24774eb1a3Sthorpej.\" SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
25774eb1a3Sthorpej.\" INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
26774eb1a3Sthorpej.\" CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
27774eb1a3Sthorpej.\" ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
28774eb1a3Sthorpej.\" POSSIBILITY OF SUCH DAMAGE.
29774eb1a3Sthorpej.\"
306947118aSbad.Dd October 10, 2009
31774eb1a3Sthorpej.Dt PROP_ARRAY 3
32774eb1a3Sthorpej.Os
33774eb1a3Sthorpej.Sh NAME
34774eb1a3Sthorpej.Nm prop_array ,
35774eb1a3Sthorpej.Nm prop_array_create ,
36774eb1a3Sthorpej.Nm prop_array_create_with_capacity ,
37774eb1a3Sthorpej.Nm prop_array_copy ,
38774eb1a3Sthorpej.Nm prop_array_copy_mutable ,
39774eb1a3Sthorpej.Nm prop_array_capacity ,
40774eb1a3Sthorpej.Nm prop_array_count ,
41774eb1a3Sthorpej.Nm prop_array_ensure_capacity ,
42774eb1a3Sthorpej.Nm prop_array_iterator ,
43774eb1a3Sthorpej.Nm prop_array_make_immutable ,
44774eb1a3Sthorpej.Nm prop_array_mutable ,
45774eb1a3Sthorpej.Nm prop_array_get ,
46774eb1a3Sthorpej.Nm prop_array_set ,
47774eb1a3Sthorpej.Nm prop_array_add ,
483e69f1b2Sthorpej.Nm prop_array_remove ,
49d21620b2Sthorpej.Nm prop_array_externalize ,
50d21620b2Sthorpej.Nm prop_array_internalize ,
51d21620b2Sthorpej.Nm prop_array_externalize_to_file ,
52d21620b2Sthorpej.Nm prop_array_internalize_from_file ,
536947118aSbad.Nm prop_array_externalize_to_pref ,
54afca4e3bSjym.Nm prop_array_internalize_from_pref ,
553e69f1b2Sthorpej.Nm prop_array_equals
56774eb1a3Sthorpej.Nd array property collection object
57774eb1a3Sthorpej.Sh LIBRARY
58774eb1a3Sthorpej.Lb libprop
59774eb1a3Sthorpej.Sh SYNOPSIS
60774eb1a3Sthorpej.In prop/proplib.h
61774eb1a3Sthorpej.\"
62774eb1a3Sthorpej.Ft prop_array_t
63774eb1a3Sthorpej.Fn prop_array_create "void"
64774eb1a3Sthorpej.Ft prop_array_t
65774eb1a3Sthorpej.Fn prop_array_create_with_capacity "unsigned int capacity"
66774eb1a3Sthorpej.\"
67774eb1a3Sthorpej.Ft prop_array_t
68774eb1a3Sthorpej.Fn prop_array_copy "prop_array_t array"
69774eb1a3Sthorpej.Ft prop_array_t
70774eb1a3Sthorpej.Fn prop_array_copy_mutable "prop_array_t array"
71774eb1a3Sthorpej.\"
72774eb1a3Sthorpej.Ft unsigned int
73774eb1a3Sthorpej.Fn prop_array_capacity "prop_array_t array"
74774eb1a3Sthorpej.Ft unsigned int
75774eb1a3Sthorpej.Fn prop_array_count "prop_array_t array"
7604377267Sthorpej.Ft bool
77774eb1a3Sthorpej.Fn prop_array_ensure_capacity "prop_array_t array" "unsigned int capacity"
78774eb1a3Sthorpej.\"
79774eb1a3Sthorpej.Ft prop_object_iterator_t
80774eb1a3Sthorpej.Fn prop_array_iterator "prop_array_t array"
81774eb1a3Sthorpej.\"
82774eb1a3Sthorpej.Ft void
83774eb1a3Sthorpej.Fn prop_array_make_immutable "prop_array_t array"
8404377267Sthorpej.Ft bool
85774eb1a3Sthorpej.Fn prop_array_mutable "prop_array_t array"
86774eb1a3Sthorpej.\"
87774eb1a3Sthorpej.Ft prop_object_t
88774eb1a3Sthorpej.Fn prop_array_get "prop_array_t array" "unsigned int index"
8904377267Sthorpej.Ft bool
90774eb1a3Sthorpej.Fn prop_array_set "prop_array_t array" "unsigned int index" "prop_object_t obj"
9104377267Sthorpej.Ft bool
92774eb1a3Sthorpej.Fn prop_array_add "prop_array_t array" "prop_object_t obj"
93774eb1a3Sthorpej.Ft void
94774eb1a3Sthorpej.Fn prop_array_remove "prop_array_t array" "unsigned int index"
953e69f1b2Sthorpej.\"
96d21620b2Sthorpej.Ft char *
97d21620b2Sthorpej.Fn prop_array_externalize "prop_array_t array"
98d21620b2Sthorpej.Ft prop_array_t
99d21620b2Sthorpej.Fn prop_array_internalize "const char *xml"
100d21620b2Sthorpej.\"
10104377267Sthorpej.Ft bool
102d21620b2Sthorpej.Fn prop_array_externalize_to_file "prop_array_t array" "const char *path"
103d21620b2Sthorpej.Ft prop_array_t
104d21620b2Sthorpej.Fn prop_array_internalize_from_file "const char *path"
105d21620b2Sthorpej.\"
10604377267Sthorpej.Ft bool
1076947118aSbad.Fn prop_array_externalize_to_pref "prop_array_t array" "struct plistref *pref"
108afca4e3bSjym.Ft bool
109afca4e3bSjym.Fn prop_array_internalize_from_pref "const struct plistref *pref" \
110afca4e3bSjym    "prop_array_t *arrayp"
1116947118aSbad.\"
1126947118aSbad.Ft bool
1133e69f1b2Sthorpej.Fn prop_array_equals "prop_array_t array1" "prop_array_t array2"
114774eb1a3Sthorpej.Sh DESCRIPTION
115774eb1a3SthorpejThe
116*a792b843Sthorpej.Nm
117774eb1a3Sthorpejfamily of functions operate on the array property collection object type.
118774eb1a3SthorpejAn array is an ordered set; an iterated array will return objects in the
119774eb1a3Sthorpejsame order with which they were stored.
120774eb1a3Sthorpej.Bl -tag -width "xxxxx"
121774eb1a3Sthorpej.It Fn prop_array_create "void"
122774eb1a3SthorpejCreate an empty array.
123774eb1a3SthorpejThe array initially has no capacity.
124428727f4SchristosReturns
125428727f4Schristos.Dv NULL
126428727f4Schristoson failure.
127774eb1a3Sthorpej.It Fn prop_array_create_with_capacity "unsigned int capacity"
128774eb1a3SthorpejCreate an array with the capacity to store
129774eb1a3Sthorpej.Fa capacity
130774eb1a3Sthorpejobjects.
131428727f4SchristosReturns
132428727f4Schristos.Dv NULL
133428727f4Schristoson failure.
134774eb1a3Sthorpej.It Fn prop_array_copy "prop_array_t array"
135774eb1a3SthorpejCopy an array.
136774eb1a3SthorpejThe new array has an initial capacity equal to the number of objects stored
137774eb1a3Sthorpejin the array being copied.
138774eb1a3SthorpejThe new array contains references to the original array's objects, not
139774eb1a3Sthorpejcopies of those objects
140774eb1a3Sthorpej.Pq i.e. a shallow copy is made .
141774eb1a3SthorpejIf the original array is immutable, the resulting array is also immutable.
142428727f4SchristosReturns
143428727f4Schristos.Dv NULL
144428727f4Schristoson failure.
145774eb1a3Sthorpej.It Fn prop_array_copy_mutable "prop_array_t array"
146774eb1a3SthorpejLike
147774eb1a3Sthorpej.Fn prop_array_copy ,
148774eb1a3Sthorpejexcept the resulting array is always mutable.
149774eb1a3Sthorpej.It Fn prop_array_capacity "prop_array_t array"
150774eb1a3SthorpejReturns the total capacity of the array, including objects already stored
151774eb1a3Sthorpejin the array.
152428727f4SchristosIf the supplied object isn't an array, zero is returned.
153774eb1a3Sthorpej.It Fn prop_array_count "prop_array_t array"
154774eb1a3SthorpejReturns the number of objects stored in the array.
155428727f4SchristosIf the supplied object isn't an array, zero is returned.
156774eb1a3Sthorpej.It Fn prop_array_ensure_capacity "prop_array_t array" "unsigned int capacity"
157774eb1a3SthorpejEnsure that the array has a total capacity of
158774eb1a3Sthorpej.Fa capacity ,
159774eb1a3Sthorpejincluding objects already stored in the array.
160774eb1a3SthorpejReturns
16104377267Sthorpej.Dv true
162774eb1a3Sthorpejif the capacity of the array is greater or equal to
163774eb1a3Sthorpej.Fa capacity
164774eb1a3Sthorpejor if expansion of the array's capacity was successful
165774eb1a3Sthorpejand
16604377267Sthorpej.Dv false
167774eb1a3Sthorpejotherwise.
168774eb1a3Sthorpej.It Fn prop_array_iterator "prop_array_t array"
169774eb1a3SthorpejCreate an iterator for the array.
170774eb1a3SthorpejThe array is retained by the iterator.
171774eb1a3SthorpejAn array iterator returns the object references stored in the array.
172774eb1a3SthorpejStoring to or removing from the array invalidates any active iterators for
173774eb1a3Sthorpejthe array.
174428727f4SchristosReturns
175428727f4Schristos.Dv NULL
176428727f4Schristoson failure.
177774eb1a3Sthorpej.It Fn prop_array_make_immutable "prop_array_t array"
178774eb1a3SthorpejMake
179774eb1a3Sthorpej.Fa array
180774eb1a3Sthorpejimmutable.
181774eb1a3Sthorpej.It Fn prop_array_mutable "prop_array_t array"
182774eb1a3SthorpejReturns
18304377267Sthorpej.Dv true
184774eb1a3Sthorpejif the array is mutable.
185774eb1a3Sthorpej.It Fn prop_array_get "prop_array_t array" "unsigned int index"
186774eb1a3SthorpejReturn the object stored at the array index
187774eb1a3Sthorpej.Fa index .
188428727f4SchristosReturns
189428727f4Schristos.Dv NULL
190428727f4Schristoson failure.
191774eb1a3Sthorpej.It Fn prop_array_set "prop_array_t array" "unsigned int index" \
192774eb1a3Sthorpej       "prop_object_t obj"
193774eb1a3SthorpejStore a reference to the object
194774eb1a3Sthorpej.Fa obj
195774eb1a3Sthorpejat the array index
196774eb1a3Sthorpej.Fa index .
197774eb1a3SthorpejThis function is not allowed to create holes in the array;
198774eb1a3Sthorpejthe caller must either be setting the object just beyond the existing
199774eb1a3Sthorpejcount or replacing an already existing object reference.
200774eb1a3SthorpejThe object will be retained by the array.
201774eb1a3SthorpejIf an existing object reference is being replaced, that object will be
202774eb1a3Sthorpejreleased.
203774eb1a3SthorpejReturns
20404377267Sthorpej.Dv true
205774eb1a3Sthorpejif storing the object was successful and
20604377267Sthorpej.Dv false
207774eb1a3Sthorpejotherwise.
208774eb1a3Sthorpej.It Fn prop_array_add "prop_array_t array" "prop_object_t obj"
209774eb1a3SthorpejAdd a reference to the object
210774eb1a3Sthorpej.Fa obj
211c05cd035Smjfto the array, appending to the end and growing the array's capacity if
212774eb1a3Sthorpejnecessary.
213774eb1a3SthorpejThe object will be retained by the array.
214774eb1a3SthorpejReturns
21504377267Sthorpej.Dv true
216774eb1a3Sthorpejif storing the object was successful and
21704377267Sthorpej.Dv false
218774eb1a3Sthorpejotherwise.
219428727f4Schristos.Pp
220428727f4SchristosDuring expansion, array's capacity is augmented by the
221428727f4Schristos.Dv EXPAND_STEP
222428727f4Schristosconstant, as defined in
223428727f4Schristos.Pa libprop/prop_array.c
224428727f4Schristosfile, e.g.
225428727f4Schristos.Pp
2261491f958Sjoerg.Dl #define	EXPAND_STEP		16
227774eb1a3Sthorpej.It Fn prop_array_remove "prop_array_t array" "unsigned int index"
228774eb1a3SthorpejRemove the reference to the object stored at array index
229774eb1a3Sthorpej.Fa index .
230774eb1a3SthorpejThe object will be released and the array compacted following
231774eb1a3Sthorpejthe removal.
232d21620b2Sthorpej.It Fn prop_array_externalize "prop_array_t array"
233d21620b2SthorpejExternalizes an array, returning a NUL-terminated buffer containing
234d21620b2Sthorpejthe XML representation of the array.
235d21620b2SthorpejThe caller is responsible for freeing the returned buffer.
236d21620b2SthorpejIf converting to the external representation fails for any reason,
237d21620b2Sthorpej.Dv NULL
238d21620b2Sthorpejis returned.
239d21620b2Sthorpej.Pp
240d21620b2SthorpejIn user space, the buffer is allocated using
241d21620b2Sthorpej.Xr malloc 3 .
242d21620b2SthorpejIn the kernel, the buffer is allocated using
243d21620b2Sthorpej.Xr malloc 9
244d21620b2Sthorpejusing the malloc type
245d21620b2Sthorpej.Dv M_TEMP .
246d21620b2Sthorpej.It Fn prop_array_internalize "const char *xml"
247d21620b2SthorpejParse the XML representation of a property list in the NUL-terminated
248d21620b2Sthorpejbuffer
249d21620b2Sthorpej.Fa xml
250d21620b2Sthorpejand return the corresponding array.
251d21620b2SthorpejReturns
252d21620b2Sthorpej.Dv NULL
253d21620b2Sthorpejif parsing fails for any reason.
254d21620b2Sthorpej.It Fn prop_array_externalize_to_file "prop_array_t array" "const char *path"
255d21620b2SthorpejExternalizes an array and writes it to the file specified by
256d21620b2Sthorpej.Fa path .
257d21620b2SthorpejThe file is saved with the mode
258d21620b2Sthorpej.Dv 0666
259d21620b2Sthorpejas modified by the process's file creation mask
260236d2fbfSnjoly.Pq see Xr umask 2
261d21620b2Sthorpejand is written atomically.
262d21620b2SthorpejReturns
26304377267Sthorpej.Dv false
264d21620b2Sthorpejif externalizing or writing the array fails for any reason.
265d21620b2Sthorpej.It Fn prop_array_internalize_from_file "const char *path"
266d21620b2SthorpejReads the XML property list contained in the file specified by
267d21620b2Sthorpej.Fa path ,
268d21620b2Sthorpejinternalizes it, and returns the corresponding array.
269428727f4SchristosReturns
270428727f4Schristos.Dv NULL
271428727f4Schristoson failure.
2726947118aSbad.It Fn prop_array_externalize_to_pref "prop_array_t array" \
2736947118aSbad    "struct plistref *pref"
2746947118aSbadExternalizes an array and packs it into the plistref specified by
2756947118aSbad.Fa pref .
2766947118aSbadReturns
2776947118aSbad.Dv false
2786947118aSbadif externalizing the array fails for any reason.
279afca4e3bSjym.It Fn prop_array_internalize_from_pref "const struct plistref *pref" \
280afca4e3bSjym    "prop_array_t *arrayp"
281afca4e3bSjymReads the plistref specified by
282afca4e3bSjym.Fa pref ,
283afca4e3bSjyminternalizes it, and returns the corresponding array.
284afca4e3bSjymReturns
285afca4e3bSjym.Dv false
286afca4e3bSjymif internalizing or writing the array fails for any reason.
287d3b64ea6Sdholland.It Fn prop_array_equals "prop_array_t array1" "prop_array_t array2"
288d3b64ea6SdhollandReturns
289d3b64ea6Sdholland.Dv true
290d3b64ea6Sdhollandif the two arrays are equivalent.
291d3b64ea6SdhollandIf at least one of the supplied objects isn't an array,
292d3b64ea6Sdholland.Dv false
293d3b64ea6Sdhollandis returned.
294d3b64ea6SdhollandNote: Objects contained in the array are compared by value, not by reference.
295774eb1a3Sthorpej.El
296774eb1a3Sthorpej.Sh SEE ALSO
297*a792b843Sthorpej.Xr prop_array_util 3 ,
298774eb1a3Sthorpej.Xr prop_bool 3 ,
299774eb1a3Sthorpej.Xr prop_data 3 ,
300774eb1a3Sthorpej.Xr prop_dictionary 3 ,
301774eb1a3Sthorpej.Xr prop_number 3 ,
302774eb1a3Sthorpej.Xr prop_object 3 ,
303774eb1a3Sthorpej.Xr prop_string 3 ,
304774eb1a3Sthorpej.Xr proplib 3
305774eb1a3Sthorpej.Sh HISTORY
306774eb1a3SthorpejThe
3073ed54b8dSabhinav.Xr proplib 3
308774eb1a3Sthorpejproperty container object library first appeared in
309774eb1a3Sthorpej.Nx 4.0 .
310