2 * This file is part of the GROMACS molecular simulation package.
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.
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.
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.
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.
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.
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.
37 #include "fflibutil.h"
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"
56 const char *fflib_forcefield_dir_ext()
61 const char *fflib_forcefield_itp()
63 return "forcefield.itp";
66 const char *fflib_forcefield_doc()
68 return "forcefield.doc";
71 void fflib_filename_base(const char *filename, char *filebase, int maxlen)
76 cptr = strrchr(filename, DIR_SEPARATOR);
79 /* Skip the separator */
86 if (strlen(filename) >= (size_t)maxlen)
88 gmx_fatal(FARGS, "filename is longer (%d) than maxlen (%d)",
89 strlen(filename), maxlen);
91 strcpy(filebase, cptr);
92 /* Remove the extension */
93 ptr = strrchr(filebase, '.');
100 int fflib_search_file_end(const char *ffdir,
101 const char *file_end,
102 gmx_bool bFatalError,
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)
114 = gmx::formatString("Could not find any files ending on '%s' "
115 "in the force field directory '%s'",
117 GMX_THROW(gmx::InvalidInputError(message));
119 const int count = static_cast<int>(result.size());
120 for (int i = 0; i < count; ++i)
122 result[i] = gmx::Path::join(ffdir, result[i]);
126 for (int i = 0; i < count; ++i)
128 fns[i] = gmx_strdup(result[i].c_str());
133 GMX_CATCH_ALL_AND_EXIT_WITH_FATAL_ERROR;
136 std::vector<gmx::DataFileInfo> fflib_enumerate_forcefields()
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));
145 std::vector<gmx::DataFileInfo> result;
146 for (size_t i = 0; i < candidates.size(); ++i)
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))
153 result.push_back(candidates[i]);
157 // TODO: Consider merging this into enumerateFiles(), such that the error
158 // could also list the directories searched.
162 = gmx::formatString("No force fields found (files with name '%s' "
163 "in subdirectories ending on '%s')",
165 GMX_THROW(gmx::InvalidInputError(message));
171 gmx_bool fflib_fexist(const char *file)
175 file_fullpath = low_gmxlibfn(file, TRUE, FALSE);
177 if (file_fullpath == NULL)
183 sfree(file_fullpath);
190 FILE *fflib_open(const char *file)
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);