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