2 * This file is part of the GROMACS molecular simulation package.
4 * Copyright (c) 2014,2015,2016,2017,2018,2019, 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 * Implements gmx::DataFileFinder.
39 * \author Teemu Murtola <teemu.murtola@gmail.com>
40 * \ingroup module_utility
44 #include "datafilefinder.h"
52 #include "buildinfo.h"
53 #include "gromacs/utility/directoryenumerator.h"
54 #include "gromacs/utility/exceptions.h"
55 #include "gromacs/utility/filestream.h"
56 #include "gromacs/utility/path.h"
57 #include "gromacs/utility/programcontext.h"
58 #include "gromacs/utility/stringutil.h"
63 /********************************************************************
64 * DataFileFinder::Impl
67 class DataFileFinder::Impl
70 static std::string getDefaultPath();
72 Impl() : envName_(nullptr), bEnvIsSet_(false) {}
76 std::vector<std::string> searchPath_;
79 std::string DataFileFinder::Impl::getDefaultPath()
81 const InstallationPrefixInfo installPrefix = getProgramContext().installationPrefix();
82 if (!isNullOrEmpty(installPrefix.path))
84 const char* const dataPath = installPrefix.bSourceLayout ? "share" : GMX_INSTALL_GMXDATADIR;
85 return Path::join(installPrefix.path, dataPath, "top");
90 /********************************************************************
94 DataFileFinder::DataFileFinder() : impl_(nullptr) {}
96 DataFileFinder::~DataFileFinder() {}
98 void DataFileFinder::setSearchPathFromEnv(const char* envVarName)
102 impl_.reset(new Impl());
104 impl_->envName_ = envVarName;
105 const char* const lib = getenv(envVarName);
106 if (!isNullOrEmpty(lib))
108 std::vector<std::string>& path = impl_->searchPath_; // convenience
109 const std::string defaultPath = impl_->getDefaultPath();
110 std::vector<std::string> tmpPath;
111 Path::splitPathEnvironment(lib, &tmpPath);
112 std::set<std::string> pathsSeen;
113 pathsSeen.insert(defaultPath);
114 for (auto& d : tmpPath)
116 if (!pathsSeen.count(d))
122 impl_->bEnvIsSet_ = true;
126 FilePtr DataFileFinder::openFile(const DataFileOptions& options) const
128 // TODO: There is a small race here, since there is some time between
129 // the exists() calls and actually opening the file. It would be better
130 // to leave the file open after a successful exists() if the desire is to
131 // actually open the file.
132 std::string filename = findFile(options);
133 if (filename.empty())
140 fprintf(debug, "Opening library file %s\n", fn);
143 return TextInputFile::openRawHandle(filename);
146 std::string DataFileFinder::findFile(const DataFileOptions& options) const
148 if (options.bCurrentDir_ && Path::exists(options.filename_))
150 return options.filename_;
152 if (impl_ != nullptr)
154 std::vector<std::string>::const_iterator i;
155 for (i = impl_->searchPath_.begin(); i != impl_->searchPath_.end(); ++i)
157 // TODO: Deal with an empty search path entry more reasonably.
158 std::string testPath = Path::join(*i, options.filename_);
159 // TODO: Consider skipping directories.
160 if (Path::exists(testPath))
166 const std::string& defaultPath = Impl::getDefaultPath();
167 if (!defaultPath.empty())
169 std::string testPath = Path::join(defaultPath, options.filename_);
170 if (Path::exists(testPath))
177 const char* const envName = (impl_ != nullptr ? impl_->envName_ : nullptr);
178 const bool bEnvIsSet = (impl_ != nullptr ? impl_->bEnvIsSet_ : false);
179 std::string message(formatString("Library file '%s' not found", options.filename_));
180 if (options.bCurrentDir_)
182 message.append(" in current dir nor");
186 message.append(formatString(" in your %s path nor", envName));
188 message.append(" in the default directories.\nThe following paths were searched:");
189 if (options.bCurrentDir_)
191 message.append("\n ");
192 message.append(Path::getWorkingDirectory());
193 message.append(" (current dir)");
195 if (impl_ != nullptr)
197 std::vector<std::string>::const_iterator i;
198 for (i = impl_->searchPath_.begin(); i != impl_->searchPath_.end(); ++i)
200 message.append("\n ");
204 if (!defaultPath.empty())
206 message.append("\n ");
207 message.append(defaultPath);
208 message.append(" (default)");
210 if (!bEnvIsSet && envName != nullptr)
213 formatString("\nYou can set additional directories to search "
214 "with the %s path variable.",
217 GMX_THROW(FileIOError(message));
219 return std::string();
222 std::vector<DataFileInfo> DataFileFinder::enumerateFiles(const DataFileOptions& options) const
224 // TODO: Consider if not being able to list one of the directories should
225 // really be a fatal error. Or alternatively, check somewhere else that
226 // paths in GMXLIB are valid.
227 std::vector<DataFileInfo> result;
228 std::vector<std::string>::const_iterator i;
229 if (options.bCurrentDir_)
231 std::vector<std::string> files =
232 DirectoryEnumerator::enumerateFilesWithExtension(".", options.filename_, false);
233 for (i = files.begin(); i != files.end(); ++i)
235 result.emplace_back(".", *i, false);
238 if (impl_ != nullptr)
240 std::vector<std::string>::const_iterator j;
241 for (j = impl_->searchPath_.begin(); j != impl_->searchPath_.end(); ++j)
243 std::vector<std::string> files = DirectoryEnumerator::enumerateFilesWithExtension(
244 j->c_str(), options.filename_, false);
245 for (i = files.begin(); i != files.end(); ++i)
247 result.emplace_back(*j, *i, false);
251 const std::string& defaultPath = Impl::getDefaultPath();
252 if (!defaultPath.empty())
254 std::vector<std::string> files = DirectoryEnumerator::enumerateFilesWithExtension(
255 defaultPath.c_str(), options.filename_, false);
256 for (i = files.begin(); i != files.end(); ++i)
258 result.emplace_back(defaultPath, *i, true);
261 if (result.empty() && options.bThrow_)
263 // TODO: Print the search path as is done in findFile().
265 formatString("Could not find any files ending on '%s' in the "
266 "current directory or the GROMACS library search path",
268 GMX_THROW(FileIOError(message));