[niepce: 4/29] fwk+rust: convert fwk::fraction_to_decimal() to Rust



commit 2a231c0dcc0e36b5184dd5f6ed2ee0a87a5dd93c
Author: Hubert Figuière <hub figuiere net>
Date:   Sat Jun 3 20:57:35 2017 -0400

    fwk+rust: convert fwk::fraction_to_decimal() to Rust

 src/fwk/base/Makefile.am           |   10 ++----
 src/fwk/base/fractions.cpp         |   54 ----------------------------------
 src/fwk/base/fractions.hpp         |    7 ++--
 src/fwk/base/fractions.rs          |   57 ++++++++++++++++++++++++++++++++++++
 src/fwk/base/mod.rs                |    3 ++
 src/fwk/base/t/testfractions.cpp   |   35 ----------------------
 src/fwk/mod.rs                     |    5 +++
 src/fwk/toolkit/metadatawidget.cpp |    2 +-
 src/lib.rs                         |   11 +++++++
 9 files changed, 83 insertions(+), 101 deletions(-)
---
diff --git a/src/fwk/base/Makefile.am b/src/fwk/base/Makefile.am
index b855bfa..7cde84f 100644
--- a/src/fwk/base/Makefile.am
+++ b/src/fwk/base/Makefile.am
@@ -7,10 +7,10 @@ AM_CPPFLAGS = -I$(top_srcdir)/src \
 noinst_LIBRARIES = libfwkbase.a
 
 
-TESTS = testmoniker testgeometry testfractions testdate testmap\
+TESTS = testmoniker testgeometry testdate testmap\
        testoption testpropertybag
 
-check_PROGRAMS = testmoniker testgeometry testfractions testdate testmap\
+check_PROGRAMS = testmoniker testgeometry testdate testmap\
        testoption testpropertybag
 
 testdate_SOURCES = t/testdate.cpp
@@ -25,10 +25,6 @@ testgeometry_SOURCES = t/testgeometry.cpp
 testgeometry_LDADD = libfwkbase.a \
        @FRAMEWORK_LIBS@
 
-testfractions_SOURCES = t/testfractions.cpp
-testfractions_LDADD = libfwkbase.a \
-       @FRAMEWORK_LIBS@
-
 testmap_SOURCES = t/testmap.cpp
 testmap_LDADD = libfwkbase.a \
        @FRAMEWORK_LIBS@
@@ -45,7 +41,7 @@ libfwkbase_a_SOURCES = colour.hpp colour.cpp \
        autoflag.hpp \
        date.hpp date.cpp \
        debug.hpp debug.cpp \
-       fractions.hpp fractions.cpp \
+       fractions.hpp \
        moniker.hpp moniker.cpp \
        geometry.hpp geometry.cpp \
        singleton.hpp \
diff --git a/src/fwk/base/fractions.hpp b/src/fwk/base/fractions.hpp
index fcd92a0..af030e2 100644
--- a/src/fwk/base/fractions.hpp
+++ b/src/fwk/base/fractions.hpp
@@ -26,12 +26,11 @@
 
 namespace fwk {
 
-
-/** convert a fraction string to a decimal */
-double fraction_to_decimal(const std::string & value);
-
 }
 
+// implemented in Rust
+extern "C" double fwk_fraction_to_decimal(const char* value);
+
 #endif
 /*
   Local Variables:
diff --git a/src/fwk/base/fractions.rs b/src/fwk/base/fractions.rs
new file mode 100644
index 0000000..8c1c6fb
--- /dev/null
+++ b/src/fwk/base/fractions.rs
@@ -0,0 +1,57 @@
+/*
+ * niepce - fwk/base/fractions.rs
+ *
+ * Copyright (C) 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
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program.  If not, see <http://www.gnu.org/licenses/>.
+ */
+
+use std::f64;
+
+
+pub fn fraction_to_decimal(value: &str) -> Option<f64>
+{
+    let numbers: Vec<i64> = value.split("/")
+        .map(|s| { s.parse::<i64>().unwrap_or(0) })
+        .collect();
+    if numbers.len() != 2 {
+        return None;
+    }
+    if numbers[1] == 0 {
+        return None;
+    }
+    Some(numbers[0] as f64 / numbers[1] as f64)
+}
+
+
+#[cfg(test)]
+mod tests {
+    #[test]
+    fn faction_to_decimal_works() {
+        use super::fraction_to_decimal;
+
+        let f = fraction_to_decimal("1/4");
+        assert!(f.is_some());
+        assert_eq!(f.unwrap(), 0.25);
+
+        let f = fraction_to_decimal("foobar");
+        assert!(f.is_none());
+
+        let f = fraction_to_decimal("1/0");
+        assert!(f.is_none());
+
+        let f = fraction_to_decimal("1/0/1");
+        assert!(f.is_none());
+    }
+}
diff --git a/src/fwk/base/mod.rs b/src/fwk/base/mod.rs
new file mode 100644
index 0000000..40444b7
--- /dev/null
+++ b/src/fwk/base/mod.rs
@@ -0,0 +1,3 @@
+
+
+pub mod fractions;
diff --git a/src/fwk/mod.rs b/src/fwk/mod.rs
index 1349746..f5e3ee8 100644
--- a/src/fwk/mod.rs
+++ b/src/fwk/mod.rs
@@ -18,6 +18,7 @@
  */
 
 pub mod utils;
+pub mod base;
 
 pub use self::utils::exempi::{
     NsDef,
@@ -25,3 +26,7 @@ pub use self::utils::exempi::{
     XmpMeta,
     gps_coord_from_xmp
 };
+
+pub use self::base::fractions::{
+    fraction_to_decimal
+};
diff --git a/src/fwk/toolkit/metadatawidget.cpp b/src/fwk/toolkit/metadatawidget.cpp
index a26b1c9..ff2fe48 100644
--- a/src/fwk/toolkit/metadatawidget.cpp
+++ b/src/fwk/toolkit/metadatawidget.cpp
@@ -269,7 +269,7 @@ bool MetaDataWidget::set_fraction_dec_data(Gtk::Widget* w,
         const std::string& str_value = get_string(value);
         DBG_OUT("set fraction dec %s", str_value.c_str());
         std::string frac = str(boost::format("%.1f")
-                               % fwk::fraction_to_decimal(str_value));
+                               % fwk_fraction_to_decimal(str_value.c_str()));
         AutoFlag flag(m_update);
         static_cast<Gtk::Label*>(w)->set_text(frac);
     }
diff --git a/src/lib.rs b/src/lib.rs
index d57ea8e..144b951 100644
--- a/src/lib.rs
+++ b/src/lib.rs
@@ -36,3 +36,14 @@ pub extern fn fwk_gps_coord_from_xmp(cvalue: *const c_char) -> f64 {
     }
     f64::NAN
 }
+
+#[no_mangle]
+pub extern fn fwk_fraction_to_decimal(cvalue: *const c_char) -> f64 {
+    let value = unsafe { CStr::from_ptr(cvalue) };
+    if let Ok(svalue) = value.to_str() {
+        if let Some(dec) = fwk::fraction_to_decimal(svalue) {
+            return dec;
+        }
+    }
+    f64::NAN
+}


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