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
43 #include <sys/types.h>
50 #include "gmx_fatal.h"
54 #if ((defined WIN32 || defined _WIN32 || defined WIN64 || defined _WIN64) && !defined __CYGWIN__ && !defined __CYGWIN32__)
64 #include "thread_mpi.h"
67 #include "fflibutil.h"
69 const char *fflib_forcefield_dir_ext()
74 const char *fflib_forcefield_itp()
76 return "forcefield.itp";
79 const char *fflib_forcefield_doc()
81 return "forcefield.doc";
84 void fflib_filename_base(const char *filename,char *filebase,int maxlen)
89 cptr = strrchr(filename,DIR_SEPARATOR);
92 /* Skip the separator */
99 if (strlen(filename) >= (size_t)maxlen)
101 gmx_fatal(FARGS,"filename is longer (%d) than maxlen (%d)",
102 strlen(filename),maxlen);
104 strcpy(filebase,cptr);
105 /* Remove the extension */
106 ptr = strrchr(filebase,'.');
113 static void sort_filenames(int n,char **name,char **name2)
115 /* Slow sort, but we usually have tens of names */
124 if (strcmp(name[j],name[f]) < 0)
144 static int low_fflib_search_file_end(const char *ffdir,
146 const char *file_end,
147 gmx_bool bFatalError,
149 char ***filenames_short)
157 char **fns,**fns_short;
158 char dir_print[GMX_PATH_MAX];
160 char *s,fn_dir[GMX_PATH_MAX];
161 gmx_directory_t dirhandle;
162 char nextname[STRLEN];
165 len_fe = strlen(file_end);
170 /* Search in current dir and ffdir */
171 libpath = gmxlibfn(ffdir);
175 /* GMXLIB can be a path now */
176 lib = getenv("GMXLIB");
177 snew(libpath,GMX_PATH_MAX);
180 sprintf(libpath,"%s%s",".",PATH_SEPARATOR);
185 strncat(libpath,lib,GMX_PATH_MAX);
187 else if (!get_libdir(libpath+strlen(libpath)))
189 strncat(libpath,GMXLIBDIR,GMX_PATH_MAX);
196 /* Loop over all the entries in libpath */
197 while ((dir=gmx_strsep(&s, PATH_SEPARATOR)) != NULL)
199 rc = gmx_directory_open(&dirhandle,dir);
202 strcpy(dir_print,dir);
205 while (gmx_directory_nextfile(dirhandle,nextname,STRLEN-1)==0)
207 nextname[STRLEN-1]=0;
210 fprintf(debug,"dir '%s' %d file '%s'\n",
211 dir,n_thisdir,nextname);
213 len_name = strlen(nextname);
214 /* What about case sensitivity? */
215 if (len_name >= len_fe &&
216 strcmp(nextname+len_name-len_fe,file_end) == 0)
218 /* We have a match */
220 sprintf(fn_dir,"%s%c%s",
221 dir_print,DIR_SEPARATOR,nextname);
223 /* Copy the file name, possibly including the path. */
224 fns[n] = strdup(fn_dir);
228 /* We are searching in a path.
229 * Use the relative path when we use share/top
230 * from the installation.
231 * Add the full path when we use the current
232 * working directory of GMXLIB.
234 srenew(fns_short,n+1);
235 if (strcmp(dir,".") == 0 || env_is_set)
237 fns_short[n] = strdup(fn_dir);
241 fns_short[n] = strdup(nextname);
248 gmx_directory_close(dirhandle);
250 sort_filenames(n_thisdir,
252 fns_short==NULL ? NULL : fns_short+n-n_thisdir);
258 if (n == 0 && bFatalError)
262 gmx_fatal(FARGS,"Could not find any files ending on '%s' in the force field directory '%s'",file_end,ffdir);
266 gmx_fatal(FARGS,"Could not find any files ending on '%s' in the current directory or the GROMACS library search path",file_end);
273 *filenames_short = fns_short;
279 int fflib_search_file_end(const char *ffdir,
280 const char *file_end,
281 gmx_bool bFatalError,
284 return low_fflib_search_file_end(ffdir,FALSE,file_end,bFatalError,
288 int fflib_search_file_in_dirend(const char *filename,const char *dirend,
295 gmx_directory_t dirhandle;
296 char nextname[STRLEN];
299 /* Find all files (not only dir's) ending on dirend */
300 nf = low_fflib_search_file_end(NULL,TRUE,dirend,FALSE,&f,&f_short);
306 rc = gmx_directory_open(&dirhandle,f[i]);
310 while (gmx_directory_nextfile(dirhandle,nextname,STRLEN-1)==0)
312 nextname[STRLEN-1]=0;
313 if (strcmp(nextname,filename) == 0)
315 /* We have a match */
317 dns[n] = strdup(f_short[i]);
321 gmx_directory_close(dirhandle);
334 gmx_bool fflib_fexist(const char *file)
338 file_fullpath = low_gmxlibfn(file,TRUE,FALSE);
340 if (file_fullpath == NULL)
346 sfree(file_fullpath);
353 FILE *fflib_open(const char *file)
358 file_fullpath = gmxlibfn(file);
359 fprintf(stderr,"Opening force field file %s\n",file_fullpath);
360 fp = ffopen(file_fullpath,"r");
361 sfree(file_fullpath);