From 9b8fda796b620a5476e8a50edcc29a3462667fc3 Mon Sep 17 00:00:00 2001 From: Eelco Dolstra Date: Tue, 24 Nov 2009 12:26:25 +0000 Subject: [PATCH] * Templatise getIntArg / string2Int. --- src/libmain/shared.cc | 18 +++--------------- src/libmain/shared.hh | 16 ++++++++++++---- src/libutil/util.cc | 16 ---------------- src/libutil/util.hh | 9 +++++++-- src/nix-store/nix-store.cc | 6 +++--- 5 files changed, 25 insertions(+), 40 deletions(-) diff --git a/src/libmain/shared.cc b/src/libmain/shared.cc index 10ccb2985..0f7a89814 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(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(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 c432dc5f7..f70f6893b 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 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 248095b34..1b86e88d7 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 5744e5692..2d75a7a50 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 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 e08908cd7..b873baacb 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(*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(*i, i, opFlags.end()); else throw UsageError(format("bad sub-operation `%1%' in GC") % *i); if (!opArgs.empty()) throw UsageError("no arguments expected");