Add missing error path
[eleutheria.git] / proplib / prop_write_array.c
blobe1136a27b4efc5c828fd78bfe878593d843531da
1 /*
2 * Compile with:
3 * gcc prop_write_array.c -o prop_write_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 #define INIT_CAPACITY 10
13 int
14 main(int argc, char *argv[])
17 * Declare a pointer to a prop_array object
18 * Note that prop_array_t is a pointer being
19 * hidden inside a typedef, i.e.
20 * typedef struct _prop_array *prop_array_t;
22 prop_array_t pa;
23 prop_string_t ps;
24 int i;
27 * Create array object with initial capacity
28 * set to `INIT_CAPACITY'.
29 * Note that the array will expand on demand
30 * by the prop_array_add() with `EXPAND_STEP' step
31 * as defined in libprop/prop_array.c
33 pa = prop_array_create_with_capacity(INIT_CAPACITY);
34 if (pa == NULL)
35 err(EXIT_FAILURE, "prop_array_create_with_capacity()");
38 * For every argument, create a prop_string_t object
39 * that references it and store it in the array
41 for (i = 0; i < argc; i++) {
42 ps = prop_string_create_cstring_nocopy(argv[i]);
43 if (ps == NULL) {
44 prop_object_release(pa);
45 err(EXIT_FAILURE, "prop_string_create_cstring_nocopy()");
48 if (prop_array_add(pa, ps) == FALSE) {
49 prop_object_release(pa);
50 prop_object_release(ps);
51 err(EXIT_FAILURE, "prop_array_add()");
54 prop_object_release(ps);
57 /* Export array contents to file as XML */
58 if (prop_array_externalize_to_file(pa, "./data.xml") == FALSE) {
59 prop_object_release(pa);
60 err(EXIT_FAILURE, "prop_array_externalize_to_file()");
63 /* Release array object */
64 prop_object_release(pa);
66 return EXIT_SUCCESS;