Dummy commit to test new ssh key
[eleutheria.git] / proplib / prop_read_array_.c
blob235a21447b559d52b68228c5f27e9d6983ec60bf
1 /*
2 * Compile with:
3 * gcc prop_read_array.c -o prop_read_array -lprop -Wall -W -Wextra -ansi -pedantic
4 */
6 #include <err.h>
7 #include <stdio.h>
8 #include <stdlib.h>
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;
19 prop_array_t pa;
20 prop_object_t po;
21 prop_object_iterator_t pit;
22 unsigned int i;
23 const char *s;
25 /* No effect in NetBSD, but increases portability */
26 setprogname(argv[0]);
28 /* Check argument count */
29 if (argc != 2) {
30 fprintf(stderr, "Usage: %s <data.xml>\n", getprogname());
31 exit(EXIT_FAILURE);
34 /* Read array contents from external XML file */
35 pa = prop_array_internalize_from_file(argv[1]);
36 if (pa == NULL)
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);
52 printf("%s\n", s);
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);
61 if (pit == NULL) {
62 prop_object_release(pa);
63 err(EXIT_FAILURE, "prop_array_iterator()");
66 /* Traverse */
67 while((po = prop_object_iterator_next(pit)) != NULL) {
68 s = prop_string_cstring_nocopy(po);
69 printf("%s\n", s);
72 /* Release objects */
73 prop_object_release(pa);
74 prop_object_iterator_release(pit);
76 return EXIT_SUCCESS;