[niepce: 9/29] engine+rust: FsFile is now implemented in Rust



commit 784c1f0ad03d562ae382538ba6e88e49c5ca0d1c
Author: Hubert Figuière <hub figuiere net>
Date:   Wed Jun 7 22:33:37 2017 -0400

    engine+rust: FsFile is now implemented in Rust

 src/engine/db/fsfile.cpp  |   10 +++++---
 src/engine/db/fsfile.hpp  |   29 +++++++------------------
 src/engine/db/fsfile.rs   |   51 +++++++++++++++++++++++++++++++++++++++++++++
 src/engine/db/libfile.cpp |    4 +-
 src/engine/db/libfile.hpp |   10 ++++----
 src/engine/db/mod.rs      |    1 +
 6 files changed, 73 insertions(+), 32 deletions(-)
---
diff --git a/src/engine/db/fsfile.cpp b/src/engine/db/fsfile.cpp
index fb4c3a2..53d4581 100644
--- a/src/engine/db/fsfile.cpp
+++ b/src/engine/db/fsfile.cpp
@@ -1,7 +1,7 @@
 /*
  * niepce - eng/db/fsfile.cpp
  *
- * Copyright (C) 2009 Hubert Figuiere
+ * Copyright (C) 2009-2017 Hubert Figuière
  *
  * This program is free software: you can redistribute it and/or modify
  * it under the terms of the GNU General Public License as published by
@@ -22,13 +22,15 @@
 #include "fsfile.hpp"
 
 
+extern "C" eng::FsFile* engine_db_fsfile_new(eng::library_id_t id, const char* path);
+extern "C" void engine_db_fsfile_delete(eng::FsFile*);
+
 namespace eng {
 
-FsFile::FsFile(library_id_t _id, const std::string & _path)
-    : m_id(_id), m_path(_path)
+FsFilePtr fsfile_new(library_id_t id, const char* path)
 {
+    return FsFilePtr(engine_db_fsfile_new(id, path), &engine_db_fsfile_delete);
 }
-
 }
 
 /*
diff --git a/src/engine/db/fsfile.hpp b/src/engine/db/fsfile.hpp
index 4d6437d..f7a5f76 100644
--- a/src/engine/db/fsfile.hpp
+++ b/src/engine/db/fsfile.hpp
@@ -1,7 +1,7 @@
 /*
  * niepce - eng/db/fsfile.hpp
  *
- * Copyright (C) 2009-2013 Hubert Figuiere
+ * Copyright (C) 2009-2017 Hubert Figuière
  *
  * This program is free software: you can redistribute it and/or modify
  * it under the terms of the GNU General Public License as published by
@@ -17,36 +17,23 @@
  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
  */
 
-#ifndef __NIEPCE_DB_FSFILE_H__
-#define __NIEPCE_DB_FSFILE_H__
+#pragma once
 
 #include <string>
 #include <memory>
 
-#include "engine/db/librarytypes.hpp"
+#include "librarytypes.hpp"
 
 namespace eng {
 
-/** @brief describe a file on file system */
-class FsFile
-{
-public:
-    typedef std::shared_ptr< FsFile > Ptr;
-
-    FsFile(library_id_t id, const std::string & path);
-
-    library_id_t id()
-        { return m_id; }
-    const std::string & path() const
-        { return m_path; }
-private:
-    library_id_t m_id;
-    std::string m_path; /**< absolute path */
-};
+class FsFile;
+typedef std::shared_ptr<FsFile> FsFilePtr;
 
+FsFilePtr fsfile_new(library_id_t id, const char* path);
 }
 
