2 * This file is part of the GROMACS molecular simulation package.
4 * Copyright (c) 2012,2013,2014, 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 classes in filenameoption.h and filenameoptionstorage.h.
39 * \author Teemu Murtola <teemu.murtola@gmail.com>
40 * \ingroup module_options
42 #include "filenameoption.h"
43 #include "filenameoptionstorage.h"
48 #include "gromacs/fileio/filenm.h"
50 #include "gromacs/utility/arrayref.h"
51 #include "gromacs/utility/file.h"
52 #include "gromacs/utility/stringutil.h"
60 class FileTypeRegistry;
62 //! \addtogroup module_options
65 //! Shorthand for a list of file extensions.
66 typedef std::vector<const char *> ExtensionList;
68 /********************************************************************
73 * Handles a single file type known to FileNameOptionStorage.
78 //! Returns the list of extensions for this file type.
79 const ExtensionList &extensions() const { return extensions_; }
81 //! Returns whether \p filename has a valid extension for this type.
82 bool hasKnownExtension(const std::string &filename) const;
83 //! Adds a default extension for this type to \p filename.
84 std::string addExtension(const std::string &filename) const;
86 * Adds an extension to \p filename if it results in an existing file.
88 * Tries to add each extension for this file type to \p filename and
89 * checks whether this results in an existing file.
90 * The first match is returned.
91 * Returns an empty string if no existing file is found.
93 std::string findFileWithExtension(const std::string &filename) const;
96 //! Possible extensions for this file type.
97 ExtensionList extensions_;
100 * Needed for initialization; all initialization is handled by
103 friend class FileTypeRegistry;
107 FileTypeHandler::hasKnownExtension(const std::string &filename) const
109 for (size_t i = 0; i < extensions_.size(); ++i)
111 if (endsWith(filename, extensions_[i]))
120 FileTypeHandler::addExtension(const std::string &filename) const
122 if (extensions_.empty())
126 return filename + extensions_[0];
130 FileTypeHandler::findFileWithExtension(const std::string &filename) const
132 for (size_t i = 0; i < extensions_.size(); ++i)
134 std::string testFilename(filename + extensions_[i]);
135 if (File::exists(testFilename))
140 return std::string();
143 /********************************************************************
148 * Singleton for managing static file type info for FileNameOptionStorage.
150 class FileTypeRegistry
153 //! Returns a singleton instance of this class.
154 static const FileTypeRegistry &instance();
155 //! Returns a handler for a single file type.
156 const FileTypeHandler &
157 handlerForType(OptionFileType type, int legacyType) const;
160 //! Initializes the file type registry.
163 //! Registers a file type that corresponds to a ftp in filenm.h.
164 void registerType(int type, int ftp);
165 //! Registers a file type with a single extension.
166 void registerType(int type, const char *extension);
168 std::vector<FileTypeHandler> filetypes_;
172 const FileTypeRegistry &
173 FileTypeRegistry::instance()
175 static FileTypeRegistry singleton;
179 const FileTypeHandler &
180 FileTypeRegistry::handlerForType(OptionFileType type, int legacyType) const
183 if (type == eftUnknown && legacyType >= 0)
185 index = eftOptionFileType_NR + legacyType;
187 GMX_RELEASE_ASSERT(index >= 0 && static_cast<size_t>(index) < filetypes_.size(),
188 "Invalid file type");
189 return filetypes_[index];
192 FileTypeRegistry::FileTypeRegistry()
194 filetypes_.resize(eftOptionFileType_NR + efNR);
195 registerType(eftTopology, efTPS);
196 registerType(eftTrajectory, efTRX);
197 registerType(eftPDB, efPDB);
198 registerType(eftIndex, efNDX);
199 registerType(eftPlot, efXVG);
200 registerType(eftGenericData, efDAT);
201 for (int i = 0; i < efNR; ++i)
203 registerType(eftOptionFileType_NR + i, i);
207 void FileTypeRegistry::registerType(int type, int ftp)
209 GMX_RELEASE_ASSERT(type >= 0 && static_cast<size_t>(type) < filetypes_.size(),
210 "Invalid file type");
211 const int genericTypeCount = ftp2generic_count(ftp);
212 if (genericTypeCount > 0)
214 const int *const genericTypes = ftp2generic_list(ftp);
215 filetypes_[type].extensions_.clear();
216 filetypes_[type].extensions_.reserve(genericTypeCount);
217 for (int i = 0; i < genericTypeCount; ++i)
219 filetypes_[type].extensions_.push_back(ftp2ext_with_dot(genericTypes[i]));
224 registerType(type, ftp2ext_with_dot(ftp));
228 void FileTypeRegistry::registerType(int type, const char *extension)
230 GMX_RELEASE_ASSERT(type >= 0 && static_cast<size_t>(type) < filetypes_.size(),
231 "Invalid file type");
232 filetypes_[type].extensions_.assign(1, extension);
236 * Helper method to complete a file name provided to a file name option.
238 * \param[in] value Value provided to the file name option.
239 * \param[in] filetype File type for the option.
240 * \param[in] legacyType If \p filetype is eftUnknown, this gives the type as
241 * an enum value from filenm.h.
242 * \param[in] bCompleteToExisting
243 * Whether to check existing files when completing the extension.
244 * \returns \p value with possible extension added.
246 std::string completeFileName(const std::string &value, OptionFileType filetype,
247 int legacyType, bool bCompleteToExisting)
249 if (bCompleteToExisting && File::exists(value))
251 // TODO: This may not work as expected if the value is passed to a
252 // function that uses fn2ftp() to determine the file type and the input
253 // file has an unrecognized extension.
256 const FileTypeRegistry ®istry = FileTypeRegistry::instance();
257 const FileTypeHandler &typeHandler = registry.handlerForType(filetype, legacyType);
258 if (typeHandler.hasKnownExtension(value))
262 if (bCompleteToExisting)
264 std::string newValue = typeHandler.findFileWithExtension(value);
265 if (!newValue.empty())
270 return typeHandler.addExtension(value);
277 /********************************************************************
278 * FileNameOptionStorage
281 FileNameOptionStorage::FileNameOptionStorage(const FileNameOption &settings)
282 : MyBase(settings), info_(this), filetype_(settings.filetype_),
283 legacyType_(settings.legacyType_),
284 bRead_(settings.bRead_), bWrite_(settings.bWrite_),
285 bLibrary_(settings.bLibrary_)
287 if (settings.defaultBasename_ != NULL)
289 std::string defaultValue =
290 completeFileName(settings.defaultBasename_, filetype_,
292 setDefaultValueIfSet(defaultValue);
295 setDefaultValue(defaultValue);
300 std::string FileNameOptionStorage::typeString() const
302 const FileTypeRegistry ®istry = FileTypeRegistry::instance();
303 const FileTypeHandler &typeHandler = registry.handlerForType(filetype_, legacyType_);
304 const ExtensionList &extensions = typeHandler.extensions();
306 ExtensionList::const_iterator i;
308 for (i = extensions.begin(); count < 2 && i != extensions.end(); ++i, ++count)
310 if (i != extensions.begin())
316 if (i != extensions.end())
318 result.append("/...");
322 if (legacyType_ == efRND)
334 std::string FileNameOptionStorage::formatExtraDescription() const
336 const FileTypeRegistry ®istry = FileTypeRegistry::instance();
337 const FileTypeHandler &typeHandler = registry.handlerForType(filetype_, legacyType_);
338 const ExtensionList &extensions = typeHandler.extensions();
340 if (extensions.size() > 2)
343 ExtensionList::const_iterator i;
344 for (i = extensions.begin(); i != extensions.end(); ++i)
347 result.append((*i) + 1);
353 std::string FileNameOptionStorage::formatSingleValue(const std::string &value) const
358 void FileNameOptionStorage::convertValue(const std::string &value)
360 bool bInput = isInputFile() || isInputOutputFile();
361 addValue(completeFileName(value, filetype_, legacyType_, bInput));
364 bool FileNameOptionStorage::isDirectoryOption() const
366 return legacyType_ == efRND;
369 ConstArrayRef<const char *> FileNameOptionStorage::extensions() const
371 const FileTypeRegistry ®istry = FileTypeRegistry::instance();
372 const FileTypeHandler &typeHandler = registry.handlerForType(filetype_, legacyType_);
373 const ExtensionList &extensions = typeHandler.extensions();
374 return ConstArrayRef<const char *>(extensions.begin(), extensions.end());
377 /********************************************************************
381 FileNameOptionInfo::FileNameOptionInfo(FileNameOptionStorage *option)
386 const FileNameOptionStorage &FileNameOptionInfo::option() const
388 return static_cast<const FileNameOptionStorage &>(OptionInfo::option());
391 bool FileNameOptionInfo::isInputFile() const
393 return option().isInputFile();
396 bool FileNameOptionInfo::isOutputFile() const
398 return option().isOutputFile();
401 bool FileNameOptionInfo::isInputOutputFile() const
403 return option().isInputOutputFile();
406 bool FileNameOptionInfo::isLibraryFile() const
408 return option().isLibraryFile();
411 bool FileNameOptionInfo::isDirectoryOption() const
413 return option().isDirectoryOption();
416 FileNameOptionInfo::ExtensionList FileNameOptionInfo::extensions() const
418 return option().extensions();
421 /********************************************************************
425 AbstractOptionStoragePointer FileNameOption::createStorage() const
427 return AbstractOptionStoragePointer(new FileNameOptionStorage(*this));