* Templatise getIntArg / string2Int.

This commit is contained in:
Eelco Dolstra 2009-11-24 12:26:25 +00:00
parent 8022015552
commit 9b8fda796b
5 changed files with 25 additions and 40 deletions

View file

@ -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")

View file

@ -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();

View file

@ -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;

View file

@ -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'. */

View file

@ -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");