3 * This source code is part of
7 * GROningen MAchine for Chemical Simulations
9 * Written by David van der Spoel, Erik Lindahl, Berk Hess, and others.
10 * Copyright (c) 1991-2000, University of Groningen, The Netherlands.
11 * Copyright (c) 2001-2009, The GROMACS development team,
12 * check out http://www.gromacs.org for more information.
14 * This program is free software; you can redistribute it and/or
15 * modify it under the terms of the GNU General Public License
16 * as published by the Free Software Foundation; either version 2
17 * of the License, or (at your option) any later version.
19 * If you want to redistribute modifications, please consider that
20 * scientific software is very special. Version control is crucial -
21 * bugs must be traceable. We will be happy to consider code for
22 * inclusion in the official distribution, but derived work must not
23 * be called official GROMACS. Details are found in the README & COPYING
24 * files - if they are missing, get the official version at www.gromacs.org.
26 * To help us fund GROMACS development, we humbly ask that you cite
27 * the papers on the package - you can find them in the top README file.
29 * For more info, check our website at http://www.gromacs.org
33 * Implements gmx::test::CommandLine.
35 * \author Teemu Murtola <teemu.murtola@cbr.su.se>
36 * \ingroup module_testutils
38 #include "cmdlinetest.h"
46 #include "gromacs/utility/programinfo.h"
53 /********************************************************************
57 class CommandLine::Impl
60 Impl(const char *const cmdline[], size_t count);
63 std::vector<char *> args_;
64 std::vector<char *> argv_;
68 CommandLine::Impl::Impl(const char *const cmdline[], size_t count)
72 argc_ = static_cast<int>(count);
73 for (size_t i = 0; i < count; ++i)
75 char *arg = strdup(cmdline[i]);
78 throw std::bad_alloc();
85 CommandLine::Impl::~Impl()
87 for (size_t i = 0; i < args_.size(); ++i)
93 /********************************************************************
97 CommandLine::CommandLine()
98 : impl_(new Impl(NULL, 0))
102 CommandLine::CommandLine(const char *const cmdline[], size_t count)
103 : impl_(new Impl(cmdline, count))
107 CommandLine::CommandLine(const CommandLine &other)
108 : impl_(new Impl(other.argv(), other.argc()))
112 CommandLine::~CommandLine()
116 void CommandLine::append(const char *arg)
118 size_t newSize = impl_->args_.size() + 1;
119 impl_->args_.reserve(newSize);
120 impl_->argv_.reserve(newSize);
121 char *newArg = strdup(arg);
124 throw std::bad_alloc();
126 impl_->args_.push_back(newArg);
127 impl_->argv_.push_back(newArg);
128 impl_->argc_ = static_cast<int>(newSize);
131 int &CommandLine::argc() { return impl_->argc_; }
132 char **CommandLine::argv() { return &impl_->argv_[0]; }
133 int CommandLine::argc() const { return impl_->argc_; }
134 const char *const *CommandLine::argv() const { return &impl_->argv_[0]; }
135 const char *CommandLine::arg(int i) const { return impl_->argv_[i]; }
137 std::string CommandLine::toString() const
139 return ProgramInfo(argc(), argv()).commandLine();