380f91248aaa8ded1e4c47c1a1d85b0b99d24688
[alexxy/gromacs.git] / src / gromacs / fileio / filetypes.cpp
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,2018,2019, 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 "filetypes.h"
40
41 #include <cstring>
42
43 #include "gromacs/utility/arraysize.h"
44 #include "gromacs/utility/cstringutil.h"
45
46 enum
47 {
48     eftASC,
49     eftXDR,
50     eftTNG,
51     eftGEN,
52     eftNR
53 };
54
55 /* To support multiple file types with one general (eg TRX) we have
56  * these arrays.
57  */
58 static const int trxs[] = { efXTC, efTRR, efCPT, efGRO, efG96, efPDB, efTNG };
59 #define NTRXS asize(trxs)
60
61 static const int trcompressed[] = { efXTC, efTNG };
62 #define NTRCOMPRESSED asize(trcompressed)
63
64 static const int tros[] = { efXTC, efTRR, efGRO, efG96, efPDB, efTNG };
65 #define NTROS asize(tros)
66
67 static const int trns[] = { efTRR, efCPT, efTNG };
68 #define NTRNS asize(trns)
69
70 static const int stos[] = { efGRO, efG96, efPDB, efBRK, efENT, efESP };
71 #define NSTOS asize(stos)
72
73 static const int stxs[] = { efGRO, efG96, efPDB, efBRK, efENT, efESP, efTPR };
74 #define NSTXS asize(stxs)
75
76 static const int tpss[] = { efTPR, efGRO, efG96, efPDB, efBRK, efENT };
77 #define NTPSS asize(tpss)
78
79 typedef struct // NOLINT(clang-analyzer-optin.performance.Padding)
80 {
81     int         ftype;
82     const char* ext;
83     const char* defnm;
84     const char* defopt;
85     const char* descr;
86     int         ntps;
87     const int*  tps;
88 } t_deffile;
89
90 /* this array should correspond to the enum in filetypes.h */
91 static const t_deffile deffile[efNR] = {
92     { eftASC, ".mdp", "grompp", "-f", "grompp input file with MD parameters" },
93     { eftGEN, ".???", "traj", "-f", "Trajectory", NTRXS, trxs },
94     { eftGEN, ".???", "trajout", "-f", "Trajectory", NTROS, tros },
95     { eftGEN, ".???", "traj", nullptr, "Full precision trajectory", NTRNS, trns },
96     { eftXDR, ".trr", "traj", nullptr, "Trajectory in portable xdr format" },
97     { eftGEN, ".???", "traj_comp", nullptr,
98       "Compressed trajectory (tng format or portable xdr format)", NTRCOMPRESSED, trcompressed },
99     { eftXDR, ".xtc", "traj", nullptr, "Compressed trajectory (portable xdr format): xtc" },
100     { eftTNG, ".tng", "traj", nullptr, "Trajectory file (tng format)" },
101     { eftXDR, ".edr", "ener", nullptr, "Energy file" },
102     { eftGEN, ".???", "conf", "-c", "Structure file", NSTXS, stxs },
103     { eftGEN, ".???", "out", "-o", "Structure file", NSTOS, stos },
104     { eftASC, ".gro", "conf", "-c", "Coordinate file in Gromos-87 format" },
105     { eftASC, ".g96", "conf", "-c", "Coordinate file in Gromos-96 format" },
106     { eftASC, ".pdb", "eiwit", "-f", "Protein data bank file" },
107     { eftASC, ".brk", "eiwit", "-f", "Brookhaven data bank file" },
108     { eftASC, ".ent", "eiwit", "-f", "Entry in the protein date bank" },
109     { eftASC, ".esp", "conf", "-f", "Coordinate file in Espresso format" },
110     { eftASC, ".pqr", "state", "-o", "Coordinate file for MEAD" },
111     { eftXDR, ".cpt", "state", "-cp", "Checkpoint file" },
112     { eftASC, ".log", "run", "-l", "Log file" },
113     { eftASC, ".xvg", "graph", "-o", "xvgr/xmgr file" },
114     { eftASC, ".out", "hello", "-o", "Generic output file" },
115     {
116             eftASC,
117             ".ndx",
118             "index",
119             "-n",
120             "Index file",
121     },
122     { eftASC, ".top", "topol", "-p", "Topology file" },
123     { eftASC, ".itp", "topinc", nullptr, "Include file for topology" },
124     { eftGEN, ".???", "topol", "-s", "Structure+mass(db)", NTPSS, tpss },
125     { eftXDR, ".tpr", "topol", "-s", "Portable xdr run input file" },
126     { eftASC, ".tex", "doc", "-o", "LaTeX file" },
127     { eftASC, ".rtp", "residue", nullptr, "Residue Type file used by pdb2gmx" },
128     { eftASC, ".atp", "atomtp", nullptr, "Atomtype file used by pdb2gmx" },
129     { eftASC, ".hdb", "polar", nullptr, "Hydrogen data base" },
130     { eftASC, ".dat", "nnnice", nullptr, "Generic data file" },
131     { eftASC, ".dlg", "user", nullptr, "Dialog Box data for ngmx" },
132     { eftASC, ".map", "ss", nullptr, "File that maps matrix data to colors" },
133     { eftASC, ".eps", "plot", nullptr, "Encapsulated PostScript (tm) file" },
134     { eftASC, ".mat", "ss", nullptr, "Matrix Data file" },
135     { eftASC, ".m2p", "ps", nullptr, "Input file for mat2ps" },
136     { eftXDR, ".mtx", "hessian", "-m", "Hessian matrix" },
137     { eftASC, ".edi", "sam", nullptr, "ED sampling input" },
138     { eftASC, ".cub", "pot", nullptr, "Gaussian cube file" },
139     { eftASC, ".xpm", "root", nullptr, "X PixMap compatible matrix file" },
140     { eftASC, "", "rundir", nullptr, "Run directory" }
141 };
142
143 const char* ftp2ext(int ftp)
144 {
145     if ((0 <= ftp) && (ftp < efNR))
146     {
147         return deffile[ftp].ext[0] != '\0' ? deffile[ftp].ext + 1 : "";
148     }
149     else
150     {
151         return "unknown";
152     }
153 }
154
155 const char* ftp2ext_generic(int ftp)
156 {
157     if ((0 <= ftp) && (ftp < efNR))
158     {
159         switch (ftp)
160         {
161             case efTRX: return "trx";
162             case efTRN: return "trn";
163             case efSTO: return "sto";
164             case efSTX: return "stx";
165             case efTPS: return "tps";
166             default: return ftp2ext(ftp);
167         }
168     }
169     else
170     {
171         return "unknown";
172     }
173 }
174
175 const char* ftp2ext_with_dot(int ftp)
176 {
177     if ((0 <= ftp) && (ftp < efNR))
178     {
179         return deffile[ftp].ext;
180     }
181     else
182     {
183         return "unknown";
184     }
185 }
186
187 int ftp2generic_count(int ftp)
188 {
189     if ((0 <= ftp) && (ftp < efNR))
190     {
191         return deffile[ftp].ntps;
192     }
193     else
194     {
195         return 0;
196     }
197 }
198
199 const int* ftp2generic_list(int ftp)
200 {
201     if ((0 <= ftp) && (ftp < efNR))
202     {
203         return deffile[ftp].tps;
204     }
205     else
206     {
207         return nullptr;
208     }
209 }
210
211 const char* ftp2desc(int ftp)
212 {
213     if ((0 <= ftp) && (ftp < efNR))
214     {
215         return deffile[ftp].descr;
216     }
217     else
218     {
219         return "unknown filetype";
220     }
221 }
222
223 gmx_bool ftp_is_text(int ftp)
224 {
225     if ((ftp >= 0) && (ftp < efNR))
226     {
227         return deffile[ftp].ftype == eftASC;
228     }
229     return FALSE;
230 }
231
232 gmx_bool ftp_is_xdr(int ftp)
233 {
234     if ((ftp >= 0) && (ftp < efNR))
235     {
236         return deffile[ftp].ftype == eftXDR;
237     }
238     return FALSE;
239 }
240
241 const char* ftp2defnm(int ftp)
242 {
243     if ((0 <= ftp) && (ftp < efNR))
244     {
245         return deffile[ftp].defnm;
246     }
247     else
248     {
249         return nullptr;
250     }
251 }
252
253 const char* ftp2defopt(int ftp)
254 {
255     if ((0 <= ftp) && (ftp < efNR))
256     {
257         return deffile[ftp].defopt;
258     }
259     else
260     {
261         return nullptr;
262     }
263 }
264
265 int fn2ftp(const char* fn)
266 {
267     int         i, len;
268     const char* feptr;
269     const char* eptr;
270
271     if (!fn)
272     {
273         return efNR;
274     }
275
276     len = std::strlen(fn);
277     if ((len >= 4) && (fn[len - 4] == '.'))
278     {
279         feptr = &(fn[len - 4]);
280     }
281     else
282     {
283         return efNR;
284     }
285
286     for (i = 0; (i < efNR); i++)
287     {
288         if ((eptr = deffile[i].ext) != nullptr)
289         {
290             if (gmx_strcasecmp(feptr, eptr) == 0)
291             {
292                 break;
293             }
294         }
295     }
296
297     return i;
298 }