Moved copyrite.* to fileio from gmxlib and legacyheaders.
[gromacs.git] / src / gromacs / fileio / filenm.cpp
blobde764bca01734d3d4caf4abeeb83c83e3bc286f2
1 /*
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, 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 #include "gmxpre.h"
39 #include "filenm.h"
41 #include <cstdio>
42 #include <cstring>
44 #include "gromacs/legacyheaders/types/commrec.h"
45 #include "gromacs/utility/arraysize.h"
46 #include "gromacs/utility/basedefinitions.h"
47 #include "gromacs/utility/cstringutil.h"
48 #include "gromacs/utility/fatalerror.h"
49 #include "gromacs/utility/smalloc.h"
51 /* Use bitflag ... */
52 #define IS_SET(fn) ((fn.flag & ffSET) != 0)
53 #define IS_OPT(fn) ((fn.flag & ffOPT) != 0)
55 enum
57 eftASC, eftXDR, eftTNG, eftGEN, eftNR
60 /* To support multiple file types with one general (eg TRX) we have
61 * these arrays.
63 static const int trxs[] =
65 efXTC, efTRR, efCPT,
66 efGRO, efG96, efPDB, efTNG
68 #define NTRXS asize(trxs)
70 static const int trcompressed[] =
72 efXTC,
73 efTNG
75 #define NTRCOMPRESSED asize(trcompressed)
77 static const int tros[] =
79 efXTC, efTRR,
80 efGRO, efG96, efPDB, efTNG
82 #define NTROS asize(tros)
84 static const int trns[] =
86 efTRR, efCPT,
87 efTNG
89 #define NTRNS asize(trns)
91 static const int stos[] =
92 { efGRO, efG96, efPDB, efBRK, efENT, efESP };
93 #define NSTOS asize(stos)
95 static const int stxs[] =
97 efGRO, efG96, efPDB, efBRK, efENT, efESP,
98 efTPR
100 #define NSTXS asize(stxs)
102 static const int tpss[] =
104 efTPR,
105 efGRO, efG96, efPDB, efBRK, efENT
107 #define NTPSS asize(tpss)
109 typedef struct
111 int ftype;
112 const char *ext;
113 const char *defnm;
114 const char *defopt;
115 const char *descr;
116 int ntps;
117 const int *tps;
118 } t_deffile;
120 /* this array should correspond to the enum in filenm.h */
121 static const t_deffile
122 deffile[efNR] =
124 { eftASC, ".mdp", "grompp", "-f", "grompp input file with MD parameters" },
125 { eftGEN, ".???", "traj", "-f", "Trajectory", NTRXS, trxs },
126 { eftGEN, ".???", "trajout", "-f", "Trajectory", NTROS, tros },
127 { eftGEN, ".???", "traj", NULL,
128 "Full precision trajectory", NTRNS, trns },
129 { eftXDR, ".trr", "traj", NULL, "Trajectory in portable xdr format" },
130 { eftGEN, ".???", "traj_comp", NULL,
131 "Compressed trajectory (tng format or portable xdr format)", NTRCOMPRESSED, trcompressed},
132 { eftXDR, ".xtc", "traj", NULL,
133 "Compressed trajectory (portable xdr format): xtc" },
134 { eftTNG, ".tng", "traj", NULL,
135 "Trajectory file (tng format)" },
136 { eftXDR, ".edr", "ener", NULL, "Energy file"},
137 { eftGEN, ".???", "conf", "-c", "Structure file", NSTXS, stxs },
138 { eftGEN, ".???", "out", "-o", "Structure file", NSTOS, stos },
139 { eftASC, ".gro", "conf", "-c", "Coordinate file in Gromos-87 format" },
140 { eftASC, ".g96", "conf", "-c", "Coordinate file in Gromos-96 format" },
141 { eftASC, ".pdb", "eiwit", "-f", "Protein data bank file"},
142 { eftASC, ".brk", "eiwit", "-f", "Brookhaven data bank file"},
143 { eftASC, ".ent", "eiwit", "-f", "Entry in the protein date bank" },
144 { eftASC, ".esp", "conf", "-f", "Coordinate file in Espresso format" },
145 { eftASC, ".pqr", "state", "-o", "Coordinate file for MEAD"},
146 { eftXDR, ".cpt", "state", "-cp", "Checkpoint file"},
147 { eftASC, ".log", "run", "-l", "Log file"},
148 { eftASC, ".xvg", "graph", "-o", "xvgr/xmgr file"},
149 { eftASC, ".out", "hello", "-o", "Generic output file"},
150 { eftASC, ".ndx", "index", "-n", "Index file", },
151 { eftASC, ".top", "topol", "-p", "Topology file"},
152 { eftASC, ".itp", "topinc", NULL, "Include file for topology"},
153 { eftGEN, ".???", "topol", "-s", "Structure+mass(db)", NTPSS, tpss },
154 { eftXDR, ".tpr", "topol", "-s", "Portable xdr run input file"},
155 { eftASC, ".tex", "doc", "-o", "LaTeX file"},
156 { eftASC, ".rtp", "residue", NULL, "Residue Type file used by pdb2gmx" },
157 { eftASC, ".atp", "atomtp", NULL, "Atomtype file used by pdb2gmx" },
158 { eftASC, ".hdb", "polar", NULL, "Hydrogen data base"},
159 { eftASC, ".dat", "nnnice", NULL, "Generic data file"},
160 { eftASC, ".dlg", "user", NULL, "Dialog Box data for ngmx"},
161 { eftASC, ".map", "ss", NULL, "File that maps matrix data to colors" },
162 { eftASC, ".eps", "plot", NULL, "Encapsulated PostScript (tm) file" },
163 { eftASC, ".mat", "ss", NULL, "Matrix Data file"},
164 { eftASC, ".m2p", "ps", NULL, "Input file for mat2ps"},
165 { eftXDR, ".mtx", "hessian", "-m", "Hessian matrix"},
166 { eftASC, ".edi", "sam", NULL, "ED sampling input"},
167 { eftASC, ".cub", "pot", NULL, "Gaussian cube file" },
168 { eftASC, ".xpm", "root", NULL, "X PixMap compatible matrix file" },
169 { eftASC, "", "rundir", NULL, "Run directory" }
172 const char *ftp2ext(int ftp)
174 if ((0 <= ftp) && (ftp < efNR))
176 return deffile[ftp].ext[0] != '\0' ? deffile[ftp].ext + 1 : "";
178 else
180 return "unknown";
184 const char *ftp2ext_generic(int ftp)
186 if ((0 <= ftp) && (ftp < efNR))
188 switch (ftp)
190 case efTRX:
191 return "trx";
192 case efTRN:
193 return "trn";
194 case efSTO:
195 return "sto";
196 case efSTX:
197 return "stx";
198 case efTPS:
199 return "tps";
200 default:
201 return ftp2ext(ftp);
204 else
206 return "unknown";
210 const char *ftp2ext_with_dot(int ftp)
212 if ((0 <= ftp) && (ftp < efNR))
214 return deffile[ftp].ext;
216 else
218 return "unknown";
222 int ftp2generic_count(int ftp)
224 if ((0 <= ftp) && (ftp < efNR))
226 return deffile[ftp].ntps;
228 else
230 return 0;
234 const int *ftp2generic_list(int ftp)
236 if ((0 <= ftp) && (ftp < efNR))
238 return deffile[ftp].tps;
240 else
242 return 0;
246 const char *ftp2desc(int ftp)
248 if ((0 <= ftp) && (ftp < efNR))
250 return deffile[ftp].descr;
252 else
254 return "unknown filetype";
258 gmx_bool ftp_is_text(int ftp)
260 if ((ftp >= 0) && (ftp < efNR))
262 return deffile[ftp].ftype == eftASC;
264 return FALSE;
267 gmx_bool ftp_is_xdr(int ftp)
269 if ((ftp >= 0) && (ftp < efNR))
271 return deffile[ftp].ftype == eftXDR;
273 return FALSE;
276 const char *ftp2defnm(int ftp)
278 if ((0 <= ftp) && (ftp < efNR))
280 return deffile[ftp].defnm;
282 else
284 return NULL;
288 const char *ftp2defopt(int ftp)
290 if ((0 <= ftp) && (ftp < efNR))
292 return deffile[ftp].defopt;
294 else
296 return NULL;
300 int fn2ftp(const char *fn)
302 int i, len;
303 const char *feptr;
304 const char *eptr;
306 if (!fn)
308 return efNR;
311 len = std::strlen(fn);
312 if ((len >= 4) && (fn[len - 4] == '.'))
314 feptr = &(fn[len - 4]);
316 else
318 return efNR;
321 for (i = 0; (i < efNR); i++)
323 if ((eptr = deffile[i].ext) != NULL)
325 if (gmx_strcasecmp(feptr, eptr) == 0)
327 break;
332 return i;
335 const char *opt2fn(const char *opt, int nfile, const t_filenm fnm[])
337 int i;
339 for (i = 0; (i < nfile); i++)
341 if (std::strcmp(opt, fnm[i].opt) == 0)
343 return fnm[i].fns[0];
347 fprintf(stderr, "No option %s\n", opt);
349 return NULL;
352 const char *opt2fn_master(const char *opt, int nfile, const t_filenm fnm[],
353 t_commrec *cr)
355 return SIMMASTER(cr) ? opt2fn(opt, nfile, fnm) : NULL;
358 int opt2fns(char **fns[], const char *opt, int nfile, const t_filenm fnm[])
360 int i;
362 for (i = 0; (i < nfile); i++)
364 if (strcmp(opt, fnm[i].opt) == 0)
366 *fns = fnm[i].fns;
367 return fnm[i].nfiles;
371 fprintf(stderr, "No option %s\n", opt);
372 return 0;
375 const char *ftp2fn(int ftp, int nfile, const t_filenm fnm[])
377 int i;
379 for (i = 0; (i < nfile); i++)
381 if (ftp == fnm[i].ftp)
383 return fnm[i].fns[0];
387 fprintf(stderr, "ftp2fn: No filetype %s\n", deffile[ftp].ext);
388 return NULL;
391 int ftp2fns(char **fns[], int ftp, int nfile, const t_filenm fnm[])
393 int i;
395 for (i = 0; (i < nfile); i++)
397 if (ftp == fnm[i].ftp)
399 *fns = fnm[i].fns;
400 return fnm[i].nfiles;
404 fprintf(stderr, "ftp2fn: No filetype %s\n", deffile[ftp].ext);
405 return 0;
408 gmx_bool ftp2bSet(int ftp, int nfile, const t_filenm fnm[])
410 int i;
412 for (i = 0; (i < nfile); i++)
414 if (ftp == fnm[i].ftp)
416 return (gmx_bool) IS_SET(fnm[i]);
420 fprintf(stderr, "ftp2bSet: No filetype %s\n", deffile[ftp].ext);
422 return FALSE;
425 gmx_bool opt2bSet(const char *opt, int nfile, const t_filenm fnm[])
427 int i;
429 for (i = 0; (i < nfile); i++)
431 if (std::strcmp(opt, fnm[i].opt) == 0)
433 return (gmx_bool) IS_SET(fnm[i]);
437 fprintf(stderr, "No option %s\n", opt);
439 return FALSE;
442 const char *opt2fn_null(const char *opt, int nfile, const t_filenm fnm[])
444 int i;
446 for (i = 0; (i < nfile); i++)
448 if (std::strcmp(opt, fnm[i].opt) == 0)
450 if (IS_OPT(fnm[i]) && !IS_SET(fnm[i]))
452 return NULL;
454 else
456 return fnm[i].fns[0];
460 fprintf(stderr, "No option %s\n", opt);
461 return NULL;
464 const char *ftp2fn_null(int ftp, int nfile, const t_filenm fnm[])
466 int i;
468 for (i = 0; (i < nfile); i++)
470 if (ftp == fnm[i].ftp)
472 if (IS_OPT(fnm[i]) && !IS_SET(fnm[i]))
474 return NULL;
476 else
478 return fnm[i].fns[0];
482 fprintf(stderr, "ftp2fn: No filetype %s\n", deffile[ftp].ext);
483 return NULL;
486 gmx_bool is_optional(const t_filenm *fnm)
488 return ((fnm->flag & ffOPT) == ffOPT);
491 gmx_bool is_output(const t_filenm *fnm)
493 return ((fnm->flag & ffWRITE) == ffWRITE);
496 gmx_bool is_set(const t_filenm *fnm)
498 return ((fnm->flag & ffSET) == ffSET);
501 int add_suffix_to_output_names(t_filenm *fnm, int nfile, const char *suffix)
503 int i, j;
504 char buf[STRLEN], newname[STRLEN];
505 char *extpos;
507 for (i = 0; i < nfile; i++)
509 if (is_output(&fnm[i]) && fnm[i].ftp != efCPT)
511 /* We never use multiple _outputs_, but we might as well check
512 for it, just in case... */
513 for (j = 0; j < fnm[i].nfiles; j++)
515 std::strncpy(buf, fnm[i].fns[j], STRLEN - 1);
516 extpos = strrchr(buf, '.');
517 *extpos = '\0';
518 sprintf(newname, "%s%s.%s", buf, suffix, extpos + 1);
519 sfree(fnm[i].fns[j]);
520 fnm[i].fns[j] = gmx_strdup(newname);
524 return 0;
527 t_filenm *dup_tfn(int nf, const t_filenm tfn[])
529 int i, j;
530 t_filenm *ret;
532 snew(ret, nf);
533 for (i = 0; i < nf; i++)
535 ret[i] = tfn[i]; /* just directly copy all non-string fields */
536 if (tfn[i].opt)
538 ret[i].opt = gmx_strdup(tfn[i].opt);
540 else
542 ret[i].opt = NULL;
545 if (tfn[i].fn)
547 ret[i].fn = gmx_strdup(tfn[i].fn);
549 else
551 ret[i].fn = NULL;
554 if (tfn[i].nfiles > 0)
556 snew(ret[i].fns, tfn[i].nfiles);
557 for (j = 0; j < tfn[i].nfiles; j++)
559 ret[i].fns[j] = gmx_strdup(tfn[i].fns[j]);
563 return ret;
566 void done_filenms(int nf, t_filenm fnm[])
568 int i, j;
570 for (i = 0; i < nf; ++i)
572 for (j = 0; j < fnm[i].nfiles; ++j)
574 sfree(fnm[i].fns[j]);
576 sfree(fnm[i].fns);
577 fnm[i].fns = NULL;