Merge branch 'release-4-6'
[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, 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 "filenm.h"
38
39 #ifdef HAVE_CONFIG_H
40 #include <config.h>
41 #endif
42
43 #include <string.h>
44 #include "sysstuff.h"
45 #include "typedefs.h"
46 #include "smalloc.h"
47 #include "string2.h"
48 #include "gmx_fatal.h"
49 #include "futil.h"
50 #include "xdrf.h"
51 #include "macros.h"
52
53 #include "gromacs/legacyheaders/thread_mpi/threads.h"
54
55 /* NOTE: this was a cesspool of thread-unsafe code, has now been
56    properly proteced by mutexes (hopefully). */
57
58 /* XDR should be available on all platforms now,
59  * but we keep the possibility of turning it off...
60  */
61 #define USE_XDR
62
63 /* Use bitflag ... */
64 #define IS_SET(fn) ((fn.flag & ffSET) != 0)
65 #define IS_OPT(fn) ((fn.flag & ffOPT) != 0)
66 #define IS_MULT(fn) ((fn.flag & ffMULT) != 0)
67 #define UN_SET(fn) (fn.flag = (fn.flag & ~ffSET))
68 #define DO_SET(fn) (fn.flag = (fn.flag |  ffSET))
69
70 enum
71 {
72     eftASC, eftBIN, eftXDR, eftGEN, eftNR
73 };
74
75 /* To support multiple file types with one general (eg TRX) we have
76  * these arrays.
77  */
78 static const int trxs[] =
79 {
80 #ifdef USE_XDR
81     efXTC, efTRR, efCPT,
82 #endif
83     efTRJ, efGRO, efG96, efPDB, efG87
84 };
85 #define NTRXS asize(trxs)
86
87 static const int tros[] =
88 {
89 #ifdef USE_XDR
90     efXTC, efTRR,
91 #endif
92     efTRJ, efGRO, efG96, efPDB, efG87
93 };
94 #define NTROS asize(tros)
95
96 static const int trns[] =
97 {
98 #ifdef USE_XDR
99     efTRR, efCPT,
100 #endif
101     efTRJ
102 };
103 #define NTRNS asize(trns)
104
105 static const int stos[] =
106 { efGRO, efG96, efPDB, efBRK, efENT, efESP, efXYZ };
107 #define NSTOS asize(stos)
108
109 static const int stxs[] =
110 {
111     efGRO, efG96, efPDB, efBRK, efENT, efESP, efXYZ,
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",
154       "Trajectory: xtc trr trj gro g96 pdb cpt", NTRXS, trxs },
155     { eftGEN, ".???", "trajout", "-f",
156       "Trajectory: xtc trr trj gro g96 pdb", NTROS, tros },
157     { eftGEN, ".???", "traj", NULL,
158       "Full precision trajectory: trr trj cpt", NTRNS, trns },
159     { eftXDR, ".trr", "traj", NULL, "Trajectory in portable xdr format" },
160     { eftBIN, ".trj", "traj", NULL, "Trajectory file (architecture specific)" },
161     { eftXDR, ".xtc", "traj", NULL,
162       "Compressed trajectory (portable xdr format)" },
163     { eftASC, ".g87", "gtraj", NULL, "Gromos-87 ASCII trajectory format" },
164     { eftXDR, ".edr", "ener",   NULL, "Energy file"},
165     { eftGEN, ".???", "conf", "-c", "Structure file: gro g96 pdb tpr etc.",
166       NSTXS, stxs },
167     { eftGEN, ".???", "out", "-o", "Structure file: gro g96 pdb etc.",
168       NSTOS, stos },
169     { eftASC, ".gro", "conf", "-c", "Coordinate file in Gromos-87 format" },
170     { eftASC, ".g96", "conf", "-c", "Coordinate file in Gromos-96 format" },
171     { eftASC, ".pdb", "eiwit",  "-f", "Protein data bank file"},
172     { eftASC, ".brk", "eiwit",  "-f", "Brookhaven data bank file"},
173     { eftASC, ".ent", "eiwit", "-f", "Entry in the protein date bank" },
174     { eftASC, ".esp", "conf", "-f", "Coordinate file in Espresso format" },
175     { eftASC, ".pqr", "state",  "-o", "Coordinate file for MEAD"},
176     { eftASC, ".xyz", "conf", "-o", "Coordinate file for some other programs" },
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: tpr tpb tpa",
185       NTPXS, tpxs },
186     { eftGEN, ".???", "topol", "-s",
187       "Structure+mass(db): tpr tpb tpa gro g96 pdb", NTPSS, tpss },
188     { eftXDR, ".tpr", "topol",  "-s", "Portable xdr run input file"},
189     { eftASC, ".tpa", "topol",  "-s", "Ascii run input file"},
190     { eftBIN, ".tpb", "topol",  "-s", "Binary run input file"},
191     { eftASC, ".tex", "doc",    "-o", "LaTeX file"},
192     { eftASC, ".rtp", "residue", NULL, "Residue Type file used by pdb2gmx" },
193     { eftASC, ".atp", "atomtp", NULL, "Atomtype file used by pdb2gmx" },
194     { eftASC, ".hdb", "polar",  NULL, "Hydrogen data base"},
195     { eftASC, ".dat", "nnnice", NULL, "Generic data file"},
196     { eftASC, ".dlg", "user",   NULL, "Dialog Box data for ngmx"},
197     { eftASC, ".map", "ss", NULL, "File that maps matrix data to colors" },
198     { eftASC, ".eps", "plot", NULL, "Encapsulated PostScript (tm) file" },
199     { eftASC, ".mat", "ss",     NULL, "Matrix Data file"},
200     { eftASC, ".m2p", "ps",     NULL, "Input file for mat2ps"},
201     { eftXDR, ".mtx", "hessian", "-m", "Hessian matrix"},
202     { eftASC, ".edi", "sam",    NULL, "ED sampling input"},
203     { eftASC, ".cub", "pot",  NULL, "Gaussian cube file" },
204     { eftASC, ".xpm", "root", NULL, "X PixMap compatible matrix file" },
205     { eftASC, "", "rundir", NULL, "Run directory" }
206 };
207
208 static char *default_file_name = NULL;
209
210 static tMPI_Thread_mutex_t filenm_mutex = TMPI_THREAD_MUTEX_INITIALIZER;
211
212 #define NZEXT 2
213 static const char *z_ext[NZEXT] =
214 { ".gz", ".Z" };
215
216 void set_default_file_name(const char *name)
217 {
218     tMPI_Thread_mutex_lock(&filenm_mutex);
219     default_file_name = strdup(name);
220     tMPI_Thread_mutex_unlock(&filenm_mutex);
221 }
222
223 const char *ftp2ext(int ftp)
224 {
225     if ((0 <= ftp) && (ftp < efNR))
226     {
227         return deffile[ftp].ext[0] != '\0' ? deffile[ftp].ext + 1 : "";
228     }
229     else
230     {
231         return "unknown";
232     }
233 }
234
235 const char *ftp2ext_generic(int ftp)
236 {
237     if ((0 <= ftp) && (ftp < efNR))
238     {
239         switch (ftp)
240         {
241             case efTRX:
242                 return "trx";
243             case efTRN:
244                 return "trn";
245             case efSTO:
246                 return "sto";
247             case efSTX:
248                 return "stx";
249             case efTPX:
250                 return "tpx";
251             case efTPS:
252                 return "tps";
253             default:
254                 return ftp2ext(ftp);
255         }
256     }
257     else
258     {
259         return "unknown";
260     }
261 }
262
263 const char *ftp2ext_with_dot(int ftp)
264 {
265     if ((0 <= ftp) && (ftp < efNR))
266     {
267         return deffile[ftp].ext;
268     }
269     else
270     {
271         return "unknown";
272     }
273 }
274
275 int ftp2generic_count(int ftp)
276 {
277     if ((0 <= ftp) && (ftp < efNR))
278     {
279         return deffile[ftp].ntps;
280     }
281     else
282     {
283         return 0;
284     }
285 }
286
287 const int *ftp2generic_list(int ftp)
288 {
289     if ((0 <= ftp) && (ftp < efNR))
290     {
291         return deffile[ftp].tps;
292     }
293     else
294     {
295         return 0;
296     }
297 }
298
299 const char *ftp2desc(int ftp)
300 {
301     if ((0 <= ftp) && (ftp < efNR))
302     {
303         return deffile[ftp].descr;
304     }
305     else
306     {
307         return "unknown filetype";
308     }
309 }
310
311 const char *ftp2ftype(int ftp)
312 {
313     if ((ftp >= 0) && (ftp < efNR))
314     {
315         switch (deffile[ftp].ftype)
316         {
317             case eftASC:
318                 return "ASCII";
319             case eftBIN:
320                 return "Binary";
321             case eftXDR:
322                 return "XDR portable";
323             case eftGEN:
324                 return "";
325             default:
326                 gmx_fatal(FARGS, "Unknown filetype %d in ftp2ftype", deffile[ftp].ftype);
327                 break;
328         }
329     }
330     return "unknown";
331 }
332
333 const char *ftp2defnm(int ftp)
334 {
335     const char *buf = NULL;
336
337     tMPI_Thread_mutex_lock(&filenm_mutex);
338
339     if (default_file_name)
340     {
341         buf = default_file_name;
342     }
343     else
344     {
345         if ((0 <= ftp) && (ftp < efNR))
346         {
347             buf = deffile[ftp].defnm;
348         }
349     }
350
351     tMPI_Thread_mutex_unlock(&filenm_mutex);
352
353     return buf;
354 }
355
356 static void check_opts(int nf, t_filenm fnm[])
357 {
358     int              i;
359     const t_deffile *df;
360
361     for (i = 0; (i < nf); i++)
362     {
363         df = &(deffile[fnm[i].ftp]);
364         if (fnm[i].opt == NULL)
365         {
366             if (df->defopt == NULL)
367             {
368                 gmx_fatal(FARGS, "No default cmd-line option for %s (type %d)\n",
369                           deffile[fnm[i].ftp].ext, fnm[i].ftp);
370             }
371             else
372             {
373                 fnm[i].opt = df->defopt;
374             }
375         }
376     }
377 }
378
379 int fn2ftp(const char *fn)
380 {
381     int         i, len;
382     const char *feptr;
383     const char *eptr;
384
385     if (!fn)
386     {
387         return efNR;
388     }
389
390     len = strlen(fn);
391     if ((len >= 4) && (fn[len - 4] == '.'))
392     {
393         feptr = &(fn[len - 4]);
394     }
395     else
396     {
397         return efNR;
398     }
399
400     for (i = 0; (i < efNR); i++)
401     {
402         if ((eptr = deffile[i].ext) != NULL)
403         {
404             if (gmx_strcasecmp(feptr, eptr) == 0)
405             {
406                 break;
407             }
408         }
409     }
410
411     return i;
412 }
413
414 static void set_extension(char *buf, int ftp)
415 {
416     int              len, extlen;
417     const t_deffile *df;
418
419     /* check if extension is already at end of filename */
420     df     = &(deffile[ftp]);
421     len    = strlen(buf);
422     extlen = strlen(df->ext);
423     if ((len <= extlen) || (gmx_strcasecmp(&(buf[len - extlen]), df->ext) != 0))
424     {
425         strcat(buf, df->ext);
426     }
427 }
428
429 static void add_filenm(t_filenm *fnm, const char *filenm)
430 {
431     srenew(fnm->fns, fnm->nfiles+1);
432     fnm->fns[fnm->nfiles] = strdup(filenm);
433     fnm->nfiles++;
434 }
435
436 static void set_grpfnm(t_filenm *fnm, const char *name, gmx_bool bCanNotOverride)
437 {
438     char       buf[256], buf2[256];
439     int        i, type;
440     gmx_bool   bValidExt;
441     int        nopts;
442     const int *ftps;
443
444     nopts = deffile[fnm->ftp].ntps;
445     ftps  = deffile[fnm->ftp].tps;
446     if ((nopts == 0) || (ftps == NULL))
447     {
448         gmx_fatal(FARGS, "nopts == 0 || ftps == NULL");
449     }
450
451     bValidExt = FALSE;
452     if (name && (bCanNotOverride || (default_file_name == NULL)))
453     {
454         strcpy(buf, name);
455         /* First check whether we have a valid filename already */
456         type = fn2ftp(name);
457         if ((fnm->flag & ffREAD) && (fnm->ftp == efTRX))
458         {
459             /*if file exist don't add an extension for trajectory reading*/
460             bValidExt = gmx_fexist(name);
461         }
462         for (i = 0; (i < nopts) && !bValidExt; i++)
463         {
464             if (type == ftps[i])
465             {
466                 bValidExt = TRUE;
467             }
468         }
469     }
470     else
471     {
472         /* No name given, set the default name */
473         strcpy(buf, ftp2defnm(fnm->ftp));
474     }
475
476     if (!bValidExt && (fnm->flag & ffREAD))
477     {
478         /* for input-files only: search for filenames in the directory */
479         for (i = 0; (i < nopts) && !bValidExt; i++)
480         {
481             type = ftps[i];
482             strcpy(buf2, buf);
483             set_extension(buf2, type);
484             if (gmx_fexist(buf2))
485             {
486                 bValidExt = TRUE;
487                 strcpy(buf, buf2);
488             }
489         }
490     }
491
492     if (!bValidExt)
493     {
494         /* Use the first extension type */
495         set_extension(buf, ftps[0]);
496     }
497
498     add_filenm(fnm, buf);
499 }
500
501 static void set_filenm(t_filenm *fnm, const char *name, gmx_bool bCanNotOverride,
502                        gmx_bool bReadNode)
503 {
504     /* Set the default filename, extension and option for those fields that
505      * are not already set. An extension is added if not present, if fn = NULL
506      * or empty, the default filename is given.
507      */
508     char buf[256];
509     int  i, len, extlen;
510
511     if ((fnm->flag & ffREAD) && !bReadNode)
512     {
513         return;
514     }
515
516     if ((fnm->ftp < 0) || (fnm->ftp >= efNR))
517     {
518         gmx_fatal(FARGS, "file type out of range (%d)", fnm->ftp);
519     }
520
521     if (name)
522     {
523         strcpy(buf, name);
524     }
525     if ((fnm->flag & ffREAD) && name && gmx_fexist(name))
526     {
527         /* check if filename ends in .gz or .Z, if so remove that: */
528         len = strlen(name);
529         for (i = 0; i < NZEXT; i++)
530         {
531             extlen = strlen(z_ext[i]);
532             if (len > extlen)
533             {
534                 if (gmx_strcasecmp(name+len-extlen, z_ext[i]) == 0)
535                 {
536                     buf[len-extlen] = '\0';
537                     break;
538                 }
539             }
540         }
541     }
542
543     if (deffile[fnm->ftp].ntps)
544     {
545         set_grpfnm(fnm, name ? buf : NULL, bCanNotOverride);
546     }
547     else
548     {
549         if ((name == NULL) || !(bCanNotOverride || (default_file_name == NULL)))
550         {
551             const char *defnm = ftp2defnm(fnm->ftp);
552             strcpy(buf, defnm);
553         }
554         set_extension(buf, fnm->ftp);
555
556         add_filenm(fnm, buf);
557     }
558 }
559
560 static void set_filenms(int nf, t_filenm fnm[], gmx_bool bReadNode)
561 {
562     int i;
563
564     for (i = 0; (i < nf); i++)
565     {
566         if (!IS_SET(fnm[i]))
567         {
568             set_filenm(&(fnm[i]), fnm[i].fn, FALSE, bReadNode);
569         }
570     }
571 }
572
573 void parse_file_args(int *argc, char *argv[], int nf, t_filenm fnm[],
574                      gmx_bool bKeep, gmx_bool bReadNode)
575 {
576     int       i, j;
577     gmx_bool *bRemove;
578
579     check_opts(nf, fnm);
580
581     for (i = 0; (i < nf); i++)
582     {
583         UN_SET(fnm[i]);
584     }
585
586     if (*argc > 1)
587     {
588         snew(bRemove, (*argc)+1);
589         i = 1;
590         do
591         {
592             for (j = 0; (j < nf); j++)
593             {
594                 if (strcmp(argv[i], fnm[j].opt) == 0)
595                 {
596                     DO_SET(fnm[j]);
597                     bRemove[i] = TRUE;
598                     i++;
599                     /* check if we are out of arguments for this option */
600                     if ((i >= *argc) || (argv[i][0] == '-'))
601                     {
602                         set_filenm(&fnm[j], fnm[j].fn, FALSE, bReadNode);
603                     }
604                     /* sweep up all file arguments for this option */
605                     while ((i < *argc) && (argv[i][0] != '-'))
606                     {
607                         set_filenm(&fnm[j], argv[i], TRUE, bReadNode);
608                         bRemove[i] = TRUE;
609                         i++;
610                         /* only repeat for 'multiple' file options: */
611                         if (!IS_MULT(fnm[j]))
612                         {
613                             break;
614                         }
615                     }
616
617                     break; /* jump out of 'j' loop */
618                 }
619             }
620             /* No file found corresponding to option argv[i] */
621             if (j == nf)
622             {
623                 i++;
624             }
625         }
626         while (i < *argc);
627
628         if (!bKeep)
629         {
630             /* Remove used entries */
631             for (i = j = 0; (i <= *argc); i++)
632             {
633                 if (!bRemove[i])
634                 {
635                     argv[j++] = argv[i];
636                 }
637             }
638             (*argc) = j - 1;
639         }
640         sfree(bRemove);
641     }
642
643     set_filenms(nf, fnm, bReadNode);
644
645 }
646
647 const char *opt2fn(const char *opt, int nfile, const t_filenm fnm[])
648 {
649     int i;
650
651     for (i = 0; (i < nfile); i++)
652     {
653         if (strcmp(opt, fnm[i].opt) == 0)
654         {
655             return fnm[i].fns[0];
656         }
657     }
658
659     fprintf(stderr, "No option %s\n", opt);
660
661     return NULL;
662 }
663
664 const char *opt2fn_master(const char *opt, int nfile, const t_filenm fnm[],
665                           t_commrec *cr)
666 {
667     return SIMMASTER(cr) ? opt2fn(opt, nfile, fnm) : NULL;
668 }
669
670 int opt2fns(char **fns[], const char *opt, int nfile, const t_filenm fnm[])
671 {
672     int i;
673
674     for (i = 0; (i < nfile); i++)
675     {
676         if (strcmp(opt, fnm[i].opt) == 0)
677         {
678             *fns = fnm[i].fns;
679             return fnm[i].nfiles;
680         }
681     }
682
683     fprintf(stderr, "No option %s\n", opt);
684     return 0;
685 }
686
687 const char *ftp2fn(int ftp, int nfile, const t_filenm fnm[])
688 {
689     int i;
690
691     for (i = 0; (i < nfile); i++)
692     {
693         if (ftp == fnm[i].ftp)
694         {
695             return fnm[i].fns[0];
696         }
697     }
698
699     fprintf(stderr, "ftp2fn: No filetype %s\n", deffile[ftp].ext);
700     return NULL;
701 }
702
703 int ftp2fns(char **fns[], int ftp, int nfile, const t_filenm fnm[])
704 {
705     int i;
706
707     for (i = 0; (i < nfile); i++)
708     {
709         if (ftp == fnm[i].ftp)
710         {
711             *fns = fnm[i].fns;
712             return fnm[i].nfiles;
713         }
714     }
715
716     fprintf(stderr, "ftp2fn: No filetype %s\n", deffile[ftp].ext);
717     return 0;
718 }
719
720 gmx_bool ftp2bSet(int ftp, int nfile, const t_filenm fnm[])
721 {
722     int i;
723
724     for (i = 0; (i < nfile); i++)
725     {
726         if (ftp == fnm[i].ftp)
727         {
728             return (gmx_bool) IS_SET(fnm[i]);
729         }
730     }
731
732     fprintf(stderr, "ftp2bSet: No filetype %s\n", deffile[ftp].ext);
733
734     return FALSE;
735 }
736
737 gmx_bool opt2bSet(const char *opt, int nfile, const t_filenm fnm[])
738 {
739     int i;
740
741     for (i = 0; (i < nfile); i++)
742     {
743         if (strcmp(opt, fnm[i].opt) == 0)
744         {
745             return (gmx_bool) IS_SET(fnm[i]);
746         }
747     }
748
749     fprintf(stderr, "No option %s\n", opt);
750
751     return FALSE;
752 }
753
754 const char *opt2fn_null(const char *opt, int nfile, const t_filenm fnm[])
755 {
756     int i;
757
758     for (i = 0; (i < nfile); i++)
759     {
760         if (strcmp(opt, fnm[i].opt) == 0)
761         {
762             if (IS_OPT(fnm[i]) && !IS_SET(fnm[i]))
763             {
764                 return NULL;
765             }
766             else
767             {
768                 return fnm[i].fns[0];
769             }
770         }
771     }
772     fprintf(stderr, "No option %s\n", opt);
773     return NULL;
774 }
775
776 const char *ftp2fn_null(int ftp, int nfile, const t_filenm fnm[])
777 {
778     int i;
779
780     for (i = 0; (i < nfile); i++)
781     {
782         if (ftp == fnm[i].ftp)
783         {
784             if (IS_OPT(fnm[i]) && !IS_SET(fnm[i]))
785             {
786                 return NULL;
787             }
788             else
789             {
790                 return fnm[i].fns[0];
791             }
792         }
793     }
794     fprintf(stderr, "ftp2fn: No filetype %s\n", deffile[ftp].ext);
795     return NULL;
796 }
797
798 gmx_bool is_optional(const t_filenm *fnm)
799 {
800     return ((fnm->flag & ffOPT) == ffOPT);
801 }
802
803 gmx_bool is_output(const t_filenm *fnm)
804 {
805     return ((fnm->flag & ffWRITE) == ffWRITE);
806 }
807
808 gmx_bool is_set(const t_filenm *fnm)
809 {
810     return ((fnm->flag & ffSET) == ffSET);
811 }
812
813 int add_suffix_to_output_names(t_filenm *fnm, int nfile, const char *suffix)
814 {
815     int   i, j, pos;
816     char  buf[STRLEN], newname[STRLEN];
817     char *extpos;
818
819     for (i = 0; i < nfile; i++)
820     {
821         if (is_output(&fnm[i]) && fnm[i].ftp != efCPT)
822         {
823             /* We never use multiple _outputs_, but we might as well check
824                for it, just in case... */
825             for (j = 0; j < fnm[i].nfiles; j++)
826             {
827                 strncpy(buf, fnm[i].fns[j], STRLEN - 1);
828                 extpos  = strrchr(buf, '.');
829                 *extpos = '\0';
830                 sprintf(newname, "%s%s.%s", buf, suffix, extpos + 1);
831                 free(fnm[i].fns[j]);
832                 fnm[i].fns[j] = strdup(newname);
833             }
834         }
835     }
836     return 0;
837 }
838
839 t_filenm *dup_tfn(int nf, const t_filenm tfn[])
840 {
841     int       i, j;
842     t_filenm *ret;
843
844     snew(ret, nf);
845     for (i = 0; i < nf; i++)
846     {
847         ret[i] = tfn[i]; /* just directly copy all non-string fields */
848         if (tfn[i].opt)
849         {
850             ret[i].opt = strdup(tfn[i].opt);
851         }
852         else
853         {
854             ret[i].opt = NULL;
855         }
856
857         if (tfn[i].fn)
858         {
859             ret[i].fn = strdup(tfn[i].fn);
860         }
861         else
862         {
863             ret[i].fn = NULL;
864         }
865
866         if (tfn[i].nfiles > 0)
867         {
868             snew(ret[i].fns, tfn[i].nfiles);
869             for (j = 0; j < tfn[i].nfiles; j++)
870             {
871                 ret[i].fns[j] = strdup(tfn[i].fns[j]);
872             }
873         }
874     }
875     return ret;
876 }
877
878 void done_filenms(int nf, t_filenm fnm[])
879 {
880     int i, j;
881
882     for (i = 0; i < nf; ++i)
883     {
884         for (j = 0; j < fnm[i].nfiles; ++j)
885         {
886             sfree(fnm[i].fns[j]);
887         }
888         sfree(fnm[i].fns);
889         fnm[i].fns = NULL;
890     }
891 }