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