b47aa18b7c86c5acc632c6561c94a64c1186b4f5
[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 <fcntl.h>
44 #include <sys/stat.h>
45 #include <sys/types.h>
46
47 #include "config.h"
48
49 #ifdef HAVE_UNISTD_H
50 #include <unistd.h>
51 #endif
52
53 #include "gromacs/legacyheaders/network.h"
54 #include "gromacs/utility/cstringutil.h"
55 #include "gromacs/utility/exceptions.h"
56 #include "gromacs/utility/fatalerror.h"
57 #include "gromacs/utility/futil.h"
58 #include "gromacs/utility/path.h"
59 #include "gromacs/utility/programcontext.h"
60 #include "gromacs/utility/smalloc.h"
61
62 const char *fflib_forcefield_dir_ext()
63 {
64     return ".ff";
65 }
66
67 const char *fflib_forcefield_itp()
68 {
69     return "forcefield.itp";
70 }
71
72 const char *fflib_forcefield_doc()
73 {
74     return "forcefield.doc";
75 }
76
77 void fflib_filename_base(const char *filename, char *filebase, int maxlen)
78 {
79     const char *cptr;
80     char       *ptr;
81
82     cptr = strrchr(filename, DIR_SEPARATOR);
83     if (cptr != NULL)
84     {
85         /* Skip the separator */
86         cptr += 1;
87     }
88     else
89     {
90         cptr = filename;
91     }
92     if (strlen(filename) >= (size_t)maxlen)
93     {
94         gmx_fatal(FARGS, "filename is longer (%d) than maxlen (%d)",
95                   strlen(filename), maxlen);
96     }
97     strcpy(filebase, cptr);
98     /* Remove the extension */
99     ptr = strrchr(filebase, '.');
100     if (ptr != NULL)
101     {
102         ptr[0] = '\0';
103     }
104 }
105
106 static void sort_filenames(int n, char **name, char **name2)
107 {
108     /* Slow sort, but we usually have tens of names */
109     int   i, j, f;
110     char *tmp;
111
112     for (i = 0; i < n-1; i++)
113     {
114         f = i;
115         for (j = i+1; j < n; j++)
116         {
117             if (strcmp(name[j], name[f]) < 0)
118             {
119                 f = j;
120             }
121         }
122         if (f > i)
123         {
124             tmp     = name[i];
125             name[i] = name[f];
126             name[f] = tmp;
127             if (name2 != NULL)
128             {
129                 tmp      = name2[i];
130                 name2[i] = name2[f];
131                 name2[f] = tmp;
132             }
133         }
134     }
135 }
136
137 static int low_fflib_search_file_end(const char *ffdir,
138                                      gmx_bool    bAddCWD,
139                                      const char *file_end,
140                                      gmx_bool    bFatalError,
141                                      char     ***filenames,
142                                      char     ***filenames_short)
143 {
144     char **fns = NULL, **fns_short = NULL;
145     int    n   = 0;
146     try
147     {
148         std::vector<std::string> libPaths;
149         bool                     bEnvIsSet = false;
150
151         if (ffdir != NULL)
152         {
153             /* Search ffdir in current dir and library dirs */
154             libPaths.push_back(gmxlibfn(ffdir));
155         }
156         else
157         {
158             /* GMXLIB can be a path now */
159             if (bAddCWD)
160             {
161                 libPaths.push_back(".");
162             }
163             const char *lib = getenv("GMXLIB");
164             if (lib != NULL)
165             {
166                 bEnvIsSet = true;
167                 gmx::Path::splitPathEnvironment(lib, &libPaths);
168             }
169             else
170             {
171                 libPaths.push_back(gmx::getProgramContext().defaultLibraryDataPath());
172             }
173         }
174
175         const int len_fe = strlen(file_end);
176
177         std::vector<std::string>::const_iterator i;
178         for (i = libPaths.begin(); i != libPaths.end(); ++i)
179         {
180             const char      *dir = i->c_str();
181             gmx_directory_t  dirhandle;
182             const int        rc  = gmx_directory_open(&dirhandle, dir);
183             if (rc == 0)
184             {
185                 char nextname[STRLEN];
186                 int  n_thisdir = 0;
187                 while (gmx_directory_nextfile(dirhandle, nextname, STRLEN-1) == 0)
188                 {
189                     nextname[STRLEN-1] = 0;
190                     if (debug)
191                     {
192                         fprintf(debug, "dir '%s' %d file '%s'\n",
193                                 dir, n_thisdir, nextname);
194                     }
195                     const int len_name = strlen(nextname);
196                     /* What about case sensitivity? */
197                     if (len_name >= len_fe &&
198                         strcmp(nextname+len_name-len_fe, file_end) == 0)
199                     {
200                         char fn_dir[GMX_PATH_MAX];
201                         /* We have a match */
202                         srenew(fns, n+1);
203                         sprintf(fn_dir, "%s%c%s", dir, DIR_SEPARATOR, nextname);
204
205                         /* Copy the file name, possibly including the path. */
206                         fns[n] = gmx_strdup(fn_dir);
207
208                         if (ffdir == NULL)
209                         {
210                             /* We are searching in a path.
211                              * Use the relative path when we use share/top
212                              * from the installation.
213                              * Add the full path when we use the current
214                              * working directory of GMXLIB.
215                              */
216                             srenew(fns_short, n+1);
217                             if (strcmp(dir, ".") == 0 || bEnvIsSet)
218                             {
219                                 fns_short[n] = gmx_strdup(fn_dir);
220                             }
221                             else
222                             {
223                                 fns_short[n] = gmx_strdup(nextname);
224                             }
225                         }
226                         n++;
227                         n_thisdir++;
228                     }
229                 }
230                 gmx_directory_close(dirhandle);
231
232                 sort_filenames(n_thisdir,
233                                fns+n-n_thisdir,
234                                fns_short == NULL ? NULL : fns_short+n-n_thisdir);
235             }
236         }
237     }
238     GMX_CATCH_ALL_AND_EXIT_WITH_FATAL_ERROR;
239
240     if (n == 0 && bFatalError)
241     {
242         if (ffdir != NULL)
243         {
244             gmx_fatal(FARGS, "Could not find any files ending on '%s' in the force field directory '%s'", file_end, ffdir);
245         }
246         else
247         {
248             gmx_fatal(FARGS, "Could not find any files ending on '%s' in the current directory or the GROMACS library search path", file_end);
249         }
250     }
251
252     *filenames = fns;
253     if (ffdir == NULL)
254     {
255         *filenames_short = fns_short;
256     }
257
258     return n;
259 }
260
261 int fflib_search_file_end(const char *ffdir,
262                           const char *file_end,
263                           gmx_bool    bFatalError,
264                           char     ***filenames)
265 {
266     return low_fflib_search_file_end(ffdir, FALSE, file_end, bFatalError,
267                                      filenames, NULL);
268 }
269
270 int fflib_search_file_in_dirend(const char *filename, const char *dirend,
271                                 char ***dirnames)
272 {
273     int             nf, i;
274     char          **f, **f_short;
275     int             n;
276     char          **dns;
277     gmx_directory_t dirhandle;
278     char            nextname[STRLEN];
279     int             rc;
280
281     /* Find all files (not only dir's) ending on dirend */
282     nf = low_fflib_search_file_end(NULL, TRUE, dirend, FALSE, &f, &f_short);
283
284     n   = 0;
285     dns = NULL;
286     for (i = 0; i < nf; i++)
287     {
288         rc = gmx_directory_open(&dirhandle, f[i]);
289
290         if (rc == 0)
291         {
292             while (gmx_directory_nextfile(dirhandle, nextname, STRLEN-1) == 0)
293             {
294                 nextname[STRLEN-1] = 0;
295                 if (strcmp(nextname, filename) == 0)
296                 {
297                     /* We have a match */
298                     srenew(dns, n+1);
299                     dns[n] = gmx_strdup(f_short[i]);
300                     n++;
301                 }
302             }
303             gmx_directory_close(dirhandle);
304         }
305         sfree(f[i]);
306         sfree(f_short[i]);
307     }
308     sfree(f);
309     sfree(f_short);
310
311     *dirnames = dns;
312
313     return n;
314 }
315
316 gmx_bool fflib_fexist(const char *file)
317 {
318     char *file_fullpath;
319
320     file_fullpath = low_gmxlibfn(file, TRUE, FALSE);
321
322     if (file_fullpath == NULL)
323     {
324         return FALSE;
325     }
326     else
327     {
328         sfree(file_fullpath);
329
330         return TRUE;
331     }
332 }
333
334
335 FILE *fflib_open(const char *file)
336 {
337     char *file_fullpath;
338     FILE *fp;
339
340     file_fullpath = gmxlibfn(file);
341     fprintf(stderr, "Opening force field file %s\n", file_fullpath);
342     fp = gmx_ffopen(file_fullpath, "r");
343     sfree(file_fullpath);
344
345     return fp;
346 }