Bump version to 0.1.2; Updated ChangeLog.
[liboconfig.git] / src / oconfig.c
index 6ba77a0..2bb80cc 100644 (file)
@@ -1,10 +1,10 @@
 /**
- * octo's object oriented config library.
- * Copyright (C) 2006  Florian octo Forster <octo at verplant.org>
+ * oconfig - src/oconfig.c
+ * Copyright (C) 2006,2007  Florian octo Forster <octo at verplant.org>
  *
  * This program is free software; you can redistribute it and/or modify it
- * under the terms of the GNU General Public License, version 2, as published
- * by the Free Software Foundation.
+ * under the terms of the GNU General Public License as published by the
+ * Free Software Foundation; only version 2 of the License is applicable.
  *
  * This program is distributed in the hope that it will be useful, but WITHOUT
  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 #include <stdio.h>
 #include <string.h>
 #include <assert.h>
+#include <errno.h>
 
 #include "oconfig.h"
 
-/*
- * private structures
- */
-struct oconfig_obj
-{
-       oconfig_item_obj_t *items;
-};
-
-struct oconfig_item_obj
-{
-       char   *key;
-       char   *value;
+extern FILE *yyin;
 
-       oconfig_item_obj_t *child;
-       oconfig_item_obj_t *sibling;
-};
+oconfig_item_t *ci_root;
 
-/*
- * private functions
- */
-static oconfig_item_obj_t *oconfig_item_alloc (const char *key, const char *value)
+static void yyset_in  (FILE *fd)
 {
-       oconfig_item_obj_t *ret;
+  yyin = fd;
+} /* void yyset_in */
 
-       ret = calloc (1, sizeof (oconfig_item_obj_t));
-
-       if ((ret->key = strdup (key)) == NULL)
-       {
-               free (ret);
-               return (NULL);
-       }
+oconfig_item_t *oconfig_parse_fh (FILE *fh)
+{
+  int status;
+  oconfig_item_t *ret;
 
-       if ((ret->value = strdup (value)) == NULL)
-       {
-               free (ret->key);
-               free (ret);
-               return (NULL);
-       }
+  yyset_in (fh);
 
-       return (ret);
-}
+  status = yyparse ();
+  if (status != 0)
+  {
+    fprintf (stderr, "yyparse returned error #%i\n", status);
+    return (NULL);
+  }
 
-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);
-}
+  ret = ci_root;
+  ci_root = NULL;
+  yyset_in ((FILE *) 0);
 
-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));
-}
+  return (ret);
+} /* oconfig_item_t *oconfig_parse_fh */
 
-/*
- * constructor and destructor
- */
-oconfig_obj_t *oconfig_construct (const char *file)
+oconfig_item_t *oconfig_parse_file (const char *file)
 {
-       oconfig_obj_t *ret;
+  FILE *fh;
+  oconfig_item_t *ret;
 
-       ret = calloc (1, sizeof (oconfig_obj_t));
+  fh = fopen (file, "r");
+  if (fh == NULL)
+  {
+    fprintf (stderr, "fopen (%s) failed: %s\n", file, strerror (errno));
+    return (NULL);
+  }
 
-       /* FIXME: Implement the actual functionality */
+  ret = oconfig_parse_fh (fh);
+  fclose (fh);
 
-       return (ret);
-}
+  return (ret);
+} /* oconfig_item_t *oconfig_parse_file */
 
-void oconfig_destroy (oconfig_obj_t *obj)
+void oconfig_free (oconfig_item_t *ci)
 {
-       assert (obj != NULL);
+  int i;
 
-       if (obj->items != NULL)
-               oconfig_item_free (obj->items);
+  if (ci->values != NULL)
+    free (ci->values);
 
-       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);