Remove gmx::File (except for File::exists())
[alexxy/gromacs.git] / src / gromacs / gmxpreprocess / fflibutil.cpp
1 /*
2  * This file is part of the GROMACS molecular simulation package.
3  *
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.
8  *
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.
13  *
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.
18  *
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.
23  *
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.
31  *
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.
34  */
35 #include "gmxpre.h"
36
37 #include "fflibutil.h"
38
39 #include <string.h>
40
41 #include <string>
42 #include <vector>
43
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"
54
55 const char *fflib_forcefield_dir_ext()
56 {
57     return ".ff";
58 }
59
60 const char *fflib_forcefield_itp()
61 {
62     return "forcefield.itp";
63 }
64
65 const char *fflib_forcefield_doc()
66 {
67     return "forcefield.doc";
68 }
69
70 void fflib_filename_base(const char *filename, char *filebase, int maxlen)
71 {
72     const char *cptr;
73     char       *ptr;
74
75     cptr = strrchr(filename, DIR_SEPARATOR);
76     if (cptr != NULL)
77     {
78         /* Skip the separator */
79         cptr += 1;
80     }
81     else
82     {
83         cptr = filename;
84     }
85     if (strlen(filename) >= (size_t)maxlen)
86     {
87         gmx_fatal(FARGS, "filename is longer (%d) than maxlen (%d)",
88                   strlen(filename), maxlen);
89     }
90     strcpy(filebase, cptr);
91     /* Remove the extension */
92     ptr = strrchr(filebase, '.');
93     if (ptr != NULL)
94     {
95         ptr[0] = '\0';
96     }
97 }
98
99 int fflib_search_file_end(const char *ffdir,
100                           const char *file_end,
101                           gmx_bool    bFatalError,
102                           char     ***filenames)
103 {
104     try
105     {
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)
111         {
112             std::string message
113                 = gmx::formatString("Could not find any files ending on '%s' "
114                                     "in the force field directory '%s'",
115                                     file_end, ffdir);
116             GMX_THROW(gmx::InvalidInputError(message));
117         }
118         const int count = static_cast<int>(result.size());
119         for (int i = 0; i < count; ++i)
120         {
121             result[i] = gmx::Path::join(ffdir, result[i]);
122         }
123         char    **fns;
124         snew(fns, count);
125         for (int i = 0; i < count; ++i)
126         {
127             fns[i] = gmx_strdup(result[i].c_str());
128         }
129         *filenames = fns;
130         return count;
131     }
132     GMX_CATCH_ALL_AND_EXIT_WITH_FATAL_ERROR;
133 }
134
135 std::vector<gmx::DataFileInfo> fflib_enumerate_forcefields()
136 {
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));
143
144     std::vector<gmx::DataFileInfo> result;
145     for (size_t i = 0; i < candidates.size(); ++i)
146     {
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))
151         {
152             result.push_back(candidates[i]);
153         }
154     }
155
156     // TODO: Consider merging this into enumerateFiles(), such that the error
157     // could also list the directories searched.
158     if (result.empty())
159     {
160         std::string message
161             = gmx::formatString("No force fields found (files with name '%s' "
162                                 "in subdirectories ending on '%s')",
163                                 filename, dirend);
164         GMX_THROW(gmx::InvalidInputError(message));
165     }
166
167     return result;
168 }
169
170 gmx_bool fflib_fexist(const char *file)
171 {
172     char *file_fullpath;
173
174     file_fullpath = low_gmxlibfn(file, TRUE, FALSE);
175
176     if (file_fullpath == NULL)
177     {
178         return FALSE;
179     }
180     else
181     {
182         sfree(file_fullpath);
183
184         return TRUE;
185     }
186 }
187
188
189 FILE *fflib_open(const char *file)
190 {
191     char *file_fullpath;
192     FILE *fp;
193
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);
198
199     return fp;
200 }