Code beautification with uncrustify
[alexxy/gromacs.git] / src / gromacs / onlinehelp / helpwritercontext.cpp
1 /*
2  *
3  *                This source code is part of
4  *
5  *                 G   R   O   M   A   C   S
6  *
7  *          GROningen MAchine for Chemical Simulations
8  *
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.
13
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.
18  *
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.
25  *
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.
28  *
29  * For more info, check our website at http://www.gromacs.org
30  */
31 /*! \internal \file
32  * \brief
33  * Implements gmx::HelpWriterContext.
34  *
35  * \author Teemu Murtola <teemu.murtola@cbr.su.se>
36  * \ingroup module_onlinehelp
37  */
38 #include "helpwritercontext.h"
39
40 #include <cctype>
41
42 #include <algorithm>
43
44 #include "gromacs/legacyheaders/smalloc.h"
45 #include "gromacs/legacyheaders/wman.h"
46
47 #include "gromacs/onlinehelp/helpformat.h"
48 #include "gromacs/utility/exceptions.h"
49 #include "gromacs/utility/file.h"
50 #include "gromacs/utility/gmxassert.h"
51 #include "gromacs/utility/programinfo.h"
52 #include "gromacs/utility/stringutil.h"
53
54 namespace
55 {
56
57 /*! \internal \brief
58  * Make the string uppercase.
59  *
60  * \param[in] text  Input text.
61  * \returns   \p text with all characters transformed to uppercase.
62  * \throws    std::bad_alloc if out of memory.
63  */
64 std::string toUpperCase(const std::string &text)
65 {
66     std::string result(text);
67     transform(result.begin(), result.end(), result.begin(), toupper);
68     return result;
69 }
70
71 } // namespace
72
73 namespace gmx
74 {
75
76 /********************************************************************
77  * HelpWriterContext::Impl
78  */
79
80 /*! \internal \brief
81  * Private implementation class for HelpWriterContext.
82  *
83  * \ingroup module_onlinehelp
84  */
85 class HelpWriterContext::Impl
86 {
87     public:
88         //! Initializes the context with the given output file and format.
89         explicit Impl(File *file, HelpOutputFormat format)
90             : file_(*file), format_(format)
91         {
92         }
93
94         //! Output file to which the help is written.
95         File                   &file_;
96         //! Output format for the help output.
97         HelpOutputFormat        format_;
98 };
99
100 /********************************************************************
101  * HelpWriterContext
102  */
103
104 HelpWriterContext::HelpWriterContext(File *file, HelpOutputFormat format)
105     : impl_(new Impl(file, format))
106 {
107     if (format != eHelpOutputFormat_Console)
108     {
109         // TODO: Implement once the situation with Redmine issue #969 is more
110         // clear.
111         GMX_THROW(NotImplementedError(
112                           "This output format is not implemented"));
113     }
114 }
115
116 HelpWriterContext::~HelpWriterContext()
117 {
118 }
119
120 HelpOutputFormat HelpWriterContext::outputFormat() const
121 {
122     return impl_->format_;
123 }
124
125 File &HelpWriterContext::outputFile() const
126 {
127     return impl_->file_;
128 }
129
130 std::string HelpWriterContext::substituteMarkup(const std::string &text) const
131 {
132     char            *resultStr = check_tty(text.c_str());
133     scoped_ptr_sfree resultGuard(resultStr);
134     return std::string(resultStr);
135 }
136
137 void HelpWriterContext::writeTitle(const std::string &title) const
138 {
139     File &file = outputFile();
140     file.writeLine(toUpperCase(title));
141     file.writeLine();
142 }
143
144 void HelpWriterContext::writeTextBlock(const std::string &text) const
145 {
146     TextLineWrapper wrapper;
147     wrapper.settings().setLineLength(78);
148     const char     *program = ProgramInfo::getInstance().programName().c_str();
149     std::string     newText = replaceAll(text, "[PROGRAM]", program);
150     outputFile().writeLine(wrapper.wrapToString(substituteMarkup(newText)));
151 }
152
153 } // namespace gmx