Replace C-style library file handling with RAII approaches
[gromacs.git] / src / gromacs / gmxpreprocess / fflibutil.cpp
blob42c7ad87c43a03ccea4ec575593ccff6c9dd0d70
1 /*
2 * This file is part of the GROMACS molecular simulation package.
4 * Copyright (c) 2010,2012,2013,2014,2015,2017,2018, 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.
35 #include "gmxpre.h"
37 #include "fflibutil.h"
39 #include <cstring>
41 #include <string>
42 #include <vector>
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()
57 return ".ff";
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)
72 const char *cptr;
73 char *ptr;
75 cptr = strrchr(filename, DIR_SEPARATOR);
76 if (cptr != nullptr)
78 /* Skip the separator */
79 cptr += 1;
81 else
83 cptr = filename;
85 if (strlen(filename) >= static_cast<size_t>(maxlen))
87 gmx_fatal(FARGS, "filename is longer (%zu) than maxlen (%d)",
88 strlen(filename), maxlen);
90 strcpy(filebase, cptr);
91 /* Remove the extension */
92 ptr = strrchr(filebase, '.');
93 if (ptr != nullptr)
95 ptr[0] = '\0';
99 std::vector<std::string> fflib_search_file_end(const char *ffdir,
100 const char *file_end,
101 bool bFatalError)
105 std::string ffdirFull(gmx::getLibraryFileFinder().findFile(ffdir));
106 std::vector<std::string> result
107 = gmx::DirectoryEnumerator::enumerateFilesWithExtension(
108 ffdirFull.c_str(), file_end, true);
109 if (result.empty() && bFatalError)
111 std::string message
112 = gmx::formatString("Could not find any files ending on '%s' "
113 "in the force field directory '%s'",
114 file_end, ffdir);
115 GMX_THROW(gmx::InvalidInputError(message));
117 for (std::string &filename : result)
119 filename = gmx::Path::join(ffdir, filename);
121 return result;
123 GMX_CATCH_ALL_AND_EXIT_WITH_FATAL_ERROR;
126 std::vector<gmx::DataFileInfo> fflib_enumerate_forcefields()
128 const char *const dirend = fflib_forcefield_dir_ext();
129 const char *const filename = fflib_forcefield_itp();
130 std::vector<gmx::DataFileInfo> candidates
131 = gmx::getLibraryFileFinder().enumerateFiles(
132 gmx::DataFileOptions(dirend)
133 .throwIfNotFound(false));
135 std::vector<gmx::DataFileInfo> result;
136 for (size_t i = 0; i < candidates.size(); ++i)
138 std::string testPath(gmx::Path::join(
139 candidates[i].dir, candidates[i].name, filename));
140 // TODO: Consider also checking that the directory can be listed.
141 if (gmx::File::exists(testPath, gmx::File::returnFalseOnError))
143 result.push_back(candidates[i]);
147 // TODO: Consider merging this into enumerateFiles(), such that the error
148 // could also list the directories searched.
149 if (result.empty())
151 std::string message
152 = gmx::formatString("No force fields found (files with name '%s' "
153 "in subdirectories ending on '%s')",
154 filename, dirend);
155 GMX_THROW(gmx::InvalidInputError(message));
158 return result;
161 bool fflib_fexist(const std::string &file)
163 return !gmx::findLibraryFile(file, true, false).empty();
167 FILE *fflib_open(const std::string &file)
169 std::string fileFullPath = gmx::findLibraryFile(file);
170 fprintf(stderr, "Opening force field file %s\n", fileFullPath.c_str());
171 return gmx_ffopen(fileFullPath.c_str(), "r");