[devhelp] parser: take a GFile parameter



commit 093a970ef68c893b517b9ab44443efc2bcf10b6f
Author: Sébastien Wilmet <swilmet gnome org>
Date:   Thu May 11 11:21:57 2017 +0200

    parser: take a GFile parameter

 src/dh-book.c   |    9 +++++----
 src/dh-parser.c |   44 ++++++++++++++++++++++----------------------
 src/dh-parser.h |   17 +++++++++--------
 3 files changed, 36 insertions(+), 34 deletions(-)
---
diff --git a/src/dh-book.c b/src/dh-book.c
index c7baa63..1bddfe4 100644
--- a/src/dh-book.c
+++ b/src/dh-book.c
@@ -297,17 +297,19 @@ dh_book_new (const gchar *index_file_path)
 {
         DhBookPrivate *priv;
         DhBook     *book;
-        GError     *error = NULL;
         GFile      *index_file;
         gchar      *language;
+        GError     *error = NULL;
 
         g_return_val_if_fail (index_file_path, NULL);
 
         book = g_object_new (DH_TYPE_BOOK, NULL);
         priv = dh_book_get_instance_private (book);
 
+        index_file = g_file_new_for_path (index_file_path);
+
         /* Parse file storing contents in the book struct */
-        if (!dh_parser_read_file (index_file_path,
+        if (!dh_parser_read_file (index_file,
                                   &priv->title,
                                   &priv->name,
                                   &language,
@@ -339,7 +341,6 @@ dh_book_new (const gchar *index_file_path)
         g_free (language);
 
         /* Setup monitor for changes */
-        index_file = g_file_new_for_path (index_file_path);
         priv->monitor = g_file_monitor_file (index_file,
                                              G_FILE_MONITOR_NONE,
                                              NULL,
@@ -353,8 +354,8 @@ dh_book_new (const gchar *index_file_path)
                 g_warning ("Couldn't setup monitoring of changes in book '%s'",
                            priv->title);
         }
-        g_object_unref (index_file);
 
+        g_object_unref (index_file);
         return book;
 }
 
diff --git a/src/dh-parser.c b/src/dh-parser.c
index 5fd2e4b..13d70ce 100644
--- a/src/dh-parser.c
+++ b/src/dh-parser.c
@@ -20,11 +20,8 @@
 
 #include "config.h"
 #include "dh-parser.h"
-
-#include <gio/gio.h>
 #include <string.h>
 #include <glib/gi18n-lib.h>
-
 #include "dh-error.h"
 #include "dh-link.h"
 
@@ -36,7 +33,7 @@ typedef struct {
         GMarkupParseContext *context;
 
         /* Parameters of dh_parser_read_file(). */
-        const gchar *index_file_path;
+        GFile *index_file;
         gchar **book_title;
         gchar **book_name;
         gchar **book_language;
@@ -151,8 +148,12 @@ parser_start_node_book (DhParser             *parser,
         *(parser->book_name) = g_strdup (name);
         *(parser->book_language) = language ? g_strdup (language) : NULL;
 
-        if (!base) {
-                base = g_path_get_dirname (parser->index_file_path);
+        if (base == NULL) {
+                GFile *directory;
+
+                directory = g_file_get_parent (parser->index_file);
+                base = g_file_get_path (directory);
+                g_object_unref (directory);
         }
 
         link = dh_link_new (DH_LINK_TYPE_BOOK,
@@ -482,32 +483,32 @@ parser_end_node_cb (GMarkupParseContext  *context,
 }
 
 gboolean
-dh_parser_read_file (const gchar  *index_file_path,
-                     gchar       **book_title,
-                     gchar       **book_name,
-                     gchar       **book_language,
-                     GNode       **book_tree,
-                     GList       **keywords,
-                     GError      **error)
+dh_parser_read_file (GFile   *index_file,
+                     gchar  **book_title,
+                     gchar  **book_name,
+                     gchar  **book_language,
+                     GNode  **book_tree,
+                     GList  **keywords,
+                     GError **error)
 {
         DhParser *parser;
+        gchar *index_file_uri;
         gboolean gz;
-        GFile *index_file = NULL;
         GFileInputStream *file_input_stream = NULL;
         GInputStream *input_stream = NULL;
         gboolean ok = TRUE;
 
         parser = g_new0 (DhParser, 1);
 
-        if (g_str_has_suffix (index_file_path, ".devhelp2")) {
+        index_file_uri = g_file_get_uri (index_file);
+
+        if (g_str_has_suffix (index_file_uri, ".devhelp2")) {
                 parser->version = 2;
                 gz = FALSE;
-        }
-        else if (g_str_has_suffix (index_file_path, ".devhelp")) {
+        } else if (g_str_has_suffix (index_file_uri, ".devhelp")) {
                 parser->version = 1;
                 gz = FALSE;
-        }
-        else if (g_str_has_suffix (index_file_path, ".devhelp2.gz")) {
+        } else if (g_str_has_suffix (index_file_uri, ".devhelp2.gz")) {
                 parser->version = 2;
                 gz = TRUE;
         } else {
@@ -521,14 +522,13 @@ dh_parser_read_file (const gchar  *index_file_path,
 
         parser->context = g_markup_parse_context_new (parser->markup_parser, 0, parser, NULL);
 
-        parser->index_file_path = index_file_path;
+        parser->index_file = index_file;
         parser->book_title = book_title;
         parser->book_name = book_name;
         parser->book_language = book_language;
         parser->book_tree = book_tree;
         parser->keywords = keywords;
 
-        index_file = g_file_new_for_path (index_file_path);
         file_input_stream = g_file_read (index_file, NULL, error);
         if (file_input_stream == NULL) {
                 ok = FALSE;
@@ -578,7 +578,7 @@ dh_parser_read_file (const gchar  *index_file_path,
                 ok = FALSE;
 
 exit:
-        g_clear_object (&index_file);
+        g_free (index_file_uri);
         g_clear_object (&file_input_stream);
         g_clear_object (&input_stream);
         dh_parser_free (parser);
diff --git a/src/dh-parser.h b/src/dh-parser.h
index 4a90483..9f2db2a 100644
--- a/src/dh-parser.h
+++ b/src/dh-parser.h
@@ -2,6 +2,7 @@
 /*
  * Copyright (C) 2003 CodeFactory AB
  * Copyright (C) 2003 Mikael Hallendal <micke imendio com>
+ * Copyright (C) 2017 Sébastien Wilmet <swilmet gnome org>
  *
  * This program is free software; you can redistribute it and/or
  * modify it under the terms of the GNU General Public License as
@@ -20,17 +21,17 @@
 #ifndef DH_PARSER_H
 #define DH_PARSER_H
 
-#include <glib.h>
+#include <gio/gio.h>
 
 G_BEGIN_DECLS
 
-gboolean dh_parser_read_file (const gchar  *index_file_path,
-                              gchar       **book_title,
-                              gchar       **book_name,
-                              gchar       **book_language,
-                              GNode       **book_tree,
-                              GList       **keywords,
-                              GError      **error);
+gboolean dh_parser_read_file (GFile   *index_file,
+                              gchar  **book_title,
+                              gchar  **book_name,
+                              gchar  **book_language,
+                              GNode  **book_tree,
+                              GList  **keywords,
+                              GError **error);
 
 G_END_DECLS
 


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]