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