From 9d244bfbe0e4cfc0fbe7d57a65810951be665d0d Mon Sep 17 00:00:00 2001 From: Andrea Zagli Date: Mon, 8 Jul 2019 16:30:32 +0200 Subject: [PATCH] Builded the tree of headers. --- src/parser.c | 94 +++++++++++++++++++++++++++++++++++++++++++++--- tests/simple.org | 2 ++ 2 files changed, 92 insertions(+), 4 deletions(-) diff --git a/src/parser.c b/src/parser.c index 8f8b428..74307d4 100644 --- a/src/parser.c +++ b/src/parser.c @@ -21,6 +21,7 @@ #endif #include +#include #include "parser.h" @@ -49,7 +50,13 @@ struct _ZakGorgParser 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) @@ -76,12 +83,17 @@ zak_gorg_parser_init (ZakGorgParser *zak_gorg_parser) 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); @@ -160,6 +172,15 @@ zak_gorg_parser_finalize (GObject *gobject) 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) { @@ -171,6 +192,16 @@ 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 @@ -205,7 +236,62 @@ zak_gorg_parser_parse (ZakGorgParser *zak_gorg_parser) 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); diff --git a/tests/simple.org b/tests/simple.org index 7d8f3b5..e56df32 100644 --- a/tests/simple.org +++ b/tests/simple.org @@ -2,5 +2,7 @@ ** sub 1 first line ** sub 2 first line * second line +*** sub 1 sub 1 second line (without parent) * third line ** sub 1 third line +*** sub 1 sub 1 third line -- 2.49.0