From 13342f6b1f1d6867945426dd3e6bec9816f3c8fa Mon Sep 17 00:00:00 2001 From: Andrea Zagli Date: Tue, 28 Jul 2020 15:50:41 +0200 Subject: [PATCH] Use of g_strsplit. --- src/parser.c | 115 ++++++++++++++++++++++++--------------------------- 1 file changed, 54 insertions(+), 61 deletions(-) diff --git a/src/parser.c b/src/parser.c index f0d6d23..09b961d 100644 --- a/src/parser.c +++ b/src/parser.c @@ -209,10 +209,10 @@ zak_gorg_parser_parse (ZakGorgParser *zak_gorg_parser) void *buffer; + gchar **lines; + guint i; guint l; - guint start; - guint line; GNode *root; GNode *cur; @@ -223,8 +223,6 @@ zak_gorg_parser_parse (ZakGorgParser *zak_gorg_parser) gint rx_start; gint rx_end; - gchar *strline; - error = NULL; giostream = g_file_read (zak_gorg_parser->gfile, FALSE, &error); if (giostream == NULL @@ -261,89 +259,84 @@ zak_gorg_parser_parse (ZakGorgParser *zak_gorg_parser) g_warning ("BUFFER\n%s", buffer); - gchar *str = buffer; - regex_header = g_regex_new ("[*]+ ", 0, 0, NULL); regex_tags = g_regex_new (":[^ .]*:$", 0, 0, NULL); root = g_node_get_root (zak_gorg_parser->nodes); - start = 0; - line = 0; cur = root; + + lines = g_strsplit (buffer, "\n", -1); + size = g_strv_length (lines); + 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; + g_printf ("%d: %s\n", i, lines[i]); - if (!g_regex_match (regex_header, strline, 0, &match_info)) + if (!g_regex_match (regex_header, lines[i], 0, &match_info)) + { + Header *h = (Header *)cur->data; + if (h->body == NULL) { - Header *h = (Header *)cur->data; - if (h->body == NULL) - { - h->body = g_string_new (""); - } - g_string_append_printf (h->body, "%s\n", strline); + h->body = g_string_new (""); } - else + g_string_append_printf (h->body, "%s\n", lines[i]); + } + else + { + Header *h = g_new0 (Header, 1); + h->body = NULL; + for (l = 0; l < strlen (lines[i]); l++) { - Header *h = g_new0 (Header, 1); - h->body = NULL; - for (l = 0; l < i - start; l++) + if (lines[i][l] == ' ') { - if (strline[l] == ' ') - { - h->level = l; - break; - } + h->level = l; + break; } + } - g_regex_match (regex_tags, strline, 0, &match_info); + g_regex_match (regex_tags, lines[i], 0, &match_info); - rx_start = 0; - h->tags = g_match_info_fetch (match_info, 0); - if (h->tags != NULL) - { - g_match_info_fetch_pos (match_info, 0, &rx_start, NULL); - } + rx_start = 0; + h->tags = g_match_info_fetch (match_info, 0); + if (h->tags != NULL) + { + g_match_info_fetch_pos (match_info, 0, &rx_start, NULL); + } - if (rx_start > 0) - { - h->str = g_strndup (strline + h->level + 1, rx_start - (h->level + 1)); - } - else - { - h->str = g_strdup (strline + h->level + 1); - } - g_strstrip (h->str); + if (rx_start > 0) + { + h->str = g_strndup (lines[i] + h->level + 1, rx_start - (h->level + 1)); + } + else + { + h->str = g_strdup (lines[i] + h->level + 1); + } + g_strstrip (h->str); - g_match_info_free (match_info); - g_free (strline); + g_match_info_free (match_info); - GNode *n = g_node_new (h); + GNode *n = g_node_new (h); - if (h->level == 0) - { - cur = root; - } - else + if (h->level == 0) + { + cur = root; + } + else + { + Header *hcur = (Header *)cur->data; + if (h->level == hcur->level) { - Header *hcur = (Header *)cur->data; - if (h->level == hcur->level) - { - cur = cur->parent; - } + cur = cur->parent; } - g_node_append (cur, n); - cur = n; } + g_node_append (cur, n); + cur = n; } } + g_strfreev (lines); + g_printf ("\n"); g_message ("Traversing"); g_node_traverse (zak_gorg_parser->nodes, -- 2.49.0