From 65e3eb1a68581bede3788501a3052e97df16c91a Mon Sep 17 00:00:00 2001 From: Matthew Petroff Date: Thu, 18 Jul 2013 15:00:03 -0400 Subject: Added quantity string parsing. (bzr r12380.1.24) --- src/util/units.cpp | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) (limited to 'src/util/units.cpp') diff --git a/src/util/units.cpp b/src/util/units.cpp index d485f6aef..705fc850c 100644 --- a/src/util/units.cpp +++ b/src/util/units.cpp @@ -16,6 +16,7 @@ #include #include #include +#include #include "io/simple-sax.h" #include "util/units.h" @@ -228,6 +229,27 @@ Unit UnitTable::getUnit(Glib::ustring const &unit_abbr) const { } } +Quantity UnitTable::getQuantity(Glib::ustring const& q) const { + Glib::MatchInfo match_info; + + // Extract value + double value = 0; + Glib::RefPtr value_regex = Glib::Regex::create("\\d+\\.?\\d"); + if (value_regex->match(q, match_info)) { + value = atof(match_info.fetch(0).c_str()); + } + + // Extract unit abbreviation + Glib::ustring abbr; + Glib::RefPtr unit_regex = Glib::Regex::create("[A-z]+"); + if (unit_regex->match(q, match_info)) { + abbr = match_info.fetch(0); + } + Unit *u = new Inkscape::Util::Unit(getUnit(abbr)); + + return Quantity(value, u); +} + bool UnitTable::deleteUnit(Unit const &u) { bool deleted = false; // Cannot delete the primary unit type since it's -- cgit v1.2.3