1 /**
2 * oconfig - src/oconfig.c
3 * Copyright (C) 2006,2007 Florian octo Forster <octo at verplant.org>
4 *
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.
8 *
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
12 * more details.
13 *
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
17 */
19 #include <stdlib.h>
20 #include <stdio.h>
21 #include <string.h>
22 #include <assert.h>
23 #include <errno.h>
25 #include "oconfig.h"
27 extern FILE *yyin;
29 oconfig_item_t *ci_root;
31 static void yyset_in (FILE *fd)
32 {
33 yyin = fd;
34 } /* void yyset_in */
36 oconfig_item_t *oconfig_parse_fh (FILE *fh)
37 {
38 int status;
39 oconfig_item_t *ret;
41 yyset_in (fh);
43 status = yyparse ();
44 if (status != 0)
45 {
46 fprintf (stderr, "yyparse returned error #%i\n", status);
47 return (NULL);
48 }
50 ret = ci_root;
51 ci_root = NULL;
52 yyset_in ((FILE *) 0);
54 return (ret);
55 } /* oconfig_item_t *oconfig_parse_fh */
57 oconfig_item_t *oconfig_parse_file (const char *file)
58 {
59 FILE *fh;
60 oconfig_item_t *ret;
62 fh = fopen (file, "r");
63 if (fh == NULL)
64 {
65 fprintf (stderr, "fopen (%s) failed: %s\n", file, strerror (errno));
66 return (NULL);
67 }
69 ret = oconfig_parse_fh (fh);
70 fclose (fh);
72 return (ret);
73 } /* oconfig_item_t *oconfig_parse_file */
75 void oconfig_free (oconfig_item_t *ci)
76 {
77 int i;
79 if (ci == NULL)
80 return;
82 if (ci->key != NULL)
83 free (ci->key);
85 for (i = 0; i < ci->values_num; i++)
86 if ((ci->values[i].type == OCONFIG_TYPE_STRING)
87 && (NULL != ci->values[i].value.string))
88 free (ci->values[i].value.string);
90 if (ci->values != NULL)
91 free (ci->values);
93 for (i = 0; i < ci->children_num; i++)
94 oconfig_free (ci->children + i);
96 if (ci->children != NULL)
97 free (ci->children);
98 }
100 /*
101 * vim:shiftwidth=2:tabstop=8:softtabstop=2
102 */