[devhelp/wip/parser] parser: use GIO to load the index files



commit 7661c95ca4c69b67fa18f04e074d0b3b06a39dec
Author: Sébastien Wilmet <swilmet gnome org>
Date:   Wed May 10 18:31:43 2017 +0200

    parser: use GIO to load the index files
    
    To have more modern code, and to remove the zlib dependency.
    
    The file is still loaded synchronously, it would require much more work
    to have asychronous I/O in Devhelp.

 configure.ac    |   18 -------
 src/Makefile.am |    7 +--
 src/dh-parser.c |  135 ++++++++++++++++--------------------------------------
 3 files changed, 42 insertions(+), 118 deletions(-)
---
diff --git a/configure.ac b/configure.ac
index ad42694..4c8ecf8 100644
--- a/configure.ac
+++ b/configure.ac
@@ -59,24 +59,6 @@ AX_PKG_CHECK_MODULES([DEVHELP],
                     [gio-2.0 >= 2.38  gtk+-3.0 >= 3.22  webkit2gtk-4.0 >= 2.6],
                     [gsettings-desktop-schemas])
 
-AC_ARG_WITH(zlib, [  --with-zlib=DIR         use zlib in DIR], zlibdir=$with_zlib)
-have_zlib=no
-if test "x$zlibdir" != x; then
-    ZLIB_CPPFLAGS=-I$zlibdir/include
-    ZLIB_LDFLAGS=-L$zlibdir/lib
-fi
-save_CPPFLAGS="$CPPFLAGS"
-save_LDFLAGS="$LDFLAGS"
-CPPFLAGS="$CPPFLAGS $ZLIB_CPPFLAGS"
-LDFLAGS="$LDFLAGS $ZLIB_LDFLAGS"
-AC_CHECK_HEADERS(zlib.h, AC_CHECK_LIB(z, gzread, have_zlib=yes))
-CPPFLAGS="$save_CPPFLAGS"
-LDFLAGS="$save_LDFLAGS"
-ZLIB_LIBS=-lz
-AC_SUBST(ZLIB_CFLAGS)
-AC_SUBST(ZLIB_LDFLAGS)
-AC_SUBST(ZLIB_LIBS)
-
 # i18n stuff
 AM_GNU_GETTEXT([external])
 AM_GNU_GETTEXT_VERSION([0.19.7])
diff --git a/src/Makefile.am b/src/Makefile.am
index 5706e31..4664b36 100644
--- a/src/Makefile.am
+++ b/src/Makefile.am
@@ -113,23 +113,20 @@ nodist_libdevhelp_3_la_SOURCES =  \
        $(libdevhelp_built_sources)
 
 libdevhelp_3_la_CPPFLAGS =     \
-       $(AM_CPPFLAGS)          \
-       $(ZLIB_CPPFLAGS)
+       $(AM_CPPFLAGS)
 
 libdevhelp_3_la_CFLAGS =       \
        $(DEVHELP_CFLAGS)
 
 libdevhelp_3_la_LIBADD =       \
        $(DEVHELP_LIBS)         \
-       $(ZLIB_LIBS)            \
        $(LIBM)
 
 libdevhelp_3_la_LDFLAGS =                      \
        $(AM_LDFLAGS)                           \
        -no-undefined                           \
        -version-info $(LIBDEVHELP_LT_VERSION)  \
-       -export-symbols-regex ^dh_              \
-       $(ZLIB_LDFLAGS)
+       -export-symbols-regex ^dh_
 
 # The application
 bin_PROGRAMS = devhelp
diff --git a/src/dh-parser.c b/src/dh-parser.c
index cccc049..6f1fe39 100644
--- a/src/dh-parser.c
+++ b/src/dh-parser.c
@@ -21,9 +21,8 @@
 #include "config.h"
 #include "dh-parser.h"
 
+#include <gio/gio.h>
 #include <string.h>
-#include <errno.h>
-#include <zlib.h>
 #include <glib/gi18n-lib.h>
 
 #include "dh-error.h"
@@ -482,55 +481,6 @@ parser_end_node_cb (GMarkupParseContext  *context,
         }
 }
 
