Add missing error path
[eleutheria.git] / proplib / prop_expand_array.c
blob007b7ab8add472189d9e204eb268dbf6f2ccf5e2
1 /* Broken, tomorrow will be fixed :) */
2 /*
3 * Compile with:
4 * gcc prop_expand_array.c -o prop_expand_array -lprop -Wall -W -Wextra -ansi -pedantic
5 */
7 #include <err.h>
8 #include <stdio.h>
9 #include <stdlib.h>
10 #include <prop/proplib.h>
12 #define INIT_CAPACITY 10
13 #define NUM_STRINGS 32
14 #define PRINT_STEP 5 /* Print stats every 5 steps */
16 /* Function prototypes */
17 void print_array_stats(const prop_array_t pa);
19 int
20 main(int argc, char *argv[])
22 prop_array_t pa;
23 prop_string_t ps;
24 unsigned int i;
27 * Create array object with initial capacity set to
28 * `INIT_CAPACITY'. The array will expand on demand
29 * if its count exceeds its capacity.
30 * This happens in prop_array_add() with `EXPAND_STEP'
31 * step, as defined in libprop/prop_array.c file.
33 pa = prop_array_create_with_capacity(INIT_CAPACITY);
34 if (pa == NULL)
35 err(EXIT_FAILURE, "prop_array_create_with_capacity()");
37 /* Create prop_string_t object */
38 ps = prop_string_create_cstring("foo");
39 if (ps == NULL) {
40 prop_object_release(pa);
41 err(EXIT_FAILURE, "prop_string_create_cstring()");
45 * Add up to `NUM_STRINGS' references of prop_string_t
46 * object to array and watch if/how the latter expands on demand.
48 for (i = 0; i < NUM_STRINGS; i++) {
49 /* Print statistics every `PRINT_STEP' step */
50 if (i % PRINT_STEP == 0)
51 print_array_stats(pa);
53 /* Add object reference in array */
54 if (prop_array_add(pa, ps) == FALSE) {
55 prop_object_release(ps);
56 prop_object_release(pa);
57 err(EXIT_FAILURE, "prop_array_add()");
61 CLEANUP:;
63 * Remove references from array and note that
64 * if an expansion has happened before, array's
65 * capacity won't reduce to its initial value,
66 * i.e. `INIT_CAPACITY'.
68 for (i = 0; i < NUM_STRINGS; i++) {
69 /* Print statistics every `PRINT_STEP' step */
70 if (i % PRINT_STEP == 0)
71 print_array_stats(pa);
73 prop_array_remove(pa, NUM_STRINGS - i - 1);
76 print_array_stats(pa);
78 /* Release objects */
79 prop_object_release(ps);
80 prop_object_release(pa);
82 return EXIT_SUCCESS;
85 void print_array_stats(const prop_array_t pa)
87 printf("count = %u\tcapacity = %u\n",
88 prop_array_count(pa),
89 prop_array_capacity(pa));