2 * This file is part of the GROMACS molecular simulation package.
4 * Copyright (c) 1991-2000, University of Groningen, The Netherlands.
5 * Copyright (c) 2001-2004, The GROMACS development team.
6 * Copyright (c) 2013,2014,2015,2016,2017, 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.
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.
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.
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.
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.
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.
44 #include "gromacs/fileio/filetypes.h"
45 #include "gromacs/utility/basedefinitions.h"
46 #include "gromacs/utility/cstringutil.h"
47 #include "gromacs/utility/smalloc.h"
50 #define IS_SET(fn) ((fn.flag &ffSET) != 0)
51 #define IS_OPT(fn) ((fn.flag &ffOPT) != 0)
53 const t_filenm
*getFilenm(const char *opt
, int nfile
, const t_filenm fnm
[])
57 for (i
= 0; (i
< nfile
); i
++)
59 if (strcmp(opt
, fnm
[i
].opt
) == 0)
68 const char *opt2fn(const char *opt
, int nfile
, const t_filenm fnm
[])
72 for (i
= 0; (i
< nfile
); i
++)
74 if (std::strcmp(opt
, fnm
[i
].opt
) == 0)
80 fprintf(stderr
, "No option %s\n", opt
);
85 int opt2fns(char **fns
[], const char *opt
, int nfile
, const t_filenm fnm
[])
89 for (i
= 0; (i
< nfile
); i
++)
91 if (strcmp(opt
, fnm
[i
].opt
) == 0)
98 fprintf(stderr
, "No option %s\n", opt
);
102 const char *ftp2fn(int ftp
, int nfile
, const t_filenm fnm
[])
106 for (i
= 0; (i
< nfile
); i
++)
108 if (ftp
== fnm
[i
].ftp
)
110 return fnm
[i
].fns
[0];
114 fprintf(stderr
, "ftp2fn: No filetype %s\n", ftp2ext_with_dot(ftp
));
118 int ftp2fns(char **fns
[], int ftp
, int nfile
, const t_filenm fnm
[])
122 for (i
= 0; (i
< nfile
); i
++)
124 if (ftp
== fnm
[i
].ftp
)
127 return fnm
[i
].nfiles
;
131 fprintf(stderr
, "ftp2fn: No filetype %s\n", ftp2ext_with_dot(ftp
));
135 gmx_bool
ftp2bSet(int ftp
, int nfile
, const t_filenm fnm
[])
139 for (i
= 0; (i
< nfile
); i
++)
141 if (ftp
== fnm
[i
].ftp
)
143 return (gmx_bool
) IS_SET(fnm
[i
]);
147 fprintf(stderr
, "ftp2fn: No filetype %s\n", ftp2ext_with_dot(ftp
));
152 gmx_bool
opt2bSet(const char *opt
, int nfile
, const t_filenm fnm
[])
156 for (i
= 0; (i
< nfile
); i
++)
158 if (std::strcmp(opt
, fnm
[i
].opt
) == 0)
160 return (gmx_bool
) IS_SET(fnm
[i
]);
164 fprintf(stderr
, "No option %s\n", opt
);
169 const char *opt2fn_null(const char *opt
, int nfile
, const t_filenm fnm
[])
173 for (i
= 0; (i
< nfile
); i
++)
175 if (std::strcmp(opt
, fnm
[i
].opt
) == 0)
177 if (IS_OPT(fnm
[i
]) && !IS_SET(fnm
[i
]))
183 return fnm
[i
].fns
[0];
187 fprintf(stderr
, "No option %s\n", opt
);
191 const char *ftp2fn_null(int ftp
, int nfile
, const t_filenm fnm
[])
195 for (i
= 0; (i
< nfile
); i
++)
197 if (ftp
== fnm
[i
].ftp
)
199 if (IS_OPT(fnm
[i
]) && !IS_SET(fnm
[i
]))
205 return fnm
[i
].fns
[0];
209 fprintf(stderr
, "ftp2fn: No filetype %s\n", ftp2ext_with_dot(ftp
));
213 gmx_bool
is_optional(const t_filenm
*fnm
)
215 return ((fnm
->flag
& ffOPT
) == ffOPT
);
218 gmx_bool
is_output(const t_filenm
*fnm
)
220 return ((fnm
->flag
& ffWRITE
) == ffWRITE
);
223 gmx_bool
is_set(const t_filenm
*fnm
)
225 return ((fnm
->flag
& ffSET
) == ffSET
);
228 int add_suffix_to_output_names(t_filenm
*fnm
, int nfile
, const char *suffix
)
231 char buf
[STRLEN
], newname
[STRLEN
];
234 for (i
= 0; i
< nfile
; i
++)
236 if (is_output(&fnm
[i
]) && fnm
[i
].ftp
!= efCPT
)
238 /* We never use multiple _outputs_, but we might as well check
239 for it, just in case... */
240 for (j
= 0; j
< fnm
[i
].nfiles
; j
++)
242 std::strncpy(buf
, fnm
[i
].fns
[j
], STRLEN
- 1);
243 extpos
= strrchr(buf
, '.');
245 sprintf(newname
, "%s%s.%s", buf
, suffix
, extpos
+ 1);
246 sfree(fnm
[i
].fns
[j
]);
247 fnm
[i
].fns
[j
] = gmx_strdup(newname
);
254 t_filenm
*dup_tfn(int nf
, const t_filenm tfn
[])
260 for (i
= 0; i
< nf
; i
++)
262 ret
[i
] = tfn
[i
]; /* just directly copy all non-string fields */
265 ret
[i
].opt
= gmx_strdup(tfn
[i
].opt
);
269 ret
[i
].opt
= nullptr;
274 ret
[i
].fn
= gmx_strdup(tfn
[i
].fn
);
281 if (tfn
[i
].nfiles
> 0)
283 snew(ret
[i
].fns
, tfn
[i
].nfiles
);
284 for (j
= 0; j
< tfn
[i
].nfiles
; j
++)
286 ret
[i
].fns
[j
] = gmx_strdup(tfn
[i
].fns
[j
]);
293 void done_filenms(int nf
, t_filenm fnm
[])
297 for (i
= 0; i
< nf
; ++i
)
299 for (j
= 0; j
< fnm
[i
].nfiles
; ++j
)
301 sfree(fnm
[i
].fns
[j
]);
304 fnm
[i
].fns
= nullptr;