2 * octo's object oriented config library.
3 * Copyright (C) 2006 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, version 2, as published
7 * by the Free Software Foundation.
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
31 oconfig_item_obj_t *items;
34 struct oconfig_item_obj
39 oconfig_item_obj_t *child;
40 oconfig_item_obj_t *sibling;
46 static oconfig_item_obj_t *oconfig_item_alloc (const char *key, const char *value)
48 oconfig_item_obj_t *ret;
50 ret = calloc (1, sizeof (oconfig_item_obj_t));
52 if ((ret->key = strdup (key)) == NULL)
58 if ((ret->value = strdup (value)) == NULL)
68 static void oconfig_item_free (oconfig_item_obj_t *item)
70 /* This temporary variable is used to prevent endless loops. They
71 * should not exist, but it doesn't cost much, so what the heck.. */
72 oconfig_item_obj_t *temp;
74 if (item->child != NULL)
78 oconfig_item_free (temp);
81 if (item->sibling != NULL)
85 oconfig_item_free (temp);
88 if (item->key != NULL)
91 if (item->value != NULL)
97 static oconfig_item_obj_t *oconfig_item_parse_line (char *buffer)
103 key = strtok (buffer, " \t\n\r");
107 value = strtok (NULL, " \t\n\r");
111 value_len = strlen (value);
112 while (value_len > 0)
114 if ((value[value_len - 1] == ' ')
115 || (value[value_len - 1] == '\t')
116 || (value[value_len - 1] == '\n')
117 || (value[value_len - 1] == '\r'))
119 value[value_len - 1] = '\0';
130 return (oconfig_item_alloc (key, value));
134 * constructor and destructor
136 oconfig_obj_t *oconfig_construct (const char *file)
140 ret = calloc (1, sizeof (oconfig_obj_t));
142 /* FIXME: Implement the actual functionality */
147 void oconfig_destroy (oconfig_obj_t *obj)
149 assert (obj != NULL);
151 if (obj->items != NULL)
152 oconfig_item_free (obj->items);
160 oconfig_item_obj_t *oconfig_item_get (oconfig_obj_t *obj);
161 oconfig_item_obj_t *oconfig_item_get_child (oconfig_item_obj_t *item);
162 oconfig_item_obj_t *oconfig_item_get_sibling (oconfig_item_obj_t *item);
164 const char *oconfig_item_get_key (oconfig_item_obj_t *);
165 size_t oconfig_item_get_value (oconfig_item_obj_t *, void *buffer, size_t *buffer_size);