diff options
| author | Matthew Petroff <matthew@mpetroff.net> | 2013-07-18 19:00:03 +0000 |
|---|---|---|
| committer | Matthew Petroff <matthew@mpetroff.net> | 2013-07-18 19:00:03 +0000 |
| commit | 65e3eb1a68581bede3788501a3052e97df16c91a (patch) | |
| tree | 4106fb49a391962ce01e0f5d280455ee8e77c776 /src/util/units.cpp | |
| parent | Ported "ui/dialog/clonetiler.*". (diff) | |
| download | inkscape-65e3eb1a68581bede3788501a3052e97df16c91a.tar.gz inkscape-65e3eb1a68581bede3788501a3052e97df16c91a.zip | |
Added quantity string parsing.
(bzr r12380.1.24)
Diffstat (limited to 'src/util/units.cpp')
| -rw-r--r-- | src/util/units.cpp | 22 |
1 files changed, 22 insertions, 0 deletions
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 <cmath> #include <cerrno> #include <glib.h> +#include <glibmm/regex.h> #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<Glib::Regex> 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<Glib::Regex> 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 |
