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
28 extern int yyparse (void);
30 oconfig_item_t *ci_root;
33 static void yyset_in (FILE *fd)
38 oconfig_item_t *oconfig_parse_fh (FILE *fh)
48 status = snprintf (file, sizeof (file), "<fd#%d>", fileno (fh));
50 if ((status < 0) || (status >= sizeof (file))) {
54 file[sizeof (file) - 1] = '\0';
62 fprintf (stderr, "yyparse returned error #%i\n", status);
70 yyset_in ((FILE *) 0);
73 } /* oconfig_item_t *oconfig_parse_fh */
75 oconfig_item_t *oconfig_parse_file (const char *file)
82 fh = fopen (file, "r");
85 fprintf (stderr, "fopen (%s) failed: %s\n", file, strerror (errno));
89 ret = oconfig_parse_fh (fh);
95 } /* oconfig_item_t *oconfig_parse_file */
97 oconfig_item_t *oconfig_clone (const oconfig_item_t *ci_orig)
99 oconfig_item_t *ci_copy;
101 ci_copy = (oconfig_item_t *) malloc (sizeof (*ci_copy));
104 fprintf (stderr, "malloc failed.\n");
107 memset (ci_copy, 0, sizeof (*ci_copy));
108 ci_copy->values = NULL;
109 ci_copy->parent = NULL;
110 ci_copy->children = NULL;
112 ci_copy->key = strdup (ci_orig->key);
113 if (ci_copy->key == NULL)
115 fprintf (stderr, "strdup failed.\n");
120 if (ci_orig->values_num > 0) /* {{{ */
124 ci_copy->values = (oconfig_value_t *) calloc (ci_orig->values_num,
125 sizeof (*ci_copy->values));
126 if (ci_copy->values == NULL)
128 fprintf (stderr, "calloc failed.\n");
133 ci_copy->values_num = ci_orig->values_num;
135 for (i = 0; i < ci_copy->values_num; i++)
137 ci_copy->values[i].type = ci_orig->values[i].type;
138 if (ci_copy->values[i].type == OCONFIG_TYPE_STRING)
140 ci_copy->values[i].value.string
141 = strdup (ci_orig->values[i].value.string);
142 if (ci_copy->values[i].value.string == NULL)
144 fprintf (stderr, "strdup failed.\n");
145 oconfig_free (ci_copy);
149 else /* ci_copy->values[i].type != OCONFIG_TYPE_STRING) */
151 ci_copy->values[i].value = ci_orig->values[i].value;
154 } /* }}} if (ci_orig->values_num > 0) */
156 if (ci_orig->children_num > 0) /* {{{ */
160 ci_copy->children = (oconfig_item_t *) calloc (ci_orig->children_num,
161 sizeof (*ci_copy->children));
162 if (ci_copy->children == NULL)
164 fprintf (stderr, "calloc failed.\n");
165 oconfig_free (ci_copy);
168 ci_copy->children_num = ci_orig->children_num;
170 for (i = 0; i < ci_copy->children_num; i++)
172 oconfig_item_t *child;
174 child = oconfig_clone (ci_orig->children + i);
177 oconfig_free (ci_copy);
180 child->parent = ci_copy;
181 ci_copy->children[i] = *child;
183 } /* for (i = 0; i < ci_copy->children_num; i++) */
184 } /* }}} if (ci_orig->children_num > 0) */
187 } /* oconfig_item_t *oconfig_clone */
189 static void oconfig_free_all (oconfig_item_t *ci)
199 for (i = 0; i < ci->values_num; i++)
200 if ((ci->values[i].type == OCONFIG_TYPE_STRING)
201 && (NULL != ci->values[i].value.string))
202 free (ci->values[i].value.string);
204 if (ci->values != NULL)
207 for (i = 0; i < ci->children_num; i++)
208 oconfig_free_all (ci->children + i);
210 if (ci->children != NULL)
214 void oconfig_free (oconfig_item_t *ci)
216 oconfig_free_all (ci);
222 * vim:shiftwidth=2:tabstop=8:softtabstop=2:fdm=marker