[libgepub/rust] Added gepub_doc_get_resources to the epub api
- From: Daniel Garcia Moreno <danigm src gnome org>
- To: commits-list gnome org
- Cc:
- Subject: [libgepub/rust] Added gepub_doc_get_resources to the epub api
- Date: Wed, 31 May 2017 18:07:28 +0000 (UTC)
commit 312aa3bafe5eb218edfa3a77b35e069158698245
Author: Daniel GarcĂa Moreno <danigm wadobo com>
Date: Wed May 31 20:06:32 2017 +0200
Added gepub_doc_get_resources to the epub api
libgepub/gepub-doc.c | 46 ++++++++++++++++++++++++++++++++++++++++++++
libgepub/gepub-doc.h | 8 +++++++
libgepub/rust/src/lib.rs | 48 ++++++++++++++++++++++++++++++++++++++++++++++
tests/test-gepub.c | 26 +++++++++++++++++++++++-
4 files changed, 127 insertions(+), 1 deletions(-)
---
diff --git a/libgepub/gepub-doc.c b/libgepub/gepub-doc.c
index 3ef615f..56fc82b 100644
--- a/libgepub/gepub-doc.c
+++ b/libgepub/gepub-doc.c
@@ -46,6 +46,12 @@ void *epub_get_cover(void *doc);
void *epub_resource_path(void *doc, const char *id);
void *epub_current_path(void *doc);
void *epub_current_id(void *doc);
+void *epub_get_resources(void *doc);
+guint *epub_resources_get_length(void *er);
+
+gchar *epub_resources_get_id(void *er, gint i);
+gchar *epub_resources_get_mime(void *er, gint i);
+gchar *epub_resources_get_path(void *er, gint i);
static void gepub_doc_initable_iface_init (GInitableIface *iface);
@@ -74,6 +80,14 @@ G_DEFINE_TYPE_WITH_CODE (GepubDoc, gepub_doc, G_TYPE_OBJECT,
G_IMPLEMENT_INTERFACE (G_TYPE_INITABLE, gepub_doc_initable_iface_init))
static void
+gepub_resource_free (GepubResource *res)
+{
+ g_free (res->mime);
+ g_free (res->uri);
+ g_free (res);
+}
+
+static void
gepub_doc_finalize (GObject *object)
{
GepubDoc *doc = GEPUB_DOC (object);
@@ -216,6 +230,37 @@ gepub_doc_new (const gchar *path)
}
/**
+ * gepub_doc_get_resources:
+ * @doc: a #GepubDoc
+ *
+ * Returns: (element-type utf8 Gepub.Resource) (transfer full): doc resource table
+ */
+GHashTable *
+gepub_doc_get_resources (GepubDoc *doc)
+{
+ g_return_val_if_fail (GEPUB_IS_DOC (doc), NULL);
+
+ GHashTable *resources = g_hash_table_new_full (g_str_hash,
+ g_str_equal,
+ (GDestroyNotify)g_free,
+ (GDestroyNotify)gepub_resource_free);
+
+ void *res = epub_get_resources (doc->rust_epub_doc);
+ guint l = epub_resources_get_length (res);
+ gint i = 0;
+
+ for (i=0; i<l; i++) {
+ gchar *key = epub_resources_get_id (res, i);
+ GepubResource *r = g_malloc (sizeof (GepubResource));
+ r->uri = epub_resources_get_path (res, i);
+ r->mime = epub_resources_get_mime (res, i);
+ g_hash_table_insert (resources, key, r);
+ }
+
+ return resources;
+}
+
+/**
* gepub_doc_get_metadata:
* @doc: a #GepubDoc
* @mdata: a metadata name string, GEPUB_META_TITLE for example
@@ -342,6 +387,7 @@ gepub_doc_get_current_with_epub_uris (GepubDoc *doc)
{
g_return_val_if_fail (GEPUB_IS_DOC (doc), NULL);
+ int size = 0;
guint8 *data = epub_get_current_with_epub_uris (doc->rust_epub_doc, &size);
return g_bytes_new_take (data, size);
}
diff --git a/libgepub/gepub-doc.h b/libgepub/gepub-doc.h
index ccc8f66..4ba11f8 100644
--- a/libgepub/gepub-doc.h
+++ b/libgepub/gepub-doc.h
@@ -35,6 +35,13 @@ G_BEGIN_DECLS
typedef struct _GepubDoc GepubDoc;
typedef struct _GepubDocClass GepubDocClass;
+struct _GepubResource {
+ gchar *mime;
+ gchar *uri;
+};
+
+typedef struct _GepubResource GepubResource;
+
GType gepub_doc_get_type (void) G_GNUC_CONST;
@@ -42,6 +49,7 @@ GepubDoc *gepub_doc_new (const gchar *path);
gchar *gepub_doc_get_metadata (GepubDoc *doc, const gchar *mdata);
GBytes *gepub_doc_get_resource (GepubDoc *doc, const gchar *path);
GBytes *gepub_doc_get_resource_by_id (GepubDoc *doc, const gchar *id);
+GHashTable *gepub_doc_get_resources (GepubDoc *doc);
gchar *gepub_doc_get_resource_mime (GepubDoc *doc, const gchar *path);
gchar *gepub_doc_get_resource_mime_by_id (GepubDoc *doc, const gchar *id);
gchar *gepub_doc_get_current_mime (GepubDoc *doc);
diff --git a/libgepub/rust/src/lib.rs b/libgepub/rust/src/lib.rs
index 1b90d12..ec473bd 100644
--- a/libgepub/rust/src/lib.rs
+++ b/libgepub/rust/src/lib.rs
@@ -9,6 +9,11 @@ use epub::doc::EpubDoc;
use self::glib::translate::*;
+pub struct EpubResources {
+ elements: Vec<(String, String, String)>,
+}
+
+
#[no_mangle]
pub extern "C" fn epub_new(path: *const libc::c_char) -> *mut EpubDoc {
let my_path = unsafe { &String::from_glib_none(path) };
@@ -43,6 +48,49 @@ pub unsafe extern "C" fn epub_get_resource(doc: *mut EpubDoc,
}
#[no_mangle]
+pub unsafe extern "C" fn epub_get_resources(doc: *mut EpubDoc)
+ -> *mut EpubResources {
+ assert!(!doc.is_null());
+
+ let mut res: EpubResources = EpubResources {
+ elements: vec![],
+ };
+
+ for (k, v) in (*doc).resources.iter() {
+ res.elements.push((k.clone(), v.0.clone(), v.1.clone()));
+ }
+
+ Box::into_raw(Box::new(res))
+}
+
+#[no_mangle]
+pub unsafe extern "C" fn epub_resources_get_length(er: *mut EpubResources)
+ -> usize {
+ (*er).elements.len()
+}
+
+#[no_mangle]
+pub unsafe extern "C" fn epub_resources_get_id(er: *mut EpubResources, i: usize)
+ -> *const libc::c_char {
+ let ref ret = (*er).elements[i].0;
+ ret.to_glib_full()
+}
+
+#[no_mangle]
+pub unsafe extern "C" fn epub_resources_get_path(er: *mut EpubResources, i: usize)
+ -> *const libc::c_char {
+ let ref ret = (*er).elements[i].1;
+ ret.to_glib_full()
+}
+
+#[no_mangle]
+pub unsafe extern "C" fn epub_resources_get_mime(er: *mut EpubResources, i: usize)
+ -> *const libc::c_char {
+ let ref ret = (*er).elements[i].2;
+ ret.to_glib_full()
+}
+
+#[no_mangle]
pub unsafe extern "C" fn epub_get_resource_by_id(doc: *mut EpubDoc,
id: *const libc::c_char,
size: *mut i32)
diff --git a/tests/test-gepub.c b/tests/test-gepub.c
index 928ea22..539efc0 100644
--- a/tests/test-gepub.c
+++ b/tests/test-gepub.c
@@ -1,6 +1,7 @@
#include <string.h>
#include <stdio.h>
#include <gtk/gtk.h>
+#include <glib.h>
#include <libgepub/gepub.h>
gchar *buf = NULL;
@@ -42,7 +43,7 @@ print_replaced_text (GepubDoc *doc)
}
void
-button_pressed (GtkButton *button, GepubDoc *doc)
+button_pressed (GtkButton *button, GepubWidget *widget)
{
GepubDoc *doc = gepub_widget_get_doc (widget);
printf("CLICKED %s\n", gtk_button_get_label (button));
@@ -117,6 +118,28 @@ test_doc_name (const char *path)
g_object_unref (G_OBJECT (doc));
}
+void
+print_res (gchar *key, GepubResource *value, gpointer data)
+{
+ PTEST ("%s: %s, %s\n", key, value->mime, value->uri);
+}
+
+void
+test_resources (const char *path)
+{
+ GepubDoc *doc = gepub_doc_new (path);
+ PTEST ("Resources:\n");
+
+ GHashTable *hash = gepub_doc_get_resources (doc);
+
+ g_hash_table_foreach (hash, (GHFunc)print_res, NULL);
+
+ g_hash_table_remove_all (hash);
+ g_hash_table_unref (hash);
+
+ g_object_unref (G_OBJECT (doc));
+}
+
int
main (int argc, char **argv)
{
@@ -255,6 +278,7 @@ main (int argc, char **argv)
// Testing all
TEST(test_doc_name, argv[1])
+ TEST(test_resources, argv[1])
// Freeing the mallocs :P
if (buf2) {
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]