2 * This file is part of the GROMACS molecular simulation package.
4 * Copyright (c) 2012,2013, by the GROMACS development team, led by
5 * Mark Abraham, David van der Spoel, Berk Hess, and Erik Lindahl,
6 * and including many others, as listed in the AUTHORS file in the
7 * top-level source directory and at http://www.gromacs.org.
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.
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.
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.
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.
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.
37 * Implements classes and functions from helptopic.h.
39 * \author Teemu Murtola <teemu.murtola@gmail.com>
40 * \ingroup module_onlinehelp
42 #include "helptopic.h"
47 #include "gromacs/onlinehelp/helpformat.h"
48 #include "gromacs/onlinehelp/helpwritercontext.h"
49 #include "gromacs/utility/exceptions.h"
50 #include "gromacs/utility/file.h"
51 #include "gromacs/utility/gmxassert.h"
52 #include "gromacs/utility/stringutil.h"
58 void writeBasicHelpTopic(const HelpWriterContext &context,
59 const HelpTopicInterface &topic,
60 const std::string &text)
62 const char *title = topic.title();
63 if (title != NULL && title[0] != '\0')
65 context.writeTitle(title);
67 context.writeTextBlock(text);
71 /********************************************************************
72 * AbstractSimpleHelpTopic
75 bool AbstractSimpleHelpTopic::hasSubTopics() const
80 const HelpTopicInterface *
81 AbstractSimpleHelpTopic::findSubTopic(const char * /* name */) const
86 void AbstractSimpleHelpTopic::writeHelp(const HelpWriterContext &context) const
88 writeBasicHelpTopic(context, *this, helpText());
91 /********************************************************************
92 * AbstractCompositeHelpTopic::Impl
96 * Private implementation class for AbstractCompositeHelpTopic.
98 * \ingroup module_onlinehelp
100 class AbstractCompositeHelpTopic::Impl
103 //! Container for mapping subtopic names to help topic objects.
104 typedef std::map<std::string, HelpTopicPointer> SubTopicMap;
107 * Maps subtopic names to help topic objects.
109 * Owns the contained subtopics.
111 SubTopicMap subtopics_;
114 /********************************************************************
115 * AbstractCompositeHelpTopic
118 AbstractCompositeHelpTopic::AbstractCompositeHelpTopic()
123 AbstractCompositeHelpTopic::~AbstractCompositeHelpTopic()
127 bool AbstractCompositeHelpTopic::hasSubTopics() const
129 return !impl_->subtopics_.empty();
132 const HelpTopicInterface *
133 AbstractCompositeHelpTopic::findSubTopic(const char *name) const
135 Impl::SubTopicMap::const_iterator topic = impl_->subtopics_.find(name);
136 if (topic == impl_->subtopics_.end())
140 return topic->second.get();
143 void AbstractCompositeHelpTopic::writeHelp(const HelpWriterContext &context) const
145 writeBasicHelpTopic(context, *this, helpText());
146 writeSubTopicList(context, "\nAvailable subtopics:");
150 AbstractCompositeHelpTopic::writeSubTopicList(const HelpWriterContext &context,
151 const std::string &title) const
153 if (context.outputFormat() != eHelpOutputFormat_Console)
155 // TODO: Implement once the situation with Redmine issue #969 is more
157 GMX_THROW(NotImplementedError(
158 "Subtopic listing is not implemented for this output format"));
160 int maxNameLength = 0;
161 Impl::SubTopicMap::const_iterator topic;
162 for (topic = impl_->subtopics_.begin(); topic != impl_->subtopics_.end(); ++topic)
164 const char *title = topic->second->title();
165 if (title == NULL || title[0] == '\0')
169 int nameLength = static_cast<int>(topic->first.length());
170 if (nameLength > maxNameLength)
172 maxNameLength = nameLength;
175 if (maxNameLength == 0)
179 File &file = context.outputFile();
180 TextTableFormatter formatter;
181 formatter.addColumn(NULL, maxNameLength + 1, false);
182 formatter.addColumn(NULL, 72 - maxNameLength, true);
183 formatter.setFirstColumnIndent(4);
184 file.writeLine(title);
185 for (topic = impl_->subtopics_.begin(); topic != impl_->subtopics_.end(); ++topic)
187 const char *name = topic->first.c_str();
188 const char *title = topic->second->title();
189 if (title != NULL && title[0] != '\0')
192 formatter.addColumnLine(0, name);
193 formatter.addColumnLine(1, title);
194 file.writeString(formatter.formatRow());
200 void AbstractCompositeHelpTopic::addSubTopic(HelpTopicPointer topic)
202 GMX_ASSERT(impl_->subtopics_.find(topic->name()) == impl_->subtopics_.end(),
203 "Attempted to register a duplicate help topic name");
204 impl_->subtopics_.insert(std::make_pair(std::string(topic->name()),