Remove no-inline-max-size and suppress remark
[alexxy/gromacs.git] / src / gromacs / onlinehelp / helptopic.cpp
1 /*
2  * This file is part of the GROMACS molecular simulation package.
3  *
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.
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 classes and functions from helptopic.h.
38  *
39  * \author Teemu Murtola <teemu.murtola@gmail.com>
40  * \ingroup module_onlinehelp
41  */
42 #include "helptopic.h"
43
44 #include <map>
45 #include <utility>
46
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"
53
54 namespace gmx
55 {
56
57 /*! \cond libapi */
58 void writeBasicHelpTopic(const HelpWriterContext  &context,
59                          const HelpTopicInterface &topic,
60                          const std::string        &text)
61 {
62     const char *title = topic.title();
63     if (title != NULL && title[0] != '\0')
64     {
65         context.writeTitle(title);
66     }
67     context.writeTextBlock(text);
68 }
69 //! \endcond
70
71 /********************************************************************
72  * AbstractSimpleHelpTopic
73  */
74
75 bool AbstractSimpleHelpTopic::hasSubTopics() const
76 {
77     return false;
78 }
79
80 const HelpTopicInterface *
81 AbstractSimpleHelpTopic::findSubTopic(const char * /* name */) const
82 {
83     return NULL;
84 }
85
86 void AbstractSimpleHelpTopic::writeHelp(const HelpWriterContext &context) const
87 {
88     writeBasicHelpTopic(context, *this, helpText());
89 }
90
91 /********************************************************************
92  * AbstractCompositeHelpTopic::Impl
93  */
94
95 /*! \internal \brief
96  * Private implementation class for AbstractCompositeHelpTopic.
97  *
98  * \ingroup module_onlinehelp
99  */
100 class AbstractCompositeHelpTopic::Impl
101 {
102     public:
103         //! Container for mapping subtopic names to help topic objects.
104         typedef std::map<std::string, HelpTopicPointer> SubTopicMap;
105
106         /*! \brief
107          * Maps subtopic names to help topic objects.
108          *
109          * Owns the contained subtopics.
110          */
111         SubTopicMap             subtopics_;
112 };
113
114 /********************************************************************
115  * AbstractCompositeHelpTopic
116  */
117
118 AbstractCompositeHelpTopic::AbstractCompositeHelpTopic()
119     : impl_(new Impl)
120 {
121 }
122
123 AbstractCompositeHelpTopic::~AbstractCompositeHelpTopic()
124 {
125 }
126
127 bool AbstractCompositeHelpTopic::hasSubTopics() const
128 {
129     return !impl_->subtopics_.empty();
130 }
131
132 const HelpTopicInterface *
133 AbstractCompositeHelpTopic::findSubTopic(const char *name) const
134 {
135     Impl::SubTopicMap::const_iterator topic = impl_->subtopics_.find(name);
136     if (topic == impl_->subtopics_.end())
137     {
138         return NULL;
139     }
140     return topic->second.get();
141 }
142
143 void AbstractCompositeHelpTopic::writeHelp(const HelpWriterContext &context) const
144 {
145     writeBasicHelpTopic(context, *this, helpText());
146     writeSubTopicList(context, "\nAvailable subtopics:");
147 }
148
149 bool
150 AbstractCompositeHelpTopic::writeSubTopicList(const HelpWriterContext &context,
151                                               const std::string       &title) const
152 {
153     if (context.outputFormat() != eHelpOutputFormat_Console)
154     {
155         // TODO: Implement once the situation with Redmine issue #969 is more
156         // clear.
157         GMX_THROW(NotImplementedError(
158                           "Subtopic listing is not implemented for this output format"));
159     }
160     int maxNameLength = 0;
161     Impl::SubTopicMap::const_iterator topic;
162     for (topic = impl_->subtopics_.begin(); topic != impl_->subtopics_.end(); ++topic)
163     {
164         const char *title = topic->second->title();
165         if (title == NULL || title[0] == '\0')
166         {
167             continue;
168         }
169         int nameLength = static_cast<int>(topic->first.length());
170         if (nameLength > maxNameLength)
171         {
172             maxNameLength = nameLength;
173         }
174     }
175     if (maxNameLength == 0)
176     {
177         return false;
178     }
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)
186     {
187         const char *name  = topic->first.c_str();
188         const char *title = topic->second->title();
189         if (title != NULL && title[0] != '\0')
190         {
191             formatter.clear();
192             formatter.addColumnLine(0, name);
193             formatter.addColumnLine(1, title);
194             file.writeString(formatter.formatRow());
195         }
196     }
197     return true;
198 }
199
200 void AbstractCompositeHelpTopic::addSubTopic(HelpTopicPointer topic)
201 {
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()),
205                                             move(topic)));
206 }
207
208 } // namespace gmx