]> saetta.ns0.it Git - libzakgorg/commitdiff
Builded the tree of headers.
authorAndrea Zagli <azagli@libero.it>
Mon, 8 Jul 2019 14:30:32 +0000 (16:30 +0200)
committerAndrea Zagli <azagli@libero.it>
Mon, 8 Jul 2019 14:30:32 +0000 (16:30 +0200)
src/parser.c
tests/simple.org

index 8f8b428991c618f875b55eea0eb970d06a851ab3..74307d4e7f04c21a5d2e3d3c4e85eb43aa1c9e6b 100644 (file)
@@ -21,6 +21,7 @@
 #endif
 
 #include <glib/gi18n-lib.h>
+#include <glib/gprintf.h>
 
 #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);
index 7d8f3b52279586465a75417620bb33f39e029e0d..e56df32de07899ed068ddcb67488cb526859d413 100644 (file)
@@ -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