Dummy commit to test new ssh key
[eleutheria.git] / proplib / prop_write_array.c
blob63fc743b391353740b2c0ac83f9126871f2c5e0b
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 main(int argc, char *argv[])
16 * Declare a pointer to a prop_array object
17 * Note that prop_array_t is a pointer being
18 * hidden inside a typedef, i.e.
19 * typedef struct _prop_array *prop_array_t;
21 prop_array_t pa;
22 prop_string_t ps;
23 int i;
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 [arguments]\n", getprogname());
31 exit(EXIT_FAILURE);
35 * Create array object with initial capacity set to `INIT_CAPACITY'.
36 * Note that the array will expand on demand by the prop_array_add()
37 * with `EXPAND_STEP' step as defined in libprop/prop_array.c
39 pa = prop_array_create_with_capacity(INIT_CAPACITY);
40 if (pa == NULL)
41 err(EXIT_FAILURE, "prop_array_create_with_capacity()");
44 * For every argument, create a prop_string_t object
45 * that references it and store it in the array
47 for (i = 0; i < argc; i++) {
48 ps = prop_string_create_cstring_nocopy(argv[i]);
49 if (ps == NULL) {
50 prop_object_release(pa);
51 err(EXIT_FAILURE, "prop_string_create_cstring_nocopy()");
54 if (prop_array_add(pa, ps) == false) {
55 prop_object_release(pa);
56 prop_object_release(ps);
57 err(EXIT_FAILURE, "prop_array_add()");
60 prop_object_release(ps);
63 /* Export array contents to file as XML */
64 if (prop_array_externalize_to_file(pa, argv[1]) == false) {
65 prop_object_release(pa);
66 err(EXIT_FAILURE, "prop_array_externalize_to_file()");
69 /* Release array object */
70 prop_object_release(pa);
72 return EXIT_SUCCESS;