/*
* This file is part of the GROMACS molecular simulation package.
*
- * Copyright (c) 2012,2013, by the GROMACS development team, led by
- * David van der Spoel, Berk Hess, Erik Lindahl, and including many
- * others, as listed in the AUTHORS file in the top-level source
- * directory and at http://www.gromacs.org.
+ * Copyright (c) 2012,2013,2014, by the GROMACS development team, led by
+ * Mark Abraham, David van der Spoel, Berk Hess, and Erik Lindahl,
+ * and including many others, as listed in the AUTHORS file in the
+ * top-level source directory and at http://www.gromacs.org.
*
* GROMACS is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public License
* \author Teemu Murtola <teemu.murtola@gmail.com>
* \ingroup module_testutils
*/
+#include "gmxpre.h"
+
#include "cmdlinetest.h"
#include <cstdlib>
#include <cstring>
#include <new>
+#include <sstream>
#include <vector>
+#include "gromacs/commandline/cmdlineprogramcontext.h"
+#include "gromacs/utility/arrayref.h"
#include "gromacs/utility/gmxassert.h"
-#include "gromacs/utility/programinfo.h"
namespace gmx
{
CommandLine::Impl::Impl(const char *const cmdline[], size_t count)
{
args_.reserve(count);
- argv_.reserve(count);
+ argv_.reserve(count + 1);
argc_ = static_cast<int>(count);
for (size_t i = 0; i < count; ++i)
{
args_.push_back(arg);
argv_.push_back(arg);
}
+ argv_.push_back(NULL);
}
CommandLine::Impl::~Impl()
{
}
-CommandLine::CommandLine(const char *const cmdline[], size_t count)
- : impl_(new Impl(cmdline, count))
+CommandLine::CommandLine(const ConstArrayRef<const char *> &cmdline)
+ : impl_(new Impl(cmdline.data(), cmdline.size()))
{
}
{
}
+void CommandLine::initFromArray(const ConstArrayRef<const char *> &cmdline)
+{
+ impl_.reset(new Impl(cmdline.data(), cmdline.size()));
+}
+
void CommandLine::append(const char *arg)
{
GMX_RELEASE_ASSERT(impl_->argc_ == static_cast<int>(impl_->args_.size()),
"Command-line has been modified externally");
size_t newSize = impl_->args_.size() + 1;
impl_->args_.reserve(newSize);
- impl_->argv_.reserve(newSize);
+ impl_->argv_.reserve(newSize + 1);
char *newArg = strdup(arg);
if (newArg == NULL)
{
throw std::bad_alloc();
}
impl_->args_.push_back(newArg);
+ impl_->argv_.pop_back(); // Remove the trailing NULL.
impl_->argv_.push_back(newArg);
+ impl_->argv_.push_back(NULL);
impl_->argc_ = static_cast<int>(newSize);
}
+namespace
+{
+
+//! Helper function for converting values to strings
+template <typename T>
+std::string value2string(T value)
+{
+ std::stringstream ss;
+ ss << value;
+ return ss.str();
+}
+
+}
+
+void CommandLine::addOption(const char *name,
+ const char *value)
+{
+ append(name);
+ append(value);
+}
+
+void CommandLine::addOption(const char *name,
+ const std::string &value)
+{
+ addOption(name, value.c_str());
+}
+
+void CommandLine::addOption(const char *name,
+ int value)
+{
+ append(name);
+ append(value2string(value));
+}
+
+void CommandLine::addOption(const char *name,
+ double value)
+{
+ append(name);
+ append(value2string(value));
+}
+
int &CommandLine::argc()
{
return impl_->argc_;
std::string CommandLine::toString() const
{
- return ProgramInfo(argc(), argv()).commandLine();
+ return CommandLineProgramContext(argc(), argv()).commandLine();
}
} // namespace test