Merge release-5-0 into master
[alexxy/gromacs.git] / src / gromacs / gmxpreprocess / fflibutil.cpp
1 /*
2  * This file is part of the GROMACS molecular simulation package.
3  *
4  * Copyright (c) 2010,2012,2013,2014, by the GROMACS development team, led by
5  * Mark Abraham, David van der Spoel, Berk Hess, and Erik Lindahl,
6  * and including many others, as listed in the AUTHORS file in the
7  * top-level source directory and at http://www.gromacs.org.
8  *
9  * GROMACS is free software; you can redistribute it and/or
10  * modify it under the terms of the GNU Lesser General Public License
11  * as published by the Free Software Foundation; either version 2.1
12  * of the License, or (at your option) any later version.
13  *
14  * GROMACS is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
17  * Lesser General Public License for more details.
18  *
19  * You should have received a copy of the GNU Lesser General Public
20  * License along with GROMACS; if not, see
21  * http://www.gnu.org/licenses, or write to the Free Software Foundation,
22  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA.
23  *
24  * If you want to redistribute modifications to GROMACS, please
25  * consider that scientific software is very special. Version
26  * control is crucial - bugs must be traceable. We will be happy to
27  * consider code for inclusion in the official distribution, but
28  * derived work must not be called official GROMACS. Details are found
29  * in the README & COPYING files - if they are missing, get the
30  * official version at http://www.gromacs.org.
31  *
32  * To help us fund GROMACS development, we humbly ask that you cite
33  * the research papers on the package. Check out http://www.gromacs.org.
34  */
35 #include "gmxpre.h"
36
37 #include "fflibutil.h"
38
39 #include <stdio.h>
40 #include <stdlib.h>
41 #include <string.h>
42
43 #include "gromacs/legacyheaders/network.h"
44 #include "gromacs/utility/cstringutil.h"
45 #include "gromacs/utility/exceptions.h"
46 #include "gromacs/utility/fatalerror.h"
47 #include "gromacs/utility/futil.h"
48 #include "gromacs/utility/path.h"
49 #include "gromacs/utility/programcontext.h"
50 #include "gromacs/utility/smalloc.h"
51
52 const char *fflib_forcefield_dir_ext()
53 {
54     return ".ff";
55 }
56
57 const char *fflib_forcefield_itp()
58 {
59     return "forcefield.itp";
60 }
61
62 const char *fflib_forcefield_doc()
63 {
64     return "forcefield.doc";
65 }
66
67 void fflib_filename_base(const char *filename, char *filebase, int maxlen)
68 {
69     const char *cptr;
70     char       *ptr;
71
72     cptr = strrchr(filename, DIR_SEPARATOR);
73     if (cptr != NULL)
74     {
75         /* Skip the separator */
76         cptr += 1;
77     }
78     else
79     {
80         cptr = filename;
81     }
82     if (strlen(filename) >= (size_t)maxlen)
83     {
84         gmx_fatal(FARGS, "filename is longer (%d) than maxlen (%d)",
85                   strlen(filename), maxlen);
86     }
87     strcpy(filebase, cptr);
88     /* Remove the extension */
89     ptr = strrchr(filebase, '.');
90     if (ptr != NULL)
91     {
92         ptr[0] = '\0';
93     }
94 }
95
96 static void sort_filenames(int n, char **name, char **name2)
97 {
98     /* Slow sort, but we usually have tens of names */
99     int   i, j, f;
100     char *tmp;
101
102     for (i = 0; i < n-1; i++)
103     {
104         f = i;
105         for (j = i+1; j < n; j++)
106         {
107             if (strcmp(name[j], name[f]) < 0)
108             {
109                 f = j;
110             }
111         }
112         if (f > i)
113         {
114             tmp     = name[i];
115             name[i] = name[f];
116             name[f] = tmp;
117             if (name2 != NULL)
118             {
119                 tmp      = name2[i];
120                 name2[i] = name2[f];
121                 name2[f] = tmp;
122             }
123         }
124     }
125 }
126
127 static int low_fflib_search_file_end(const char *ffdir,
128                                      gmx_bool    bAddCWD,
129                                      const char *file_end,
130                                      gmx_bool    bFatalError,
131                                      char     ***filenames,
132                                      char     ***filenames_short)
133 {
134     char **fns = NULL, **fns_short = NULL;
135     int    n   = 0;
136     try
137     {
138         std::vector<std::string> libPaths;
139         bool                     bEnvIsSet = false;
140
141         if (ffdir != NULL)
142         {
143             /* Search ffdir in current dir and library dirs */
144             libPaths.push_back(gmxlibfn(ffdir));
145         }
146         else
147         {
148             /* GMXLIB can be a path now */
149             if (bAddCWD)
150             {
151                 libPaths.push_back(".");
152             }
153             const char *lib = getenv("GMXLIB");
154             if (lib != NULL)
155             {
156                 bEnvIsSet = true;
157                 gmx::Path::splitPathEnvironment(lib, &libPaths);
158             }
159             else
160             {
161                 libPaths.push_back(gmx::getProgramContext().defaultLibraryDataPath());
162             }
163         }
164
165         const int len_fe = strlen(file_end);
166
167         std::vector<std::string>::const_iterator i;
168         for (i = libPaths.begin(); i != libPaths.end(); ++i)
169         {
170             const char      *dir = i->c_str();
171             gmx_directory_t  dirhandle;
172             const int        rc  = gmx_directory_open(&dirhandle, dir);
173             if (rc == 0)
174             {
175                 char nextname[STRLEN];
176                 int  n_thisdir = 0;
177                 while (gmx_directory_nextfile(dirhandle, nextname, STRLEN-1) == 0)
178                 {
179                     nextname[STRLEN-1] = 0;
180                     if (debug)
181                     {
182                         fprintf(debug, "dir '%s' %d file '%s'\n",
183                                 dir, n_thisdir, nextname);
184                     }
185                     const int len_name = strlen(nextname);
186                     /* What about case sensitivity? */
187                     if (len_name >= len_fe &&
188                         strcmp(nextname+len_name-len_fe, file_end) == 0)
189                     {
190                         char fn_dir[GMX_PATH_MAX];
191                         /* We have a match */
192                         srenew(fns, n+1);
193                         sprintf(fn_dir, "%s%c%s", dir, DIR_SEPARATOR, nextname);
194
195                         /* Copy the file name, possibly including the path. */
196                         fns[n] = gmx_strdup(fn_dir);
197
198                         if (ffdir == NULL)
199                         {
200                             /* We are searching in a path.
201                              * Use the relative path when we use share/top
202                              * from the installation.
203                              * Add the full path when we use the current
204                              * working directory of GMXLIB.
205                              */
206                             srenew(fns_short, n+1);
207                             if (strcmp(dir, ".") == 0 || bEnvIsSet)
208                             {
209                                 fns_short[n] = gmx_strdup(fn_dir);
210                             }
211                             else
212                             {
213                                 fns_short[n] = gmx_strdup(nextname);
214                             }
215                         }
216                         n++;
217                         n_thisdir++;
218                     }
219                 }
220                 gmx_directory_close(dirhandle);
221
222                 sort_filenames(n_thisdir,
223                                fns+n-n_thisdir,
224                                fns_short == NULL ? NULL : fns_short+n-n_thisdir);
225             }
226         }
227     }
228     GMX_CATCH_ALL_AND_EXIT_WITH_FATAL_ERROR;
229
230     if (n == 0 && bFatalError)
231     {
232         if (ffdir != NULL)
233         {
234             gmx_fatal(FARGS, "Could not find any files ending on '%s' in the force field directory '%s'", file_end, ffdir);
235         }
236         else
237         {
238             gmx_fatal(FARGS, "Could not find any files ending on '%s' in the current directory or the GROMACS library search path", file_end);
239         }
240     }
241
242     *filenames = fns;
243     if (ffdir == NULL)
244     {
245         *filenames_short = fns_short;
246     }
247
248     return n;
249 }
250
251 int fflib_search_file_end(const char *ffdir,
252                           const char *file_end,
253                           gmx_bool    bFatalError,
254                           char     ***filenames)
255 {
256     return low_fflib_search_file_end(ffdir, FALSE, file_end, bFatalError,
257                                      filenames, NULL);
258 }
259
260 int fflib_search_file_in_dirend(const char *filename, const char *dirend,
261                                 char ***dirnames)
262 {
263     int             nf, i;
264     char          **f, **f_short;
265     int             n;
266     char          **dns;
267     gmx_directory_t dirhandle;
268     char            nextname[STRLEN];
269     int             rc;
270
271     /* Find all files (not only dir's) ending on dirend */
272     nf = low_fflib_search_file_end(NULL, TRUE, dirend, FALSE, &f, &f_short);
273
274     n   = 0;
275     dns = NULL;
276     for (i = 0; i < nf; i++)
277     {
278         rc = gmx_directory_open(&dirhandle, f[i]);
279
280         if (rc == 0)
281         {
282             while (gmx_directory_nextfile(dirhandle, nextname, STRLEN-1) == 0)
283             {
284                 nextname[STRLEN-1] = 0;
285                 if (strcmp(nextname, filename) == 0)
286                 {
287                     /* We have a match */
288                     srenew(dns, n+1);
289                     dns[n] = gmx_strdup(f_short[i]);
290                     n++;
291                 }
292             }
293             gmx_directory_close(dirhandle);
294         }
295         sfree(f[i]);
296         sfree(f_short[i]);
297     }
298     sfree(f);
299     sfree(f_short);
300
301     *dirnames = dns;
302
303     return n;
304 }
305
306 gmx_bool fflib_fexist(const char *file)
307 {
308     char *file_fullpath;
309
310     file_fullpath = low_gmxlibfn(file, TRUE, FALSE);
311
312     if (file_fullpath == NULL)
313     {
314         return FALSE;
315     }
316     else
317     {
318         sfree(file_fullpath);
319
320         return TRUE;
321     }
322 }
323
324
325 FILE *fflib_open(const char *file)
326 {
327     char *file_fullpath;
328     FILE *fp;
329
330     file_fullpath = gmxlibfn(file);
331     fprintf(stderr, "Opening force field file %s\n", file_fullpath);
332     fp = gmx_ffopen(file_fullpath, "r");
333     sfree(file_fullpath);
334
335     return fp;
336 }