d132e44d66ee051d95642120a73faae6666d5a9b
[alexxy/gromacs.git] / src / gromacs / fileio / filenm.c
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, 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 "config.h"
42
43 #include <stdio.h>
44 #include <string.h>
45
46 #include "gromacs/legacyheaders/macros.h"
47 #include "gromacs/legacyheaders/types/commrec.h"
48
49 #include "gromacs/utility/basedefinitions.h"
50 #include "gromacs/utility/cstringutil.h"
51 #include "gromacs/utility/fatalerror.h"
52 #include "gromacs/utility/smalloc.h"
53
54 /* XDR should be available on all platforms now,
55  * but we keep the possibility of turning it off...
56  */
57 #define USE_XDR
58
59 /* Use bitflag ... */
60 #define IS_SET(fn) ((fn.flag & ffSET) != 0)
61 #define IS_OPT(fn) ((fn.flag & ffOPT) != 0)
62
63 enum
64 {
65     eftASC, eftBIN, eftXDR, eftTNG, eftGEN, eftNR
66 };
67
68 /* To support multiple file types with one general (eg TRX) we have
69  * these arrays.
70  */
71 static const int trxs[] =
72 {
73 #ifdef USE_XDR
74     efXTC, efTRR, efCPT,
75 #endif
76     efTRJ, efGRO, efG96, efPDB, efTNG
77 };
78 #define NTRXS asize(trxs)
79
80 static const int trcompressed[] =
81 {
82 #ifdef USE_XDR
83     efXTC,
84 #endif
85     efTNG
86 };
87 #define NTRCOMPRESSED asize(trcompressed)
88
89 static const int tros[] =
90 {
91 #ifdef USE_XDR
92     efXTC, efTRR,
93 #endif
94     efTRJ, efGRO, efG96, efPDB, efTNG
95 };
96 #define NTROS asize(tros)
97
98 static const int trns[] =
99 {
100 #ifdef USE_XDR
101     efTRR, efCPT,
102 #endif
103     efTRJ, efTNG
104 };
105 #define NTRNS asize(trns)
106
107 static const int stos[] =
108 { efGRO, efG96, efPDB, efBRK, efENT, efESP };
109 #define NSTOS asize(stos)
110
111 static const int stxs[] =
112 {
113     efGRO, efG96, efPDB, efBRK, efENT, efESP,
114 #ifdef USE_XDR
115     efTPR,
116 #endif
117     efTPB, efTPA
118 };
119 #define NSTXS asize(stxs)
120
121 static const int tpxs[] =
122 {
123 #ifdef USE_XDR
124     efTPR,
125 #endif
126     efTPB, efTPA
127 };
128 #define NTPXS asize(tpxs)
129
130 static const int tpss[] =
131 {
132 #ifdef USE_XDR
133     efTPR,
134 #endif
135     efTPB, efTPA, efGRO, efG96, efPDB, efBRK, efENT
136 };
137 #define NTPSS asize(tpss)
138
139 typedef struct
140 {
141     int         ftype;
142     const char *ext;
143     const char *defnm;
144     const char *defopt;
145     const char *descr;
146     int         ntps;
147     const int  *tps;
148 } t_deffile;
149
150 /* this array should correspond to the enum in filenm.h */
151 static const t_deffile
152     deffile[efNR] =
153 {
154     { eftASC, ".mdp", "grompp", "-f", "grompp input file with MD parameters" },
155     { eftGEN, ".???", "traj", "-f", "Trajectory", NTRXS, trxs },
156     { eftGEN, ".???", "trajout", "-f", "Trajectory", NTROS, tros },
157     { eftGEN, ".???", "traj", NULL,
158       "Full precision trajectory", NTRNS, trns },
159     { eftXDR, ".trr", "traj", NULL, "Trajectory in portable xdr format" },
160     { eftBIN, ".trj", "traj", NULL, "Trajectory file (architecture specific)" },
161     { eftGEN, ".???", "traj_comp", NULL,
162       "Compressed trajectory (tng format or portable xdr format)", NTRCOMPRESSED, trcompressed},
163     { eftXDR, ".xtc", "traj", NULL,
164       "Compressed trajectory (portable xdr format): xtc" },
165     { eftTNG, ".tng", "traj", NULL,
166       "Trajectory file (tng format)" },
167     { eftXDR, ".edr", "ener",   NULL, "Energy file"},
168     { eftGEN, ".???", "conf", "-c", "Structure file", NSTXS, stxs },
169     { eftGEN, ".???", "out", "-o", "Structure file", NSTOS, stos },
170     { eftASC, ".gro", "conf", "-c", "Coordinate file in Gromos-87 format" },
171     { eftASC, ".g96", "conf", "-c", "Coordinate file in Gromos-96 format" },
172     { eftASC, ".pdb", "eiwit",  "-f", "Protein data bank file"},
173     { eftASC, ".brk", "eiwit",  "-f", "Brookhaven data bank file"},
174     { eftASC, ".ent", "eiwit", "-f", "Entry in the protein date bank" },
175     { eftASC, ".esp", "conf", "-f", "Coordinate file in Espresso format" },
176     { eftASC, ".pqr", "state",  "-o", "Coordinate file for MEAD"},
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", NTPXS, tpxs },
185     { eftGEN, ".???", "topol", "-s", "Structure+mass(db)", NTPSS, tpss },
186     { eftXDR, ".tpr", "topol",  "-s", "Portable xdr run input file"},
187     { eftASC, ".tpa", "topol",  "-s", "Ascii run input file"},
188     { eftBIN, ".tpb", "topol",  "-s", "Binary run input file"},
189     { eftASC, ".tex", "doc",    "-o", "LaTeX file"},
190     { eftASC, ".rtp", "residue", NULL, "Residue Type file used by pdb2gmx" },
191     { eftASC, ".atp", "atomtp", NULL, "Atomtype file used by pdb2gmx" },
192     { eftASC, ".hdb", "polar",  NULL, "Hydrogen data base"},
193     { eftASC, ".dat", "nnnice", NULL, "Generic data file"},
194     { eftASC, ".dlg", "user",   NULL, "Dialog Box data for ngmx"},
195     { eftASC, ".map", "ss", NULL, "File that maps matrix data to colors" },
196     { eftASC, ".eps", "plot", NULL, "Encapsulated PostScript (tm) file" },
197     { eftASC, ".mat", "ss",     NULL, "Matrix Data file"},
198     { eftASC, ".m2p", "ps",     NULL, "Input file for mat2ps"},
199     { eftXDR, ".mtx", "hessian", "-m", "Hessian matrix"},
200     { eftASC, ".edi", "sam",    NULL, "ED sampling input"},
201     { eftASC, ".cub", "pot",  NULL, "Gaussian cube file" },
202     { eftASC, ".xpm", "root", NULL, "X PixMap compatible matrix file" },
203     { eftASC, "", "rundir", NULL, "Run directory" }
204 };
205
206 #define NZEXT 2
207 static const char *z_ext[NZEXT] =
208 { ".gz", ".Z" };
209
210 const char *ftp2ext(int ftp)
211 {
212     if ((0 <= ftp) && (ftp < efNR))
213     {
214         return deffile[ftp].ext[0] != '\0' ? deffile[ftp].ext + 1 : "";
215     }
216     else
217     {
218         return "unknown";
219     }
220 }
221
222 const char *ftp2ext_generic(int ftp)
223 {
224     if ((0 <= ftp) && (ftp < efNR))
225     {
226         switch (ftp)
227         {
228             case efTRX:
229                 return "trx";
230             case efTRN:
231                 return "trn";
232             case efSTO:
233                 return "sto";
234             case efSTX:
235                 return "stx";
236             case efTPX:
237                 return "tpx";
238             case efTPS:
239                 return "tps";
240             default:
241                 return ftp2ext(ftp);
242         }
243     }
244     else
245     {
246         return "unknown";
247     }
248 }
249
250 const char *ftp2ext_with_dot(int ftp)
251 {
252     if ((0 <= ftp) && (ftp < efNR))
253     {
254         return deffile[ftp].ext;
255     }
256     else
257     {
258         return "unknown";
259     }
260 }
261
262 int ftp2generic_count(int ftp)
263 {
264     if ((0 <= ftp) && (ftp < efNR))
265     {
266         return deffile[ftp].ntps;
267     }
268     else
269     {
270         return 0;
271     }
272 }
273
274 const int *ftp2generic_list(int ftp)
275 {
276     if ((0 <= ftp) && (ftp < efNR))
277     {
278         return deffile[ftp].tps;
279     }
280     else
281     {
282         return 0;
283     }
284 }
285
286 const char *ftp2desc(int ftp)
287 {
288     if ((0 <= ftp) && (ftp < efNR))
289     {
290         return deffile[ftp].descr;
291     }
292     else
293     {
294         return "unknown filetype";
295     }
296 }
297
298 const char *ftp2ftype(int ftp)
299 {
300     if ((ftp >= 0) && (ftp < efNR))
301     {
302         switch (deffile[ftp].ftype)
303         {
304             case eftASC:
305                 return "ASCII";
306             case eftBIN:
307                 return "Binary";
308             case eftXDR:
309                 return "XDR portable";
310             case eftTNG:
311                 return "TNG";
312             case eftGEN:
313                 return "";
314             default:
315                 gmx_fatal(FARGS, "Unknown filetype %d in ftp2ftype", deffile[ftp].ftype);
316                 break;
317         }
318     }
319     return "unknown";
320 }
321
322 const char *ftp2defnm(int ftp)
323 {
324     if ((0 <= ftp) && (ftp < efNR))
325     {
326         return deffile[ftp].defnm;
327     }
328     else
329     {
330         return NULL;
331     }
332 }
333
334 const char *ftp2defopt(int ftp)
335 {
336     if ((0 <= ftp) && (ftp < efNR))
337     {
338         return deffile[ftp].defopt;
339     }
340     else
341     {
342         return NULL;
343     }
344 }
345
346 int fn2ftp(const char *fn)
347 {
348     int         i, len;
349     const char *feptr;
350     const char *eptr;
351
352     if (!fn)
353     {
354         return efNR;
355     }
356
357     len = strlen(fn);
358     if ((len >= 4) && (fn[len - 4] == '.'))
359     {
360         feptr = &(fn[len - 4]);
361     }
362     else
363     {
364         return efNR;
365     }
366
367     for (i = 0; (i < efNR); i++)
368     {
369         if ((eptr = deffile[i].ext) != NULL)
370         {
371             if (gmx_strcasecmp(feptr, eptr) == 0)
372             {
373                 break;
374             }
375         }
376     }
377
378     return i;
379 }
380
381 const char *opt2fn(const char *opt, int nfile, const t_filenm fnm[])
382 {
383     int i;
384
385     for (i = 0; (i < nfile); i++)
386     {
387         if (strcmp(opt, fnm[i].opt) == 0)
388         {
389             return fnm[i].fns[0];
390         }
391     }
392
393     fprintf(stderr, "No option %s\n", opt);
394
395     return NULL;
396 }
397
398 const char *opt2fn_master(const char *opt, int nfile, const t_filenm fnm[],
399                           t_commrec *cr)
400 {
401     return SIMMASTER(cr) ? opt2fn(opt, nfile, fnm) : NULL;
402 }
403
404 int opt2fns(char **fns[], const char *opt, int nfile, const t_filenm fnm[])
405 {
406     int i;
407
408     for (i = 0; (i < nfile); i++)
409     {
410         if (strcmp(opt, fnm[i].opt) == 0)
411         {
412             *fns = fnm[i].fns;
413             return fnm[i].nfiles;
414         }
415     }
416
417     fprintf(stderr, "No option %s\n", opt);
418     return 0;
419 }
420
421 const char *ftp2fn(int ftp, int nfile, const t_filenm fnm[])
422 {
423     int i;
424
425     for (i = 0; (i < nfile); i++)
426     {
427         if (ftp == fnm[i].ftp)
428         {
429             return fnm[i].fns[0];
430         }
431     }
432
433     fprintf(stderr, "ftp2fn: No filetype %s\n", deffile[ftp].ext);
434     return NULL;
435 }
436
437 int ftp2fns(char **fns[], int ftp, int nfile, const t_filenm fnm[])
438 {
439     int i;
440
441     for (i = 0; (i < nfile); i++)
442     {
443         if (ftp == fnm[i].ftp)
444         {
445             *fns = fnm[i].fns;
446             return fnm[i].nfiles;
447         }
448     }
449
450     fprintf(stderr, "ftp2fn: No filetype %s\n", deffile[ftp].ext);
451     return 0;
452 }
453
454 gmx_bool ftp2bSet(int ftp, int nfile, const t_filenm fnm[])
455 {
456     int i;
457
458     for (i = 0; (i < nfile); i++)
459     {
460         if (ftp == fnm[i].ftp)
461         {
462             return (gmx_bool) IS_SET(fnm[i]);
463         }
464     }
465
466     fprintf(stderr, "ftp2bSet: No filetype %s\n", deffile[ftp].ext);
467
468     return FALSE;
469 }
470
471 gmx_bool opt2bSet(const char *opt, int nfile, const t_filenm fnm[])
472 {
473     int i;
474
475     for (i = 0; (i < nfile); i++)
476     {
477         if (strcmp(opt, fnm[i].opt) == 0)
478         {
479             return (gmx_bool) IS_SET(fnm[i]);
480         }
481     }
482
483     fprintf(stderr, "No option %s\n", opt);
484
485     return FALSE;
486 }
487
488 const char *opt2fn_null(const char *opt, int nfile, const t_filenm fnm[])
489 {
490     int i;
491
492     for (i = 0; (i < nfile); i++)
493     {
494         if (strcmp(opt, fnm[i].opt) == 0)
495         {
496             if (IS_OPT(fnm[i]) && !IS_SET(fnm[i]))
497             {
498                 return NULL;
499             }
500             else
501             {
502                 return fnm[i].fns[0];
503             }
504         }
505     }
506     fprintf(stderr, "No option %s\n", opt);
507     return NULL;
508 }
509
510 const char *ftp2fn_null(int ftp, int nfile, const t_filenm fnm[])
511 {
512     int i;
513
514     for (i = 0; (i < nfile); i++)
515     {
516         if (ftp == fnm[i].ftp)
517         {
518             if (IS_OPT(fnm[i]) && !IS_SET(fnm[i]))
519             {
520                 return NULL;
521             }
522             else
523             {
524                 return fnm[i].fns[0];
525             }
526         }
527     }
528     fprintf(stderr, "ftp2fn: No filetype %s\n", deffile[ftp].ext);
529     return NULL;
530 }
531
532 gmx_bool is_optional(const t_filenm *fnm)
533 {
534     return ((fnm->flag & ffOPT) == ffOPT);
535 }
536
537 gmx_bool is_output(const t_filenm *fnm)
538 {
539     return ((fnm->flag & ffWRITE) == ffWRITE);
540 }
541
542 gmx_bool is_set(const t_filenm *fnm)
543 {
544     return ((fnm->flag & ffSET) == ffSET);
545 }
546
547 int add_suffix_to_output_names(t_filenm *fnm, int nfile, const char *suffix)
548 {
549     int   i, j, pos;
550     char  buf[STRLEN], newname[STRLEN];
551     char *extpos;
552
553     for (i = 0; i < nfile; i++)
554     {
555         if (is_output(&fnm[i]) && fnm[i].ftp != efCPT)
556         {
557             /* We never use multiple _outputs_, but we might as well check
558                for it, just in case... */
559             for (j = 0; j < fnm[i].nfiles; j++)
560             {
561                 strncpy(buf, fnm[i].fns[j], STRLEN - 1);
562                 extpos  = strrchr(buf, '.');
563                 *extpos = '\0';
564                 sprintf(newname, "%s%s.%s", buf, suffix, extpos + 1);
565                 sfree(fnm[i].fns[j]);
566                 fnm[i].fns[j] = gmx_strdup(newname);
567             }
568         }
569     }
570     return 0;
571 }
572
573 t_filenm *dup_tfn(int nf, const t_filenm tfn[])
574 {
575     int       i, j;
576     t_filenm *ret;
577
578     snew(ret, nf);
579     for (i = 0; i < nf; i++)
580     {
581         ret[i] = tfn[i]; /* just directly copy all non-string fields */
582         if (tfn[i].opt)
583         {
584             ret[i].opt = gmx_strdup(tfn[i].opt);
585         }
586         else
587         {
588             ret[i].opt = NULL;
589         }
590
591         if (tfn[i].fn)
592         {
593             ret[i].fn = gmx_strdup(tfn[i].fn);
594         }
595         else
596         {
597             ret[i].fn = NULL;
598         }
599
600         if (tfn[i].nfiles > 0)
601         {
602             snew(ret[i].fns, tfn[i].nfiles);
603             for (j = 0; j < tfn[i].nfiles; j++)
604             {
605                 ret[i].fns[j] = gmx_strdup(tfn[i].fns[j]);
606             }
607         }
608     }
609     return ret;
610 }
611
612 void done_filenms(int nf, t_filenm fnm[])
613 {
614     int i, j;
615
616     for (i = 0; i < nf; ++i)
617     {
618         for (j = 0; j < fnm[i].nfiles; ++j)
619         {
620             sfree(fnm[i].fns[j]);
621         }
622         sfree(fnm[i].fns);
623         fnm[i].fns = NULL;
624     }
625 }