Let user decide the name of the output file
[eleutheria.git] / proplib / prop_read_array_.c
blobae1a4c9fceea952d45b011218afd26c277bfca3f
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[])
13 prop_array_t pa;
14 prop_object_t po;
15 prop_object_iterator_t pit;
16 unsigned int i;
17 const char *s;
19 /* No effect in NetBSD, but increases portability */
20 setprogname(argv[0]);
22 /* Check argument count */
23 if (argc != 2) {
24 fprintf(stderr, "Usage: %s <data.xml>\n", getprogname());
25 exit(EXIT_FAILURE);
28 /* Read array contents from external XML file */
29 pa = prop_array_internalize_from_file(argv[1]);
30 if (pa == NULL)
31 err(EXIT_FAILURE, "prop_array_internalize_from_file()");
33 /* Skim through every item of the array */
34 for (i = 0; i < prop_array_count(pa); i++) {
35 /* Get the object stored at the i-th array index */
36 po = prop_array_get(pa, i);
38 /* Just a sanity check */
39 if (prop_object_type(po) != PROP_TYPE_STRING) {
40 prop_object_release(pa);
41 errx(EXIT_FAILURE, "invalid string type");
44 s = prop_string_cstring_nocopy(po);
45 printf("%s\n", s);
49 * We will now iterate through the items of the array,
50 * but this time we will exploit a prop_array_iterator_t
52 pit = prop_array_iterator(pa);
53 if (pit == NULL) {
54 prop_object_release(pa);
55 err(EXIT_FAILURE, "prop_array_iterator()");
58 /* Traverse */
59 while((po = prop_object_iterator_next(pit)) != NULL) {
60 s = prop_string_cstring_nocopy(po);
61 printf("%s\n", s);
64 /* Release objects */
65 prop_object_release(pa);
66 prop_object_iterator_release(pit);
68 return EXIT_SUCCESS;