X-Git-Url: https://git.octo.it/?a=blobdiff_plain;f=src%2Foconfig.c;h=e1c1b54ced21cbb9b91838aefb196574d5a0291a;hb=f96b1aa398e1a6a480aac8ff3128ec8e093a5aaf;hp=6ba77a0f70163ddf8650c61630711da2b0f75f4f;hpb=ecc2abe9d0b6834303db0983a0a81ba17de44475;p=liboconfig.git diff --git a/src/oconfig.c b/src/oconfig.c index 6ba77a0..e1c1b54 100644 --- a/src/oconfig.c +++ b/src/oconfig.c @@ -23,143 +23,20 @@ #include "oconfig.h" -/* - * private structures - */ -struct oconfig_obj -{ - oconfig_item_obj_t *items; -}; +oconfig_item_t *oconfig_parse_fh (FILE *fh); +oconfig_item_t *oconfig_parse_file (const char *file); -struct oconfig_item_obj +void oconfig_free (oconfig_item_t *ci) { - char *key; - char *value; + int i; - oconfig_item_obj_t *child; - oconfig_item_obj_t *sibling; -}; + if (ci->values != NULL) + free (ci->values); -/* - * private functions - */ -static oconfig_item_obj_t *oconfig_item_alloc (const char *key, const char *value) -{ - oconfig_item_obj_t *ret; - - ret = calloc (1, sizeof (oconfig_item_obj_t)); - - if ((ret->key = strdup (key)) == NULL) - { - free (ret); - return (NULL); - } - - if ((ret->value = strdup (value)) == NULL) - { - free (ret->key); - free (ret); - return (NULL); - } - - return (ret); -} - -static void oconfig_item_free (oconfig_item_obj_t *item) -{ - /* This temporary variable is used to prevent endless loops. They - * should not exist, but it doesn't cost much, so what the heck.. */ - oconfig_item_obj_t *temp; - - if (item->child != NULL) - { - temp = item->child; - item->child = NULL; - oconfig_item_free (temp); - } - - if (item->sibling != NULL) - { - temp = item->sibling; - item->sibling = NULL; - oconfig_item_free (temp); - } - - if (item->key != NULL) - free (item->key); - - if (item->value != NULL) - free (item->value); - - free (item); -} - -static oconfig_item_obj_t *oconfig_item_parse_line (char *buffer) -{ - char *key; - char *value; - size_t value_len; - - key = strtok (buffer, " \t\n\r"); - if (key == NULL) - return (NULL); - - value = strtok (NULL, " \t\n\r"); - if (value == NULL) - return (NULL); - - value_len = strlen (value); - while (value_len > 0) - { - if ((value[value_len - 1] == ' ') - || (value[value_len - 1] == '\t') - || (value[value_len - 1] == '\n') - || (value[value_len - 1] == '\r')) - { - value[value_len - 1] = '\0'; - value_len--; - continue; - } - - break; - } - - if (value_len == 0) - return (NULL); - - return (oconfig_item_alloc (key, value)); -} - -/* - * constructor and destructor - */ -oconfig_obj_t *oconfig_construct (const char *file) -{ - oconfig_obj_t *ret; - - ret = calloc (1, sizeof (oconfig_obj_t)); - - /* FIXME: Implement the actual functionality */ - - return (ret); -} - -void oconfig_destroy (oconfig_obj_t *obj) -{ - assert (obj != NULL); - - if (obj->items != NULL) - oconfig_item_free (obj->items); - - free (obj); + for (i = 0; i < ci->children_num; i++) + oconfig_free (ci->children + i); } /* - * public methods + * vim:shiftwidth=2:tabstop=8:softtabstop=2 */ -oconfig_item_obj_t *oconfig_item_get (oconfig_obj_t *obj); -oconfig_item_obj_t *oconfig_item_get_child (oconfig_item_obj_t *item); -oconfig_item_obj_t *oconfig_item_get_sibling (oconfig_item_obj_t *item); - -const char *oconfig_item_get_key (oconfig_item_obj_t *); -size_t oconfig_item_get_value (oconfig_item_obj_t *, void *buffer, size_t *buffer_size);