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