[librsvg: 2/6] rsvg_io_acquire_stream(): Port to Rust



commit 70c6eb71f5e131f5f3021f5d92dd1f8e1bc11286
Author: Federico Mena Quintero <federico gnome org>
Date:   Fri Nov 23 17:26:39 2018 -0600

    rsvg_io_acquire_stream(): Port to Rust

 librsvg/rsvg-handle.c     | 10 +++++++---
 librsvg/rsvg-io.c         | 48 -----------------------------------------------
 librsvg/rsvg-io.h         |  4 ----
 rsvg_internals/Cargo.toml |  2 +-
 rsvg_internals/src/io.rs  | 44 +++++++++++++++++++++++++++++++++++++++++++
 rsvg_internals/src/lib.rs |  2 +-
 6 files changed, 53 insertions(+), 57 deletions(-)
---
diff --git a/librsvg/rsvg-handle.c b/librsvg/rsvg-handle.c
index a3bc1bf0..cd036a69 100644
--- a/librsvg/rsvg-handle.c
+++ b/librsvg/rsvg-handle.c
@@ -1688,6 +1688,12 @@ _rsvg_handle_acquire_data (RsvgHandle *handle,
     return data;
 }
 
+/* Implemented in rsvg_internals/src/io.rs */
+G_GNUC_INTERNAL
+GInputStream *rsvg_io_acquire_stream (const char *uri,
+                                     GCancellable *cancellable,
+                                     GError **error);
+
 GInputStream *
 _rsvg_handle_acquire_stream (RsvgHandle *handle,
                              const char *href,
@@ -1700,9 +1706,7 @@ _rsvg_handle_acquire_stream (RsvgHandle *handle,
     uri = rsvg_handle_resolve_uri (handle, href);
 
     if (allow_load (priv->base_gfile, uri, error)) {
-        stream = _rsvg_io_acquire_stream (uri,
-                                          handle->priv->cancellable,
-                                          error);
+        stream = rsvg_io_acquire_stream (uri, handle->priv->cancellable, error);
     } else {
         stream = NULL;
     }
diff --git a/librsvg/rsvg-io.c b/librsvg/rsvg-io.c
index e87b37d1..bd5e0a0f 100644
--- a/librsvg/rsvg-io.c
+++ b/librsvg/rsvg-io.c
@@ -35,26 +35,6 @@ rsvg_decode_data_uri (const char *uri,
                       gsize *out_len,
                       GError **error);
 
-static GInputStream *
-rsvg_acquire_gvfs_stream (const char *uri, 
-                          GCancellable *cancellable,
-                          GError **error)
-{
-    GFile *file;
-    GFileInputStream *stream;
-
-    file = g_file_new_for_uri (uri);
-
-    stream = g_file_read (file, cancellable, error);
-    g_object_unref (file);
-
-    if (stream == NULL) {
-        return NULL;
-    }
-
-    return G_INPUT_STREAM (stream);
-}
-
 static char *
 rsvg_acquire_gvfs_data (const char *uri,
                         char **out_mime_type,
@@ -109,31 +89,3 @@ _rsvg_io_acquire_data (const char *uri,
 
     return NULL;
 }
-
-GInputStream *
-_rsvg_io_acquire_stream (const char *uri,
-                         GCancellable *cancellable,
-                         GError **error)
-{
-    GInputStream *stream;
-
-    if (strncmp (uri, "data:", 5) == 0) {
-        char *mime_type = NULL;
-        char *data;
-        gsize len;
-
-        data = rsvg_decode_data_uri (uri, &mime_type, &len, error);
-        g_free (mime_type);
-
-        if (!data) {
-            return NULL;
-        }
-
-        return g_memory_input_stream_new_from_data (data, len, (GDestroyNotify) g_free);
-    }
-
-    if ((stream = rsvg_acquire_gvfs_stream (uri, cancellable, error)))
-      return stream;
-
-    return NULL;
-}
diff --git a/librsvg/rsvg-io.h b/librsvg/rsvg-io.h
index 1d38a74d..268f10d5 100644
--- a/librsvg/rsvg-io.h
+++ b/librsvg/rsvg-io.h
@@ -32,8 +32,4 @@ char *_rsvg_io_acquire_data (const char *uri,
                             GCancellable *cancellable,
                             GError **error);
 
-GInputStream *_rsvg_io_acquire_stream (const char *uri,
-                                       GCancellable *cancellable,
-                                       GError **error);
-
 #endif /* RSVG_IO_H */
diff --git a/rsvg_internals/Cargo.toml b/rsvg_internals/Cargo.toml
index 02257492..f7d54d07 100644
--- a/rsvg_internals/Cargo.toml
+++ b/rsvg_internals/Cargo.toml
@@ -28,7 +28,7 @@ downcast-rs = "^1.0.0"
 encoding = "0.2.33"
 float-cmp = "0.4.0"
 gdk-pixbuf = "0.5.0"
-gio = "0.5.1"
+gio = { version="0.5.1", features=["v2_48"] } # per configure.ac
 gio-sys = "0.7.0"
 glib = "0.6.0"
 glib-sys = "0.7.0"
diff --git a/rsvg_internals/src/io.rs b/rsvg_internals/src/io.rs
index a3e6b700..caa040f9 100644
--- a/rsvg_internals/src/io.rs
+++ b/rsvg_internals/src/io.rs
@@ -8,11 +8,15 @@ use gio::{
     BufferedInputStreamExt,
     Cancellable,
     ConverterInputStream,
+    File as GFile,
+    FileExt,
     InputStream,
+    MemoryInputStream,
     ZlibCompressorFormat,
     ZlibDecompressor,
 };
 use glib::translate::*;
+use glib::Bytes as GBytes;
 use glib::Cast;
 use std::ptr;
 
@@ -136,3 +140,43 @@ pub unsafe fn rsvg_get_input_stream_for_loading(
         }
     }
 }
+
+/// Returns an input stream.  The uri can be a data: URL or a plain URI
+fn acquire_stream(
+    uri: &str,
+    cancellable: Option<Cancellable>,
+) -> Result<InputStream, LoadingError> {
+    if uri.starts_with("data:") {
+        let BinaryData { data, .. } = decode_data_uri(uri)?;
+
+        let stream = MemoryInputStream::new_from_bytes(&GBytes::from_owned(data));
+        Ok(stream.upcast::<InputStream>())
+    } else {
+        let file = GFile::new_for_uri(uri);
+        let stream = file.read(cancellable.as_ref())?;
+
+        Ok(stream.upcast::<InputStream>())
+    }
+}
+
+#[no_mangle]
+pub unsafe fn rsvg_io_acquire_stream(
+    uri: *const libc::c_char,
+    cancellable: *mut gio_sys::GCancellable,
+    error: *mut *mut glib_sys::GError,
+) -> *mut gio_sys::GInputStream {
+    assert!(!uri.is_null());
+
+    let uri: String = from_glib_none(uri);
+    let cancellable = from_glib_borrow(cancellable);
+
+    match acquire_stream(&uri, cancellable) {
+        Ok(stream) => stream.to_glib_full(),
+
+        Err(_e) => {
+            set_gerror(error, 0, "Could not acquire stream");
+
+            ptr::null_mut()
+        }
+    }
+}
diff --git a/rsvg_internals/src/lib.rs b/rsvg_internals/src/lib.rs
index fd007e8e..47249299 100644
--- a/rsvg_internals/src/lib.rs
+++ b/rsvg_internals/src/lib.rs
@@ -47,7 +47,7 @@ pub use drawing_ctx::{
 
 pub use handle::rsvg_handle_load_css;
 
-pub use io::{rsvg_decode_data_uri, rsvg_get_input_stream_for_loading};
+pub use io::{rsvg_decode_data_uri, rsvg_get_input_stream_for_loading, rsvg_io_acquire_stream};
 
 pub use node::rsvg_node_unref;
 


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