2 * collectd - src/liboconfig/oconfig.c
3 * Copyright (C) 2006,2007 Florian Forster
5 * Permission is hereby granted, free of charge, to any person obtaining a
6 * copy of this software and associated documentation files (the "Software"),
7 * to deal in the Software without restriction, including without limitation
8 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
9 * and/or sell copies of the Software, and to permit persons to whom the
10 * Software is furnished to do so, subject to the following conditions:
12 * The above copyright notice and this permission notice shall be included in
13 * all copies or substantial portions of the Software.
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
21 * DEALINGS IN THE SOFTWARE.
24 * Florian Forster <octo at collectd.org>
36 extern int yyparse (void);
38 oconfig_item_t *ci_root;
41 static void yyset_in (FILE *fd)
46 oconfig_item_t *oconfig_parse_fh (FILE *fh)
58 status = snprintf (file, sizeof (file), "<fd#%d>", fileno (fh));
60 if ((status < 0) || (status >= sizeof (file))) {
64 file[sizeof (file) - 1] = '\0';
72 fprintf (stderr, "yyparse returned error #%i\n", status);
80 yyset_in ((FILE *) 0);
83 } /* oconfig_item_t *oconfig_parse_fh */
85 oconfig_item_t *oconfig_parse_file (const char *file)
92 fh = fopen (file, "r");
95 fprintf (stderr, "fopen (%s) failed: %s\n", file, strerror (errno));
99 ret = oconfig_parse_fh (fh);
105 } /* oconfig_item_t *oconfig_parse_file */
107 oconfig_item_t *oconfig_clone (const oconfig_item_t *ci_orig)
109 oconfig_item_t *ci_copy;
111 ci_copy = (oconfig_item_t *) malloc (sizeof (*ci_copy));
114 fprintf (stderr, "malloc failed.\n");
117 memset (ci_copy, 0, sizeof (*ci_copy));
118 ci_copy->values = NULL;
119 ci_copy->parent = NULL;
120 ci_copy->children = NULL;
122 ci_copy->key = strdup (ci_orig->key);
123 if (ci_copy->key == NULL)
125 fprintf (stderr, "strdup failed.\n");
130 if (ci_orig->values_num > 0) /* {{{ */
134 ci_copy->values = (oconfig_value_t *) calloc (ci_orig->values_num,
135 sizeof (*ci_copy->values));
136 if (ci_copy->values == NULL)
138 fprintf (stderr, "calloc failed.\n");
143 ci_copy->values_num = ci_orig->values_num;
145 for (i = 0; i < ci_copy->values_num; i++)
147 ci_copy->values[i].type = ci_orig->values[i].type;
148 if (ci_copy->values[i].type == OCONFIG_TYPE_STRING)
150 ci_copy->values[i].value.string
151 = strdup (ci_orig->values[i].value.string);
152 if (ci_copy->values[i].value.string == NULL)
154 fprintf (stderr, "strdup failed.\n");
155 oconfig_free (ci_copy);
159 else /* ci_copy->values[i].type != OCONFIG_TYPE_STRING) */
161 ci_copy->values[i].value = ci_orig->values[i].value;
164 } /* }}} if (ci_orig->values_num > 0) */
166 if (ci_orig->children_num > 0) /* {{{ */
170 ci_copy->children = (oconfig_item_t *) calloc (ci_orig->children_num,
171 sizeof (*ci_copy->children));
172 if (ci_copy->children == NULL)
174 fprintf (stderr, "calloc failed.\n");
175 oconfig_free (ci_copy);
178 ci_copy->children_num = ci_orig->children_num;
180 for (i = 0; i < ci_copy->children_num; i++)
182 oconfig_item_t *child;
184 child = oconfig_clone (ci_orig->children + i);
187 oconfig_free (ci_copy);
190 child->parent = ci_copy;
191 ci_copy->children[i] = *child;
193 } /* for (i = 0; i < ci_copy->children_num; i++) */
194 } /* }}} if (ci_orig->children_num > 0) */
197 } /* oconfig_item_t *oconfig_clone */
199 void oconfig_free_all (oconfig_item_t *ci)
209 for (i = 0; i < ci->values_num; i++)
210 if ((ci->values[i].type == OCONFIG_TYPE_STRING)
211 && (NULL != ci->values[i].value.string))
212 free (ci->values[i].value.string);
214 if (ci->values != NULL)
217 for (i = 0; i < ci->children_num; i++)
218 oconfig_free_all (ci->children + i);
220 if (ci->children != NULL)
224 void oconfig_free (oconfig_item_t *ci)
226 oconfig_free_all (ci);
232 * vim:shiftwidth=2:tabstop=8:softtabstop=2:fdm=marker