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"
52 #include "types/simple.h"
53 #include "types/commrec.h"
55 /* XDR should be available on all platforms now,
56 * but we keep the possibility of turning it off...
61 #define IS_SET(fn) ((fn.flag & ffSET) != 0)
62 #define IS_OPT(fn) ((fn.flag & ffOPT) != 0)
63 #define IS_MULT(fn) ((fn.flag & ffMULT) != 0)
64 #define UN_SET(fn) (fn.flag = (fn.flag & ~ffSET))
65 #define DO_SET(fn) (fn.flag = (fn.flag | ffSET))
69 eftASC, eftBIN, eftXDR, eftGEN, eftNR
72 /* To support multiple file types with one general (eg TRX) we have
75 static const int trxs[] =
80 efTRJ, efGRO, efG96, efPDB, efG87
82 #define NTRXS asize(trxs)
84 static const int tros[] =
89 efTRJ, efGRO, efG96, efPDB, efG87
91 #define NTROS asize(tros)
93 static const int trns[] =
100 #define NTRNS asize(trns)
102 static const int stos[] =
103 { efGRO, efG96, efPDB, efBRK, efENT, efESP, efXYZ };
104 #define NSTOS asize(stos)
106 static const int stxs[] =
108 efGRO, efG96, efPDB, efBRK, efENT, efESP, efXYZ,
114 #define NSTXS asize(stxs)
116 static const int tpxs[] =
123 #define NTPXS asize(tpxs)
125 static const int tpss[] =
130 efTPB, efTPA, efGRO, efG96, efPDB, efBRK, efENT
132 #define NTPSS asize(tpss)
145 /* this array should correspond to the enum in filenm.h */
146 static const t_deffile
149 { eftASC, ".mdp", "grompp", "-f", "grompp input file with MD parameters" },
150 { eftGEN, ".???", "traj", "-f",
151 "Trajectory: xtc trr trj gro g96 pdb cpt", NTRXS, trxs },
152 { eftGEN, ".???", "trajout", "-f",
153 "Trajectory: xtc trr trj gro g96 pdb", NTROS, tros },
154 { eftGEN, ".???", "traj", NULL,
155 "Full precision trajectory: trr trj cpt", NTRNS, trns },
156 { eftXDR, ".trr", "traj", NULL, "Trajectory in portable xdr format" },
157 { eftBIN, ".trj", "traj", NULL, "Trajectory file (architecture specific)" },
158 { eftXDR, ".xtc", "traj", NULL,
159 "Compressed trajectory (portable xdr format)" },
160 { eftASC, ".g87", "gtraj", NULL, "Gromos-87 ASCII trajectory format" },
161 { eftXDR, ".edr", "ener", NULL, "Energy file"},
162 { eftGEN, ".???", "conf", "-c", "Structure file: gro g96 pdb tpr etc.",
164 { eftGEN, ".???", "out", "-o", "Structure file: gro g96 pdb etc.",
166 { eftASC, ".gro", "conf", "-c", "Coordinate file in Gromos-87 format" },
167 { eftASC, ".g96", "conf", "-c", "Coordinate file in Gromos-96 format" },
168 { eftASC, ".pdb", "eiwit", "-f", "Protein data bank file"},
169 { eftASC, ".brk", "eiwit", "-f", "Brookhaven data bank file"},
170 { eftASC, ".ent", "eiwit", "-f", "Entry in the protein date bank" },
171 { eftASC, ".esp", "conf", "-f", "Coordinate file in Espresso format" },
172 { eftASC, ".pqr", "state", "-o", "Coordinate file for MEAD"},
173 { eftASC, ".xyz", "conf", "-o", "Coordinate file for some other programs" },
174 { eftXDR, ".cpt", "state", "-cp", "Checkpoint file"},
175 { eftASC, ".log", "run", "-l", "Log file"},
176 { eftASC, ".xvg", "graph", "-o", "xvgr/xmgr file"},
177 { eftASC, ".out", "hello", "-o", "Generic output file"},
178 { eftASC, ".ndx", "index", "-n", "Index file", },
179 { eftASC, ".top", "topol", "-p", "Topology file"},
180 { eftASC, ".itp", "topinc", NULL, "Include file for topology"},
181 { eftGEN, ".???", "topol", "-s", "Run input file: tpr tpb tpa",
183 { eftGEN, ".???", "topol", "-s",
184 "Structure+mass(db): tpr tpb tpa gro g96 pdb", NTPSS, tpss },
185 { eftXDR, ".tpr", "topol", "-s", "Portable xdr run input file"},
186 { eftASC, ".tpa", "topol", "-s", "Ascii run input file"},
187 { eftBIN, ".tpb", "topol", "-s", "Binary run input file"},
188 { eftASC, ".tex", "doc", "-o", "LaTeX file"},
189 { eftASC, ".rtp", "residue", NULL, "Residue Type file used by pdb2gmx" },
190 { eftASC, ".atp", "atomtp", NULL, "Atomtype file used by pdb2gmx" },
191 { eftASC, ".hdb", "polar", NULL, "Hydrogen data base"},
192 { eftASC, ".dat", "nnnice", NULL, "Generic data file"},
193 { eftASC, ".dlg", "user", NULL, "Dialog Box data for ngmx"},
194 { eftASC, ".map", "ss", NULL, "File that maps matrix data to colors" },
195 { eftASC, ".eps", "plot", NULL, "Encapsulated PostScript (tm) file" },
196 { eftASC, ".mat", "ss", NULL, "Matrix Data file"},
197 { eftASC, ".m2p", "ps", NULL, "Input file for mat2ps"},
198 { eftXDR, ".mtx", "hessian", "-m", "Hessian matrix"},
199 { eftASC, ".edi", "sam", NULL, "ED sampling input"},
200 { eftASC, ".cub", "pot", NULL, "Gaussian cube file" },
201 { eftASC, ".xpm", "root", NULL, "X PixMap compatible matrix file" },
202 { eftASC, "", "rundir", NULL, "Run directory" }
206 static const char *z_ext[NZEXT] =
209 const char *ftp2ext(int ftp)
211 if ((0 <= ftp) && (ftp < efNR))
213 return deffile[ftp].ext[0] != '\0' ? deffile[ftp].ext + 1 : "";
221 const char *ftp2ext_generic(int ftp)
223 if ((0 <= ftp) && (ftp < efNR))
249 const char *ftp2ext_with_dot(int ftp)
251 if ((0 <= ftp) && (ftp < efNR))
253 return deffile[ftp].ext;
261 int ftp2generic_count(int ftp)
263 if ((0 <= ftp) && (ftp < efNR))
265 return deffile[ftp].ntps;
273 const int *ftp2generic_list(int ftp)
275 if ((0 <= ftp) && (ftp < efNR))
277 return deffile[ftp].tps;
285 const char *ftp2desc(int ftp)
287 if ((0 <= ftp) && (ftp < efNR))
289 return deffile[ftp].descr;
293 return "unknown filetype";
297 const char *ftp2ftype(int ftp)
299 if ((ftp >= 0) && (ftp < efNR))
301 switch (deffile[ftp].ftype)
308 return "XDR portable";
312 gmx_fatal(FARGS, "Unknown filetype %d in ftp2ftype", deffile[ftp].ftype);
319 const char *ftp2defnm(int ftp)
321 if ((0 <= ftp) && (ftp < efNR))
323 return deffile[ftp].defnm;
331 static void check_opts(int nf, t_filenm fnm[])
336 for (i = 0; (i < nf); i++)
338 df = &(deffile[fnm[i].ftp]);
339 if (fnm[i].opt == NULL)
341 if (df->defopt == NULL)
343 gmx_fatal(FARGS, "No default cmd-line option for %s (type %d)\n",
344 deffile[fnm[i].ftp].ext, fnm[i].ftp);
348 fnm[i].opt = df->defopt;
354 int fn2ftp(const char *fn)
366 if ((len >= 4) && (fn[len - 4] == '.'))
368 feptr = &(fn[len - 4]);
375 for (i = 0; (i < efNR); i++)
377 if ((eptr = deffile[i].ext) != NULL)
379 if (gmx_strcasecmp(feptr, eptr) == 0)
389 static void set_extension(char *buf, int ftp)
394 /* check if extension is already at end of filename */
395 df = &(deffile[ftp]);
397 extlen = strlen(df->ext);
398 if ((len <= extlen) || (gmx_strcasecmp(&(buf[len - extlen]), df->ext) != 0))
400 strcat(buf, df->ext);
404 static void add_filenm(t_filenm *fnm, const char *filenm)
406 srenew(fnm->fns, fnm->nfiles+1);
407 fnm->fns[fnm->nfiles] = strdup(filenm);
411 static void set_grpfnm(t_filenm *fnm, const char *name, const char *deffnm)
413 char buf[256], buf2[256];
419 nopts = deffile[fnm->ftp].ntps;
420 ftps = deffile[fnm->ftp].tps;
421 if ((nopts == 0) || (ftps == NULL))
423 gmx_fatal(FARGS, "nopts == 0 || ftps == NULL");
427 if (name && deffnm == NULL)
430 /* First check whether we have a valid filename already */
432 if ((fnm->flag & ffREAD) && (fnm->ftp == efTRX))
434 /*if file exist don't add an extension for trajectory reading*/
435 bValidExt = gmx_fexist(name);
437 for (i = 0; (i < nopts) && !bValidExt; i++)
445 else if (deffnm != NULL)
451 /* No name given, set the default name */
452 strcpy(buf, ftp2defnm(fnm->ftp));
455 if (!bValidExt && (fnm->flag & ffREAD))
457 /* for input-files only: search for filenames in the directory */
458 for (i = 0; (i < nopts) && !bValidExt; i++)
462 set_extension(buf2, type);
463 if (gmx_fexist(buf2))
473 /* Use the first extension type */
474 set_extension(buf, ftps[0]);
477 add_filenm(fnm, buf);
480 static void set_filenm(t_filenm *fnm, const char *name, const char *deffnm,
483 /* Set the default filename, extension and option for those fields that
484 * are not already set. An extension is added if not present, if fn = NULL
485 * or empty, the default filename is given.
490 if ((fnm->flag & ffREAD) && !bReadNode)
495 if ((fnm->ftp < 0) || (fnm->ftp >= efNR))
497 gmx_fatal(FARGS, "file type out of range (%d)", fnm->ftp);
504 if ((fnm->flag & ffREAD) && name && gmx_fexist(name))
506 /* check if filename ends in .gz or .Z, if so remove that: */
508 for (i = 0; i < NZEXT; i++)
510 extlen = strlen(z_ext[i]);
513 if (gmx_strcasecmp(name+len-extlen, z_ext[i]) == 0)
515 buf[len-extlen] = '\0';
522 if (deffile[fnm->ftp].ntps)
524 set_grpfnm(fnm, name ? buf : NULL, deffnm);
528 if (name == NULL || deffnm != NULL)
536 strcpy(buf, ftp2defnm(fnm->ftp));
539 set_extension(buf, fnm->ftp);
541 add_filenm(fnm, buf);
545 static void set_filenms(int nf, t_filenm fnm[], const char *deffnm, gmx_bool bReadNode)
549 for (i = 0; (i < nf); i++)
553 set_filenm(&(fnm[i]), fnm[i].fn, deffnm, bReadNode);
558 void parse_file_args(int *argc, char *argv[], int nf, t_filenm fnm[],
559 const char *deffnm, gmx_bool bKeep, gmx_bool bReadNode)
566 for (i = 0; (i < nf); i++)
573 snew(bRemove, (*argc)+1);
577 for (j = 0; (j < nf); j++)
579 if (strcmp(argv[i], fnm[j].opt) == 0)
584 /* check if we are out of arguments for this option */
585 if ((i >= *argc) || (argv[i][0] == '-'))
587 set_filenm(&fnm[j], fnm[j].fn, deffnm, bReadNode);
589 /* sweep up all file arguments for this option */
590 while ((i < *argc) && (argv[i][0] != '-'))
592 set_filenm(&fnm[j], argv[i], NULL, bReadNode);
595 /* only repeat for 'multiple' file options: */
596 if (!IS_MULT(fnm[j]))
602 break; /* jump out of 'j' loop */
605 /* No file found corresponding to option argv[i] */
615 /* Remove used entries */
616 for (i = j = 0; (i <= *argc); i++)
628 set_filenms(nf, fnm, deffnm, bReadNode);
632 const char *opt2fn(const char *opt, int nfile, const t_filenm fnm[])
636 for (i = 0; (i < nfile); i++)
638 if (strcmp(opt, fnm[i].opt) == 0)
640 return fnm[i].fns[0];
644 fprintf(stderr, "No option %s\n", opt);
649 const char *opt2fn_master(const char *opt, int nfile, const t_filenm fnm[],
652 return SIMMASTER(cr) ? opt2fn(opt, nfile, fnm) : NULL;
655 int opt2fns(char **fns[], const char *opt, int nfile, const t_filenm fnm[])
659 for (i = 0; (i < nfile); i++)
661 if (strcmp(opt, fnm[i].opt) == 0)
664 return fnm[i].nfiles;
668 fprintf(stderr, "No option %s\n", opt);
672 const char *ftp2fn(int ftp, int nfile, const t_filenm fnm[])
676 for (i = 0; (i < nfile); i++)
678 if (ftp == fnm[i].ftp)
680 return fnm[i].fns[0];
684 fprintf(stderr, "ftp2fn: No filetype %s\n", deffile[ftp].ext);
688 int ftp2fns(char **fns[], int ftp, int nfile, const t_filenm fnm[])
692 for (i = 0; (i < nfile); i++)
694 if (ftp == fnm[i].ftp)
697 return fnm[i].nfiles;
701 fprintf(stderr, "ftp2fn: No filetype %s\n", deffile[ftp].ext);
705 gmx_bool ftp2bSet(int ftp, int nfile, const t_filenm fnm[])
709 for (i = 0; (i < nfile); i++)
711 if (ftp == fnm[i].ftp)
713 return (gmx_bool) IS_SET(fnm[i]);
717 fprintf(stderr, "ftp2bSet: No filetype %s\n", deffile[ftp].ext);
722 gmx_bool opt2bSet(const char *opt, int nfile, const t_filenm fnm[])
726 for (i = 0; (i < nfile); i++)
728 if (strcmp(opt, fnm[i].opt) == 0)
730 return (gmx_bool) IS_SET(fnm[i]);
734 fprintf(stderr, "No option %s\n", opt);
739 const char *opt2fn_null(const char *opt, int nfile, const t_filenm fnm[])
743 for (i = 0; (i < nfile); i++)
745 if (strcmp(opt, fnm[i].opt) == 0)
747 if (IS_OPT(fnm[i]) && !IS_SET(fnm[i]))
753 return fnm[i].fns[0];
757 fprintf(stderr, "No option %s\n", opt);
761 const char *ftp2fn_null(int ftp, int nfile, const t_filenm fnm[])
765 for (i = 0; (i < nfile); i++)
767 if (ftp == fnm[i].ftp)
769 if (IS_OPT(fnm[i]) && !IS_SET(fnm[i]))
775 return fnm[i].fns[0];
779 fprintf(stderr, "ftp2fn: No filetype %s\n", deffile[ftp].ext);
783 gmx_bool is_optional(const t_filenm *fnm)
785 return ((fnm->flag & ffOPT) == ffOPT);
788 gmx_bool is_output(const t_filenm *fnm)
790 return ((fnm->flag & ffWRITE) == ffWRITE);
793 gmx_bool is_set(const t_filenm *fnm)
795 return ((fnm->flag & ffSET) == ffSET);
798 int add_suffix_to_output_names(t_filenm *fnm, int nfile, const char *suffix)
801 char buf[STRLEN], newname[STRLEN];
804 for (i = 0; i < nfile; i++)
806 if (is_output(&fnm[i]) && fnm[i].ftp != efCPT)
808 /* We never use multiple _outputs_, but we might as well check
809 for it, just in case... */
810 for (j = 0; j < fnm[i].nfiles; j++)
812 strncpy(buf, fnm[i].fns[j], STRLEN - 1);
813 extpos = strrchr(buf, '.');
815 sprintf(newname, "%s%s.%s", buf, suffix, extpos + 1);
817 fnm[i].fns[j] = strdup(newname);
824 t_filenm *dup_tfn(int nf, const t_filenm tfn[])
830 for (i = 0; i < nf; i++)
832 ret[i] = tfn[i]; /* just directly copy all non-string fields */
835 ret[i].opt = strdup(tfn[i].opt);
844 ret[i].fn = strdup(tfn[i].fn);
851 if (tfn[i].nfiles > 0)
853 snew(ret[i].fns, tfn[i].nfiles);
854 for (j = 0; j < tfn[i].nfiles; j++)
856 ret[i].fns[j] = strdup(tfn[i].fns[j]);
863 void done_filenms(int nf, t_filenm fnm[])
867 for (i = 0; i < nf; ++i)
869 for (j = 0; j < fnm[i].nfiles; ++j)
871 sfree(fnm[i].fns[j]);