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>
37 oconfig_item_t *ci_root;
40 static void yyset_in (FILE *fd)
45 oconfig_item_t *oconfig_parse_fh (FILE *fh)
57 status = snprintf (file, sizeof (file), "<fd#%d>", fileno (fh));
59 if ((status < 0) || (status >= sizeof (file))) {
63 file[sizeof (file) - 1] = '\0';
71 fprintf (stderr, "yyparse returned error #%i\n", status);
79 yyset_in ((FILE *) 0);
82 } /* oconfig_item_t *oconfig_parse_fh */
84 oconfig_item_t *oconfig_parse_file (const char *file)
91 fh = fopen (file, "r");
94 fprintf (stderr, "fopen (%s) failed: %s\n", file, strerror (errno));
98 ret = oconfig_parse_fh (fh);
104 } /* oconfig_item_t *oconfig_parse_file */
106 oconfig_item_t *oconfig_clone (const oconfig_item_t *ci_orig)
108 oconfig_item_t *ci_copy;
110 ci_copy = (oconfig_item_t *) malloc (sizeof (*ci_copy));
113 fprintf (stderr, "malloc failed.\n");
116 memset (ci_copy, 0, sizeof (*ci_copy));
117 ci_copy->values = NULL;
118 ci_copy->parent = NULL;
119 ci_copy->children = NULL;
121 ci_copy->key = strdup (ci_orig->key);
122 if (ci_copy->key == NULL)
124 fprintf (stderr, "strdup failed.\n");
129 if (ci_orig->values_num > 0) /* {{{ */
133 ci_copy->values = (oconfig_value_t *) calloc (ci_orig->values_num,
134 sizeof (*ci_copy->values));
135 if (ci_copy->values == NULL)
137 fprintf (stderr, "calloc failed.\n");
142 ci_copy->values_num = ci_orig->values_num;
144 for (i = 0; i < ci_copy->values_num; i++)
146 ci_copy->values[i].type = ci_orig->values[i].type;
147 if (ci_copy->values[i].type == OCONFIG_TYPE_STRING)
149 ci_copy->values[i].value.string
150 = strdup (ci_orig->values[i].value.string);
151 if (ci_copy->values[i].value.string == NULL)
153 fprintf (stderr, "strdup failed.\n");
154 oconfig_free (ci_copy);
158 else /* ci_copy->values[i].type != OCONFIG_TYPE_STRING) */
160 ci_copy->values[i].value = ci_orig->values[i].value;
163 } /* }}} if (ci_orig->values_num > 0) */
165 if (ci_orig->children_num > 0) /* {{{ */
169 ci_copy->children = (oconfig_item_t *) calloc (ci_orig->children_num,
170 sizeof (*ci_copy->children));
171 if (ci_copy->children == NULL)
173 fprintf (stderr, "calloc failed.\n");
174 oconfig_free (ci_copy);
177 ci_copy->children_num = ci_orig->children_num;
179 for (i = 0; i < ci_copy->children_num; i++)
181 oconfig_item_t *child;
183 child = oconfig_clone (ci_orig->children + i);
186 oconfig_free (ci_copy);
189 child->parent = ci_copy;
190 ci_copy->children[i] = *child;
192 } /* for (i = 0; i < ci_copy->children_num; i++) */
193 } /* }}} if (ci_orig->children_num > 0) */
196 } /* oconfig_item_t *oconfig_clone */
198 void oconfig_free (oconfig_item_t *ci)
208 for (i = 0; i < ci->values_num; i++)
209 if ((ci->values[i].type == OCONFIG_TYPE_STRING)
210 && (NULL != ci->values[i].value.string))
211 free (ci->values[i].value.string);
213 if (ci->values != NULL)
216 for (i = 0; i < ci->children_num; i++)
217 oconfig_free (ci->children + i);
219 if (ci->children != NULL)
224 * vim:shiftwidth=2:tabstop=8:softtabstop=2:fdm=marker