[niepce] engine+rust: FileBundle is no longer called from C++.



commit 54f8447d3588c5361d9bc181b4795d0b50ff6919
Author: Hubert Figuière <hub figuiere net>
Date:   Sat Sep 30 21:50:08 2017 -0400

    engine+rust: FileBundle is no longer called from C++.
    
    - Fix FileBunde::filter_bundle()
    - now all in Rust.
    - remove the C++ test. Add the Rust test.

 src/engine/Makefile.am            |    8 +---
 src/engine/db/filebundle.cpp      |   76 -------------------------------
 src/engine/db/filebundle.hpp      |   52 ---------------------
 src/engine/db/filebundle.rs       |   91 +++++++++++++++---------------------
 src/engine/db/test_filebundle.cpp |   76 -------------------------------
 src/engine/library/commands.rs    |   11 ++++-
 src/fwk/mod.rs                    |    2 -
 src/rust_bindings.hpp             |    1 -
 8 files changed, 50 insertions(+), 267 deletions(-)
---
diff --git a/src/engine/Makefile.am b/src/engine/Makefile.am
index 2b56a7b..1538b8c 100644
--- a/src/engine/Makefile.am
+++ b/src/engine/Makefile.am
@@ -4,7 +4,7 @@ AM_CPPFLAGS = -I$(top_srcdir)/src/  @FRAMEWORK_CFLAGS@ \
        @GPHOTO_CFLAGS@ \
        $(NULL)
 
-TESTS = test_filebundle test_opqueue
+TESTS = test_opqueue
 
 TEST_LIBS =  \
        libniepceengine.a \
@@ -20,10 +20,7 @@ TEST_LIBS =  \
        $(NULL)
 
 
-check_PROGRAMS = test_filebundle test_opqueue
-
-test_filebundle_SOURCES = db/test_filebundle.cpp
-test_filebundle_LDADD = $(TEST_LIBS)
+check_PROGRAMS = test_opqueue
 
 test_opqueue_SOURCES = library/test_opqueue.cpp library/opqueue.hpp
 test_opqueue_LDADD = $(TEST_LIBS)
@@ -39,7 +36,6 @@ libniepceengine_a_SOURCES = \
        db/keyword.hpp db/keyword.cpp \
        db/storage.hpp \
        db/fsfile.hpp \
-       db/filebundle.hpp db/filebundle.cpp \
        db/metadata.hpp \
        db/properties.hpp db/properties.cpp \
        db/properties-def.hpp \
diff --git a/src/engine/db/filebundle.rs b/src/engine/db/filebundle.rs
index fee99dc..4ba08cf 100644
--- a/src/engine/db/filebundle.rs
+++ b/src/engine/db/filebundle.rs
@@ -17,24 +17,17 @@
  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
  */
 
-use libc::c_char;
-use std::ffi::CStr;
-use std::ffi::CString;
 use std::ffi::OsString;
 use std::path::Path;
 
 use engine::db::libfile::FileType;
 use fwk::MimeType;