-#endif
+extern "C" const char* engine_db_fsfile_path(eng::FsFile*);
+
 /*
   Local Variables:
   mode:c++
diff --git a/src/engine/db/fsfile.rs b/src/engine/db/fsfile.rs
new file mode 100644
index 0000000..5953f44
--- /dev/null
+++ b/src/engine/db/fsfile.rs
@@ -0,0 +1,51 @@
+
+
+use libc::c_char;
+use std::ffi::CStr;
+use std::ffi::CString;
+use std::path::{ Path, PathBuf };
+
+use super::LibraryId;
+
+/// Describe a file on the file system
+pub struct FsFile {
+    id: LibraryId,
+    path: PathBuf,
+    pub cstr: CString,
+}
+
+impl FsFile {
+
+    pub fn new(id: LibraryId, path: PathBuf) -> FsFile {
+        FsFile {
+            id: id, path: path,
+            cstr: CString::new("").unwrap(),
+        }
+    }
+
+    pub fn id(&self) -> LibraryId {
+        self.id
+    }
+
+    pub fn path(&self) -> &Path {
+        self.path.as_path()
+    }
+}
+
+#[no_mangle]
+pub extern fn engine_db_fsfile_new(id: i64, name: *const c_char) -> *mut FsFile {
+    let path = PathBuf::from(&*unsafe { CStr::from_ptr(name) }.to_string_lossy());
+    let lf = Box::new(FsFile::new(id, path));
+    Box::into_raw(lf)
+}
+
+#[no_mangle]
+pub extern fn engine_db_fsfile_delete(lf: *mut FsFile) {
+    unsafe { Box::from_raw(lf) };
+}
+
+#[no_mangle]
+pub extern fn engine_db_fsfile_path(this: &mut FsFile) -> *const c_char {
+    this.cstr = CString::new(&*this.path().to_string_lossy()).unwrap();
+    this.cstr.as_ptr()
+}
diff --git a/src/engine/db/libfile.cpp b/src/engine/db/libfile.cpp
index 6d59bf7..193fdea 100644
--- a/src/engine/db/libfile.cpp
+++ b/src/engine/db/libfile.cpp
@@ -28,8 +28,8 @@ namespace eng {
 LibFile::LibFile(library_id_t _id, library_id_t _folderId, library_id_t _fsfileid, const std::string & p,
                  const std::string & _name )
        : m_id(_id), m_folderId(_folderId),
-         m_name(_name), 
-    m_main_file(_fsfileid, p),
+         m_name(_name),
+          m_main_file(fsfile_new(_fsfileid, p.c_str())),
          m_orientation(0), m_rating(0), m_label(0),
     m_flag(0),
     m_file_type(FileType::UNKNOWN)
diff --git a/src/engine/db/libfile.hpp b/src/engine/db/libfile.hpp
index 26caa63..e9948bc 100644
--- a/src/engine/db/libfile.hpp
+++ b/src/engine/db/libfile.hpp
@@ -62,8 +62,8 @@ public:
         { return m_folderId; }
     const std::string & name() const
         { return m_name; }
-    const std::string & path() const
-        { return m_main_file.path(); }
+    std::string path() const
+        { return engine_db_fsfile_path(m_main_file.get()); }
 
     void setOrientation(int32_t v);
     int32_t orientation() const
@@ -87,12 +87,12 @@ public:
     /** Getter for the filetype enumeration. */
     FileType fileType() const
         { return m_file_type; }
-    
+
     /** return an URI of the real path
      * because the Gtk stuff want that.
      */
     const std::string uri() const
-        { return std::string("file://") + m_main_file.path(); }
+        { return std::string("file://") + engine_db_fsfile_path(m_main_file.get()); }
     /** check is the library file is at uri
      * @return true of the uri match
      * @todo
@@ -103,7 +103,7 @@ private:
     library_id_t         m_id;           /**< file ID */
     library_id_t         m_folderId;     /**< parent folder */
     std::string m_name;         /**< name */
-    FsFile      m_main_file;
+    FsFilePtr      m_main_file;
 //    boost::filesystem::path  m_path;/**< path name relative to the folder */
 //             std::string m_type;
     int32_t     m_orientation;  /**< Exif orientatoin */
diff --git a/src/engine/db/mod.rs b/src/engine/db/mod.rs
index 28fabfe..dfea170 100644
--- a/src/engine/db/mod.rs
+++ b/src/engine/db/mod.rs
@@ -17,6 +17,7 @@
  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
  */
 
+pub mod fsfile;
 pub mod keyword;
 pub mod libfolder;
 


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