Sort includes outside src/gromacs
[alexxy/gromacs.git] / src / testutils / cmdlinetest.cpp
index a3397caa740db9f60847e7cd2a8d3ee72f790db9..5192ee7853a65e73500bd562c7a916f6cd08da22 100644 (file)
@@ -1,10 +1,10 @@
 /*
  * 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
 {
@@ -73,7 +77,7 @@ class CommandLine::Impl
 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)
     {
@@ -85,6 +89,7 @@ CommandLine::Impl::Impl(const char *const cmdline[], size_t count)
         args_.push_back(arg);
         argv_.push_back(arg);
     }
+    argv_.push_back(NULL);
 }
 
 CommandLine::Impl::~Impl()
@@ -104,8 +109,8 @@ CommandLine::CommandLine()
 {
 }
 
-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()))
 {
 }
 
@@ -118,23 +123,71 @@ CommandLine::~CommandLine()
 {
 }
 
+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_;
@@ -158,7 +211,7 @@ const char *CommandLine::arg(int i) const
 
 std::string CommandLine::toString() const
 {
-    return ProgramInfo(argc(), argv()).commandLine();
+    return CommandLineProgramContext(argc(), argv()).commandLine();
 }
 
 } // namespace test