Handle wrapper binary options also for symlinks.
[alexxy/gromacs.git] / src / gromacs / options / optionsassigner.cpp
1 /*
2  * This file is part of the GROMACS molecular simulation package.
3  *
4  * Copyright (c) 2010,2011,2012,2013, by the GROMACS development team, led by
5  * David van der Spoel, Berk Hess, Erik Lindahl, and including many
6  * others, as listed in the AUTHORS file in the top-level source
7  * directory and at http://www.gromacs.org.
8  *
9  * GROMACS is free software; you can redistribute it and/or
10  * modify it under the terms of the GNU Lesser General Public License
11  * as published by the Free Software Foundation; either version 2.1
12  * of the License, or (at your option) any later version.
13  *
14  * GROMACS is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
17  * Lesser General Public License for more details.
18  *
19  * You should have received a copy of the GNU Lesser General Public
20  * License along with GROMACS; if not, see
21  * http://www.gnu.org/licenses, or write to the Free Software Foundation,
22  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA.
23  *
24  * If you want to redistribute modifications to GROMACS, please
25  * consider that scientific software is very special. Version
26  * control is crucial - bugs must be traceable. We will be happy to
27  * consider code for inclusion in the official distribution, but
28  * derived work must not be called official GROMACS. Details are found
29  * in the README & COPYING files - if they are missing, get the
30  * official version at http://www.gromacs.org.
31  *
32  * To help us fund GROMACS development, we humbly ask that you cite
33  * the research papers on the package. Check out http://www.gromacs.org.
34  */
35 /*! \internal \file
36  * \brief
37  * Implements gmx::OptionsAssigner.
38  *
39  * \author Teemu Murtola <teemu.murtola@gmail.com>
40  * \ingroup module_options
41  */
42 #include "gromacs/options/optionsassigner.h"
43
44 #include <deque>
45
46 #include "gromacs/options/abstractoptionstorage.h"
47 #include "gromacs/options/options.h"
48 #include "gromacs/utility/exceptions.h"
49 #include "gromacs/utility/gmxassert.h"
50
51 #include "options-impl.h"
52
53 namespace gmx
54 {
55
56 /********************************************************************
57  * OptionsAssigner::Impl
58  */
59
60 /*! \internal \brief
61  * Private implementation class for OptionsAssigner.
62  *
63  * \ingroup module_options
64  */
65 class OptionsAssigner::Impl
66 {
67     public:
68         //! Sets the option object to assign to.
69         explicit Impl(Options *options);
70
71         //! Returns true if a subsection has been set.
72         bool inSubSection() const { return sectionStack_.size() > 1; }
73         //! Returns the Options object for the current section.
74         Options &currentSection() const { return *sectionStack_.back(); }
75         /*! \brief
76          * Finds an option by the given name.
77          *
78          * \param[in] name  Name of the option to look for.
79          * \returns Pointer to the found option, or NULL if none found.
80          *
81          * This function takes into account the flags specified, and may change
82          * the internal state of the assigner to match the option found.
83          * If no option is found, the internal state is not modified.
84          */
85         AbstractOptionStorage *findOption(const char *name);
86
87         //! Options object to assign to.
88         Options                &options_;
89         //! Recognize boolean option "name" also as "noname".
90         bool                    bAcceptBooleanNoPrefix_;
91         //! Look for options in all sections, not just the current one.
92         bool                    bNoStrictSectioning_;
93         /*! \brief
94          * List of (sub)sections being assigned to.
95          *
96          * The first element always points to \a options_.
97          */
98         std::vector<Options *>  sectionStack_;
99         //! Current option being assigned to, or NULL if none.
100         AbstractOptionStorage  *currentOption_;
101         /*! \brief
102          * Number of values assigned so far to the current option.
103          *
104          * Counts the number of attempted assignments, whether they have been
105          * successful or not.
106          */
107         int                     currentValueCount_;
108         //! If true, a "no" prefix was given for the current boolean option.
109         bool                    reverseBoolean_;
110 };
111
112 OptionsAssigner::Impl::Impl(Options *options)
113     : options_(*options), bAcceptBooleanNoPrefix_(false),
114       bNoStrictSectioning_(false), currentOption_(NULL),
115       currentValueCount_(0), reverseBoolean_(false)
116 {
117     sectionStack_.push_back(&options_);
118 }
119
120 AbstractOptionStorage *
121 OptionsAssigner::Impl::findOption(const char *name)
122 {
123     GMX_RELEASE_ASSERT(currentOption_ == NULL,
124                        "Cannot search for another option while processing one");
125     AbstractOptionStorage *option  = NULL;
126     Options               *section = NULL;
127     Options               *root    = &currentSection();
128     Options               *oldRoot = NULL;
129     int                    upcount = 0;
130     std::deque<Options *>  searchList;
131     searchList.push_back(root);
132     while (option == NULL && !searchList.empty())
133     {
134         section = searchList.front();
135         option  = section->impl_->findOption(name);
136         if (option == NULL && bAcceptBooleanNoPrefix_)
137         {
138             if (name[0] == 'n' && name[1] == 'o')
139             {
140                 option = section->impl_->findOption(name + 2);
141                 if (option != NULL && option->isBoolean())
142                 {
143                     reverseBoolean_ = true;
144                 }
145                 else
146                 {
147                     option = NULL;
148                 }
149             }
150         }
151         searchList.pop_front();
152         if (bNoStrictSectioning_)
153         {
154             Options::Impl::SubSectionList::const_iterator i;
155             for (i = section->impl_->subSections_.begin();
156                  i != section->impl_->subSections_.end(); ++i)
157             {
158                 if (*i != oldRoot)
159                 {
160                     searchList.push_back(*i);
161                 }
162             }
163             if (searchList.empty() && root != &options_)
164             {
165                 root = root->impl_->parent_;
166                 ++upcount;
167                 searchList.push_back(root);
168             }
169         }
170     }
171     if (bNoStrictSectioning_ && option != NULL)
172     {
173         while (upcount > 0)
174         {
175             sectionStack_.pop_back();
176             --upcount;
177         }
178         std::vector<Options *> sections;
179         while (section != &currentSection())
180         {
181             sections.push_back(section);
182             section = section->impl_->parent_;
183         }
184         while (!sections.empty())
185         {
186             sectionStack_.push_back(sections.back());
187             sections.pop_back();
188         }
189     }
190     return option;
191 }
192
193 /********************************************************************
194  * OptionsAssigner
195  */
196
197 OptionsAssigner::OptionsAssigner(Options *options)
198     : impl_(new Impl(options))
199 {
200 }
201
202 OptionsAssigner::~OptionsAssigner()
203 {
204 }
205
206 void OptionsAssigner::setAcceptBooleanNoPrefix(bool bEnabled)
207 {
208     impl_->bAcceptBooleanNoPrefix_ = bEnabled;
209 }
210
211 void OptionsAssigner::setNoStrictSectioning(bool bEnabled)
212 {
213     impl_->bNoStrictSectioning_ = bEnabled;
214 }
215
216 void OptionsAssigner::start()
217 {
218     impl_->options_.impl_->startSource();
219 }
220
221 void OptionsAssigner::startSubSection(const char *name)
222 {
223     Options *section = impl_->currentSection().impl_->findSubSection(name);
224     if (section == NULL)
225     {
226         GMX_THROW(InvalidInputError("Unknown subsection"));
227     }
228     impl_->sectionStack_.push_back(section);
229 }
230
231 void OptionsAssigner::startOption(const char *name)
232 {
233     if (!tryStartOption(name))
234     {
235         GMX_THROW(InvalidInputError("Unknown option"));
236     }
237 }
238
239 bool OptionsAssigner::tryStartOption(const char *name)
240 {
241     GMX_RELEASE_ASSERT(impl_->currentOption_ == NULL, "finishOption() not called");
242     AbstractOptionStorage *option = impl_->findOption(name);
243     if (option == NULL)
244     {
245         return false;
246     }
247     option->startSet();
248     impl_->currentOption_     = option;
249     impl_->currentValueCount_ = 0;
250     return true;
251 }
252
253 void OptionsAssigner::appendValue(const std::string &value)
254 {
255     AbstractOptionStorage *option = impl_->currentOption_;
256     GMX_RELEASE_ASSERT(option != NULL, "startOption() not called");
257     ++impl_->currentValueCount_;
258     option->appendValue(value);
259 }
260
261 void OptionsAssigner::finishOption()
262 {
263     AbstractOptionStorage *option = impl_->currentOption_;
264     GMX_RELEASE_ASSERT(option != NULL, "startOption() not called");
265     bool                   bBoolReverseValue = false;
266     if (option->isBoolean())
267     {
268         if (impl_->currentValueCount_ == 0)
269         {
270             // Should not throw, otherwise something is wrong.
271             // TODO: Get rid of the hard-coded values.
272             option->appendValue(impl_->reverseBoolean_ ? "0" : "1");
273         }
274         else if (impl_->reverseBoolean_)
275         {
276             bBoolReverseValue = true;
277         }
278     }
279     impl_->currentOption_  = NULL;
280     impl_->reverseBoolean_ = false;
281     option->finishSet();
282     if (bBoolReverseValue)
283     {
284         GMX_THROW(InvalidInputError("Cannot specify a value together with 'no' prefix"));
285     }
286 }
287
288 void OptionsAssigner::finishSubSection()
289 {
290     // Should only be called if we are in a subsection.
291     GMX_RELEASE_ASSERT(impl_->inSubSection(), "startSubSection() not called");
292     impl_->sectionStack_.pop_back();
293 }
294
295 void OptionsAssigner::finish()
296 {
297     GMX_RELEASE_ASSERT(impl_->currentOption_ == NULL, "finishOption() not called");
298     if (impl_->bNoStrictSectioning_)
299     {
300         while (impl_->inSubSection())
301         {
302             finishSubSection();
303         }
304     }
305     GMX_RELEASE_ASSERT(!impl_->inSubSection(), "finishSubSection() not called");
306 }
307
308 } // namespace gmx