1 /* -*- mode: c; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4; c-file-style: "stroustrup"; -*-
4 * This source code is part of
8 * GROningen MAchine for Chemical Simulations
11 * Written by David van der Spoel, Erik Lindahl, Berk Hess, and others.
12 * Copyright (c) 1991-2000, University of Groningen, The Netherlands.
13 * Copyright (c) 2001-2004, The GROMACS development team,
14 * check out http://www.gromacs.org for more information.
16 * This program is free software; you can redistribute it and/or
17 * modify it under the terms of the GNU General Public License
18 * as published by the Free Software Foundation; either version 2
19 * of the License, or (at your option) any later version.
21 * If you want to redistribute modifications, please consider that
22 * scientific software is very special. Version control is crucial -
23 * bugs must be traceable. We will be happy to consider code for
24 * inclusion in the official distribution, but derived work must not
25 * be called official GROMACS. Details are found in the README & COPYING
26 * files - if they are missing, get the official version at www.gromacs.org.
28 * To help us fund GROMACS development, we humbly ask that you cite
29 * the papers on the package - you can find them in the top README file.
31 * For more info, check our website at http://www.gromacs.org
34 * GROningen Mixture of Alchemy and Childrens' Stories
39 #include "gmx_header_config.h"
44 #include <sys/types.h>
51 #include "gmx_fatal.h"
55 #ifdef GMX_NATIVE_WINDOWS
65 #include "thread_mpi.h"
68 #include "fflibutil.h"
70 const char *fflib_forcefield_dir_ext()
75 const char *fflib_forcefield_itp()
77 return "forcefield.itp";
80 const char *fflib_forcefield_doc()
82 return "forcefield.doc";
85 void fflib_filename_base(const char *filename,char *filebase,int maxlen)
90 cptr = strrchr(filename,DIR_SEPARATOR);
93 /* Skip the separator */
100 if (strlen(filename) >= (size_t)maxlen)
102 gmx_fatal(FARGS,"filename is longer (%d) than maxlen (%d)",
103 strlen(filename),maxlen);
105 strcpy(filebase,cptr);
106 /* Remove the extension */
107 ptr = strrchr(filebase,'.');
114 static void sort_filenames(int n,char **name,char **name2)
116 /* Slow sort, but we usually have tens of names */
125 if (strcmp(name[j],name[f]) < 0)
145 static int low_fflib_search_file_end(const char *ffdir,
147 const char *file_end,
148 gmx_bool bFatalError,
150 char ***filenames_short)
158 char **fns,**fns_short;
159 char dir_print[GMX_PATH_MAX];
161 char *s,fn_dir[GMX_PATH_MAX];
162 gmx_directory_t dirhandle;
163 char nextname[STRLEN];
166 len_fe = strlen(file_end);
171 /* Search in current dir and ffdir */
172 libpath = gmxlibfn(ffdir);
176 /* GMXLIB can be a path now */
177 lib = getenv("GMXLIB");
178 snew(libpath,GMX_PATH_MAX);
181 sprintf(libpath,"%s%s",".",PATH_SEPARATOR);
186 strncat(libpath,lib,GMX_PATH_MAX);
188 else if (!get_libdir(libpath+strlen(libpath)))
190 strncat(libpath,GMXLIBDIR,GMX_PATH_MAX);
197 /* Loop over all the entries in libpath */
198 while ((dir=gmx_strsep(&s, PATH_SEPARATOR)) != NULL)
200 rc = gmx_directory_open(&dirhandle,dir);
203 strcpy(dir_print,dir);
206 while (gmx_directory_nextfile(dirhandle,nextname,STRLEN-1)==0)
208 nextname[STRLEN-1]=0;
211 fprintf(debug,"dir '%s' %d file '%s'\n",
212 dir,n_thisdir,nextname);
214 len_name = strlen(nextname);
215 /* What about case sensitivity? */
216 if (len_name >= len_fe &&
217 strcmp(nextname+len_name-len_fe,file_end) == 0)
219 /* We have a match */
221 sprintf(fn_dir,"%s%c%s",
222 dir_print,DIR_SEPARATOR,nextname);
224 /* Copy the file name, possibly including the path. */
225 fns[n] = strdup(fn_dir);
229 /* We are searching in a path.
230 * Use the relative path when we use share/top
231 * from the installation.
232 * Add the full path when we use the current
233 * working directory of GMXLIB.
235 srenew(fns_short,n+1);
236 if (strcmp(dir,".") == 0 || env_is_set)
238 fns_short[n] = strdup(fn_dir);
242 fns_short[n] = strdup(nextname);
249 gmx_directory_close(dirhandle);
251 sort_filenames(n_thisdir,
253 fns_short==NULL ? NULL : fns_short+n-n_thisdir);
259 if (n == 0 && bFatalError)
263 gmx_fatal(FARGS,"Could not find any files ending on '%s' in the force field directory '%s'",file_end,ffdir);
267 gmx_fatal(FARGS,"Could not find any files ending on '%s' in the current directory or the GROMACS library search path",file_end);
274 *filenames_short = fns_short;
280 int fflib_search_file_end(const char *ffdir,
281 const char *file_end,
282 gmx_bool bFatalError,
285 return low_fflib_search_file_end(ffdir,FALSE,file_end,bFatalError,
289 int fflib_search_file_in_dirend(const char *filename,const char *dirend,
296 gmx_directory_t dirhandle;
297 char nextname[STRLEN];
300 /* Find all files (not only dir's) ending on dirend */
301 nf = low_fflib_search_file_end(NULL,TRUE,dirend,FALSE,&f,&f_short);
307 rc = gmx_directory_open(&dirhandle,f[i]);
311 while (gmx_directory_nextfile(dirhandle,nextname,STRLEN-1)==0)
313 nextname[STRLEN-1]=0;
314 if (strcmp(nextname,filename) == 0)
316 /* We have a match */
318 dns[n] = strdup(f_short[i]);
322 gmx_directory_close(dirhandle);
335 gmx_bool fflib_fexist(const char *file)
339 file_fullpath = low_gmxlibfn(file,TRUE,FALSE);
341 if (file_fullpath == NULL)
347 sfree(file_fullpath);
354 FILE *fflib_open(const char *file)
359 file_fullpath = gmxlibfn(file);
360 fprintf(stderr,"Opening force field file %s\n",file_fullpath);
361 fp = ffopen(file_fullpath,"r");
362 sfree(file_fullpath);