2 * oconfig - src/oconfig.c
3 * Copyright (C) 2006,2007 Florian octo Forster <octo at verplant.org>
5 * This program is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License as published by the
7 * Free Software Foundation; only version 2 of the License is applicable.
9 * This program is distributed in the hope that it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
14 * You should have received a copy of the GNU General Public License along with
15 * this program; if not, write to the Free Software Foundation, Inc.,
16 * 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
30 oconfig_item_t *ci_root;
33 static void yyset_in (FILE *fd)
38 oconfig_item_t *oconfig_parse_fh (FILE *fh)
50 status = snprintf (file, sizeof (file), "<fd#%d>", fileno (fh));
52 if ((status < 0) || (((size_t) status) >= sizeof (file))) {
56 file[sizeof (file) - 1] = '\0';
64 fprintf (stderr, "yyparse returned error #%i\n", status);
72 yyset_in ((FILE *) 0);
75 } /* oconfig_item_t *oconfig_parse_fh */
77 oconfig_item_t *oconfig_parse_file (const char *file)
84 fh = fopen (file, "r");
87 fprintf (stderr, "fopen (%s) failed: %s\n", file, strerror (errno));
91 ret = oconfig_parse_fh (fh);
97 } /* oconfig_item_t *oconfig_parse_file */
99 oconfig_item_t *oconfig_clone (const oconfig_item_t *ci_orig)
101 oconfig_item_t *ci_copy;
103 ci_copy = (oconfig_item_t *) malloc (sizeof (*ci_copy));
106 fprintf (stderr, "malloc failed.\n");
109 memset (ci_copy, 0, sizeof (*ci_copy));
110 ci_copy->values = NULL;
111 ci_copy->parent = NULL;
112 ci_copy->children = NULL;
114 ci_copy->key = strdup (ci_orig->key);
115 if (ci_copy->key == NULL)
117 fprintf (stderr, "strdup failed.\n");
122 if (ci_orig->values_num > 0) /* {{{ */
126 ci_copy->values = (oconfig_value_t *) calloc (ci_orig->values_num,
127 sizeof (*ci_copy->values));
128 if (ci_copy->values == NULL)
130 fprintf (stderr, "calloc failed.\n");
135 ci_copy->values_num = ci_orig->values_num;
137 for (i = 0; i < ci_copy->values_num; i++)
139 ci_copy->values[i].type = ci_orig->values[i].type;
140 if (ci_copy->values[i].type == OCONFIG_TYPE_STRING)
142 ci_copy->values[i].value.string
143 = strdup (ci_orig->values[i].value.string);
144 if (ci_copy->values[i].value.string == NULL)
146 fprintf (stderr, "strdup failed.\n");
147 oconfig_free (ci_copy);
151 else /* ci_copy->values[i].type != OCONFIG_TYPE_STRING) */
153 ci_copy->values[i].value = ci_orig->values[i].value;
156 } /* }}} if (ci_orig->values_num > 0) */
158 if (ci_orig->children_num > 0) /* {{{ */
162 ci_copy->children = (oconfig_item_t *) calloc (ci_orig->children_num,
163 sizeof (*ci_copy->children));
164 if (ci_copy->children == NULL)
166 fprintf (stderr, "calloc failed.\n");
167 oconfig_free (ci_copy);
170 ci_copy->children_num = ci_orig->children_num;
172 for (i = 0; i < ci_copy->children_num; i++)
174 oconfig_item_t *child;
176 child = oconfig_clone (ci_orig->children + i);
179 oconfig_free (ci_copy);
182 child->parent = ci_copy;
183 ci_copy->children[i] = *child;
185 } /* for (i = 0; i < ci_copy->children_num; i++) */
186 } /* }}} if (ci_orig->children_num > 0) */
189 } /* oconfig_item_t *oconfig_clone */
191 void oconfig_free (oconfig_item_t *ci)
201 for (i = 0; i < ci->values_num; i++)
202 if ((ci->values[i].type == OCONFIG_TYPE_STRING)
203 && (NULL != ci->values[i].value.string))
204 free (ci->values[i].value.string);
206 if (ci->values != NULL)
209 for (i = 0; i < ci->children_num; i++)
210 oconfig_free (ci->children + i);
212 if (ci->children != NULL)
217 * vim:shiftwidth=2:tabstop=8:softtabstop=2:fdm=marker