2 * This file is part of the GROMACS molecular simulation package.
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.
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.
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.
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.
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.
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.
42 #include <sys/types.h>
47 #include "gmx_fatal.h"
48 #include "gromacs/utility/smalloc.h"
54 #include "fflibutil.h"
56 #include "gromacs/fileio/futil.h"
57 #include "gromacs/fileio/path.h"
58 #include "gromacs/utility/exceptions.h"
59 #include "gromacs/utility/programcontext.h"
61 const char *fflib_forcefield_dir_ext()
66 const char *fflib_forcefield_itp()
68 return "forcefield.itp";
71 const char *fflib_forcefield_doc()
73 return "forcefield.doc";
76 void fflib_filename_base(const char *filename, char *filebase, int maxlen)
81 cptr = strrchr(filename, DIR_SEPARATOR);
84 /* Skip the separator */
91 if (strlen(filename) >= (size_t)maxlen)
93 gmx_fatal(FARGS, "filename is longer (%d) than maxlen (%d)",
94 strlen(filename), maxlen);
96 strcpy(filebase, cptr);
97 /* Remove the extension */
98 ptr = strrchr(filebase, '.');
105 static void sort_filenames(int n, char **name, char **name2)
107 /* Slow sort, but we usually have tens of names */
111 for (i = 0; i < n-1; i++)
114 for (j = i+1; j < n; j++)
116 if (strcmp(name[j], name[f]) < 0)
136 static int low_fflib_search_file_end(const char *ffdir,
138 const char *file_end,
139 gmx_bool bFatalError,
141 char ***filenames_short)
143 char **fns = NULL, **fns_short = NULL;
147 std::vector<std::string> libPaths;
148 bool bEnvIsSet = false;
152 /* Search ffdir in current dir and library dirs */
153 libPaths.push_back(gmxlibfn(ffdir));
157 /* GMXLIB can be a path now */
160 libPaths.push_back(".");
162 const char *lib = getenv("GMXLIB");
166 gmx::Path::splitPathEnvironment(lib, &libPaths);
170 libPaths.push_back(gmx::getProgramContext().defaultLibraryDataPath());
174 const int len_fe = strlen(file_end);
176 std::vector<std::string>::const_iterator i;
177 for (i = libPaths.begin(); i != libPaths.end(); ++i)
179 const char *dir = i->c_str();
180 gmx_directory_t dirhandle;
181 const int rc = gmx_directory_open(&dirhandle, dir);
184 char nextname[STRLEN];
186 while (gmx_directory_nextfile(dirhandle, nextname, STRLEN-1) == 0)
188 nextname[STRLEN-1] = 0;
191 fprintf(debug, "dir '%s' %d file '%s'\n",
192 dir, n_thisdir, nextname);
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)
199 char fn_dir[GMX_PATH_MAX];
200 /* We have a match */
202 sprintf(fn_dir, "%s%c%s", dir, DIR_SEPARATOR, nextname);
204 /* Copy the file name, possibly including the path. */
205 fns[n] = strdup(fn_dir);
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.
215 srenew(fns_short, n+1);
216 if (strcmp(dir, ".") == 0 || bEnvIsSet)
218 fns_short[n] = strdup(fn_dir);
222 fns_short[n] = strdup(nextname);
229 gmx_directory_close(dirhandle);
231 sort_filenames(n_thisdir,
233 fns_short == NULL ? NULL : fns_short+n-n_thisdir);
237 GMX_CATCH_ALL_AND_EXIT_WITH_FATAL_ERROR;
239 if (n == 0 && bFatalError)
243 gmx_fatal(FARGS, "Could not find any files ending on '%s' in the force field directory '%s'", file_end, ffdir);
247 gmx_fatal(FARGS, "Could not find any files ending on '%s' in the current directory or the GROMACS library search path", file_end);
254 *filenames_short = fns_short;
260 int fflib_search_file_end(const char *ffdir,
261 const char *file_end,
262 gmx_bool bFatalError,
265 return low_fflib_search_file_end(ffdir, FALSE, file_end, bFatalError,
269 int fflib_search_file_in_dirend(const char *filename, const char *dirend,
276 gmx_directory_t dirhandle;
277 char nextname[STRLEN];
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);
285 for (i = 0; i < nf; i++)
287 rc = gmx_directory_open(&dirhandle, f[i]);
291 while (gmx_directory_nextfile(dirhandle, nextname, STRLEN-1) == 0)
293 nextname[STRLEN-1] = 0;
294 if (strcmp(nextname, filename) == 0)
296 /* We have a match */
298 dns[n] = strdup(f_short[i]);
302 gmx_directory_close(dirhandle);
315 gmx_bool fflib_fexist(const char *file)
319 file_fullpath = low_gmxlibfn(file, TRUE, FALSE);
321 if (file_fullpath == NULL)
327 sfree(file_fullpath);
334 FILE *fflib_open(const char *file)
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);