Merge branch release-5-1
[alexxy/gromacs.git] / src / gromacs / fileio / filenm.cpp
1 /*
2  * This file is part of the GROMACS molecular simulation package.
3  *
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.
10  *
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.
15  *
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.
20  *
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.
25  *
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.
33  *
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.
36  */
37 #include "gmxpre.h"
38
39 #include "filenm.h"
40
41 #include <cstdio>
42 #include <cstring>
43
44 #include "gromacs/legacyheaders/macros.h"
45 #include "gromacs/legacyheaders/types/commrec.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"
50
51 /* Use bitflag ... */
52 #define IS_SET(fn) ((fn.flag & ffSET) != 0)
53 #define IS_OPT(fn) ((fn.flag & ffOPT) != 0)
54
55 enum
56 {
57     eftASC, eftXDR, eftTNG, eftGEN, eftNR
58 };
59
60 /* To support multiple file types with one general (eg TRX) we have
61  * these arrays.
62  */
63 static const int trxs[] =
64 {
65     efXTC, efTRR, efCPT,
66     efGRO, efG96, efPDB, efTNG
67 };
68 #define NTRXS asize(trxs)
69
70 static const int trcompressed[] =
71 {
72     efXTC,
73     efTNG
74 };
75 #define NTRCOMPRESSED asize(trcompressed)
76
77 static const int tros[] =
78 {
79     efXTC, efTRR,
80     efGRO, efG96, efPDB, efTNG
81 };
82 #define NTROS asize(tros)
83
84 static const int trns[] =
85 {
86     efTRR, efCPT,
87     efTNG
88 };
89 #define NTRNS asize(trns)
90
91 static const int stos[] =
92 { efGRO, efG96, efPDB, efBRK, efENT, efESP };
93 #define NSTOS asize(stos)
94
95 static const int stxs[] =
96 {
97     efGRO, efG96, efPDB, efBRK, efENT, efESP,
98     efTPR
99 };
100 #define NSTXS asize(stxs)
101
102 static const int tpss[] =
103 {
104     efTPR,
105     efGRO, efG96, efPDB, efBRK, efENT
106 };
107 #define NTPSS asize(tpss)
108
109 typedef struct
110 {
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;
119
120 /* this array should correspond to the enum in filenm.h */
121 static const t_deffile
122     deffile[efNR] =
123 {
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" }
170 };
171
172 const char *ftp2ext(int ftp)
173 {
174     if ((0 <= ftp) && (ftp < efNR))
175     {
176         return deffile[ftp].ext[0] != '\0' ? deffile[ftp].ext + 1 : "";
177     }
178     else
179     {
180         return "unknown";
181     }
182 }
183
184 const char *ftp2ext_generic(int ftp)
185 {
186     if ((0 <= ftp) && (ftp < efNR))
187     {
188         switch (ftp)
189         {
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);
202         }
203     }
204     else
205     {
206         return "unknown";
207     }
208 }
209
210 const char *ftp2ext_with_dot(int ftp)
211 {
212     if ((0 <= ftp) && (ftp < efNR))
213     {
214         return deffile[ftp].ext;
215     }
216     else
217     {
218         return "unknown";
219     }
220 }
221
222 int ftp2generic_count(int ftp)
223 {
224     if ((0 <= ftp) && (ftp < efNR))
225     {
226         return deffile[ftp].ntps;
227     }
228     else
229     {
230         return 0;
231     }
232 }
233
234 const int *ftp2generic_list(int ftp)
235 {
236     if ((0 <= ftp) && (ftp < efNR))
237     {
238         return deffile[ftp].tps;
239     }
240     else
241     {
242         return 0;
243     }
244 }
245
246 const char *ftp2desc(int ftp)
247 {
248     if ((0 <= ftp) && (ftp < efNR))
249     {
250         return deffile[ftp].descr;
251     }
252     else
253     {
254         return "unknown filetype";
255     }
256 }
257
258 gmx_bool ftp_is_text(int ftp)
259 {
260     if ((ftp >= 0) && (ftp < efNR))
261     {
262         return deffile[ftp].ftype == eftASC;
263     }
264     return FALSE;
265 }
266
267 gmx_bool ftp_is_xdr(int ftp)
268 {
269     if ((ftp >= 0) && (ftp < efNR))
270     {
271         return deffile[ftp].ftype == eftXDR;
272     }
273     return FALSE;
274 }
275
276 const char *ftp2defnm(int ftp)
277 {
278     if ((0 <= ftp) && (ftp < efNR))
279     {
280         return deffile[ftp].defnm;
281     }
282     else
283     {
284         return NULL;
285     }
286 }
287
288 const char *ftp2defopt(int ftp)
289 {
290     if ((0 <= ftp) && (ftp < efNR))
291     {
292         return deffile[ftp].defopt;
293     }
294     else
295     {
296         return NULL;
297     }
298 }
299
300 int fn2ftp(const char *fn)
301 {
302     int         i, len;
303     const char *feptr;
304     const char *eptr;
305
306     if (!fn)
307     {
308         return efNR;
309     }
310
311     len = std::strlen(fn);
312     if ((len >= 4) && (fn[len - 4] == '.'))
313     {
314         feptr = &(fn[len - 4]);
315     }
316     else
317     {
318         return efNR;
319     }
320
321     for (i = 0; (i < efNR); i++)
322     {
323         if ((eptr = deffile[i].ext) != NULL)
324         {
325             if (gmx_strcasecmp(feptr, eptr) == 0)
326             {
327                 break;
328             }
329         }
330     }
331
332     return i;
333 }
334
335 const char *opt2fn(const char *opt, int nfile, const t_filenm fnm[])
336 {
337     int i;
338
339     for (i = 0; (i < nfile); i++)
340     {
341         if (std::strcmp(opt, fnm[i].opt) == 0)
342         {
343             return fnm[i].fns[0];
344         }
345     }
346
347     fprintf(stderr, "No option %s\n", opt);
348
349     return NULL;
350 }
351
352 const char *opt2fn_master(const char *opt, int nfile, const t_filenm fnm[],
353                           t_commrec *cr)
354 {
355     return SIMMASTER(cr) ? opt2fn(opt, nfile, fnm) : NULL;
356 }
357
358 int opt2fns(char **fns[], const char *opt, int nfile, const t_filenm fnm[])
359 {
360     int i;
361
362     for (i = 0; (i < nfile); i++)
363     {
364         if (strcmp(opt, fnm[i].opt) == 0)
365         {
366             *fns = fnm[i].fns;
367             return fnm[i].nfiles;
368         }
369     }
370
371     fprintf(stderr, "No option %s\n", opt);
372     return 0;
373 }
374
375 const char *ftp2fn(int ftp, int nfile, const t_filenm fnm[])
376 {
377     int i;
378
379     for (i = 0; (i < nfile); i++)
380     {
381         if (ftp == fnm[i].ftp)
382         {
383             return fnm[i].fns[0];
384         }
385     }
386
387     fprintf(stderr, "ftp2fn: No filetype %s\n", deffile[ftp].ext);
388     return NULL;
389 }
390
391 int ftp2fns(char **fns[], int ftp, int nfile, const t_filenm fnm[])
392 {
393     int i;
394
395     for (i = 0; (i < nfile); i++)
396     {
397         if (ftp == fnm[i].ftp)
398         {
399             *fns = fnm[i].fns;
400             return fnm[i].nfiles;
401         }
402     }
403
404     fprintf(stderr, "ftp2fn: No filetype %s\n", deffile[ftp].ext);
405     return 0;
406 }
407
408 gmx_bool ftp2bSet(int ftp, int nfile, const t_filenm fnm[])
409 {
410     int i;
411
412     for (i = 0; (i < nfile); i++)
413     {
414         if (ftp == fnm[i].ftp)
415         {
416             return (gmx_bool) IS_SET(fnm[i]);
417         }
418     }
419
420     fprintf(stderr, "ftp2bSet: No filetype %s\n", deffile[ftp].ext);
421
422     return FALSE;
423 }
424
425 gmx_bool opt2bSet(const char *opt, int nfile, const t_filenm fnm[])
426 {
427     int i;
428
429     for (i = 0; (i < nfile); i++)
430     {
431         if (std::strcmp(opt, fnm[i].opt) == 0)
432         {
433             return (gmx_bool) IS_SET(fnm[i]);
434         }
435     }
436
437     fprintf(stderr, "No option %s\n", opt);
438
439     return FALSE;
440 }
441
442 const char *opt2fn_null(const char *opt, int nfile, const t_filenm fnm[])
443 {
444     int i;
445
446     for (i = 0; (i < nfile); i++)
447     {
448         if (std::strcmp(opt, fnm[i].opt) == 0)
449         {
450             if (IS_OPT(fnm[i]) && !IS_SET(fnm[i]))
451             {
452                 return NULL;
453             }
454             else
455             {
456                 return fnm[i].fns[0];
457             }
458         }
459     }
460     fprintf(stderr, "No option %s\n", opt);
461     return NULL;
462 }
463
464 const char *ftp2fn_null(int ftp, int nfile, const t_filenm fnm[])
465 {
466     int i;
467
468     for (i = 0; (i < nfile); i++)
469     {
470         if (ftp == fnm[i].ftp)
471         {
472             if (IS_OPT(fnm[i]) && !IS_SET(fnm[i]))
473             {
474                 return NULL;
475             }
476             else
477             {
478                 return fnm[i].fns[0];
479             }
480         }
481     }
482     fprintf(stderr, "ftp2fn: No filetype %s\n", deffile[ftp].ext);
483     return NULL;
484 }
485
486 gmx_bool is_optional(const t_filenm *fnm)
487 {
488     return ((fnm->flag & ffOPT) == ffOPT);
489 }
490
491 gmx_bool is_output(const t_filenm *fnm)
492 {
493     return ((fnm->flag & ffWRITE) == ffWRITE);
494 }
495
496 gmx_bool is_set(const t_filenm *fnm)
497 {
498     return ((fnm->flag & ffSET) == ffSET);
499 }
500
501 int add_suffix_to_output_names(t_filenm *fnm, int nfile, const char *suffix)
502 {
503     int   i, j;
504     char  buf[STRLEN], newname[STRLEN];
505     char *extpos;
506
507     for (i = 0; i < nfile; i++)
508     {
509         if (is_output(&fnm[i]) && fnm[i].ftp != efCPT)
510         {
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++)
514             {
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);
521             }
522         }
523     }
524     return 0;
525 }
526
527 t_filenm *dup_tfn(int nf, const t_filenm tfn[])
528 {
529     int       i, j;
530     t_filenm *ret;
531
532     snew(ret, nf);
533     for (i = 0; i < nf; i++)
534     {
535         ret[i] = tfn[i]; /* just directly copy all non-string fields */
536         if (tfn[i].opt)
537         {
538             ret[i].opt = gmx_strdup(tfn[i].opt);
539         }
540         else
541         {
542             ret[i].opt = NULL;
543         }
544
545         if (tfn[i].fn)
546         {
547             ret[i].fn = gmx_strdup(tfn[i].fn);
548         }
549         else
550         {
551             ret[i].fn = NULL;
552         }
553
554         if (tfn[i].nfiles > 0)
555         {
556             snew(ret[i].fns, tfn[i].nfiles);
557             for (j = 0; j < tfn[i].nfiles; j++)
558             {
559                 ret[i].fns[j] = gmx_strdup(tfn[i].fns[j]);
560             }
561         }
562     }
563     return ret;
564 }
565
566 void done_filenms(int nf, t_filenm fnm[])
567 {
568     int i, j;
569
570     for (i = 0; i < nf; ++i)
571     {
572         for (j = 0; j < fnm[i].nfiles; ++j)
573         {
574             sfree(fnm[i].fns[j]);
575         }
576         sfree(fnm[i].fns);
577         fnm[i].fns = NULL;
578     }
579 }