src/graph_instance.[ch]: Implement "inst_data_to_json".
[collection4.git] / src / oconfig.c
1 /**
2  * collection4 - oconfig.c
3  * Copyright (C) 2006-2010  Florian octo Forster
4  * 
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.
9  * 
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.
14  * 
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
19  *
20  * Authors:
21  *   Florian octo Forster <ff at octo.it>
22  **/
23
24 #include <stdlib.h>
25 #include <stdio.h>
26 #include <string.h>
27 #include <assert.h>
28 #include <errno.h>
29
30 #include "oconfig.h"
31
32 extern FILE *yyin;
33 int yyparse (void);
34
35 oconfig_item_t *ci_root;
36 const char     *c_file;
37
38 static void yyset_in  (FILE *fd)
39 {
40   yyin = fd;
41 } /* void yyset_in */
42
43 oconfig_item_t *oconfig_parse_fh (FILE *fh)
44 {
45   int status;
46   oconfig_item_t *ret;
47
48   char file[10];
49
50   yyset_in (fh);
51
52   if (NULL == c_file) {
53     int status;
54
55     status = snprintf (file, sizeof (file), "<fd#%d>", fileno (fh));
56
57     if ((status < 0) || (((size_t) status) >= sizeof (file))) {
58       c_file = "<unknown>";
59     }
60     else {
61       file[sizeof (file) - 1] = '\0';
62       c_file = file;
63     }
64   }
65
66   status = yyparse ();
67   if (status != 0)
68   {
69     fprintf (stderr, "yyparse returned error #%i\n", status);
70     return (NULL);
71   }
72
73   c_file = NULL;
74
75   ret = ci_root;
76   ci_root = NULL;
77   yyset_in ((FILE *) 0);
78
79   return (ret);
80 } /* oconfig_item_t *oconfig_parse_fh */
81
82 oconfig_item_t *oconfig_parse_file (const char *file)
83 {
84   FILE *fh;
85   oconfig_item_t *ret;
86
87   c_file = file;
88
89   fh = fopen (file, "r");
90   if (fh == NULL)
91   {
92     fprintf (stderr, "fopen (%s) failed: %s\n", file, strerror (errno));
93     return (NULL);
94   }
95
96   ret = oconfig_parse_fh (fh);
97   fclose (fh);
98
99   c_file = NULL;
100
101   return (ret);
102 } /* oconfig_item_t *oconfig_parse_file */
103
104 oconfig_item_t *oconfig_clone (const oconfig_item_t *ci_orig)
105 {
106   oconfig_item_t *ci_copy;
107
108   ci_copy = (oconfig_item_t *) malloc (sizeof (*ci_copy));
109   if (ci_copy == NULL)
110   {
111     fprintf (stderr, "malloc failed.\n");
112     return (NULL);
113   }
114   memset (ci_copy, 0, sizeof (*ci_copy));
115   ci_copy->values = NULL;
116   ci_copy->parent = NULL;
117   ci_copy->children = NULL;
118
119   ci_copy->key = strdup (ci_orig->key);
120   if (ci_copy->key == NULL)
121   {
122     fprintf (stderr, "strdup failed.\n");
123     free (ci_copy);
124     return (NULL);
125   }
126
127   if (ci_orig->values_num > 0) /* {{{ */
128   {
129     int i;
130
131     ci_copy->values = (oconfig_value_t *) calloc (ci_orig->values_num,
132         sizeof (*ci_copy->values));
133     if (ci_copy->values == NULL)
134     {
135       fprintf (stderr, "calloc failed.\n");
136       free (ci_copy->key);
137       free (ci_copy);
138       return (NULL);
139     }
140     ci_copy->values_num = ci_orig->values_num;
141
142     for (i = 0; i < ci_copy->values_num; i++)
143     {
144        ci_copy->values[i].type = ci_orig->values[i].type;
145        if (ci_copy->values[i].type == OCONFIG_TYPE_STRING)
146        {
147          ci_copy->values[i].value.string
148            = strdup (ci_orig->values[i].value.string);
149          if (ci_copy->values[i].value.string == NULL)
150          {
151            fprintf (stderr, "strdup failed.\n");
152            oconfig_free (ci_copy);
153            return (NULL);
154          }
155        }
156        else /* ci_copy->values[i].type != OCONFIG_TYPE_STRING) */
157        {
158          ci_copy->values[i].value = ci_orig->values[i].value;
159        }
160     }
161   } /* }}} if (ci_orig->values_num > 0) */
162
163   if (ci_orig->children_num > 0) /* {{{ */
164   {
165     int i;
166
167     ci_copy->children = (oconfig_item_t *) calloc (ci_orig->children_num,
168         sizeof (*ci_copy->children));
169     if (ci_copy->children == NULL)
170     {
171       fprintf (stderr, "calloc failed.\n");
172       oconfig_free (ci_copy);
173       return (NULL);
174     }
175     ci_copy->children_num = ci_orig->children_num;
176
177     for (i = 0; i < ci_copy->children_num; i++)
178     {
179       oconfig_item_t *child;
180       
181       child = oconfig_clone (ci_orig->children + i);
182       if (child == NULL)
183       {
184         oconfig_free (ci_copy);
185         return (NULL);
186       }
187       child->parent = ci_copy;
188       ci_copy->children[i] = *child;
189       free (child);
190     } /* for (i = 0; i < ci_copy->children_num; i++) */
191   } /* }}} if (ci_orig->children_num > 0) */
192
193   return (ci_copy);
194 } /* oconfig_item_t *oconfig_clone */
195
196 void oconfig_free (oconfig_item_t *ci)
197 {
198   int i;
199
200   if (ci == NULL)
201     return;
202
203   if (ci->key != NULL)
204     free (ci->key);
205
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);
210
211   if (ci->values != NULL)
212     free (ci->values);
213
214   for (i = 0; i < ci->children_num; i++)
215     oconfig_free (ci->children + i);
216
217   if (ci->children != NULL)
218     free (ci->children);
219 }
220
221 /*
222  * vim:shiftwidth=2:tabstop=8:softtabstop=2:fdm=marker
223  */