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"
51 /********************************************************************
55 class CommandLine::Impl
58 Impl(const char *const cmdline[], size_t count);
61 std::vector<char *> args_;
62 std::vector<char *> argv_;
66 CommandLine::Impl::Impl(const char *const cmdline[], size_t count)
70 argc_ = static_cast<int>(count);
71 for (size_t i = 0; i < count; ++i)
73 char *arg = strdup(cmdline[i]);
76 throw std::bad_alloc();
83 CommandLine::Impl::~Impl()
85 for (size_t i = 0; i < args_.size(); ++i)
91 /********************************************************************
95 CommandLine::CommandLine()
96 : impl_(new Impl(NULL, 0))
100 CommandLine::CommandLine(const char *const cmdline[], size_t count)
101 : impl_(new Impl(cmdline, count))
105 CommandLine::CommandLine(const CommandLine &other)
106 : impl_(new Impl(other.argv(), other.argc()))
110 CommandLine::~CommandLine()
114 void CommandLine::append(const char *arg)
116 size_t newSize = impl_->args_.size() + 1;
117 impl_->args_.reserve(newSize);
118 impl_->argv_.reserve(newSize);
119 char *newArg = strdup(arg);
122 throw std::bad_alloc();
124 impl_->args_.push_back(newArg);
125 impl_->argv_.push_back(newArg);
126 impl_->argc_ = static_cast<int>(newSize);
129 int &CommandLine::argc() { return impl_->argc_; }
130 char **CommandLine::argv() { return &impl_->argv_[0]; }
131 int CommandLine::argc() const { return impl_->argc_; }
132 const char *const *CommandLine::argv() const { return &impl_->argv_[0]; }
133 const char *CommandLine::arg(int i) const { return impl_->argv_[i]; }
135 std::string CommandLine::toString() const
138 for (size_t i = 0; i < impl_->args_.size(); ++i)
144 const char *arg = impl_->args_[i];
145 bool bSpaces = (std::strchr(arg, ' ') != NULL);