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, 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
45 #include "gromacs/utility/arrayref.h"
46 #include "gromacs/utility/basedefinitions.h"
49 typedef warninp *warninp_t;
53 class KeyValueTreeObject;
54 class TextInputStream;
55 class TextOutputStream;
58 /* !\brief Input file structure that is populated with entries read from a file.
60 * This structure contains the information read from the mdp file that is later used
61 * to build the ir from it. It is first constructed with a set of names and values,
62 * and later populated when checking against the available options in readir.
63 * Uses the functions below to both check entries and set the information.
67 /*!\brief Minimum allowed constructor sets all elements */
68 t_inpfile (int count, int inp_count, bool bObsolete, bool bSet, bool bHandledAsKeyValueTree,
69 std::string name, std::string value) :
71 bObsolete_(bObsolete),
73 bHandledAsKeyValueTree_(bHandledAsKeyValueTree),
79 int count_; /* sort order for output */
80 bool bObsolete_; /* whether it is an obsolete param value */
81 bool bSet_; /* whether it it has been read out */
82 bool bHandledAsKeyValueTree_; /* whether it it has been handled with key-value machinery */
83 std::string name_; /* name of the parameter */
84 std::string value_; /* parameter value string */
85 int inp_count_; /* number of einps read. Only valid for the first item
86 in the inpfile list. */
89 /*! \brief Create and return a vector of t_inpfile structs
90 * from "key = value" lines in \c stream corresponding to file \c fn.
92 * \param[in] stream Text stream to read.
93 * \param[in] fn Filename corresponding to \c reader.
94 * \param[out] wi Handler for context-sensitive warnings.
95 * \throws std::bad_alloc If out of memory.
96 * \throws Anything the stream underlying \c reader can throw. */
97 std::vector<t_inpfile>
98 read_inpfile(gmx::TextInputStream *stream, const char *fn,
101 gmx::KeyValueTreeObject flatKeyValueTreeFromInpFile(gmx::ArrayRef<const t_inpfile> inp);
103 enum class WriteMdpHeader
108 /*! \brief Write "key = value" lines from \c inp to \c stream.
110 * \param[in] stream Text stream to write.
111 * \param[in] fn Filename corresponding to \c stream.
112 * \param[in] inp vector of key-value pairs.
113 * \param[in] bHaltOnUnknown Whether to issue a fatal error if an unknown key is found.
114 * \param[in] writeHeader Whether to write a header recording some context a user might like.
115 * \param[out] wi Handler for context-sensitive warnings.
116 * \throws std::bad_alloc If out of memory.
117 * \throws Anything the stream underlying \c writer can throw. */
118 void write_inpfile(gmx::TextOutputStream *stream, const char *fn, std::vector<t_inpfile> *inp,
119 gmx_bool bHaltOnUnknown,
120 WriteMdpHeader writeHeader,
122 /* Write inp to fn, warning (and perhaps halting) if any fields are
123 * unknown. The helpful header contains irreproducible content, so
124 * its writing can be suppressed to make testing more useful. */
126 void replace_inp_entry(gmx::ArrayRef<t_inpfile> inp,
127 const char *old_entry, const char *new_entry);
129 int search_einp(gmx::ArrayRef<const t_inpfile> inp, const char *name);
130 /* Return the index of an .mdp field with the given name within the
131 * inp vector, if it exists. Return -1 if it does not exist. */
133 void mark_einp_set(gmx::ArrayRef<t_inpfile> inp, const char *name);
135 int get_eint(std::vector<t_inpfile> *inp, const char *name, int def,
138 int64_t get_eint64(std::vector<t_inpfile> *inp,
139 const char *name, int64_t def,
142 double get_ereal(std::vector<t_inpfile> *inp, const char *name, double def,
145 const char *get_estr(std::vector<t_inpfile> *inp, const char *name, const char *def);
147 int get_eeenum(std::vector<t_inpfile> *inp, const char *name, const char **defs,
149 /* defs must be NULL terminated */
151 int get_eenum(std::vector<t_inpfile> *inp, const char *name, const char **defs);
152 /* defs must be NULL terminated */
154 //! Replace for macro CCTYPE, prints comment string after newline
155 void printStringNewline(std::vector<t_inpfile> *inp, const char *line);
156 //! Replace for macro CTYPE, prints comment string
157 void printStringNoNewline(std::vector<t_inpfile> *inp, const char *line);
158 //! Replace for macro STYPE, checks for existing string entry and if possible replaces it
159 void setStringEntry(std::vector<t_inpfile> *inp, const char *name, char *newName, const char *def);