Add some comments plus fix a possible mem leak
[eleutheria.git] / proplib / prop_write_array.c
blobba4c9aa35dcd0ae1d920892ebf579feb6dc774a8
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;
26 /* No effect in NetBSD, but increases portability */
27 setprogname(argv[0]);
29 /* Check argument count */
30 if (argc < 2) {
31 fprintf(stderr, "Usage: %s data.xml [arguments]\n", getprogname());
32 exit(EXIT_FAILURE);
36 * Create array object with initial capacity
37 * set to `INIT_CAPACITY'.
38 * Note that the array will expand on demand
39 * by the prop_array_add() with `EXPAND_STEP' step
40 * as defined in libprop/prop_array.c
42 pa = prop_array_create_with_capacity(INIT_CAPACITY);
43 if (pa == NULL)
44 err(EXIT_FAILURE, "prop_array_create_with_capacity()");
47 * For every argument, create a prop_string_t object
48 * that references it and store it in the array
50 for (i = 0; i < argc; i++) {
51 ps = prop_string_create_cstring_nocopy(argv[i]);
52 if (ps == NULL) {
53 prop_object_release(pa);
54 err(EXIT_FAILURE, "prop_string_create_cstring_nocopy()");
57 if (prop_array_add(pa, ps) == false) {
58 prop_object_release(pa);
59 prop_object_release(ps);
60 err(EXIT_FAILURE, "prop_array_add()");
63 prop_object_release(ps);
66 /* Export array contents to file as XML */
67 if (prop_array_externalize_to_file(pa, argv[1]) == false) {
68 prop_object_release(pa);
69 err(EXIT_FAILURE, "prop_array_externalize_to_file()");
72 /* Release array object */
73 prop_object_release(pa);
75 return EXIT_SUCCESS;