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)
56 status = snprintf (file, sizeof (file), "<fd#%d>", fileno (fh));
58 if ((status < 0) || (status >= sizeof (file))) {
62 file[sizeof (file) - 1] = '\0';
70 fprintf (stderr, "yyparse returned error #%i\n", status);
78 yyset_in ((FILE *) 0);
81 } /* oconfig_item_t *oconfig_parse_fh */
83 oconfig_item_t *oconfig_parse_file (const char *file)
90 fh = fopen (file, "r");
93 fprintf (stderr, "fopen (%s) failed: %s\n", file, strerror (errno));
97 ret = oconfig_parse_fh (fh);
103 } /* oconfig_item_t *oconfig_parse_file */
105 oconfig_item_t *oconfig_clone (const oconfig_item_t *ci_orig)
107 oconfig_item_t *ci_copy;
109 ci_copy = (oconfig_item_t *) malloc (sizeof (*ci_copy));
112 fprintf (stderr, "malloc failed.\n");
115 memset (ci_copy, 0, sizeof (*ci_copy));
116 ci_copy->values = NULL;
117 ci_copy->parent = NULL;
118 ci_copy->children = NULL;
120 ci_copy->key = strdup (ci_orig->key);
121 if (ci_copy->key == NULL)
123 fprintf (stderr, "strdup failed.\n");
128 if (ci_orig->values_num > 0) /* {{{ */
132 ci_copy->values = (oconfig_value_t *) calloc (ci_orig->values_num,
133 sizeof (*ci_copy->values));
134 if (ci_copy->values == NULL)
136 fprintf (stderr, "calloc failed.\n");
141 ci_copy->values_num = ci_orig->values_num;
143 for (i = 0; i < ci_copy->values_num; i++)
145 ci_copy->values[i].type = ci_orig->values[i].type;
146 if (ci_copy->values[i].type == OCONFIG_TYPE_STRING)
148 ci_copy->values[i].value.string
149 = strdup (ci_orig->values[i].value.string);
150 if (ci_copy->values[i].value.string == NULL)
152 fprintf (stderr, "strdup failed.\n");
153 oconfig_free (ci_copy);
157 else /* ci_copy->values[i].type != OCONFIG_TYPE_STRING) */
159 ci_copy->values[i].value = ci_orig->values[i].value;
162 } /* }}} if (ci_orig->values_num > 0) */
164 if (ci_orig->children_num > 0) /* {{{ */
168 ci_copy->children = (oconfig_item_t *) calloc (ci_orig->children_num,
169 sizeof (*ci_copy->children));
170 if (ci_copy->children == NULL)
172 fprintf (stderr, "calloc failed.\n");
173 oconfig_free (ci_copy);
176 ci_copy->children_num = ci_orig->children_num;
178 for (i = 0; i < ci_copy->children_num; i++)
180 oconfig_item_t *child;
182 child = oconfig_clone (ci_orig->children + i);
185 oconfig_free (ci_copy);
188 child->parent = ci_copy;
189 ci_copy->children[i] = *child;
191 } /* for (i = 0; i < ci_copy->children_num; i++) */
192 } /* }}} if (ci_orig->children_num > 0) */
195 } /* oconfig_item_t *oconfig_clone */
197 static void oconfig_free_all (oconfig_item_t *ci)
207 for (i = 0; i < ci->values_num; i++)
208 if ((ci->values[i].type == OCONFIG_TYPE_STRING)
209 && (NULL != ci->values[i].value.string))
210 free (ci->values[i].value.string);
212 if (ci->values != NULL)
215 for (i = 0; i < ci->children_num; i++)
216 oconfig_free_all (ci->children + i);
218 if (ci->children != NULL)
222 void oconfig_free (oconfig_item_t *ci)
224 oconfig_free_all (ci);
230 * vim:shiftwidth=2:tabstop=8:softtabstop=2:fdm=marker