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