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