Enable clang-tidy for headers
[alexxy/gromacs.git] / src / gromacs / fileio / readinp.h
1 /*
2  * This file is part of the GROMACS molecular simulation package.
3  *
4  * Copyright (c) 1991-2000, University of Groningen, The Netherlands.
5  * Copyright (c) 2001-2004, The GROMACS development team.
6  * Copyright (c) 2013,2014,2015,2016,2017,2018, by the GROMACS development team, led by
7  * Mark Abraham, David van der Spoel, Berk Hess, and Erik Lindahl,
8  * and including many others, as listed in the AUTHORS file in the
9  * top-level source directory and at http://www.gromacs.org.
10  *
11  * GROMACS is free software; you can redistribute it and/or
12  * modify it under the terms of the GNU Lesser General Public License
13  * as published by the Free Software Foundation; either version 2.1
14  * of the License, or (at your option) any later version.
15  *
16  * GROMACS is distributed in the hope that it will be useful,
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
19  * Lesser General Public License for more details.
20  *
21  * You should have received a copy of the GNU Lesser General Public
22  * License along with GROMACS; if not, see
23  * http://www.gnu.org/licenses, or write to the Free Software Foundation,
24  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA.
25  *
26  * If you want to redistribute modifications to GROMACS, please
27  * consider that scientific software is very special. Version
28  * control is crucial - bugs must be traceable. We will be happy to
29  * consider code for inclusion in the official distribution, but
30  * derived work must not be called official GROMACS. Details are found
31  * in the README & COPYING files - if they are missing, get the
32  * official version at http://www.gromacs.org.
33  *
34  * To help us fund GROMACS development, we humbly ask that you cite
35  * the research papers on the package. Check out http://www.gromacs.org.
36  */
37 #ifndef GMX_FILEIO_READINP_H
38 #define GMX_FILEIO_READINP_H
39
40 #include <cstring>
41
42 #include <string>
43 #include <utility>
44 #include <vector>
45
46 #include "gromacs/utility/arrayref.h"
47 #include "gromacs/utility/basedefinitions.h"
48
49 struct warninp;
50 typedef warninp *warninp_t;
51
52 namespace gmx
53 {
54 class KeyValueTreeObject;
55 class TextInputStream;
56 class TextOutputStream;
57 }
58
59 /* !\brief Input file structure that is populated with entries read from a file.
60  *
61  * This structure contains the information read from the mdp file that is later used
62  * to build the ir from it. It is first constructed with a set of names and values,
63  * and later populated when checking against the available options in readir.
64  * Uses the functions below to both check entries and set the information.
65  */
66 struct t_inpfile
67 {
68     /*!\brief Minimum allowed constructor sets all elements */
69     t_inpfile (int count, int inp_count, bool bObsolete, bool bSet, bool bHandledAsKeyValueTree,
70                std::string name, std::string value) :
71         count_(count),
72         bObsolete_(bObsolete),
73         bSet_(bSet),
74         bHandledAsKeyValueTree_(bHandledAsKeyValueTree),
75         name_(std::move(name)),
76         value_(std::move(value)),
77         inp_count_(inp_count)
78     {
79     }
80     int         count_;                  /* sort order for output  */
81     bool        bObsolete_;              /* whether it is an obsolete param value */
82     bool        bSet_;                   /* whether it it has been read out */
83     bool        bHandledAsKeyValueTree_; /* whether it it has been handled with key-value machinery */
84     std::string name_;                   /* name of the parameter */
85     std::string value_;                  /* parameter value string */
86     int         inp_count_;              /* number of einps read. Only valid for the first item
87                                                                  in the inpfile list. */
88 };
89
90 /*! \brief Create and return a vector of t_inpfile structs
91  * from "key = value" lines in \c stream corresponding to file \c fn.
92  *
93  * \param[in]  stream          Text stream to read.
94  * \param[in]  fn              Filename corresponding to \c reader.
95  * \param[out] wi              Handler for context-sensitive warnings.
96  * \throws     std::bad_alloc  If out of memory.
97  * \throws     Anything the stream underlying \c reader can throw. */
98 std::vector<t_inpfile>
99 read_inpfile(gmx::TextInputStream *stream, const char *fn,
100              warninp_t wi);
101
102 gmx::KeyValueTreeObject flatKeyValueTreeFromInpFile(gmx::ArrayRef<const t_inpfile> inp);
103
104 enum class WriteMdpHeader
105 {
106     no, yes
107 };
108
109 /*! \brief Write "key = value" lines from \c inp to \c stream.
110  *
111  * \param[in]  stream          Text stream to write.
112  * \param[in]  fn              Filename corresponding to \c stream.
113  * \param[in]  inp             vector of key-value pairs.
114  * \param[in]  bHaltOnUnknown  Whether to issue a fatal error if an unknown key is found.
115  * \param[in]  writeHeader     Whether to write a header recording some context a user might like.
116  * \param[out] wi              Handler for context-sensitive warnings.
117  * \throws     std::bad_alloc  If out of memory.
118  * \throws     Anything the stream underlying \c writer can throw. */
119 void write_inpfile(gmx::TextOutputStream *stream, const char *fn, std::vector<t_inpfile> *inp,
120                    gmx_bool bHaltOnUnknown,
121                    WriteMdpHeader writeHeader,
122                    warninp_t wi);
123 /* Write inp to fn, warning (and perhaps halting) if any fields are
124  * unknown. The helpful header contains irreproducible content, so
125  * its writing can be suppressed to make testing more useful. */
126
127 void replace_inp_entry(gmx::ArrayRef<t_inpfile> inp,
128                        const char *old_entry, const char *new_entry);
129
130 int search_einp(gmx::ArrayRef<const t_inpfile> inp, const char *name);
131 /* Return the index of an .mdp field with the given name within the
132  * inp vector, if it exists. Return -1 if it does not exist. */
133
134 void mark_einp_set(gmx::ArrayRef<t_inpfile> inp, const char *name);
135
136 int get_eint(std::vector<t_inpfile> *inp, const char *name, int def,
137              warninp_t wi);
138
139 int64_t get_eint64(std::vector<t_inpfile> *inp,
140                    const char *name, int64_t def,
141                    warninp_t /*wi*/);
142
143 double get_ereal(std::vector<t_inpfile> *inp, const char *name, double def,
144                  warninp_t wi);
145
146 const char *get_estr(std::vector<t_inpfile> *inp, const char *name, const char *def);
147
148 int get_eeenum(std::vector<t_inpfile> *inp, const char *name, const char **defs,
149                warninp_t wi);
150 /* defs must be NULL terminated */
151
152 int get_eenum(std::vector<t_inpfile> *inp, const char *name, const char **defs);
153 /* defs must be NULL terminated */
154
155 //! Replace for macro CCTYPE, prints comment string after newline
156 void printStringNewline(std::vector<t_inpfile> *inp, const char *line);
157 //! Replace for macro CTYPE, prints comment string
158 void printStringNoNewline(std::vector<t_inpfile> *inp, const char *line);
159 //! Replace for macro STYPE, checks for existing string entry and if possible replaces it
160 void setStringEntry(std::vector<t_inpfile> *inp, const char *name, char *newName, const char *def);
161
162 #endif