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, 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.
41 #include "gmx_header_config.h"
46 #include <sys/types.h>
53 #include "gmx_fatal.h"
57 #ifdef GMX_NATIVE_WINDOWS
67 #include "thread_mpi.h"
70 #include "fflibutil.h"
72 const char *fflib_forcefield_dir_ext()
77 const char *fflib_forcefield_itp()
79 return "forcefield.itp";
82 const char *fflib_forcefield_doc()
84 return "forcefield.doc";
87 void fflib_filename_base(const char *filename,char *filebase,int maxlen)
92 cptr = strrchr(filename,DIR_SEPARATOR);
95 /* Skip the separator */
102 if (strlen(filename) >= (size_t)maxlen)
104 gmx_fatal(FARGS,"filename is longer (%d) than maxlen (%d)",
105 strlen(filename),maxlen);
107 strcpy(filebase,cptr);
108 /* Remove the extension */
109 ptr = strrchr(filebase,'.');
116 static void sort_filenames(int n,char **name,char **name2)
118 /* Slow sort, but we usually have tens of names */
127 if (strcmp(name[j],name[f]) < 0)
147 static int low_fflib_search_file_end(const char *ffdir,
149 const char *file_end,
150 gmx_bool bFatalError,
152 char ***filenames_short)
160 char **fns,**fns_short;
161 char dir_print[GMX_PATH_MAX];
163 char *s,fn_dir[GMX_PATH_MAX];
164 gmx_directory_t dirhandle;
165 char nextname[STRLEN];
168 len_fe = strlen(file_end);
173 /* Search in current dir and ffdir */
174 libpath = gmxlibfn(ffdir);
178 /* GMXLIB can be a path now */
179 lib = getenv("GMXLIB");
180 snew(libpath,GMX_PATH_MAX);
183 sprintf(libpath,"%s%s",".",PATH_SEPARATOR);
188 strncat(libpath,lib,GMX_PATH_MAX);
190 else if (!get_libdir(libpath+strlen(libpath)))
192 strncat(libpath,GMXLIBDIR,GMX_PATH_MAX);
199 /* Loop over all the entries in libpath */
200 while ((dir=gmx_strsep(&s, PATH_SEPARATOR)) != NULL)
202 rc = gmx_directory_open(&dirhandle,dir);
205 strcpy(dir_print,dir);
208 while (gmx_directory_nextfile(dirhandle,nextname,STRLEN-1)==0)
210 nextname[STRLEN-1]=0;
213 fprintf(debug,"dir '%s' %d file '%s'\n",
214 dir,n_thisdir,nextname);
216 len_name = strlen(nextname);
217 /* What about case sensitivity? */
218 if (len_name >= len_fe &&
219 strcmp(nextname+len_name-len_fe,file_end) == 0)
221 /* We have a match */
223 sprintf(fn_dir,"%s%c%s",
224 dir_print,DIR_SEPARATOR,nextname);
226 /* Copy the file name, possibly including the path. */
227 fns[n] = strdup(fn_dir);
231 /* We are searching in a path.
232 * Use the relative path when we use share/top
233 * from the installation.
234 * Add the full path when we use the current
235 * working directory of GMXLIB.
237 srenew(fns_short,n+1);
238 if (strcmp(dir,".") == 0 || env_is_set)
240 fns_short[n] = strdup(fn_dir);
244 fns_short[n] = strdup(nextname);
251 gmx_directory_close(dirhandle);
253 sort_filenames(n_thisdir,
255 fns_short==NULL ? NULL : fns_short+n-n_thisdir);
261 if (n == 0 && bFatalError)
265 gmx_fatal(FARGS,"Could not find any files ending on '%s' in the force field directory '%s'",file_end,ffdir);
269 gmx_fatal(FARGS,"Could not find any files ending on '%s' in the current directory or the GROMACS library search path",file_end);
276 *filenames_short = fns_short;
282 int fflib_search_file_end(const char *ffdir,
283 const char *file_end,
284 gmx_bool bFatalError,
287 return low_fflib_search_file_end(ffdir,FALSE,file_end,bFatalError,
291 int fflib_search_file_in_dirend(const char *filename,const char *dirend,
298 gmx_directory_t dirhandle;
299 char nextname[STRLEN];
302 /* Find all files (not only dir's) ending on dirend */
303 nf = low_fflib_search_file_end(NULL,TRUE,dirend,FALSE,&f,&f_short);
309 rc = gmx_directory_open(&dirhandle,f[i]);
313 while (gmx_directory_nextfile(dirhandle,nextname,STRLEN-1)==0)
315 nextname[STRLEN-1]=0;
316 if (strcmp(nextname,filename) == 0)
318 /* We have a match */
320 dns[n] = strdup(f_short[i]);
324 gmx_directory_close(dirhandle);
337 gmx_bool fflib_fexist(const char *file)
341 file_fullpath = low_gmxlibfn(file,TRUE,FALSE);
343 if (file_fullpath == NULL)
349 sfree(file_fullpath);
356 FILE *fflib_open(const char *file)
361 file_fullpath = gmxlibfn(file);
362 fprintf(stderr,"Opening force field file %s\n",file_fullpath);
363 fp = ffopen(file_fullpath,"r");
364 sfree(file_fullpath);