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