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, 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.
42 #include "gromacs/utility/cstringutil.h"
43 #include "gromacs/utility/futil.h"
44 #include "gromacs/utility/smalloc.h"
47 #include "gromacs/fileio/gmxfio.h"
50 #include "gromacs/utility/fatalerror.h"
52 t_inpfile *read_inpfile(const char *fn, int *ninp,
56 char buf[STRLEN], lbuf[STRLEN], rbuf[STRLEN], warn_buf[STRLEN];
58 t_inpfile *inp = NULL;
60 /* setting cppopts from command-line options would be cooler */
61 gmx_bool allow_override = FALSE;
66 fprintf(debug, "Reading MDP file %s\n", fn);
69 in = gmx_ffopen(fn, "r");
74 ptr = fgets2(buf, STRLEN-1, in);
76 set_warning_line(wi, fn, lc);
80 if ((cptr = strchr(buf, COMMENTSIGN)) != NULL)
87 for (j = 0; (buf[j] != '=') && (buf[j] != '\0'); j++)
97 fprintf(debug, "No = on line %d in file %s, ignored\n", lc, fn);
103 for (i = 0; (i < j); i++)
113 fprintf(debug, "Empty left hand side on line %d in file %s, ignored\n", lc, fn);
118 for (i = j+1, k = 0; (buf[i] != '\0'); i++, k++)
128 fprintf(debug, "Empty right hand side on line %d in file %s, ignored\n", lc, fn);
133 /* Now finally something sensible */
136 /* first check whether we hit the 'multiple_entries' option */
137 if (gmx_strcasecmp_min(eMultentOpt_names[eMultentOptName], lbuf) == 0)
139 /* we now check whether to allow overrides from here or not */
140 if (gmx_strcasecmp_min(eMultentOpt_names[eMultentOptNo], rbuf) == 0)
142 allow_override = FALSE;
144 else if (gmx_strcasecmp_min(eMultentOpt_names[eMultentOptLast], rbuf) == 0)
146 allow_override = TRUE;
151 "Parameter \"%s\" should either be %s or %s\n",
153 eMultentOpt_names[eMultentOptNo],
154 eMultentOpt_names[eMultentOptLast]);
155 warning_error(wi, warn_buf);
160 /* it is a regular option; check for duplicates */
161 found_index = search_einp(nin, inp, lbuf);
163 if (found_index == -1)
167 inp[nin-1].inp_count = 1;
168 inp[nin-1].count = 0;
169 inp[nin-1].bObsolete = FALSE;
170 inp[nin-1].bSet = FALSE;
171 inp[nin-1].name = gmx_strdup(lbuf);
172 inp[nin-1].value = gmx_strdup(rbuf);
179 "Parameter \"%s\" doubly defined (and multiple assignments not allowed)\n",
181 warning_error(wi, warn_buf);
186 sfree(inp[found_index].value);
187 inp[found_index].value = gmx_strdup(rbuf);
189 "Overriding existing parameter \"%s\" with value \"%s\"\n",
191 warning_note(wi, warn_buf);
206 fprintf(debug, "Done reading MDP file, there were %d entries in there\n",
218 static int inp_comp(const void *a, const void *b)
220 return ((t_inpfile *)a)->count - ((t_inpfile *)b)->count;
223 static void sort_inp(int ninp, t_inpfile inp[])
228 for (i = 0; (i < ninp); i++)
230 mm = max(mm, inp[i].count);
232 for (i = 0; (i < ninp); i++)
234 if (inp[i].count == 0)
239 qsort(inp, ninp, (size_t)sizeof(inp[0]), inp_comp);
242 void write_inpfile(const char *fn, int ninp, t_inpfile inp[], gmx_bool bHaltOnUnknown,
247 char warn_buf[STRLEN];
250 out = gmx_fio_fopen(fn, "w");
251 nice_header(out, fn);
252 for (i = 0; (i < ninp); i++)
256 if (inp[i].name[0] == ';' || (strlen(inp[i].name) > 2 && inp[i].name[1] == ';'))
258 fprintf(out, "%-24s\n", inp[i].name);
262 fprintf(out, "%-24s = %s\n", inp[i].name, inp[i].value ? inp[i].value : "");
265 else if (!inp[i].bObsolete)
267 sprintf(warn_buf, "Unknown left-hand '%s' in parameter file\n",
271 warning_error(wi, warn_buf);
275 warning(wi, warn_buf);
281 check_warning_error(wi, FARGS);
284 void replace_inp_entry(int ninp, t_inpfile *inp, const char *old_entry, const char *new_entry)
288 for (i = 0; (i < ninp); i++)
290 if (gmx_strcasecmp_min(old_entry, inp[i].name) == 0)
294 fprintf(stderr, "Replacing old mdp entry '%s' by '%s'\n",
295 inp[i].name, new_entry);
297 inp[i].name = gmx_strdup(new_entry);
301 fprintf(stderr, "Ignoring obsolete mdp entry '%s'\n",
303 inp[i].bObsolete = TRUE;
309 int search_einp(int ninp, const t_inpfile *inp, const char *name)
317 for (i = 0; i < ninp; i++)
319 if (gmx_strcasecmp_min(name, inp[i].name) == 0)
327 static int get_einp(int *ninp, t_inpfile **inp, const char *name)
330 int notfound = FALSE;
331 char warn_buf[STRLEN];
335 for(i=0; (i<(*ninp)); i++)
336 if (gmx_strcasecmp_min(name,(*inp)[i].name) == 0)
338 if (i == (*ninp)) {*/
339 i = search_einp(*ninp, *inp, name);
344 srenew(*inp, (*ninp));
345 (*inp)[i].name = gmx_strdup(name);
346 (*inp)[i].bSet = TRUE;
348 (*inp)[i].count = (*inp)[0].inp_count++;
349 (*inp)[i].bSet = TRUE;
352 fprintf(debug, "Inp %d = %s\n", (*inp)[i].count, (*inp)[i].name);
355 /*if (i == (*ninp)-1)*/
366 int get_eint(int *ninp, t_inpfile **inp, const char *name, int def,
369 char buf[32], *ptr, warn_buf[STRLEN];
373 ii = get_einp(ninp, inp, name);
377 sprintf(buf, "%d", def);
378 (*inp)[(*ninp)-1].value = gmx_strdup(buf);
384 ret = strtol((*inp)[ii].value, &ptr, 10);
385 if (ptr == (*inp)[ii].value)
387 sprintf(warn_buf, "Right hand side '%s' for parameter '%s' in parameter file is not an integer value\n", (*inp)[ii].value, (*inp)[ii].name);
388 warning_error(wi, warn_buf);
395 gmx_int64_t get_eint64(int *ninp, t_inpfile **inp,
396 const char *name, gmx_int64_t def,
399 char buf[32], *ptr, warn_buf[STRLEN];
403 ii = get_einp(ninp, inp, name);
407 sprintf(buf, "%"GMX_PRId64, def);
408 (*inp)[(*ninp)-1].value = gmx_strdup(buf);
414 ret = str_to_int64_t((*inp)[ii].value, &ptr);
415 if (ptr == (*inp)[ii].value)
417 sprintf(warn_buf, "Right hand side '%s' for parameter '%s' in parameter file is not an integer value\n", (*inp)[ii].value, (*inp)[ii].name);
418 warning_error(wi, warn_buf);
425 double get_ereal(int *ninp, t_inpfile **inp, const char *name, double def,
428 char buf[32], *ptr, warn_buf[STRLEN];
432 ii = get_einp(ninp, inp, name);
436 sprintf(buf, "%g", def);
437 (*inp)[(*ninp)-1].value = gmx_strdup(buf);
443 ret = strtod((*inp)[ii].value, &ptr);
444 if (ptr == (*inp)[ii].value)
446 sprintf(warn_buf, "Right hand side '%s' for parameter '%s' in parameter file is not a real value\n", (*inp)[ii].value, (*inp)[ii].name);
447 warning_error(wi, warn_buf);
454 const char *get_estr(int *ninp, t_inpfile **inp, const char *name, const char *def)
459 ii = get_einp(ninp, inp, name);
465 sprintf(buf, "%s", def);
466 (*inp)[(*ninp)-1].value = gmx_strdup(buf);
470 (*inp)[(*ninp)-1].value = NULL;
477 return (*inp)[ii].value;
481 int get_eeenum(int *ninp, t_inpfile **inp, const char *name, const char **defs,
488 ii = get_einp(ninp, inp, name);
492 (*inp)[(*ninp)-1].value = gmx_strdup(defs[0]);
497 for (i = 0; (defs[i] != NULL); i++)
499 if (gmx_strcasecmp_min(defs[i], (*inp)[ii].value) == 0)
507 n += sprintf(buf, "Invalid enum '%s' for variable %s, using '%s'\n",
508 (*inp)[ii].value, name, defs[0]);
509 n += sprintf(buf+n, "Next time use one of:");
513 n += sprintf(buf+n, " '%s'", defs[j]);
518 warning_error(wi, buf);
522 fprintf(stderr, "%s\n", buf);
525 (*inp)[ii].value = gmx_strdup(defs[0]);
533 int get_eenum(int *ninp, t_inpfile **inp, const char *name, const char **defs)
537 return get_eeenum(ninp, inp, name, defs, NULL);