f986f5e545afac46530ad7042743a2ed528bbdbc
[alexxy/gromacs.git] / src / gromacs / gmxpreprocess / fflibutil.cpp
1 /*
2  * This file is part of the GROMACS molecular simulation package.
3  *
4  * Copyright (c) 2010,2012,2013,2014,2015, by the GROMACS development team, led by
5  * Mark Abraham, David van der Spoel, Berk Hess, and Erik Lindahl,
6  * and including many others, as listed in the AUTHORS file in the
7  * top-level source directory and at http://www.gromacs.org.
8  *
9  * GROMACS is free software; you can redistribute it and/or
10  * modify it under the terms of the GNU Lesser General Public License
11  * as published by the Free Software Foundation; either version 2.1
12  * of the License, or (at your option) any later version.
13  *
14  * GROMACS is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
17  * Lesser General Public License for more details.
18  *
19  * You should have received a copy of the GNU Lesser General Public
20  * License along with GROMACS; if not, see
21  * http://www.gnu.org/licenses, or write to the Free Software Foundation,
22  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA.
23  *
24  * If you want to redistribute modifications to GROMACS, please
25  * consider that scientific software is very special. Version
26  * control is crucial - bugs must be traceable. We will be happy to
27  * consider code for inclusion in the official distribution, but
28  * derived work must not be called official GROMACS. Details are found
29  * in the README & COPYING files - if they are missing, get the
30  * official version at http://www.gromacs.org.
31  *
32  * To help us fund GROMACS development, we humbly ask that you cite
33  * the research papers on the package. Check out http://www.gromacs.org.
34  */
35 #include "gmxpre.h"
36
37 #include "fflibutil.h"
38
39 #include <string.h>
40
41 #include <string>
42 #include <vector>
43
44 #include "gromacs/utility/cstringutil.h"
45 #include "gromacs/utility/datafilefinder.h"
46 #include "gromacs/utility/dir_separator.h"
47 #include "gromacs/utility/directoryenumerator.h"
48 #include "gromacs/utility/exceptions.h"
49 #include "gromacs/utility/fatalerror.h"
50 #include "gromacs/utility/file.h"
51 #include "gromacs/utility/futil.h"
52 #include "gromacs/utility/path.h"
53 #include "gromacs/utility/smalloc.h"
54 #include "gromacs/utility/stringutil.h"
55
56 const char *fflib_forcefield_dir_ext()
57 {
58     return ".ff";
59 }
60
61 const char *fflib_forcefield_itp()
62 {
63     return "forcefield.itp";
64 }
65
66 const char *fflib_forcefield_doc()
67 {
68     return "forcefield.doc";
69 }
70
71 void fflib_filename_base(const char *filename, char *filebase, int maxlen)
72 {
73     const char *cptr;
74     char       *ptr;
75
76     cptr = strrchr(filename, DIR_SEPARATOR);
77     if (cptr != NULL)
78     {
79         /* Skip the separator */
80         cptr += 1;
81     }
82     else
83     {
84         cptr = filename;
85     }
86     if (strlen(filename) >= (size_t)maxlen)
87     {
88         gmx_fatal(FARGS, "filename is longer (%d) than maxlen (%d)",
89                   strlen(filename), maxlen);
90     }
91     strcpy(filebase, cptr);
92     /* Remove the extension */
93     ptr = strrchr(filebase, '.');
94     if (ptr != NULL)
95     {
96         ptr[0] = '\0';
97     }
98 }
99
100 int fflib_search_file_end(const char *ffdir,
101                           const char *file_end,
102                           gmx_bool    bFatalError,
103                           char     ***filenames)
104 {
105     try
106     {
107         std::string              ffdirFull(gmx::getLibraryFileFinder().findFile(ffdir));
108         std::vector<std::string> result
109             = gmx::DirectoryEnumerator::enumerateFilesWithExtension(
110                         ffdirFull.c_str(), file_end, true);
111         if (result.empty() && bFatalError)
112         {
113             std::string message
114                 = gmx::formatString("Could not find any files ending on '%s' "
115                                     "in the force field directory '%s'",
116                                     file_end, ffdir);
117             GMX_THROW(gmx::InvalidInputError(message));
118         }
119         const int count = static_cast<int>(result.size());
120         for (int i = 0; i < count; ++i)
121         {
122             result[i] = gmx::Path::join(ffdir, result[i]);
123         }
124         char    **fns;
125         snew(fns, count);
126         for (int i = 0; i < count; ++i)
127         {
128             fns[i] = gmx_strdup(result[i].c_str());
129         }
130         *filenames = fns;
131         return count;
132     }
133     GMX_CATCH_ALL_AND_EXIT_WITH_FATAL_ERROR;
134 }
135
136 std::vector<gmx::DataFileInfo> fflib_enumerate_forcefields()
137 {
138     const char *const              dirend   = fflib_forcefield_dir_ext();
139     const char *const              filename = fflib_forcefield_itp();
140     std::vector<gmx::DataFileInfo> candidates
141         = gmx::getLibraryFileFinder().enumerateFiles(
142                     gmx::DataFileOptions(dirend)
143                         .throwIfNotFound(false));
144
145     std::vector<gmx::DataFileInfo> result;
146     for (size_t i = 0; i < candidates.size(); ++i)
147     {
148         std::string testPath(gmx::Path::join(
149                                      candidates[i].dir, candidates[i].name, filename));
150         // TODO: Consider also checking that the directory can be listed.
151         if (gmx::File::exists(testPath))
152         {
153             result.push_back(candidates[i]);
154         }
155     }
156
157     // TODO: Consider merging this into enumerateFiles(), such that the error
158     // could also list the directories searched.
159     if (result.empty())
160     {
161         std::string message
162             = gmx::formatString("No force fields found (files with name '%s' "
163                                 "in subdirectories ending on '%s')",
164                                 filename, dirend);
165         GMX_THROW(gmx::InvalidInputError(message));
166     }
167
168     return result;
169 }
170
171 gmx_bool fflib_fexist(const char *file)
172 {
173     char *file_fullpath;
174
175     file_fullpath = low_gmxlibfn(file, TRUE, FALSE);
176
177     if (file_fullpath == NULL)
178     {
179         return FALSE;
180     }
181     else
182     {
183         sfree(file_fullpath);
184
185         return TRUE;
186     }
187 }
188
189
190 FILE *fflib_open(const char *file)
191 {
192     char *file_fullpath;
193     FILE *fp;
194
195     file_fullpath = gmxlibfn(file);
196     fprintf(stderr, "Opening force field file %s\n", file_fullpath);
197     fp = gmx_ffopen(file_fullpath, "r");
198     sfree(file_fullpath);
199
200     return fp;
201 }