10d2952d94409c6072261ec4826b5d8936f4c365
[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,2019, 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 } // namespace gmx
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,
70               int         inp_count,
71               bool        bObsolete,
72               bool        bSet,
73               bool        bHandledAsKeyValueTree,
74               std::string name,
75               std::string value) :
76         count_(count),
77         bObsolete_(bObsolete),
78         bSet_(bSet),
79         bHandledAsKeyValueTree_(bHandledAsKeyValueTree),
80         name_(std::move(name)),
81         value_(std::move(value)),
82         inp_count_(inp_count)
83     {
84     }
85     int  count_;                  /* sort order for output  */
86     bool bObsolete_;              /* whether it is an obsolete param value */
87     bool bSet_;                   /* whether it it has been read out */
88     bool bHandledAsKeyValueTree_; /* whether it it has been handled with key-value machinery */
89     std::string name_;            /* name of the parameter */
90     std::string value_;           /* parameter value string */
91     int         inp_count_;       /* number of einps read. Only valid for the first item
92                                                           in the inpfile list. */
93 };
94
95 /*! \brief Create and return a vector of t_inpfile structs
96  * from "key = value" lines in \c stream corresponding to file \c fn.
97  *
98  * \param[in]  stream          Text stream to read.
99  * \param[in]  fn              Filename corresponding to \c reader.
100  * \param[out] wi              Handler for context-sensitive warnings.
101  * \throws     std::bad_alloc  If out of memory.
102  * \throws     Anything the stream underlying \c reader can throw. */
103 std::vector<t_inpfile> read_inpfile(gmx::TextInputStream* stream, const char* fn, warninp_t wi);
104
105 gmx::KeyValueTreeObject flatKeyValueTreeFromInpFile(gmx::ArrayRef<const t_inpfile> inp);
106
107 enum class WriteMdpHeader
108 {
109     no,
110     yes
111 };
112
113 /*! \brief Write "key = value" lines from \c inp to \c stream.
114  *
115  * \param[in]  stream          Text stream to write.
116  * \param[in]  fn              Filename corresponding to \c stream.
117  * \param[in]  inp             vector of key-value pairs.
118  * \param[in]  bHaltOnUnknown  Whether to issue a fatal error if an unknown key is found.
119  * \param[in]  writeHeader     Whether to write a header recording some context a user might like.
120  * \param[out] wi              Handler for context-sensitive warnings.
121  * \throws     std::bad_alloc  If out of memory.
122  * \throws     Anything the stream underlying \c writer can throw. */
123 void write_inpfile(gmx::TextOutputStream*  stream,
124                    const char*             fn,
125                    std::vector<t_inpfile>* inp,
126                    gmx_bool                bHaltOnUnknown,
127                    WriteMdpHeader          writeHeader,
128                    warninp_t               wi);
129 /* Write inp to fn, warning (and perhaps halting) if any fields are
130  * unknown. The helpful header contains irreproducible content, so
131  * its writing can be suppressed to make testing more useful. */
132
133 void replace_inp_entry(gmx::ArrayRef<t_inpfile> inp, const char* old_entry, const char* new_entry);
134
135 int search_einp(gmx::ArrayRef<const t_inpfile> inp, const char* name);
136 /* Return the index of an .mdp field with the given name within the
137  * inp vector, if it exists. Return -1 if it does not exist. */
138
139 void mark_einp_set(gmx::ArrayRef<t_inpfile> inp, const char* name);
140
141 int get_eint(std::vector<t_inpfile>* inp, const char* name, int def, warninp_t wi);
142 int get_eint(std::vector<t_inpfile>* inp, const std::string& name, int def, warninp_t wi);
143
144 int64_t get_eint64(std::vector<t_inpfile>* inp, const char* name, int64_t def, warninp_t wi);
145 int64_t get_eint64(std::vector<t_inpfile>* inp, const std::string& name, int64_t def, warninp_t wi);
146
147 double get_ereal(std::vector<t_inpfile>* inp, const char* name, double def, warninp_t wi);
148 double get_ereal(std::vector<t_inpfile>* inp, const std::string& name, double def, warninp_t wi);
149
150 const char* get_estr(std::vector<t_inpfile>* inp, const char* name, const char* def);
151 const char* get_estr(std::vector<t_inpfile>* inp, const std::string& name, const char* def);
152
153 int get_eeenum(std::vector<t_inpfile>* inp, const char* name, const char** defs, warninp_t wi);
154 /* defs must be NULL terminated */
155 int get_eeenum(std::vector<t_inpfile>* inp, const std::string& name, const char** defs, warninp_t wi);
156 /* defs must be NULL terminated */
157
158 int get_eenum(std::vector<t_inpfile>* inp, const char* name, const char** defs);
159 /* defs must be NULL terminated */
160
161 //! Replace for macro CCTYPE, prints comment string after newline
162 void printStringNewline(std::vector<t_inpfile>* inp, const char* line);
163 //! Replace for macro CTYPE, prints comment string
164 void printStringNoNewline(std::vector<t_inpfile>* inp, const char* line);
165 //! Replace for macro STYPE, checks for existing string entry and if possible replaces it
166 void setStringEntry(std::vector<t_inpfile>* inp, const char* name, char* newName, const char* def);
167
168 #endif