2 * This file is part of the GROMACS molecular simulation package.
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.
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.
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.
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.
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.
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.
37 #ifndef GMX_FILEIO_READINP_H
38 #define GMX_FILEIO_READINP_H
46 #include "gromacs/utility/arrayref.h"
47 #include "gromacs/utility/basedefinitions.h"
50 typedef warninp* warninp_t;
54 class KeyValueTreeObject;
55 class TextInputStream;
56 class TextOutputStream;
59 /* !\brief Input file structure that is populated with entries read from a file.
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.
68 /*!\brief Minimum allowed constructor sets all elements */
73 bool bHandledAsKeyValueTree,
77 bObsolete_(bObsolete),
79 bHandledAsKeyValueTree_(bHandledAsKeyValueTree),
80 name_(std::move(name)),
81 value_(std::move(value)),
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. */
95 /*! \brief Create and return a vector of t_inpfile structs
96 * from "key = value" lines in \c stream corresponding to file \c fn.
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);
105 gmx::KeyValueTreeObject flatKeyValueTreeFromInpFile(gmx::ArrayRef<const t_inpfile> inp);
107 enum class WriteMdpHeader
113 /*! \brief Write "key = value" lines from \c inp to \c stream.
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,
125 std::vector<t_inpfile>* inp,
126 gmx_bool bHaltOnUnknown,
127 WriteMdpHeader writeHeader,
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. */
133 void replace_inp_entry(gmx::ArrayRef<t_inpfile> inp, const char* old_entry, const char* new_entry);
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. */
139 void mark_einp_set(gmx::ArrayRef<t_inpfile> inp, const char* name);
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);
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);
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);
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);
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 */
158 int get_eenum(std::vector<t_inpfile>* inp, const char* name, const char** defs);
159 /* defs must be NULL terminated */
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);