[niepce: 3/29] fwk+rust: Replace gpsCoordFromXmp() by fwk_gps_coord_from_xmp() implemented in Rust



commit aa09363081ee080fa9a8840320757b5302884d11
Author: Hubert Figuière <hub figuiere net>
Date:   Sat Jun 3 19:12:57 2017 -0400

    fwk+rust: Replace gpsCoordFromXmp() by fwk_gps_coord_from_xmp() implemented in Rust

 Cargo.toml                           |    9 +++-
 src/fwk/mod.rs                       |    9 +++-
 src/fwk/utils/exempi.cpp             |   91 ----------------------------------
 src/fwk/utils/exempi.hpp             |    6 ++-
 src/fwk/utils/testxmp.cpp            |   34 -------------
 src/lib.rs                           |   16 ++++++
 src/niepce/Makefile.am               |    5 ++-
 src/niepce/modules/map/mapmodule.cpp |    6 +--
 8 files changed, 41 insertions(+), 135 deletions(-)
---
diff --git a/Cargo.toml b/Cargo.toml
index 7bc8d91..8aa2cd2 100644
--- a/Cargo.toml
+++ b/Cargo.toml
@@ -1,9 +1,14 @@
 [package]
-name = "niepce"
+name = "niepce_rust"
 version = "0.1.0"
 authors = ["Hubert Figuière <hub figuiere net>"]
 
 [dependencies]
+libc = "0.2.23"
 sqlite = "0.23.4"
 exempi = "2.4.0"
-#gphoto = "0.1.1"
\ No newline at end of file
+#gphoto = "0.1.1"
+
+[lib]
+name = "niece_rust"
+crate-type = ["staticlib"]
diff --git a/src/fwk/mod.rs b/src/fwk/mod.rs
index 78c0239..1349746 100644
--- a/src/fwk/mod.rs
+++ b/src/fwk/mod.rs
@@ -17,4 +17,11 @@
  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
  */
 
-mod utils;
+pub mod utils;
+
+pub use self::utils::exempi::{
+    NsDef,
+    ExempiManager,
+    XmpMeta,
+    gps_coord_from_xmp
+};
diff --git a/src/fwk/utils/exempi.cpp b/src/fwk/utils/exempi.cpp
index 608b7e8..12a808a 100644
--- a/src/fwk/utils/exempi.cpp
+++ b/src/fwk/utils/exempi.cpp
@@ -250,97 +250,6 @@ const std::vector< std::string > & XmpMeta::keywords() const
     return m_keywords;
 }
 
-double
-XmpMeta::gpsCoordFromXmp(const std::string & xmps)
-{
-    double coord = NAN;
-
-    const char* s = xmps.c_str();
-
-    // step 1 - degrees
-//    auto iter;
-    const char* current = strchr(s, ',');
-    if (current == nullptr) {
-        return NAN;
-    }
-
-    std::string degs = std::string(s, current - s);
-
-    current++;
-    if (!*current) {
-        return NAN;
-    }
-    // step 2 - minutes
-    size_t len = strlen(current);
-    if (len <= 1) {
-        // too short
-        return NAN;
-    }
-    const char *orientation = current + len - 1;
-    if (*orientation != 'N' &&
-        *orientation != 'S' &&
-        *orientation != 'E' &&
-        *orientation != 'W') {
-
-        return NAN;
-    }
-
-    // extract minutes. There are two formats
-    double fminutes = 0.;
-    const char *next = strchr(current, ',');
-    if (next) {
-        // DD,mm,ss format
-        next++;
-        if (!*next) {
-            return NAN;
-        }
-        len = strlen(next);
-        if (len <= 1) {
-            // too short
-            return NAN;
-        }
-        std::string seconds = std::string(next, len - 1);
-        double sseconds = 0.;
-        try {
-            sseconds = boost::lexical_cast<double>(seconds) / 60;
-            std::string minutes = std::string(current, next - current - 1);
-            fminutes = boost::lexical_cast<double>(minutes);
-            fminutes += sseconds;
-        }
-        catch(const std::exception & e) {
-            return NAN;
-        }
-    }
-    else {
-        // DD,mm.mm format
-        try {
-            std::string minutes = std::string(current, len - 1);
-            fminutes = boost::lexical_cast<double>(minutes);
-        }
-        catch(const std::exception & e) {
-            return NAN;
-        }
-    }
-
-    // degrees.
-    try {
-        coord = boost::lexical_cast<int>(degs);
-    }
-    catch(const std::exception & e) {
-        return NAN;
-    }
-    if (coord > 180) {
-        return NAN;
-    }
-    coord += fminutes / 60.f;
-
-    if (*orientation == 'S' || *orientation == 'W') {
-        coord = -coord;
-    }
-
-    return coord;
-}
-
 }
 
 /*
diff --git a/src/fwk/utils/exempi.hpp b/src/fwk/utils/exempi.hpp
index 4c1b990..2bd9190 100644
--- a/src/fwk/utils/exempi.hpp
+++ b/src/fwk/utils/exempi.hpp
@@ -133,8 +133,6 @@ public:
     std::string creation_date_str() const;
     const std::vector< std::string > & keywords() const;
 
-    static double gpsCoordFromXmp(const std::string & s);
-
 private:
 
     XmpPtr m_xmp;
@@ -142,8 +140,12 @@ private:
     mutable bool m_keyword_fetched;
     mutable std::vector< std::string > m_keywords;
 };
+
 }
 
+// implemented in Rust
+extern "C" double fwk_gps_coord_from_xmp(const char* value);
+
 /*
   Local Variables:
   mode:c++
diff --git a/src/fwk/utils/testxmp.cpp b/src/fwk/utils/testxmp.cpp
index 54c15a2..a27d6d3 100644
--- a/src/fwk/utils/testxmp.cpp
+++ b/src/fwk/utils/testxmp.cpp
@@ -53,40 +53,6 @@ int test_main( int, char *[] )             // note the name!
        BOOST_CHECK(keywords[3] == "ottawa");
        BOOST_CHECK(keywords[4] == "parliament of canada");
 
-        double output = fwk::XmpMeta::gpsCoordFromXmp("foobar");
-        BOOST_CHECK(isnan(output));
-
-        // malformed 1
-        output = fwk::XmpMeta::gpsCoordFromXmp("45,29.6681666667");
-        BOOST_CHECK(isnan(output));
-
-        // malformed 2
-        output = fwk::XmpMeta::gpsCoordFromXmp("45,W");
-        BOOST_CHECK(isnan(output));
-
-        // malformed 3
-        output = fwk::XmpMeta::gpsCoordFromXmp("45,29,N");
-        BOOST_CHECK(isnan(output));
-
-        // out of bounds
-        output = fwk::XmpMeta::gpsCoordFromXmp("200,29.6681666667N");
-        BOOST_CHECK(isnan(output));
-
-        // well-formed 1
-        std::string gps1 = "45,29.6681666667N";
-        output = fwk::XmpMeta::gpsCoordFromXmp(gps1);
-        BOOST_CHECK(output == 45.494469444445002181964810006320476531982421875);
-
-        // well-formed 2
-        std::string gps2 = "73,38.2871666667W";
-        output = fwk::XmpMeta::gpsCoordFromXmp(gps2);
-        BOOST_CHECK(output == -73.6381194444449960201382054947316646575927734375);
-
-        // well-formed 3
-        std::string gps3 = "45,29,30.45N";
-        output = fwk::XmpMeta::gpsCoordFromXmp(gps3);
-        BOOST_CHECK(output == 45.49179166666666418450404307805001735687255859375);
-
        return 0;
 }
 
diff --git a/src/lib.rs b/src/lib.rs
index fc51214..d57ea8e 100644
--- a/src/lib.rs
+++ b/src/lib.rs
@@ -18,5 +18,21 @@
  */
 
 extern crate exempi;
