summaryrefslogtreecommitdiffstats
path: root/src/util
diff options
context:
space:
mode:
authorMatthew Petroff <matthew@mpetroff.net>2013-07-18 19:00:03 +0000
committerMatthew Petroff <matthew@mpetroff.net>2013-07-18 19:00:03 +0000
commit65e3eb1a68581bede3788501a3052e97df16c91a (patch)
tree4106fb49a391962ce01e0f5d280455ee8e77c776 /src/util
parentPorted "ui/dialog/clonetiler.*". (diff)
downloadinkscape-65e3eb1a68581bede3788501a3052e97df16c91a.tar.gz
inkscape-65e3eb1a68581bede3788501a3052e97df16c91a.zip
Added quantity string parsing.
(bzr r12380.1.24)
Diffstat (limited to 'src/util')
-rw-r--r--src/util/units.cpp22
-rw-r--r--src/util/units.h27
2 files changed, 37 insertions, 12 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
diff --git a/src/util/units.h b/src/util/units.h
index c6f124203..5d7bfaeaa 100644
--- a/src/util/units.h
+++ b/src/util/units.h
@@ -82,6 +82,18 @@ class Unit {
int metric() const;
};
+class Quantity {
+public:
+ const Unit *unit;
+ double quantity;
+
+ Quantity(double q, const Unit *u); // constructor
+ bool compatibleWith(const Unit *u) const;
+ double value(Unit *u) const;
+
+ static double convert(const double from_dist, const Unit *from, const Unit *to);
+};
+
class UnitTable {
public:
/**
@@ -99,6 +111,9 @@ class UnitTable {
/** Retrieve a given unit based on its string identifier */
Unit getUnit(Glib::ustring const& name) const;
+
+ /** Retrieve a quantity based on its string identifier */
+ Quantity getQuantity(Glib::ustring const& q) const;
/** Remove a unit definition from the given unit type table */
bool deleteUnit(Unit const& u);
@@ -142,18 +157,6 @@ class UnitTable {
};
-class Quantity {
-public:
- const Unit *unit;
- double quantity;
-
- Quantity(double q, const Unit *u); // constructor
- bool compatibleWith(const Unit *u) const;
- double value(Unit *u) const;
-
- static double convert(const double from_dist, const Unit *from, const Unit *to);
-};
-
} // namespace Util
} // namespace Inkscape