[libgepub] Adding GError to gepub_doc_new



commit 7f962eb5e2b0951c8788be0bcc0210231c40be66
Author: Daniel GarcĂ­a Moreno <danigm wadobo com>
Date:   Wed Aug 2 11:04:00 2017 +0200

    Adding GError to gepub_doc_new

 libgepub/gepub-doc.c |   41 +++++++++++++++++++++++++++++++++++++----
 libgepub/gepub-doc.h |    2 +-
 tests/test-gepub.c   |   10 +++++-----
 3 files changed, 43 insertions(+), 10 deletions(-)
---
diff --git a/libgepub/gepub-doc.c b/libgepub/gepub-doc.c
index 5e73c71..faca44e 100644
--- a/libgepub/gepub-doc.c
+++ b/libgepub/gepub-doc.c
@@ -27,6 +27,28 @@
 #include "gepub-archive.h"
 #include "gepub-text-chunk.h"
 
+
+static GQuark
+gepub_error_quark (void)
+{
+    static GQuark q = 0;
+    if (q == 0)
+        q = g_quark_from_string ("gepub-quark");
+    return q;
+}
+
+/**
+ * GepubDocError:
+ * @GEPUB_ERROR_INVALID: Invalid file
+ *
+ * Common errors that may be reported by GepubDoc.
+ */
+typedef enum {
+    GEPUB_ERROR_INVALID = 0,  /*< nick=Invalid >*/
+} GepubDocError;
+
+
+
 static void gepub_doc_fill_resources (GepubDoc *doc);
 static void gepub_doc_fill_spine (GepubDoc *doc);
 static void gepub_doc_initable_iface_init (GInitableIface *iface);
@@ -181,11 +203,21 @@ gepub_doc_initable_init (GInitable     *initable,
 
     doc->archive = gepub_archive_new (doc->path);
     file = gepub_archive_get_root_file (doc->archive);
-    if (!file)
+    if (!file) {
+        if (error != NULL) {
+            g_set_error (error, gepub_error_quark (), GEPUB_ERROR_INVALID,
+                         "Invalid epub file: %s", doc->path);
+        }
         return FALSE;
+    }
     doc->content = gepub_archive_read_entry (doc->archive, file);
-    if (!doc->content)
+    if (!doc->content) {
+        if (error != NULL) {
+            g_set_error (error, gepub_error_quark (), GEPUB_ERROR_INVALID,
+                         "Invalid epub file: %s", doc->path);
+        }
         return FALSE;
+    }
 
     len = strlen (file);
     doc->content_base = g_strdup ("");
@@ -214,14 +246,15 @@ gepub_doc_initable_iface_init (GInitableIface *iface)
 /**
  * gepub_doc_new:
  * @path: the epub doc path
+ * @error: (nullable): Error
  *
  * Returns: (transfer full): the new GepubDoc created
  */
 GepubDoc *
-gepub_doc_new (const gchar *path)
+gepub_doc_new (const gchar *path, GError **error)
 {
     return g_initable_new (GEPUB_TYPE_DOC,
-                           NULL, NULL,
+                           NULL, error,
                            "path", path,
                            NULL);
 }
diff --git a/libgepub/gepub-doc.h b/libgepub/gepub-doc.h
index 68c2a93..ac12a7d 100644
--- a/libgepub/gepub-doc.h
+++ b/libgepub/gepub-doc.h
@@ -44,7 +44,7 @@ typedef struct _GepubResource GepubResource;
 
 GType             gepub_doc_get_type                        (void) G_GNUC_CONST;
 
-GepubDoc         *gepub_doc_new                             (const gchar *path);
+GepubDoc         *gepub_doc_new                             (const gchar *path, GError **error);
 GBytes           *gepub_doc_get_content                     (GepubDoc *doc);
 gchar            *gepub_doc_get_metadata                    (GepubDoc *doc, const gchar *mdata);
 GBytes           *gepub_doc_get_resource                    (GepubDoc *doc, const gchar *path);
diff --git a/tests/test-gepub.c b/tests/test-gepub.c
index fd257bb..73e1666 100644
--- a/tests/test-gepub.c
+++ b/tests/test-gepub.c
@@ -171,7 +171,7 @@ test_read (const char *path)
 
     a = gepub_archive_new (path);
 
-    doc = gepub_doc_new (path);
+    doc = gepub_doc_new (path, NULL);
     ht = (GHashTable*)gepub_doc_get_resources (doc);
     g_hash_table_foreach (ht, (GHFunc)find_xhtml, &file);
 
@@ -207,7 +207,7 @@ test_root_file (const char *path)
 static void
 test_doc_name (const char *path)
 {
-    GepubDoc *doc = gepub_doc_new (path);
+    GepubDoc *doc = gepub_doc_new (path, NULL);
     gchar *title = gepub_doc_get_metadata (doc, GEPUB_META_TITLE);
     gchar *lang = gepub_doc_get_metadata (doc, GEPUB_META_LANG);
     gchar *id = gepub_doc_get_metadata (doc, GEPUB_META_ID);
@@ -248,7 +248,7 @@ test_doc_resources (const char *path)
     const guchar *data;
     gsize size;
 
-    doc = gepub_doc_new (path);
+    doc = gepub_doc_new (path, NULL);
     ht = (GHashTable*)gepub_doc_get_resources (doc);
     g_hash_table_foreach (ht, (GHFunc)pk, NULL);
 
@@ -263,7 +263,7 @@ test_doc_resources (const char *path)
 static void
 test_doc_spine (const char *path)
 {
-    GepubDoc *doc = gepub_doc_new (path);
+    GepubDoc *doc = gepub_doc_new (path, NULL);
     int id = 0;
 
     do {
@@ -327,7 +327,7 @@ main (int argc, char **argv)
     gtk_container_add (GTK_CONTAINER (window), vpaned);
 
     // gepub widget
-    doc = gepub_doc_new (argv[1]);
+    doc = gepub_doc_new (argv[1], NULL);
     if (!doc) {
         perror ("BAD epub FILE");
         return -1;


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