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, 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.
48 #include "gmx_fatal.h"
53 #include "gromacs/legacyheaders/thread_mpi/threads.h"
55 /* NOTE: this was a cesspool of thread-unsafe code, has now been
56 properly proteced by mutexes (hopefully). */
58 /* XDR should be available on all platforms now,
59 * but we keep the possibility of turning it off...
64 #define IS_SET(fn) ((fn.flag & ffSET) != 0)
65 #define IS_OPT(fn) ((fn.flag & ffOPT) != 0)
66 #define IS_MULT(fn) ((fn.flag & ffMULT) != 0)
67 #define UN_SET(fn) (fn.flag = (fn.flag & ~ffSET))
68 #define DO_SET(fn) (fn.flag = (fn.flag | ffSET))
72 eftASC, eftBIN, eftXDR, eftGEN, eftNR
75 /* To support multiple file types with one general (eg TRX) we have
78 static const int trxs[] =
83 efTRJ, efGRO, efG96, efPDB, efG87
85 #define NTRXS asize(trxs)
87 static const int tros[] =
92 efTRJ, efGRO, efG96, efPDB, efG87
94 #define NTROS asize(tros)
96 static const int trns[] =
103 #define NTRNS asize(trns)
105 static const int stos[] =
106 { efGRO, efG96, efPDB, efBRK, efENT, efESP, efXYZ };
107 #define NSTOS asize(stos)
109 static const int stxs[] =
111 efGRO, efG96, efPDB, efBRK, efENT, efESP, efXYZ,
117 #define NSTXS asize(stxs)
119 static const int tpxs[] =
126 #define NTPXS asize(tpxs)
128 static const int tpss[] =
133 efTPB, efTPA, efGRO, efG96, efPDB, efBRK, efENT
135 #define NTPSS asize(tpss)
148 /* this array should correspond to the enum in filenm.h */
149 static const t_deffile
152 { eftASC, ".mdp", "grompp", "-f", "grompp input file with MD parameters" },
153 { eftGEN, ".???", "traj", "-f",
154 "Trajectory: xtc trr trj gro g96 pdb cpt", NTRXS, trxs },
155 { eftGEN, ".???", "trajout", "-f",
156 "Trajectory: xtc trr trj gro g96 pdb", NTROS, tros },
157 { eftGEN, ".???", "traj", NULL,
158 "Full precision trajectory: trr trj cpt", NTRNS, trns },
159 { eftXDR, ".trr", "traj", NULL, "Trajectory in portable xdr format" },
160 { eftBIN, ".trj", "traj", NULL, "Trajectory file (architecture specific)" },
161 { eftXDR, ".xtc", "traj", NULL,
162 "Compressed trajectory (portable xdr format)" },
163 { eftASC, ".g87", "gtraj", NULL, "Gromos-87 ASCII trajectory format" },
164 { eftXDR, ".edr", "ener", NULL, "Energy file"},
165 { eftGEN, ".???", "conf", "-c", "Structure file: gro g96 pdb tpr etc.",
167 { eftGEN, ".???", "out", "-o", "Structure file: gro g96 pdb etc.",
169 { eftASC, ".gro", "conf", "-c", "Coordinate file in Gromos-87 format" },
170 { eftASC, ".g96", "conf", "-c", "Coordinate file in Gromos-96 format" },
171 { eftASC, ".pdb", "eiwit", "-f", "Protein data bank file"},
172 { eftASC, ".brk", "eiwit", "-f", "Brookhaven data bank file"},
173 { eftASC, ".ent", "eiwit", "-f", "Entry in the protein date bank" },
174 { eftASC, ".esp", "conf", "-f", "Coordinate file in Espresso format" },
175 { eftASC, ".pqr", "state", "-o", "Coordinate file for MEAD"},
176 { eftASC, ".xyz", "conf", "-o", "Coordinate file for some other programs" },
177 { eftXDR, ".cpt", "state", "-cp", "Checkpoint file"},
178 { eftASC, ".log", "run", "-l", "Log file"},
179 { eftASC, ".xvg", "graph", "-o", "xvgr/xmgr file"},
180 { eftASC, ".out", "hello", "-o", "Generic output file"},
181 { eftASC, ".ndx", "index", "-n", "Index file", },
182 { eftASC, ".top", "topol", "-p", "Topology file"},
183 { eftASC, ".itp", "topinc", NULL, "Include file for topology"},
184 { eftGEN, ".???", "topol", "-s", "Run input file: tpr tpb tpa",
186 { eftGEN, ".???", "topol", "-s",
187 "Structure+mass(db): tpr tpb tpa gro g96 pdb", NTPSS, tpss },
188 { eftXDR, ".tpr", "topol", "-s", "Portable xdr run input file"},
189 { eftASC, ".tpa", "topol", "-s", "Ascii run input file"},
190 { eftBIN, ".tpb", "topol", "-s", "Binary run input file"},
191 { eftASC, ".tex", "doc", "-o", "LaTeX file"},
192 { eftASC, ".rtp", "residue", NULL, "Residue Type file used by pdb2gmx" },
193 { eftASC, ".atp", "atomtp", NULL, "Atomtype file used by pdb2gmx" },
194 { eftASC, ".hdb", "polar", NULL, "Hydrogen data base"},
195 { eftASC, ".dat", "nnnice", NULL, "Generic data file"},
196 { eftASC, ".dlg", "user", NULL, "Dialog Box data for ngmx"},
197 { eftASC, ".map", "ss", NULL, "File that maps matrix data to colors" },
198 { eftASC, ".eps", "plot", NULL, "Encapsulated PostScript (tm) file" },
199 { eftASC, ".mat", "ss", NULL, "Matrix Data file"},
200 { eftASC, ".m2p", "ps", NULL, "Input file for mat2ps"},
201 { eftXDR, ".mtx", "hessian", "-m", "Hessian matrix"},
202 { eftASC, ".edi", "sam", NULL, "ED sampling input"},
203 { eftASC, ".cub", "pot", NULL, "Gaussian cube file" },
204 { eftASC, ".xpm", "root", NULL, "X PixMap compatible matrix file" },
205 { eftASC, "", "rundir", NULL, "Run directory" }
208 static char *default_file_name = NULL;
210 static tMPI_Thread_mutex_t filenm_mutex = TMPI_THREAD_MUTEX_INITIALIZER;
213 static const char *z_ext[NZEXT] =
216 void set_default_file_name(const char *name)
218 tMPI_Thread_mutex_lock(&filenm_mutex);
219 default_file_name = strdup(name);
220 tMPI_Thread_mutex_unlock(&filenm_mutex);
223 const char *ftp2ext(int ftp)
225 if ((0 <= ftp) && (ftp < efNR))
227 return deffile[ftp].ext[0] != '\0' ? deffile[ftp].ext + 1 : "";
235 const char *ftp2ext_generic(int ftp)
237 if ((0 <= ftp) && (ftp < efNR))
263 const char *ftp2ext_with_dot(int ftp)
265 if ((0 <= ftp) && (ftp < efNR))
267 return deffile[ftp].ext;
275 int ftp2generic_count(int ftp)
277 if ((0 <= ftp) && (ftp < efNR))
279 return deffile[ftp].ntps;
287 const int *ftp2generic_list(int ftp)
289 if ((0 <= ftp) && (ftp < efNR))
291 return deffile[ftp].tps;
299 const char *ftp2desc(int ftp)
301 if ((0 <= ftp) && (ftp < efNR))
303 return deffile[ftp].descr;
307 return "unknown filetype";
311 const char *ftp2ftype(int ftp)
313 if ((ftp >= 0) && (ftp < efNR))
315 switch (deffile[ftp].ftype)
322 return "XDR portable";
326 gmx_fatal(FARGS, "Unknown filetype %d in ftp2ftype", deffile[ftp].ftype);
333 const char *ftp2defnm(int ftp)
335 const char *buf = NULL;
337 tMPI_Thread_mutex_lock(&filenm_mutex);
339 if (default_file_name)
341 buf = default_file_name;
345 if ((0 <= ftp) && (ftp < efNR))
347 buf = deffile[ftp].defnm;
351 tMPI_Thread_mutex_unlock(&filenm_mutex);
356 static void check_opts(int nf, t_filenm fnm[])
361 for (i = 0; (i < nf); i++)
363 df = &(deffile[fnm[i].ftp]);
364 if (fnm[i].opt == NULL)
366 if (df->defopt == NULL)
368 gmx_fatal(FARGS, "No default cmd-line option for %s (type %d)\n",
369 deffile[fnm[i].ftp].ext, fnm[i].ftp);
373 fnm[i].opt = df->defopt;
379 int fn2ftp(const char *fn)
391 if ((len >= 4) && (fn[len - 4] == '.'))
393 feptr = &(fn[len - 4]);
400 for (i = 0; (i < efNR); i++)
402 if ((eptr = deffile[i].ext) != NULL)
404 if (gmx_strcasecmp(feptr, eptr) == 0)
414 static void set_extension(char *buf, int ftp)
419 /* check if extension is already at end of filename */
420 df = &(deffile[ftp]);
422 extlen = strlen(df->ext);
423 if ((len <= extlen) || (gmx_strcasecmp(&(buf[len - extlen]), df->ext) != 0))
425 strcat(buf, df->ext);
429 static void add_filenm(t_filenm *fnm, const char *filenm)
431 srenew(fnm->fns, fnm->nfiles+1);
432 fnm->fns[fnm->nfiles] = strdup(filenm);
436 static void set_grpfnm(t_filenm *fnm, const char *name, gmx_bool bCanNotOverride)
438 char buf[256], buf2[256];
444 nopts = deffile[fnm->ftp].ntps;
445 ftps = deffile[fnm->ftp].tps;
446 if ((nopts == 0) || (ftps == NULL))
448 gmx_fatal(FARGS, "nopts == 0 || ftps == NULL");
452 if (name && (bCanNotOverride || (default_file_name == NULL)))
455 /* First check whether we have a valid filename already */
457 if ((fnm->flag & ffREAD) && (fnm->ftp == efTRX))
459 /*if file exist don't add an extension for trajectory reading*/
460 bValidExt = gmx_fexist(name);
462 for (i = 0; (i < nopts) && !bValidExt; i++)
472 /* No name given, set the default name */
473 strcpy(buf, ftp2defnm(fnm->ftp));
476 if (!bValidExt && (fnm->flag & ffREAD))
478 /* for input-files only: search for filenames in the directory */
479 for (i = 0; (i < nopts) && !bValidExt; i++)
483 set_extension(buf2, type);
484 if (gmx_fexist(buf2))
494 /* Use the first extension type */
495 set_extension(buf, ftps[0]);
498 add_filenm(fnm, buf);
501 static void set_filenm(t_filenm *fnm, const char *name, gmx_bool bCanNotOverride,
504 /* Set the default filename, extension and option for those fields that
505 * are not already set. An extension is added if not present, if fn = NULL
506 * or empty, the default filename is given.
511 if ((fnm->flag & ffREAD) && !bReadNode)
516 if ((fnm->ftp < 0) || (fnm->ftp >= efNR))
518 gmx_fatal(FARGS, "file type out of range (%d)", fnm->ftp);
525 if ((fnm->flag & ffREAD) && name && gmx_fexist(name))
527 /* check if filename ends in .gz or .Z, if so remove that: */
529 for (i = 0; i < NZEXT; i++)
531 extlen = strlen(z_ext[i]);
534 if (gmx_strcasecmp(name+len-extlen, z_ext[i]) == 0)
536 buf[len-extlen] = '\0';
543 if (deffile[fnm->ftp].ntps)
545 set_grpfnm(fnm, name ? buf : NULL, bCanNotOverride);
549 if ((name == NULL) || !(bCanNotOverride || (default_file_name == NULL)))
551 const char *defnm = ftp2defnm(fnm->ftp);
554 set_extension(buf, fnm->ftp);
556 add_filenm(fnm, buf);
560 static void set_filenms(int nf, t_filenm fnm[], gmx_bool bReadNode)
564 for (i = 0; (i < nf); i++)
568 set_filenm(&(fnm[i]), fnm[i].fn, FALSE, bReadNode);
573 void parse_file_args(int *argc, char *argv[], int nf, t_filenm fnm[],
574 gmx_bool bKeep, gmx_bool bReadNode)
581 for (i = 0; (i < nf); i++)
588 snew(bRemove, (*argc)+1);
592 for (j = 0; (j < nf); j++)
594 if (strcmp(argv[i], fnm[j].opt) == 0)
599 /* check if we are out of arguments for this option */
600 if ((i >= *argc) || (argv[i][0] == '-'))
602 set_filenm(&fnm[j], fnm[j].fn, FALSE, bReadNode);
604 /* sweep up all file arguments for this option */
605 while ((i < *argc) && (argv[i][0] != '-'))
607 set_filenm(&fnm[j], argv[i], TRUE, bReadNode);
610 /* only repeat for 'multiple' file options: */
611 if (!IS_MULT(fnm[j]))
617 break; /* jump out of 'j' loop */
620 /* No file found corresponding to option argv[i] */
630 /* Remove used entries */
631 for (i = j = 0; (i <= *argc); i++)
643 set_filenms(nf, fnm, bReadNode);
647 const char *opt2fn(const char *opt, int nfile, const t_filenm fnm[])
651 for (i = 0; (i < nfile); i++)
653 if (strcmp(opt, fnm[i].opt) == 0)
655 return fnm[i].fns[0];
659 fprintf(stderr, "No option %s\n", opt);
664 const char *opt2fn_master(const char *opt, int nfile, const t_filenm fnm[],
667 return SIMMASTER(cr) ? opt2fn(opt, nfile, fnm) : NULL;
670 int opt2fns(char **fns[], const char *opt, int nfile, const t_filenm fnm[])
674 for (i = 0; (i < nfile); i++)
676 if (strcmp(opt, fnm[i].opt) == 0)
679 return fnm[i].nfiles;
683 fprintf(stderr, "No option %s\n", opt);
687 const char *ftp2fn(int ftp, int nfile, const t_filenm fnm[])
691 for (i = 0; (i < nfile); i++)
693 if (ftp == fnm[i].ftp)
695 return fnm[i].fns[0];
699 fprintf(stderr, "ftp2fn: No filetype %s\n", deffile[ftp].ext);
703 int ftp2fns(char **fns[], int ftp, int nfile, const t_filenm fnm[])
707 for (i = 0; (i < nfile); i++)
709 if (ftp == fnm[i].ftp)
712 return fnm[i].nfiles;
716 fprintf(stderr, "ftp2fn: No filetype %s\n", deffile[ftp].ext);
720 gmx_bool ftp2bSet(int ftp, int nfile, const t_filenm fnm[])
724 for (i = 0; (i < nfile); i++)
726 if (ftp == fnm[i].ftp)
728 return (gmx_bool) IS_SET(fnm[i]);
732 fprintf(stderr, "ftp2bSet: No filetype %s\n", deffile[ftp].ext);
737 gmx_bool opt2bSet(const char *opt, int nfile, const t_filenm fnm[])
741 for (i = 0; (i < nfile); i++)
743 if (strcmp(opt, fnm[i].opt) == 0)
745 return (gmx_bool) IS_SET(fnm[i]);
749 fprintf(stderr, "No option %s\n", opt);
754 const char *opt2fn_null(const char *opt, int nfile, const t_filenm fnm[])
758 for (i = 0; (i < nfile); i++)
760 if (strcmp(opt, fnm[i].opt) == 0)
762 if (IS_OPT(fnm[i]) && !IS_SET(fnm[i]))
768 return fnm[i].fns[0];
772 fprintf(stderr, "No option %s\n", opt);
776 const char *ftp2fn_null(int ftp, int nfile, const t_filenm fnm[])
780 for (i = 0; (i < nfile); i++)
782 if (ftp == fnm[i].ftp)
784 if (IS_OPT(fnm[i]) && !IS_SET(fnm[i]))
790 return fnm[i].fns[0];
794 fprintf(stderr, "ftp2fn: No filetype %s\n", deffile[ftp].ext);
798 gmx_bool is_optional(const t_filenm *fnm)
800 return ((fnm->flag & ffOPT) == ffOPT);
803 gmx_bool is_output(const t_filenm *fnm)
805 return ((fnm->flag & ffWRITE) == ffWRITE);
808 gmx_bool is_set(const t_filenm *fnm)
810 return ((fnm->flag & ffSET) == ffSET);
813 int add_suffix_to_output_names(t_filenm *fnm, int nfile, const char *suffix)
816 char buf[STRLEN], newname[STRLEN];
819 for (i = 0; i < nfile; i++)
821 if (is_output(&fnm[i]) && fnm[i].ftp != efCPT)
823 /* We never use multiple _outputs_, but we might as well check
824 for it, just in case... */
825 for (j = 0; j < fnm[i].nfiles; j++)
827 strncpy(buf, fnm[i].fns[j], STRLEN - 1);
828 extpos = strrchr(buf, '.');
830 sprintf(newname, "%s%s.%s", buf, suffix, extpos + 1);
832 fnm[i].fns[j] = strdup(newname);
839 t_filenm *dup_tfn(int nf, const t_filenm tfn[])
845 for (i = 0; i < nf; i++)
847 ret[i] = tfn[i]; /* just directly copy all non-string fields */
850 ret[i].opt = strdup(tfn[i].opt);
859 ret[i].fn = strdup(tfn[i].fn);
866 if (tfn[i].nfiles > 0)
868 snew(ret[i].fns, tfn[i].nfiles);
869 for (j = 0; j < tfn[i].nfiles; j++)
871 ret[i].fns[j] = strdup(tfn[i].fns[j]);
878 void done_filenms(int nf, t_filenm fnm[])
882 for (i = 0; i < nf; ++i)
884 for (j = 0; j < fnm[i].nfiles; ++j)
886 sfree(fnm[i].fns[j]);