#endif
#include <glib/gi18n-lib.h>
+#include <glib/gprintf.h>
#include "parser.h"
GNode *nodes;
};
-G_DEFINE_TYPE (ZakGorgParser, zak_gorg_parser, ZAK_GORG_TYPE_PARSER)
+G_DEFINE_TYPE (ZakGorgParser, zak_gorg_parser, G_TYPE_OBJECT)
+
+typedef struct
+ {
+ guint level;
+ gchar *str;
+ } Header;
static void
zak_gorg_parser_class_init (ZakGorgParserClass *klass)
ZakGorgParser
*zak_gorg_parser_new_gfile (GFile *gfile)
{
+ Header *root;
+
ZakGorgParser *zak_gorg_parser;
zak_gorg_parser = g_object_new (ZAK_GORG_TYPE_PARSER, NULL);
- g_warning("gfile");
- zak_gorg_parser->nodes = NULL;
+ root = g_new0 (Header, 1);
+ root->level = 0;
+ root->str = "The root";
+
+ zak_gorg_parser->nodes = g_node_new (root);
zak_gorg_parser->gfile = gfile;
zak_gorg_parser_parse (zak_gorg_parser);
parent_class->finalize (gobject);
}
+static gboolean
+traverse_func (GNode *node,
+ gpointer data)
+{
+ g_printf ("%d: %s\n", g_node_depth (node), ((Header *)node->data)->str);
+
+ return FALSE;
+}
+
static void
zak_gorg_parser_parse (ZakGorgParser *zak_gorg_parser)
{
void *buffer;
+ guint i;
+ guint l;
+ guint start;
+ guint line;
+
+ GNode *root;
+ GNode *cur;
+
+ gchar *strline;
+
error = NULL;
giostream = g_file_read (zak_gorg_parser->gfile, FALSE, &error);
if (giostream == NULL
return;
}
- g_warning ("BUFFER %s", buffer);
+ g_warning ("BUFFER\n%s", buffer);
+
+ gchar *str = buffer;
+
+ root = g_node_get_root (zak_gorg_parser->nodes);
+
+ start = 0;
+ line = 0;
+ cur = root;
+ for (i = 0; i < size; i++)
+ {
+ if (str[i] == '\n')
+ {
+ line++;
+ strline = g_strndup (buffer + start, i - start);
+ g_printf ("%d: %s\n", line, strline);
+ start = i + 1;
+
+ Header *h = g_new0 (Header, 1);
+ for (l = 0; l < i - start; l++)
+ {
+ if (strline[l] == ' ')
+ {
+ h->level = l - 1;
+ break;
+ }
+ }
+ h->str = g_strdup (strline);
+
+ GNode *n = g_node_new (h);
+
+ if (h->level == 0)
+ {
+ cur = root;
+ }
+ else
+ {
+ Header *hcur = (Header *)cur->data;
+ if (h->level == hcur->level)
+ {
+ cur = cur->parent;
+ }
+ }
+ g_node_append (cur, n);
+ cur = n;
+ }
+ }
+
+ g_printf ("\n");
+ g_message ("Traversing");
+ g_node_traverse (zak_gorg_parser->nodes,
+ G_PRE_ORDER,
+ G_TRAVERSE_ALL,
+ -1,
+ traverse_func,
+ NULL);
g_free (buffer);
g_object_unref (ginfo);