Split lines with many copyright years
[gromacs.git] / src / gromacs / gmxpreprocess / fflibutil.cpp
blob9942775507f008c588a81bf8201b3d30a847ef75
1 /*
2 * This file is part of the GROMACS molecular simulation package.
4 * Copyright (c) 2010,2012,2013,2014,2015 by the GROMACS development team.
5 * Copyright (c) 2017,2018,2019,2020, by the GROMACS development team, led by
6 * Mark Abraham, David van der Spoel, Berk Hess, and Erik Lindahl,
7 * and including many others, as listed in the AUTHORS file in the
8 * top-level source directory and at http://www.gromacs.org.
10 * GROMACS is free software; you can redistribute it and/or
11 * modify it under the terms of the GNU Lesser General Public License
12 * as published by the Free Software Foundation; either version 2.1
13 * of the License, or (at your option) any later version.
15 * GROMACS is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 * Lesser General Public License for more details.
20 * You should have received a copy of the GNU Lesser General Public
21 * License along with GROMACS; if not, see
22 * http://www.gnu.org/licenses, or write to the Free Software Foundation,
23 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
25 * If you want to redistribute modifications to GROMACS, please
26 * consider that scientific software is very special. Version
27 * control is crucial - bugs must be traceable. We will be happy to
28 * consider code for inclusion in the official distribution, but
29 * derived work must not be called official GROMACS. Details are found
30 * in the README & COPYING files - if they are missing, get the
31 * official version at http://www.gromacs.org.
33 * To help us fund GROMACS development, we humbly ask that you cite
34 * the research papers on the package. Check out http://www.gromacs.org.
36 #include "gmxpre.h"
38 #include "fflibutil.h"
40 #include <cstring>
42 #include <string>
43 #include <vector>
45 #include "gromacs/utility/cstringutil.h"
46 #include "gromacs/utility/datafilefinder.h"
47 #include "gromacs/utility/dir_separator.h"
48 #include "gromacs/utility/directoryenumerator.h"
49 #include "gromacs/utility/exceptions.h"
50 #include "gromacs/utility/fatalerror.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()
58 return ".ff";
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)
73 const char* cptr;
74 char* ptr;
76 cptr = strrchr(filename, DIR_SEPARATOR);
77 if (cptr != nullptr)
79 /* Skip the separator */
80 cptr += 1;
82 else
84 cptr = filename;
86 if (strlen(filename) >= static_cast<size_t>(maxlen))
88 gmx_fatal(FARGS, "filename is longer (%zu) than maxlen (%d)", 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, const char* file_end, bool bFatalError)
103 std::string ffdirFull(gmx::getLibraryFileFinder().findFile(ffdir));
104 std::vector<std::string> result = gmx::DirectoryEnumerator::enumerateFilesWithExtension(
105 ffdirFull.c_str(), file_end, true);
106 if (result.empty() && bFatalError)
108 std::string message = gmx::formatString(
109 "Could not find any files ending on '%s' "
110 "in the force field directory '%s'",
111 file_end, ffdir);
112 GMX_THROW(gmx::InvalidInputError(message));
114 for (std::string& filename : result)
116 filename = gmx::Path::join(ffdir, filename);
118 return result;
120 GMX_CATCH_ALL_AND_EXIT_WITH_FATAL_ERROR
123 std::vector<gmx::DataFileInfo> fflib_enumerate_forcefields()
125 const char* const dirend = fflib_forcefield_dir_ext();
126 const char* const filename = fflib_forcefield_itp();
127 std::vector<gmx::DataFileInfo> candidates = gmx::getLibraryFileFinder().enumerateFiles(
128 gmx::DataFileOptions(dirend).throwIfNotFound(false));
130 std::vector<gmx::DataFileInfo> result;
131 for (size_t i = 0; i < candidates.size(); ++i)
133 std::string testPath(gmx::Path::join(candidates[i].dir, candidates[i].name, filename));
134 // TODO: Consider also checking that the directory can be listed.
135 if (gmx::File::exists(testPath, gmx::File::returnFalseOnError))
137 result.push_back(candidates[i]);
141 // TODO: Consider merging this into enumerateFiles(), such that the error
142 // could also list the directories searched.
143 if (result.empty())
145 std::string message = gmx::formatString(
146 "No force fields found (files with name '%s' "
147 "in subdirectories ending on '%s')",
148 filename, dirend);
149 GMX_THROW(gmx::InvalidInputError(message));
152 return result;
155 bool fflib_fexist(const std::string& file)
157 return !gmx::findLibraryFile(file, true, false).empty();
161 FILE* fflib_open(const std::string& file)
163 std::string fileFullPath = gmx::findLibraryFile(file);
164 fprintf(stderr, "Opening force field file %s\n", fileFullPath.c_str());
165 return gmx_ffopen(fileFullPath, "r");