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