+extern crate libc;
 
 pub mod fwk;
+
+use std::f64;
+use std::ffi::CStr;
+use libc::c_char;
+
+#[no_mangle]
+pub extern fn fwk_gps_coord_from_xmp(cvalue: *const c_char) -> f64 {
+    let value = unsafe { CStr::from_ptr(cvalue) };
+    if let Ok(svalue) = value.to_str() {
+        if let Some(coord) = fwk::gps_coord_from_xmp(svalue) {
+            return coord;
+        }
+    }
+    f64::NAN
+}
diff --git a/src/niepce/Makefile.am b/src/niepce/Makefile.am
index 196424a..de334a7 100644
--- a/src/niepce/Makefile.am
+++ b/src/niepce/Makefile.am
@@ -21,10 +21,13 @@ niepce_LDADD = \
        $(top_builddir)/src/fwk/base/libfwkbase.a \
        $(top_builddir)/src/ncr/libncr.a \
        $(top_builddir)/src/ext/libview/libview.a \
+       $(top_builddir)/target/release/libniece_rust.a \
        @FRAMEWORK_LIBS@ \
        @GPHOTO_LIBS@ \
        @BABL_LIBS@ \
-       @GEGL_LIBS@ @OPENRAW_LIBS@
+       @GEGL_LIBS@ @OPENRAW_LIBS@ \
+       -ldl \
+       $(NULL)
 
 
 niepce_SOURCES = \
diff --git a/src/niepce/modules/map/mapmodule.cpp b/src/niepce/modules/map/mapmodule.cpp
index 8d7d99c..43896d2 100644
--- a/src/niepce/modules/map/mapmodule.cpp
+++ b/src/niepce/modules/map/mapmodule.cpp
@@ -87,8 +87,7 @@ MapModule::on_lib_notification(const eng::LibNotification &ln)
                 fwk::PropertyValue val = result.unwrap();
                 // it is a string
                 if (is_string(val)) {
-                    longitude = fwk::XmpMeta::gpsCoordFromXmp(
-                        fwk::get_string(val));
+                    longitude = fwk_gps_coord_from_xmp(fwk::get_string(val).c_str());
                 }
             }
             result = properties.get_value_for_property(eng::NpExifGpsLatProp);
@@ -96,8 +95,7 @@ MapModule::on_lib_notification(const eng::LibNotification &ln)
                 fwk::PropertyValue val = result.unwrap();
                 // it is a string
                 if (is_string(val)) {
-                    latitude = fwk::XmpMeta::gpsCoordFromXmp(
-                        fwk::get_string(val));
+                    latitude = fwk_gps_coord_from_xmp(fwk::get_string(val).c_str());
                 }
             }
 


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