3 * gcc prop_read_array.c -o prop_read_array -lprop -Wall -W -Wextra -ansi -pedantic
9 #include <prop/proplib.h>
11 int main(int argc
, char *argv
[])
14 * Declare a pointer to a prop_array object
15 * Note that prop_array_t is a pointer being
16 * hidden inside a typedef, i.e.
17 * typedef struct _prop_array *prop_array_t;
21 prop_object_iterator_t pit
;
25 /* No effect in NetBSD, but increases portability */
28 /* Check argument count */
30 fprintf(stderr
, "Usage: %s <data.xml>\n", getprogname());
34 /* Read array contents from external XML file */
35 pa
= prop_array_internalize_from_file(argv
[1]);
37 err(EXIT_FAILURE
, "prop_array_internalize_from_file()");
39 /* Skim through every item of the array */
40 printf("-> Iterate with prop_array_count()\n");
41 for (i
= 0; i
< prop_array_count(pa
); i
++) {
42 /* Get the object stored at the i-th array index */
43 po
= prop_array_get(pa
, i
);
45 /* Just a sanity check */
46 if (prop_object_type(po
) != PROP_TYPE_STRING
) {
47 prop_object_release(pa
);
48 errx(EXIT_FAILURE
, "invalid string type");
51 s
= prop_string_cstring_nocopy(po
);
56 * We will now iterate through the items of the array,
57 * but this time we will exploit a prop_array_iterator_t
59 printf("-> Iterate with prop_array_iterator_t\n");
60 pit
= prop_array_iterator(pa
);
62 prop_object_release(pa
);
63 err(EXIT_FAILURE
, "prop_array_iterator()");
67 while((po
= prop_object_iterator_next(pit
)) != NULL
) {
68 s
= prop_string_cstring_nocopy(po
);
73 prop_object_release(pa
);
74 prop_object_iterator_release(pit
);