1 /**
2 * collection4 - rrd_args.c
3 * Copyright (C) 2010 Florian octo Forster
4 *
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public
7 * License as published by the Free Software Foundation; either
8 * version 2.1 of the License, or (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
14 *
15 * You should have received a copy of the GNU Lesser General Public
16 * License along with this program; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin Street, Fifth Floor,
18 * Boston, MA 02110-1301 USA
19 *
20 * Authors:
21 * Florian octo Forster <ff at octo.it>
22 **/
24 #include <stdlib.h>
25 #include <string.h>
26 #include <errno.h>
28 #include "rrd_args.h"
30 rrd_args_t *ra_create (void) /* {{{ */
31 {
32 rrd_args_t *ra;
34 ra = malloc (sizeof (*ra));
35 if (ra == NULL)
36 return (NULL);
37 memset (ra, 0, sizeof (*ra));
39 ra->options = array_create ();
40 ra->data = array_create ();
41 ra->calc = array_create ();
42 ra->areas = array_create ();
43 ra->lines = array_create ();
45 if ((ra->options == NULL)
46 || (ra->data == NULL)
47 || (ra->calc == NULL)
48 || (ra->areas == NULL)
49 || (ra->lines == NULL))
50 {
51 ra_destroy (ra);
52 return (NULL);
53 }
55 return (ra);
56 } /* }}} rrd_args_t *ra_create */
58 void ra_destroy (rrd_args_t *ra) /* {{{ */
59 {
60 if (ra == NULL)
61 return;
63 array_destroy (ra->options);
64 array_destroy (ra->data);
65 array_destroy (ra->calc);
66 array_destroy (ra->areas);
67 array_destroy (ra->lines);
69 free (ra);
70 } /* }}} void ra_destroy */
72 int ra_argc (rrd_args_t *ra)
73 {
74 if (ra == NULL)
75 return (-EINVAL);
77 return (array_argc (ra->options)
78 + array_argc (ra->data)
79 + array_argc (ra->calc)
80 + array_argc (ra->areas)
81 + array_argc (ra->lines));
82 } /* }}} int ra_argc */
84 char **ra_argv (rrd_args_t *ra) /* {{{ */
85 {
86 size_t argc;
87 char **argv;
89 size_t pos;
90 int tmp;
92 if (ra == NULL)
93 return (NULL);
95 tmp = ra_argc (ra);
96 if (tmp < 0)
97 return (NULL);
98 argc = (size_t) tmp;
100 argv = calloc (argc + 1, sizeof (*argv));
101 if (argv == NULL)
102 return (NULL);
104 pos = 0;
105 argv[0] = NULL;
107 #define APPEND_FIELD(field) do \
108 { \
109 size_t ary_argc; \
110 char **ary_argv; \
111 \
112 ary_argc = (size_t) array_argc (ra->field); \
113 ary_argv = array_argv (ra->field); \
114 if ((ary_argc > 0) && (ary_argv != NULL)) \
115 { \
116 memcpy (argv + pos, ary_argv, ary_argc * sizeof (*ary_argv)); \
117 pos += ary_argc; \
118 argv[pos] = NULL; \
119 } \
120 } while (0)
122 APPEND_FIELD (options);
123 APPEND_FIELD (data);
124 APPEND_FIELD (calc);
125 APPEND_FIELD (areas);
126 APPEND_FIELD (lines);
128 #undef APPEND_FIELD
130 return (argv);
131 } /* }}} char **ra_argv */
133 void ra_argv_free (char **argv) /* {{{ */
134 {
135 /* The pointers contained in the "argv" come from "array_argv". We don't need
136 * to free them. We only need to free what we actually alloced directly in
137 * "ra_argv". */
138 free (argv);
139 } /* }}} void ra_argv_free */
141 /* vim: set sw=2 sts=2 et fdm=marker : */