2 * This file is part of the GROMACS molecular simulation package.
4 * Copyright (c) 1991-2000, University of Groningen, The Netherlands.
5 * Copyright (c) 2001-2004, The GROMACS development team,
6 * check out http://www.gromacs.org for more information.
7 * Copyright (c) 2012,2013, by the GROMACS development team, led by
8 * David van der Spoel, Berk Hess, Erik Lindahl, and including many
9 * others, as listed in the AUTHORS file in the top-level source
10 * directory and at http://www.gromacs.org.
12 * GROMACS is free software; you can redistribute it and/or
13 * modify it under the terms of the GNU Lesser General Public License
14 * as published by the Free Software Foundation; either version 2.1
15 * of the License, or (at your option) any later version.
17 * GROMACS is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
20 * Lesser General Public License for more details.
22 * You should have received a copy of the GNU Lesser General Public
23 * License along with GROMACS; if not, see
24 * http://www.gnu.org/licenses, or write to the Free Software Foundation,
25 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
27 * If you want to redistribute modifications to GROMACS, please
28 * consider that scientific software is very special. Version
29 * control is crucial - bugs must be traceable. We will be happy to
30 * consider code for inclusion in the official distribution, but
31 * derived work must not be called official GROMACS. Details are found
32 * in the README & COPYING files - if they are missing, get the
33 * official version at http://www.gromacs.org.
35 * To help us fund GROMACS development, we humbly ask that you cite
36 * the research papers on the package. Check out http://www.gromacs.org.
45 #include <sys/types.h>
52 #include "gmx_fatal.h"
56 #ifdef GMX_NATIVE_WINDOWS
66 #include "thread_mpi.h"
69 #include "fflibutil.h"
71 const char *fflib_forcefield_dir_ext()
76 const char *fflib_forcefield_itp()
78 return "forcefield.itp";
81 const char *fflib_forcefield_doc()
83 return "forcefield.doc";
86 void fflib_filename_base(const char *filename,char *filebase,int maxlen)
91 cptr = strrchr(filename,DIR_SEPARATOR);
94 /* Skip the separator */
101 if (strlen(filename) >= (size_t)maxlen)
103 gmx_fatal(FARGS,"filename is longer (%d) than maxlen (%d)",
104 strlen(filename),maxlen);
106 strcpy(filebase,cptr);
107 /* Remove the extension */
108 ptr = strrchr(filebase,'.');
115 static void sort_filenames(int n,char **name,char **name2)
117 /* Slow sort, but we usually have tens of names */
126 if (strcmp(name[j],name[f]) < 0)
146 static int low_fflib_search_file_end(const char *ffdir,
148 const char *file_end,
149 gmx_bool bFatalError,
151 char ***filenames_short)
159 char **fns,**fns_short;
160 char dir_print[GMX_PATH_MAX];
162 char *s,fn_dir[GMX_PATH_MAX];
163 gmx_directory_t dirhandle;
164 char nextname[STRLEN];
167 len_fe = strlen(file_end);
172 /* Search in current dir and ffdir */
173 libpath = gmxlibfn(ffdir);
177 /* GMXLIB can be a path now */
178 lib = getenv("GMXLIB");
179 snew(libpath,GMX_PATH_MAX);
182 sprintf(libpath,"%s%s",".",PATH_SEPARATOR);
187 strncat(libpath,lib,GMX_PATH_MAX);
189 else if (!get_libdir(libpath+strlen(libpath)))
191 strncat(libpath,GMXLIBDIR,GMX_PATH_MAX);
198 /* Loop over all the entries in libpath */
199 while ((dir=gmx_strsep(&s, PATH_SEPARATOR)) != NULL)
201 rc = gmx_directory_open(&dirhandle,dir);
204 strcpy(dir_print,dir);
207 while (gmx_directory_nextfile(dirhandle,nextname,STRLEN-1)==0)
209 nextname[STRLEN-1]=0;
212 fprintf(debug,"dir '%s' %d file '%s'\n",
213 dir,n_thisdir,nextname);
215 len_name = strlen(nextname);
216 /* What about case sensitivity? */
217 if (len_name >= len_fe &&
218 strcmp(nextname+len_name-len_fe,file_end) == 0)
220 /* We have a match */
222 sprintf(fn_dir,"%s%c%s",
223 dir_print,DIR_SEPARATOR,nextname);
225 /* Copy the file name, possibly including the path. */
226 fns[n] = strdup(fn_dir);
230 /* We are searching in a path.
231 * Use the relative path when we use share/top
232 * from the installation.
233 * Add the full path when we use the current
234 * working directory of GMXLIB.
236 srenew(fns_short,n+1);
237 if (strcmp(dir,".") == 0 || env_is_set)
239 fns_short[n] = strdup(fn_dir);
243 fns_short[n] = strdup(nextname);
250 gmx_directory_close(dirhandle);
252 sort_filenames(n_thisdir,
254 fns_short==NULL ? NULL : fns_short+n-n_thisdir);
260 if (n == 0 && bFatalError)
264 gmx_fatal(FARGS,"Could not find any files ending on '%s' in the force field directory '%s'",file_end,ffdir);
268 gmx_fatal(FARGS,"Could not find any files ending on '%s' in the current directory or the GROMACS library search path",file_end);
275 *filenames_short = fns_short;
281 int fflib_search_file_end(const char *ffdir,
282 const char *file_end,
283 gmx_bool bFatalError,
286 return low_fflib_search_file_end(ffdir,FALSE,file_end,bFatalError,
290 int fflib_search_file_in_dirend(const char *filename,const char *dirend,
297 gmx_directory_t dirhandle;
298 char nextname[STRLEN];
301 /* Find all files (not only dir's) ending on dirend */
302 nf = low_fflib_search_file_end(NULL,TRUE,dirend,FALSE,&f,&f_short);
308 rc = gmx_directory_open(&dirhandle,f[i]);
312 while (gmx_directory_nextfile(dirhandle,nextname,STRLEN-1)==0)
314 nextname[STRLEN-1]=0;
315 if (strcmp(nextname,filename) == 0)
317 /* We have a match */
319 dns[n] = strdup(f_short[i]);
323 gmx_directory_close(dirhandle);
336 gmx_bool fflib_fexist(const char *file)
340 file_fullpath = low_gmxlibfn(file,TRUE,FALSE);
342 if (file_fullpath == NULL)
348 sfree(file_fullpath);
355 FILE *fflib_open(const char *file)
360 file_fullpath = gmxlibfn(file);
361 fprintf(stderr,"Opening force field file %s\n",file_fullpath);
362 fp = ffopen(file_fullpath,"r");
363 sfree(file_fullpath);