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/futil.h"
51 #include "gromacs/utility/path.h"
52 #include "gromacs/utility/smalloc.h"
53 #include "gromacs/utility/stringutil.h"
55 const char *fflib_forcefield_dir_ext()
60 const char *fflib_forcefield_itp()
62 return "forcefield.itp";
65 const char *fflib_forcefield_doc()
67 return "forcefield.doc";
70 void fflib_filename_base(const char *filename, char *filebase, int maxlen)
75 cptr = strrchr(filename, DIR_SEPARATOR);
78 /* Skip the separator */
85 if (strlen(filename) >= (size_t)maxlen)
87 gmx_fatal(FARGS, "filename is longer (%d) than maxlen (%d)",
88 strlen(filename), maxlen);
90 strcpy(filebase, cptr);
91 /* Remove the extension */
92 ptr = strrchr(filebase, '.');
99 int fflib_search_file_end(const char *ffdir,
100 const char *file_end,
101 gmx_bool bFatalError,
106 std::string ffdirFull(gmx::getLibraryFileFinder().findFile(ffdir));
107 std::vector<std::string> result
108 = gmx::DirectoryEnumerator::enumerateFilesWithExtension(
109 ffdirFull.c_str(), file_end, true);
110 if (result.empty() && bFatalError)
113 = gmx::formatString("Could not find any files ending on '%s' "
114 "in the force field directory '%s'",
116 GMX_THROW(gmx::InvalidInputError(message));
118 const int count = static_cast<int>(result.size());
119 for (int i = 0; i < count; ++i)
121 result[i] = gmx::Path::join(ffdir, result[i]);
125 for (int i = 0; i < count; ++i)
127 fns[i] = gmx_strdup(result[i].c_str());
132 GMX_CATCH_ALL_AND_EXIT_WITH_FATAL_ERROR;
135 std::vector<gmx::DataFileInfo> fflib_enumerate_forcefields()
137 const char *const dirend = fflib_forcefield_dir_ext();
138 const char *const filename = fflib_forcefield_itp();
139 std::vector<gmx::DataFileInfo> candidates
140 = gmx::getLibraryFileFinder().enumerateFiles(
141 gmx::DataFileOptions(dirend)
142 .throwIfNotFound(false));
144 std::vector<gmx::DataFileInfo> result;
145 for (size_t i = 0; i < candidates.size(); ++i)
147 std::string testPath(gmx::Path::join(
148 candidates[i].dir, candidates[i].name, filename));
149 // TODO: Consider also checking that the directory can be listed.
150 if (gmx::File::exists(testPath))
152 result.push_back(candidates[i]);
156 // TODO: Consider merging this into enumerateFiles(), such that the error
157 // could also list the directories searched.
161 = gmx::formatString("No force fields found (files with name '%s' "
162 "in subdirectories ending on '%s')",
164 GMX_THROW(gmx::InvalidInputError(message));
170 gmx_bool fflib_fexist(const char *file)
174 file_fullpath = low_gmxlibfn(file, TRUE, FALSE);
176 if (file_fullpath == NULL)
182 sfree(file_fullpath);
189 FILE *fflib_open(const char *file)
194 file_fullpath = gmxlibfn(file);
195 fprintf(stderr, "Opening force field file %s\n", file_fullpath);
196 fp = gmx_ffopen(file_fullpath, "r");
197 sfree(file_fullpath);