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,2015,2016,2017 by the GROMACS development team.
7  * Copyright (c) 2018,2019,2020, by the GROMACS development team, led by
8  * Mark Abraham, David van der Spoel, Berk Hess, and Erik Lindahl,
9  * and including many others, as listed in the AUTHORS file in the
10  * top-level source directory and at http://www.gromacs.org.
11  *
12  * GROMACS is free software; you can redistribute it and/or
13  * modify it under the terms of the GNU Lesser General Public License
14  * as published by the Free Software Foundation; either version 2.1
15  * of the License, or (at your option) any later version.
16  *
17  * GROMACS is distributed in the hope that it will be useful,
18  * but WITHOUT ANY WARRANTY; without even the implied warranty of
19  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
20  * Lesser General Public License for more details.
21  *
22  * You should have received a copy of the GNU Lesser General Public
23  * License along with GROMACS; if not, see
24  * http://www.gnu.org/licenses, or write to the Free Software Foundation,
25  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA.
26  *
27  * If you want to redistribute modifications to GROMACS, please
28  * consider that scientific software is very special. Version
29  * control is crucial - bugs must be traceable. We will be happy to
30  * consider code for inclusion in the official distribution, but
31  * derived work must not be called official GROMACS. Details are found
32  * in the README & COPYING files - if they are missing, get the
33  * official version at http://www.gromacs.org.
34  *
35  * To help us fund GROMACS development, we humbly ask that you cite
36  * the research papers on the package. Check out http://www.gromacs.org.
37  */
38 #include "gmxpre.h"
39 
40 #include "filetypes.h"
41 
42 #include <cstring>
43 
44 #include "gromacs/utility/arraysize.h"
45 #include "gromacs/utility/cstringutil.h"
46 
47 enum
48 {
49     eftASC,
50     eftXDR,
51     eftTNG,
52     eftGEN,
53     eftNR
54 };
55 
56 /* To support multiple file types with one general (eg TRX) we have
57  * these arrays.
58  */
59 static const int trxs[] = { efXTC, efTRR, efCPT, efGRO, efG96, efPDB, efTNG };
60 #define NTRXS asize(trxs)
61 
62 static const int trcompressed[] = { efXTC, efTNG };
63 #define NTRCOMPRESSED asize(trcompressed)
64 
65 static const int tros[] = { efXTC, efTRR, efGRO, efG96, efPDB, efTNG };
66 #define NTROS asize(tros)
67 
68 static const int trns[] = { efTRR, efCPT, efTNG };
69 #define NTRNS asize(trns)
70 
71 static const int stos[] = { efGRO, efG96, efPDB, efBRK, efENT, efESP };
72 #define NSTOS asize(stos)
73 
74 static const int stxs[] = { efGRO, efG96, efPDB, efBRK, efENT, efESP, efTPR };
75 #define NSTXS asize(stxs)
76 
77 static const int tpss[] = { efTPR, efGRO, efG96, efPDB, efBRK, efENT };
78 #define NTPSS asize(tpss)
79 
80 typedef struct // NOLINT(clang-analyzer-optin.performance.Padding)
81 {
82     int         ftype;
83     const char* ext;
84     const char* defnm;
85     const char* defopt;
86     const char* descr;
87     int         ntps;
88     const int*  tps;
89 } t_deffile;
90 
91 /* this array should correspond to the enum in filetypes.h */
92 static const t_deffile deffile[efNR] = {
93     { eftASC, ".mdp", "grompp", "-f", "grompp input file with MD parameters" },
94     { eftGEN, ".???", "traj", "-f", "Trajectory", NTRXS, trxs },
95     { eftGEN, ".???", "trajout", "-f", "Trajectory", NTROS, tros },
96     { eftGEN, ".???", "traj", nullptr, "Full precision trajectory", NTRNS, trns },
97     { eftXDR, ".trr", "traj", nullptr, "Trajectory in portable xdr format" },
98     { eftGEN, ".???", "traj_comp", nullptr,
99       "Compressed trajectory (tng format or portable xdr format)", NTRCOMPRESSED, trcompressed },
100     { eftXDR, ".xtc", "traj", nullptr, "Compressed trajectory (portable xdr format): xtc" },
101     { eftTNG, ".tng", "traj", nullptr, "Trajectory file (tng format)" },
102     { eftXDR, ".edr", "ener", nullptr, "Energy file" },
103     { eftGEN, ".???", "conf", "-c", "Structure file", NSTXS, stxs },
104     { eftGEN, ".???", "out", "-o", "Structure file", NSTOS, stos },
105     { eftASC, ".gro", "conf", "-c", "Coordinate file in Gromos-87 format" },
106     { eftASC, ".g96", "conf", "-c", "Coordinate file in Gromos-96 format" },
107     { eftASC, ".pdb", "eiwit", "-f", "Protein data bank file" },
108     { eftASC, ".brk", "eiwit", "-f", "Brookhaven data bank file" },
109     { eftASC, ".ent", "eiwit", "-f", "Entry in the protein date bank" },
110     { eftASC, ".esp", "conf", "-f", "Coordinate file in Espresso format" },
111     { eftASC, ".pqr", "state", "-o", "Coordinate file for MEAD" },
112     { eftXDR, ".cpt", "state", "-cp", "Checkpoint file" },
113     { eftASC, ".log", "run", "-l", "Log file" },
114     { eftASC, ".xvg", "graph", "-o", "xvgr/xmgr file" },
115     { eftASC, ".out", "hello", "-o", "Generic output file" },
116     {
117             eftASC,
118             ".ndx",
119             "index",
120             "-n",
121             "Index file",
122     },
123     { eftASC, ".top", "topol", "-p", "Topology file" },
124     { eftASC, ".itp", "topinc", nullptr, "Include file for topology" },
125     { eftGEN, ".???", "topol", "-s", "Structure+mass(db)", NTPSS, tpss },
126     { eftXDR, ".tpr", "topol", "-s", "Portable xdr run input file" },
127     { eftASC, ".tex", "doc", "-o", "LaTeX file" },
128     { eftASC, ".rtp", "residue", nullptr, "Residue Type file used by pdb2gmx" },
129     { eftASC, ".atp", "atomtp", nullptr, "Atomtype file used by pdb2gmx" },
130     { eftASC, ".hdb", "polar", nullptr, "Hydrogen data base" },
131     { eftASC, ".dat", "nnnice", nullptr, "Generic data file" },
132     { eftASC, ".dlg", "user", nullptr, "Dialog Box data for ngmx" },
133     { eftASC, ".map", "ss", nullptr, "File that maps matrix data to colors" },
134     { eftASC, ".eps", "plot", nullptr, "Encapsulated PostScript (tm) file" },
135     { eftASC, ".mat", "ss", nullptr, "Matrix Data file" },
136     { eftASC, ".m2p", "ps", nullptr, "Input file for mat2ps" },
137     { eftXDR, ".mtx", "hessian", "-m", "Hessian matrix" },
138     { eftASC, ".edi", "sam", nullptr, "ED sampling input" },
139     { eftASC, ".cub", "pot", nullptr, "Gaussian cube file" },
140     { eftASC, ".xpm", "root", nullptr, "X PixMap compatible matrix file" },
141     { eftASC, "", "rundir", nullptr, "Run directory" },
142     { eftASC, ".csv", "bench", nullptr, "CSV data file" }
143 };
144 
ftp2ext(int ftp)145 const char* ftp2ext(int ftp)
146 {
147     if ((0 <= ftp) && (ftp < efNR))
148     {
149         return deffile[ftp].ext[0] != '\0' ? deffile[ftp].ext + 1 : "";
150     }
151     else
152     {
153         return "unknown";
154     }
155 }
156 
ftp2ext_generic(int ftp)157 const char* ftp2ext_generic(int ftp)
158 {
159     if ((0 <= ftp) && (ftp < efNR))
160     {
161         switch (ftp)
162         {
163             case efTRX: return "trx";
164             case efTRN: return "trn";
165             case efSTO: return "sto";
166             case efSTX: return "stx";
167             case efTPS: return "tps";
168             default: return ftp2ext(ftp);
169         }
170     }
171     else
172     {
173         return "unknown";
174     }
175 }
176 
ftp2ext_with_dot(int ftp)177 const char* ftp2ext_with_dot(int ftp)
178 {
179     if ((0 <= ftp) && (ftp < efNR))
180     {
181         return deffile[ftp].ext;
182     }
183     else
184     {
185         return "unknown";
186     }
187 }
188 
ftp2generic_count(int ftp)189 int ftp2generic_count(int ftp)
190 {
191     if ((0 <= ftp) && (ftp < efNR))
192     {
193         return deffile[ftp].ntps;
194     }
195     else
196     {
197         return 0;
198     }
199 }
200 
ftp2generic_list(int ftp)201 const int* ftp2generic_list(int ftp)
202 {
203     if ((0 <= ftp) && (ftp < efNR))
204     {
205         return deffile[ftp].tps;
206     }
207     else
208     {
209         return nullptr;
210     }
211 }
212 
ftp2desc(int ftp)213 const char* ftp2desc(int ftp)
214 {
215     if ((0 <= ftp) && (ftp < efNR))
216     {
217         return deffile[ftp].descr;
218     }
219     else
220     {
221         return "unknown filetype";
222     }
223 }
224 
ftp_is_text(int ftp)225 gmx_bool ftp_is_text(int ftp)
226 {
227     if ((ftp >= 0) && (ftp < efNR))
228     {
229         return deffile[ftp].ftype == eftASC;
230     }
231     return FALSE;
232 }
233 
ftp_is_xdr(int ftp)234 gmx_bool ftp_is_xdr(int ftp)
235 {
236     if ((ftp >= 0) && (ftp < efNR))
237     {
238         return deffile[ftp].ftype == eftXDR;
239     }
240     return FALSE;
241 }
242 
ftp2defnm(int ftp)243 const char* ftp2defnm(int ftp)
244 {
245     if ((0 <= ftp) && (ftp < efNR))
246     {
247         return deffile[ftp].defnm;
248     }
249     else
250     {
251         return nullptr;
252     }
253 }
254 
ftp2defopt(int ftp)255 const char* ftp2defopt(int ftp)
256 {
257     if ((0 <= ftp) && (ftp < efNR))
258     {
259         return deffile[ftp].defopt;
260     }
261     else
262     {
263         return nullptr;
264     }
265 }
266 
fn2ftp(const char * fn)267 int fn2ftp(const char* fn)
268 {
269     int         i, len;
270     const char* feptr;
271     const char* eptr;
272 
273     if (!fn)
274     {
275         return efNR;
276     }
277 
278     len = std::strlen(fn);
279     if ((len >= 4) && (fn[len - 4] == '.'))
280     {
281         feptr = &(fn[len - 4]);
282     }
283     else
284     {
285         return efNR;
286     }
287 
288     for (i = 0; (i < efNR); i++)
289     {
290         if ((eptr = deffile[i].ext) != nullptr)
291         {
292             if (gmx_strcasecmp(feptr, eptr) == 0)
293             {
294                 break;
295             }
296         }
297     }
298 
299     return i;
300 }
301