-static gboolean
-parser_read_gz_file (DhParser     *parser,
-                     const gchar  *path,
-                     GError      **error)
-{
-        gchar  buf[BYTES_PER_READ];
-        gzFile file;
-
-        file = gzopen (path, "r");
-        if (!file) {
-                g_set_error (error,
-                             DH_ERROR,
-                             DH_ERROR_FILE_NOT_FOUND,
-                             "%s", g_strerror (errno));
-                return FALSE;
-        }
-
-        while (TRUE) {
-                gint bytes_read;
-
-                bytes_read = gzread (file, buf, BYTES_PER_READ);
-                if (bytes_read == -1) {
-                        gint         err;
-                        const gchar *message;
-
-                        message = gzerror (file, &err);
-                        g_set_error (error,
-                                     DH_ERROR,
-                                     DH_ERROR_INTERNAL_ERROR,
-                                     _("Cannot uncompress book '%s': %s"),
-                                     path, message);
-                        return FALSE;
-                }
-
-                g_markup_parse_context_parse (parser->context, buf,
-                                              bytes_read, error);
-                if (error != NULL && *error != NULL) {
-                        return FALSE;
-                }
-                if (bytes_read < BYTES_PER_READ) {
-                        break;
-                }
-        }
-
-        gzclose (file);
-
-        return TRUE;
-}
-
 gboolean
 dh_parser_read_file (const gchar  *index_file_path,
                      gchar       **book_title,
@@ -540,11 +490,11 @@ dh_parser_read_file (const gchar  *index_file_path,
                      GList       **keywords,
                      GError      **error)
 {
-        DhParser   *parser;
-        gboolean    gz;
-        GIOChannel *io = NULL;
-        gchar       buf[BYTES_PER_READ];
-        gboolean    result = TRUE;
+        DhParser *parser;
+        gboolean gz;
+        GFile *index_file = NULL;
+        GFileInputStream *file_input_stream = NULL;
+        gboolean ok = TRUE;
 
         parser = g_new0 (DhParser, 1);
 
@@ -565,7 +515,6 @@ dh_parser_read_file (const gchar  *index_file_path,
         }
 
         parser->markup_parser = g_new0 (GMarkupParser, 1);
-
         parser->markup_parser->start_element = parser_start_node_cb;
         parser->markup_parser->end_element = parser_end_node_cb;
 
@@ -578,54 +527,50 @@ dh_parser_read_file (const gchar  *index_file_path,
         parser->book_tree = book_tree;
         parser->keywords = keywords;
 
-        if (gz) {
-                if (!parser_read_gz_file (parser,
-                                          index_file_path,
-                                          error)) {
-                        result = FALSE;
-                }
+        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;
                 goto exit;
-        } else {
-                io = g_io_channel_new_file (index_file_path, "r", error);
-                if (io == NULL) {
-                        result = FALSE;
-                        goto exit;
-                }
-
-                while (TRUE) {
-                        GIOStatus io_status;
-                        gsize     bytes_read;
-
-                        do {
-                                io_status = g_io_channel_read_chars (io, buf, BYTES_PER_READ,
-                                                                     &bytes_read, error);
-                        } while (io_status == G_IO_STATUS_AGAIN);
+        }
 
-                        if (io_status == G_IO_STATUS_ERROR) {
-                                result = FALSE;
-                                goto exit;
-                        }
-                        if (io_status == G_IO_STATUS_EOF || io_status == G_IO_STATUS_ERROR) {
-                                break;
-                        }
-                        if (!g_markup_parse_context_parse (parser->context, buf,
-                                                           bytes_read, error)) {
-                                result = FALSE;
+        while (TRUE) {
+                gchar buffer[BYTES_PER_READ];
+                gssize bytes_read;
+
+                bytes_read = g_input_stream_read (G_INPUT_STREAM (file_input_stream),
+                                                  buffer,
+                                                  BYTES_PER_READ,
+                                                  NULL,
+                                                  error);
+
+                if (bytes_read > 0) {
+                        if (!g_markup_parse_context_parse (parser->context,
+                                                           buffer,
+                                                           bytes_read,
+                                                           error)) {
+                                ok = FALSE;
                                 goto exit;
                         }
-                }
 
-                if (!g_markup_parse_context_end_parse (parser->context, error)) {
-                        result = FALSE;
+                } else if (bytes_read == 0) {
+                        /* End of file */
+                        break;
+                } else {
+                        ok = FALSE;
                         goto exit;
                 }
         }
 
- exit:
-        if (io != NULL) {
-                g_io_channel_unref (io);
+        if (!g_markup_parse_context_end_parse (parser->context, error)) {
+                ok = FALSE;
+                goto exit;
         }
+
+exit:
+        g_clear_object (&index_file);
+        g_clear_object (&file_input_stream);
         dh_parser_free (parser);
 
-        return result;
+        return ok;
 }


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