about summary refs log tree commit diff
path: root/src/libmain/shared.hh
diff options
context:
space:
mode:
authorChristian Theune <ct@flyingcircus.io>2016-01-04T23·40+0100
committerChristian Theune <ct@flyingcircus.io>2016-01-04T23·40+0100
commit14ebde52893263930cdcde1406cc91cc5c42556f (patch)
tree2b65c11405f9aef33eb284208716f9cb5b434599 /src/libmain/shared.hh
parentb8258a4475726b56a4caa6553568c67a343a091d (diff)
First hit at providing support for floats in the language.
Diffstat (limited to 'src/libmain/shared.hh')
-rw-r--r--src/libmain/shared.hh24
1 files changed, 24 insertions, 0 deletions
diff --git a/src/libmain/shared.hh b/src/libmain/shared.hh
index 65b288e1ff..a350f496d1 100644
--- a/src/libmain/shared.hh
+++ b/src/libmain/shared.hh
@@ -66,6 +66,30 @@ template<class N> N getIntArg(const string & opt,
     return n * multiplier;
 }
 
+template<class N> N getFloatArg(const string & opt,
+    Strings::iterator & i, const Strings::iterator & end, bool allowUnit)
+{
+    ++i;
+    if (i == end) throw UsageError(format("‘%1%’ requires an argument") % opt);
+    string s = *i;
+    N multiplier = 1;
+    if (allowUnit && !s.empty()) {
+        char u = std::toupper(*s.rbegin());
+        if (std::isalpha(u)) {
+            if (u == 'K') multiplier = 1ULL << 10;
+            else if (u == 'M') multiplier = 1ULL << 20;
+            else if (u == 'G') multiplier = 1ULL << 30;
+            else if (u == 'T') multiplier = 1ULL << 40;
+            else throw UsageError(format("invalid unit specifier ‘%1%’") % u);
+            s.resize(s.size() - 1);
+        }
+    }
+    N n;
+    if (!string2Float(s, n))
+        throw UsageError(format("‘%1%’ requires a float argument") % opt);
+    return n * multiplier;
+}
+
 /* Show the manual page for the specified program. */
 void showManPage(const string & name);