-use fwk::FileList;
 
 pub struct FileBundle {
     file_type: FileType,
     main: String,
-    pub main_c: CString,
     xmp_sidecar: String,
-    pub xmp_sidecar_c: CString,
     jpeg: String,
-    pub jpeg_c: CString,
 }
 
 impl FileBundle {
@@ -43,34 +36,28 @@ impl FileBundle {
         FileBundle {
             file_type: FileType::UNKNOWN,
             main: String::from(""),
-            main_c: CString::new("").unwrap(),
             xmp_sidecar: String::from(""),
-            xmp_sidecar_c: CString::new("").unwrap(),
             jpeg: String::from(""),
-            jpeg_c: CString::new("").unwrap(),
         }
     }
 
-    pub fn filter_bundles(files: &mut FileList) -> Vec<FileBundle> {
+    pub fn filter_bundles(mut files: Vec<String>) -> Vec<FileBundle> {
         let mut bundles: Vec<FileBundle> = vec!();
-        unsafe { files.sort(); }
-        let len = unsafe { files.size() };
+        files.sort();
         let mut current_base = OsString::new();
         let mut current_bundle: Option<FileBundle> = None;
 
-        for i in 0..len {
-            let f = unsafe { files.at_cstr(i) };
-            let cstr = unsafe { CStr::from_ptr(f) }.to_string_lossy();
-            let path = Path::new(&*cstr);
+        for f in files {
+            let path = Path::new(&f);
             if let Some(basename) = path.file_stem() {
                 if basename == current_base {
-                    current_bundle.as_mut().unwrap().add(&basename.to_string_lossy());
+                    current_bundle.as_mut().unwrap().add(&f);
                 } else {
                     if current_bundle.is_some() {
                         bundles.push(current_bundle.unwrap());
                     }
                     let mut bundle = FileBundle::new();
-                    bundle.add(&cstr);
+                    bundle.add(&f);
                     current_base = basename.to_os_string();
                     current_bundle = Some(bundle);
                 }
@@ -83,30 +70,24 @@ impl FileBundle {
     }
 
     pub fn add(&mut self, path: &str) -> bool {
-        dbg_out!("path {}", path);
+        dbg_out!("FileBundle::add path {}", path);
         let mime_type = MimeType::new(path);
         let mut added = true;
 
         if mime_type.is_image() {
             if mime_type.is_digicam_raw() {
-                println!("is RAW");
                 if !self.main.is_empty() && self.jpeg.is_empty() {
-                    println!("+JPEG");
                     self.jpeg = self.main.clone();
                     self.file_type = FileType::RAW_JPEG;
                 } else {
-                    println!("just RAW");
                     self.file_type = FileType::RAW;
                 }
                 self.main = String::from(path);
             } else {
-                println!("is JPEG?");
                 if !self.main.is_empty() {
-                    println!("RAW+JPEG");
                     self.jpeg = String::from(path);
                     self.file_type = FileType::RAW_JPEG;
                 } else {
-                    println!("JPEG");
                     self.main = String::from(path);
                     self.file_type = FileType::IMAGE;
                 }
@@ -136,36 +117,40 @@ impl FileBundle {
     }
 }
 
-#[no_mangle]
-pub extern fn engine_db_filebundle_new() -> *mut FileBundle {
-    let fb = Box::new(FileBundle::new());
-    Box::into_raw(fb)
-}
+#[cfg(test)]
+mod test {
+    use super::FileBundle;
 
-#[no_mangle]
-pub extern fn engine_db_filebundle_delete(fb: *mut FileBundle) {
-    unsafe { Box::from_raw(fb) };
-}
+    #[test]
+    fn test_filebundle() {
+        let mut thelist : Vec<String> = vec!();
 
-#[no_mangle]
-pub extern fn engine_db_filebundle_sidecar(obj: &mut FileBundle) -> *const c_char {
-    obj.xmp_sidecar_c = CString::new(obj.sidecar()).unwrap();
-    obj.xmp_sidecar_c.as_ptr()
-}
+        thelist.push(String::from("/foo/bar/img_0001.cr2"));
+        thelist.push(String::from("/foo/bar/img_0001.jpg"));
+        thelist.push(String::from("/foo/bar/img_0001.xmp"));
 
-#[no_mangle]
-pub extern fn engine_db_filebundle_main(obj: &mut FileBundle) -> *const c_char {
-    obj.main_c = CString::new(obj.main()).unwrap();
-    obj.main_c.as_ptr()
-}
+        thelist.push(String::from("/foo/bar/dcs_0001.jpg"));
+        thelist.push(String::from("/foo/bar/dcs_0001.nef"));
+        thelist.push(String::from("/foo/bar/dcs_0001.xmp"));
 
-#[no_mangle]
-pub extern fn engine_db_filebundle_jpeg(obj: &mut FileBundle) -> *const c_char {
-    obj.jpeg_c = CString::new(obj.jpeg()).unwrap();
-    obj.jpeg_c.as_ptr()
-}
+        let bundles_list = FileBundle::filter_bundles(thelist);
 
-#[no_mangle]
-pub extern fn engine_db_filebundle_add(obj: &mut FileBundle, f: *const c_char) -> c_char {
-    obj.add(&*unsafe { CStr::from_ptr(f) }.to_string_lossy()) as c_char
+        assert!(bundles_list.len() == 2);
+        let mut iter = bundles_list.iter();
+        if let Some(b) = iter.next() {
+            assert_eq!(b.main(), "/foo/bar/dcs_0001.nef");
+            assert_eq!(b.jpeg(), "/foo/bar/dcs_0001.jpg");
+            assert_eq!(b.sidecar(), "/foo/bar/dcs_0001.xmp");
+        } else {
+            assert!(false);
+        }
+
+        if let Some(b) = iter.next() {
+            assert_eq!(b.main(), "/foo/bar/img_0001.cr2");
+            assert_eq!(b.jpeg(), "/foo/bar/img_0001.jpg");
+            assert_eq!(b.sidecar(), "/foo/bar/img_0001.xmp");
+        } else {
+            assert!(false);
+        }
+    }
 }
diff --git a/src/engine/library/commands.rs b/src/engine/library/commands.rs
index c33d62a..d28f46b 100644
--- a/src/engine/library/commands.rs
+++ b/src/engine/library/commands.rs
@@ -92,10 +92,19 @@ pub extern "C" fn cmd_import_file(lib: &Library, file_path: *const c_char, manag
 }
 
 #[no_mangle]
-pub extern "C" fn cmd_import_files(lib: &Library, folder: *const c_char, files: &mut FileList,
+pub extern "C" fn cmd_import_files(lib: &Library, folder: *const c_char, cfiles: &mut FileList,
                         manage: Managed) -> bool {
     dbg_assert!(manage == Managed::NO, "managing file is currently unsupported");
 
+    let mut files: Vec<String> = vec!();
+    {
+        let len = unsafe { cfiles.size() };
+        for i in 0..len {
+            let f = unsafe { cfiles.at_cstr(i) };
+            let cstr = unsafe { CStr::from_ptr(f) }.to_string_lossy();
+            files.push(String::from(cstr));
+        }
+    }
     let bundles = FileBundle::filter_bundles(files);
     let libfolder: LibFolder;
     let folder = unsafe { CStr::from_ptr(folder) }.to_string_lossy();
diff --git a/src/fwk/mod.rs b/src/fwk/mod.rs
index c861b4b..1d8f3ed 100644
--- a/src/fwk/mod.rs
+++ b/src/fwk/mod.rs
@@ -41,8 +41,6 @@ pub use self::toolkit::mimetype::{
     MimeType
 };
 
-pub use root::fwk::FileList;
-
 use std::f64;
 use std::ffi::CStr;
 use libc::c_char;
diff --git a/src/rust_bindings.hpp b/src/rust_bindings.hpp
index e47d422..26e91a8 100644
--- a/src/rust_bindings.hpp
+++ b/src/rust_bindings.hpp
@@ -54,7 +54,6 @@ namespace eng {
 typedef ffi::LibraryId library_id_t; // XXX change this to LibraryId
 typedef ffi::FileType FileType;
 typedef ffi::Library Library;
-typedef ffi::FileBundle FileBundle;
 typedef ffi::Keyword Keyword;
 typedef ffi::LibFile LibFile;
 typedef ffi::LibFolder LibFolder;


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