about summary refs log tree commit diff
diff options
context:
space:
mode:
authorEelco Dolstra <e.dolstra@tudelft.nl>2009-11-24T12·26+0000
committerEelco Dolstra <e.dolstra@tudelft.nl>2009-11-24T12·26+0000
commit9b8fda796b620a5476e8a50edcc29a3462667fc3 (patch)
treefa546a5b5a0b08c01a7bdc9ee50e69d7bfd87165
parent80220155523fb21c938d595e96597f6511aa7bd0 (diff)
* Templatise getIntArg / string2Int.
-rw-r--r--src/libmain/shared.cc18
-rw-r--r--src/libmain/shared.hh16
-rw-r--r--src/libutil/util.cc16
-rw-r--r--src/libutil/util.hh9
-rw-r--r--src/nix-store/nix-store.cc6
5 files changed, 25 insertions, 40 deletions
diff --git a/src/libmain/shared.cc b/src/libmain/shared.cc
index 10ccb2985f74..0f7a89814b6d 100644
--- a/src/libmain/shared.cc
+++ b/src/libmain/shared.cc
@@ -87,18 +87,6 @@ static void setLogType(string lt)
 }
 
 
-unsigned long long getIntArg(const string & opt,
-    Strings::iterator & i, const Strings::iterator & end)
-{
-    ++i;
-    if (i == end) throw UsageError(format("`%1%' requires an argument") % opt);
-    long long n;
-    if (!string2Int(*i, n) || n < 0)
-        throw UsageError(format("`%1%' requires a non-negative integer") % opt);
-    return n;
-}
-
-
 void initDerivationsHelpers();
 
 
@@ -195,7 +183,7 @@ static void initAndRun(int argc, char * * argv)
     for (Strings::iterator i = args.begin(); i != args.end(); ++i) {
         string arg = *i;
         if (string(arg, 0, 4) == "-at-") ;
-        else if (arg.length() > 2 && arg[0] == '-' && arg[1] != '-') {
+        else if (arg.length() > 2 && arg[0] == '-' && arg[1] != '-' && !isdigit(arg[1])) {
             for (unsigned int j = 1; j < arg.length(); j++)
                 if (isalpha(arg[j]))
                     remaining.push_back((string) "-" + arg[j]);
@@ -239,11 +227,11 @@ static void initAndRun(int argc, char * * argv)
         else if (arg == "--fallback")
             tryFallback = true;
         else if (arg == "--max-jobs" || arg == "-j")
-            maxBuildJobs = getIntArg(arg, i, args.end());
+            maxBuildJobs = getIntArg<unsigned int>(arg, i, args.end());
         else if (arg == "--readonly-mode")
             readOnlyMode = true;
         else if (arg == "--max-silent-time")
-            maxSilentTime = getIntArg(arg, i, args.end());
+            maxSilentTime = getIntArg<unsigned int>(arg, i, args.end());
         else if (arg == "--no-build-hook")
             useBuildHook = false;
         else if (arg == "--show-trace")
diff --git a/src/libmain/shared.hh b/src/libmain/shared.hh
index c432dc5f7b56..f70f6893b4bd 100644
--- a/src/libmain/shared.hh
+++ b/src/libmain/shared.hh
@@ -22,22 +22,30 @@ extern std::string programId;
 
 namespace nix {
 
+MakeError(UsageError, nix::Error);
+
 /* Ugh.  No better place to put this. */
 Path makeRootName(const Path & gcRoot, int & counter);
 void printGCWarning();
 
 void printMissing(const PathSet & paths);
 
-unsigned long long getIntArg(const string & opt,
-    Strings::iterator & i, const Strings::iterator & end);
+template<class N> N getIntArg(const string & opt,
+    Strings::iterator & i, const Strings::iterator & end)
+{
+    ++i;
+    if (i == end) throw UsageError(format("`%1%' requires an argument") % opt);
+    N n;
+    if (!string2Int(*i, n))
+        throw UsageError(format("`%1%' requires an integer argument") % opt);
+    return n;
+}
 
 /* Whether we're running setuid. */
 extern bool setuidMode;
 
 extern volatile ::sig_atomic_t blockInt;
 
-MakeError(UsageError, nix::Error);
-
 struct RemoveTempRoots 
 {
     ~RemoveTempRoots();    
diff --git a/src/libutil/util.cc b/src/libutil/util.cc
index 248095b34296..1b86e88d7ff3 100644
--- a/src/libutil/util.cc
+++ b/src/libutil/util.cc
@@ -1055,22 +1055,6 @@ string int2String(int n)
 }
 
 
-bool string2Int(const string & s, int & n)
-{
-    std::istringstream str(s);
-    str >> n;
-    return str && str.get() == EOF;
-}
-
-
-bool string2Int(const string & s, long long & n)
-{
-    std::istringstream str(s);
-    str >> n;
-    return str && str.get() == EOF;
-}
-
-
 bool hasSuffix(const string & s, const string & suffix)
 {
     return s.size() >= suffix.size() && string(s, s.size() - suffix.size()) == suffix;
diff --git a/src/libutil/util.hh b/src/libutil/util.hh
index 5744e5692280..2d75a7a506aa 100644
--- a/src/libutil/util.hh
+++ b/src/libutil/util.hh
@@ -295,9 +295,14 @@ bool statusOk(int status);
 
 
 /* Parse a string into an integer. */
+template<class N> bool string2Int(const string & s, N & n)
+{
+    std::istringstream str(s);
+    str >> n;
+    return str && str.get() == EOF;
+}
+
 string int2String(int n);
-bool string2Int(const string & s, int & n);
-bool string2Int(const string & s, long long & n);
 
 
 /* Return true iff `s' ends in `suffix'. */
diff --git a/src/nix-store/nix-store.cc b/src/nix-store/nix-store.cc
index e08908cd78ec..b873baacb932 100644
--- a/src/nix-store/nix-store.cc
+++ b/src/nix-store/nix-store.cc
@@ -532,10 +532,10 @@ static void opGC(Strings opFlags, Strings opArgs)
         else if (*i == "--print-dead") options.action = GCOptions::gcReturnDead;
         else if (*i == "--delete") options.action = GCOptions::gcDeleteDead;
         else if (*i == "--max-freed") {
-            options.maxFreed = getIntArg(*i, i, opFlags.end());
-            if (options.maxFreed == 0) options.maxFreed = 1;
+            long long maxFreed = getIntArg<long long>(*i, i, opFlags.end());
+            options.maxFreed = maxFreed >= 1 ? maxFreed : 1;
         }
-        else if (*i == "--max-links") options.maxLinks = getIntArg(*i, i, opFlags.end());
+        else if (*i == "--max-links") options.maxLinks = getIntArg<unsigned int>(*i, i, opFlags.end());
         else throw UsageError(format("bad sub-operation `%1%' in GC") % *i);
 
     if (!opArgs.empty()) throw UsageError("no arguments expected");