2 * collection4 - oconfig.c
3 * Copyright (C) 2006-2010 Florian octo Forster
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.
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.
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
21 * Florian octo Forster <ff at octo.it>
35 oconfig_item_t *ci_root;
38 static void yyset_in (FILE *fd)
43 oconfig_item_t *oconfig_parse_fh (FILE *fh)
55 status = snprintf (file, sizeof (file), "<fd#%d>", fileno (fh));
57 if ((status < 0) || (((size_t) status) >= sizeof (file))) {
61 file[sizeof (file) - 1] = '\0';
69 fprintf (stderr, "yyparse returned error #%i\n", status);
77 yyset_in ((FILE *) 0);
80 } /* oconfig_item_t *oconfig_parse_fh */
82 oconfig_item_t *oconfig_parse_file (const char *file)
89 fh = fopen (file, "r");
92 fprintf (stderr, "fopen (%s) failed: %s\n", file, strerror (errno));
96 ret = oconfig_parse_fh (fh);
102 } /* oconfig_item_t *oconfig_parse_file */
104 oconfig_item_t *oconfig_clone (const oconfig_item_t *ci_orig)
106 oconfig_item_t *ci_copy;
108 ci_copy = (oconfig_item_t *) malloc (sizeof (*ci_copy));
111 fprintf (stderr, "malloc failed.\n");
114 memset (ci_copy, 0, sizeof (*ci_copy));
115 ci_copy->values = NULL;
116 ci_copy->parent = NULL;
117 ci_copy->children = NULL;
119 ci_copy->key = strdup (ci_orig->key);
120 if (ci_copy->key == NULL)
122 fprintf (stderr, "strdup failed.\n");
127 if (ci_orig->values_num > 0) /* {{{ */
131 ci_copy->values = (oconfig_value_t *) calloc (ci_orig->values_num,
132 sizeof (*ci_copy->values));
133 if (ci_copy->values == NULL)
135 fprintf (stderr, "calloc failed.\n");
140 ci_copy->values_num = ci_orig->values_num;
142 for (i = 0; i < ci_copy->values_num; i++)
144 ci_copy->values[i].type = ci_orig->values[i].type;
145 if (ci_copy->values[i].type == OCONFIG_TYPE_STRING)
147 ci_copy->values[i].value.string
148 = strdup (ci_orig->values[i].value.string);
149 if (ci_copy->values[i].value.string == NULL)
151 fprintf (stderr, "strdup failed.\n");
152 oconfig_free (ci_copy);
156 else /* ci_copy->values[i].type != OCONFIG_TYPE_STRING) */
158 ci_copy->values[i].value = ci_orig->values[i].value;
161 } /* }}} if (ci_orig->values_num > 0) */
163 if (ci_orig->children_num > 0) /* {{{ */
167 ci_copy->children = (oconfig_item_t *) calloc (ci_orig->children_num,
168 sizeof (*ci_copy->children));
169 if (ci_copy->children == NULL)
171 fprintf (stderr, "calloc failed.\n");
172 oconfig_free (ci_copy);
175 ci_copy->children_num = ci_orig->children_num;
177 for (i = 0; i < ci_copy->children_num; i++)
179 oconfig_item_t *child;
181 child = oconfig_clone (ci_orig->children + i);
184 oconfig_free (ci_copy);
187 child->parent = ci_copy;
188 ci_copy->children[i] = *child;
190 } /* for (i = 0; i < ci_copy->children_num; i++) */
191 } /* }}} if (ci_orig->children_num > 0) */
194 } /* oconfig_item_t *oconfig_clone */
196 void oconfig_free (oconfig_item_t *ci)
206 for (i = 0; i < ci->values_num; i++)
207 if ((ci->values[i].type == OCONFIG_TYPE_STRING)
208 && (NULL != ci->values[i].value.string))
209 free (ci->values[i].value.string);
211 if (ci->values != NULL)
214 for (i = 0; i < ci->children_num; i++)
215 oconfig_free (ci->children + i);
217 if (ci->children != NULL)
222 * vim:shiftwidth=2:tabstop=8:softtabstop=2:fdm=marker