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