7d5306838e89b68ba9b6f325b9da6fcdf5e42863
[alexxy/gromacs.git] / src / kernel / fflibutil.c
1 /*  -*- mode: c; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4; c-file-style: "stroustrup"; -*-
2  *
3  * 
4  *                This source code is part of
5  * 
6  *                 G   R   O   M   A   C   S
7  * 
8  *          GROningen MAchine for Chemical Simulations
9  * 
10  *                        VERSION 3.2.0
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.
15
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.
20  * 
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.
27  * 
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.
30  * 
31  * For more info, check our website at http://www.gromacs.org
32  * 
33  * And Hey:
34  * GROningen Mixture of Alchemy and Childrens' Stories
35  */
36 #ifdef HAVE_CONFIG_H
37 #include <config.h>
38 #endif
39 #include "gmx_header_config.h"
40
41 #include <stdio.h>
42 #include <stdlib.h>
43 #include <string.h>
44 #include <sys/types.h>
45 #include <sys/stat.h>
46 #include <fcntl.h>
47 #include "sysstuff.h"
48 #include "string2.h"
49 #include "futil.h"
50 #include "network.h"
51 #include "gmx_fatal.h"
52 #include "smalloc.h"
53 #include "statutil.h"
54
55 #ifdef GMX_NATIVE_WINDOWS
56 #include <direct.h>
57 #include <io.h>
58 #endif
59
60 #ifdef HAVE_UNISTD_H
61 #include <unistd.h>
62 #endif
63
64 #ifdef GMX_THREAD_MPI
65 #include "thread_mpi.h"
66 #endif
67
68 #include "fflibutil.h"
69
70 const char *fflib_forcefield_dir_ext()
71 {
72     return ".ff";
73 }
74
75 const char *fflib_forcefield_itp()
76 {
77     return "forcefield.itp";
78 }
79
80 const char *fflib_forcefield_doc()
81 {
82     return "forcefield.doc";
83 }
84
85 void fflib_filename_base(const char *filename,char *filebase,int maxlen)
86 {
87     const char *cptr;
88     char *ptr;
89
90     cptr = strrchr(filename,DIR_SEPARATOR);
91     if (cptr != NULL)
92     {
93         /* Skip the separator */
94         cptr += 1;
95     }
96     else
97     {
98         cptr = filename;
99     }
100     if (strlen(filename) >= (size_t)maxlen)
101     {
102         gmx_fatal(FARGS,"filename is longer (%d) than maxlen (%d)",
103                   strlen(filename),maxlen);
104     }
105     strcpy(filebase,cptr);
106     /* Remove the extension */
107     ptr = strrchr(filebase,'.');
108     if (ptr != NULL)
109     {
110         ptr[0] = '\0';
111     }
112 }
113
114 static void sort_filenames(int n,char **name,char **name2)
115 {
116     /* Slow sort, but we usually have tens of names */
117     int  i,j,f;
118     char *tmp;
119
120     for(i=0; i<n-1; i++)
121     {
122         f = i;
123         for(j=i+1; j<n; j++)
124         {
125             if (strcmp(name[j],name[f]) < 0)
126             {
127                 f = j;
128             }
129         }
130         if (f > i)
131         {
132             tmp     = name[i];
133             name[i] = name[f];
134             name[f] = tmp;
135             if (name2 != NULL)
136             {
137                 tmp      = name2[i];
138                 name2[i] = name2[f];
139                 name2[f] = tmp;
140             }
141         }
142     }
143 }
144
145 static int low_fflib_search_file_end(const char *ffdir,
146                                      gmx_bool bAddCWD,
147                                      const char *file_end,
148                                      gmx_bool bFatalError,
149                                      char ***filenames,
150                                      char ***filenames_short)
151 {
152     char *ret=NULL;
153     char *lib,*dir;
154     char buf[1024];
155     char *libpath;
156     gmx_bool env_is_set;
157     int  len_fe,len_name;
158     char **fns,**fns_short;
159     char dir_print[GMX_PATH_MAX];
160     char *pdum;
161     char *s,fn_dir[GMX_PATH_MAX];
162     gmx_directory_t dirhandle;
163     char nextname[STRLEN];
164     int  n,n_thisdir,rc;
165
166     len_fe = strlen(file_end);
167
168     env_is_set = FALSE;
169     if (ffdir != NULL)
170     {
171         /* Search in current dir and ffdir */
172         libpath = gmxlibfn(ffdir);
173     }
174     else
175     {
176         /* GMXLIB can be a path now */
177         lib = getenv("GMXLIB");
178         snew(libpath,GMX_PATH_MAX);
179         if (bAddCWD)
180         {
181             sprintf(libpath,"%s%s",".",PATH_SEPARATOR);
182         }
183         if (lib != NULL)
184         {
185             env_is_set = TRUE;
186             strncat(libpath,lib,GMX_PATH_MAX);
187         } 
188         else if (!get_libdir(libpath+strlen(libpath)))
189         {
190             strncat(libpath,GMXLIBDIR,GMX_PATH_MAX);
191         }
192     }
193     s = libpath;
194     n = 0;
195     fns       = NULL;
196     fns_short = NULL;
197     /* Loop over all the entries in libpath */
198     while ((dir=gmx_strsep(&s, PATH_SEPARATOR)) != NULL)
199     {
200         rc = gmx_directory_open(&dirhandle,dir);
201         if (rc==0)
202         {
203             strcpy(dir_print,dir);
204
205             n_thisdir = 0;
206             while (gmx_directory_nextfile(dirhandle,nextname,STRLEN-1)==0)
207             {
208                 nextname[STRLEN-1]=0;
209                 if (debug)
210                 {
211                     fprintf(debug,"dir '%s' %d file '%s'\n",
212                             dir,n_thisdir,nextname);
213                 }
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)
218                 {
219                     /* We have a match */
220                     srenew(fns,n+1);
221                     sprintf(fn_dir,"%s%c%s",
222                             dir_print,DIR_SEPARATOR,nextname);
223
224                     /* Copy the file name, possibly including the path. */
225                     fns[n] = strdup(fn_dir);
226
227                     if (ffdir == NULL)
228                     {
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.
234                          */
235                         srenew(fns_short,n+1);
236                         if (strcmp(dir,".") == 0 || env_is_set)
237                         {
238                             fns_short[n] = strdup(fn_dir);
239                         }
240                         else
241                         {
242                             fns_short[n] = strdup(nextname);
243                         }
244                     }
245                     n++;
246                     n_thisdir++;
247                 }
248             }
249             gmx_directory_close(dirhandle);
250
251             sort_filenames(n_thisdir,
252                            fns+n-n_thisdir,
253                            fns_short==NULL ? NULL : fns_short+n-n_thisdir);
254         }
255     }
256
257     sfree(libpath);
258
259     if (n == 0 && bFatalError)
260     {
261         if (ffdir != NULL)
262         {
263             gmx_fatal(FARGS,"Could not find any files ending on '%s' in the force field directory '%s'",file_end,ffdir);
264         }
265         else
266         {
267             gmx_fatal(FARGS,"Could not find any files ending on '%s' in the current directory or the GROMACS library search path",file_end);
268         }
269     }
270
271     *filenames = fns;
272     if (ffdir == NULL)
273     {
274         *filenames_short = fns_short;
275     }
276
277     return n;
278 }
279
280 int fflib_search_file_end(const char *ffdir,
281                           const char *file_end,
282                           gmx_bool bFatalError,
283                           char ***filenames)
284 {
285     return low_fflib_search_file_end(ffdir,FALSE,file_end,bFatalError,
286                                      filenames,NULL);
287 }
288
289 int fflib_search_file_in_dirend(const char *filename,const char *dirend,
290                                 char ***dirnames)
291 {
292     int  nf,i;
293     char **f,**f_short;
294     int  n;
295     char **dns;
296     gmx_directory_t dirhandle;
297     char nextname[STRLEN];
298     int  rc;
299     
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);
302
303     n = 0;
304     dns = NULL;
305     for(i=0; i<nf; i++)
306     {
307         rc = gmx_directory_open(&dirhandle,f[i]);
308
309         if (rc==0)
310         {
311             while (gmx_directory_nextfile(dirhandle,nextname,STRLEN-1)==0)
312             {
313                 nextname[STRLEN-1]=0;
314                 if (strcmp(nextname,filename) == 0)
315                 {
316                     /* We have a match */
317                     srenew(dns,n+1);
318                     dns[n] = strdup(f_short[i]);
319                     n++;
320                 }
321             }
322             gmx_directory_close(dirhandle);
323         }
324         sfree(f[i]);
325         sfree(f_short[i]);
326     }
327     sfree(f);
328     sfree(f_short);
329
330     *dirnames = dns;
331
332     return n;
333 }
334
335 gmx_bool fflib_fexist(const char *file)
336 {
337     char *file_fullpath;
338
339     file_fullpath = low_gmxlibfn(file,TRUE,FALSE);
340     
341     if (file_fullpath == NULL)
342     {
343         return FALSE;
344     }
345     else
346     {
347         sfree(file_fullpath);
348
349         return TRUE;
350     }
351 }
352
353
354 FILE *fflib_open(const char *file)
355 {
356     char *file_fullpath;
357     FILE *fp;
358
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);
363
364     return fp;
365 }