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/file.h"
51 #include "gromacs/utility/stringutil.h"
59 class FileTypeRegistry;
61 //! \addtogroup module_options
64 //! Shorthand for a list of file extensions.
65 typedef std::vector<const char *> ExtensionList;
67 /********************************************************************
72 * Handles a single file type known to FileNameOptionStorage.
77 //! Returns the list of extensions for this file type.
78 const ExtensionList &extensions() const { return extensions_; }
80 //! Returns whether \p filename has a valid extension for this type.
81 bool hasKnownExtension(const std::string &filename) const;
82 //! Adds a default extension for this type to \p filename.
83 std::string addExtension(const std::string &filename) const;
85 * Adds an extension to \p filename if it results in an existing file.
87 * Tries to add each extension for this file type to \p filename and
88 * checks whether this results in an existing file.
89 * The first match is returned.
90 * Returns an empty string if no existing file is found.
92 std::string findFileWithExtension(const std::string &filename) const;
95 //! Possible extensions for this file type.
96 ExtensionList extensions_;
99 * Needed for initialization; all initialization is handled by
102 friend class FileTypeRegistry;
106 FileTypeHandler::hasKnownExtension(const std::string &filename) const
108 for (size_t i = 0; i < extensions_.size(); ++i)
110 if (endsWith(filename, extensions_[i]))
119 FileTypeHandler::addExtension(const std::string &filename) const
121 if (extensions_.empty())
125 return filename + extensions_[0];
129 FileTypeHandler::findFileWithExtension(const std::string &filename) const
131 for (size_t i = 0; i < extensions_.size(); ++i)
133 std::string testFilename(filename + extensions_[i]);
134 if (File::exists(testFilename))
139 return std::string();
142 /********************************************************************
147 * Singleton for managing static file type info for FileNameOptionStorage.
149 class FileTypeRegistry
152 //! Returns a singleton instance of this class.
153 static const FileTypeRegistry &instance();
154 //! Returns a handler for a single file type.
155 const FileTypeHandler &
156 handlerForType(OptionFileType type, int legacyType) const;
159 //! Initializes the file type registry.
162 //! Registers a file type that corresponds to a ftp in filenm.h.
163 void registerType(int type, int ftp);
164 //! Registers a file type with a single extension.
165 void registerType(int type, const char *extension);
167 std::vector<FileTypeHandler> filetypes_;
171 const FileTypeRegistry &
172 FileTypeRegistry::instance()
174 static FileTypeRegistry singleton;
178 const FileTypeHandler &
179 FileTypeRegistry::handlerForType(OptionFileType type, int legacyType) const
182 if (type == eftUnknown && legacyType >= 0)
184 index = eftOptionFileType_NR + legacyType;
186 GMX_RELEASE_ASSERT(index >= 0 && static_cast<size_t>(index) < filetypes_.size(),
187 "Invalid file type");
188 return filetypes_[index];
191 FileTypeRegistry::FileTypeRegistry()
193 filetypes_.resize(eftOptionFileType_NR + efNR);
194 registerType(eftTopology, efTPS);
195 registerType(eftTrajectory, efTRX);
196 registerType(eftPDB, efPDB);
197 registerType(eftIndex, efNDX);
198 registerType(eftPlot, efXVG);
199 registerType(eftGenericData, efDAT);
200 for (int i = 0; i < efNR; ++i)
202 registerType(eftOptionFileType_NR + i, i);
206 void FileTypeRegistry::registerType(int type, int ftp)
208 GMX_RELEASE_ASSERT(type >= 0 && static_cast<size_t>(type) < filetypes_.size(),
209 "Invalid file type");
210 const int genericTypeCount = ftp2generic_count(ftp);
211 if (genericTypeCount > 0)
213 const int *const genericTypes = ftp2generic_list(ftp);
214 filetypes_[type].extensions_.clear();
215 filetypes_[type].extensions_.reserve(genericTypeCount);
216 for (int i = 0; i < genericTypeCount; ++i)
218 filetypes_[type].extensions_.push_back(ftp2ext_with_dot(genericTypes[i]));
223 registerType(type, ftp2ext_with_dot(ftp));
227 void FileTypeRegistry::registerType(int type, const char *extension)
229 GMX_RELEASE_ASSERT(type >= 0 && static_cast<size_t>(type) < filetypes_.size(),
230 "Invalid file type");
231 filetypes_[type].extensions_.assign(1, extension);
235 * Helper method to complete a file name provided to a file name option.
237 * \param[in] value Value provided to the file name option.
238 * \param[in] filetype File type for the option.
239 * \param[in] legacyType If \p filetype is eftUnknown, this gives the type as
240 * an enum value from filenm.h.
241 * \param[in] bCompleteToExisting
242 * Whether to check existing files when completing the extension.
243 * \returns \p value with possible extension added.
245 std::string completeFileName(const std::string &value, OptionFileType filetype,
246 int legacyType, bool bCompleteToExisting)
248 if (bCompleteToExisting && File::exists(value))
250 // TODO: This may not work as expected if the value is passed to a
251 // function that uses fn2ftp() to determine the file type and the input
252 // file has an unrecognized extension.
255 const FileTypeRegistry ®istry = FileTypeRegistry::instance();
256 const FileTypeHandler &typeHandler = registry.handlerForType(filetype, legacyType);
257 if (typeHandler.hasKnownExtension(value))
261 if (bCompleteToExisting)
263 std::string newValue = typeHandler.findFileWithExtension(value);
264 if (!newValue.empty())
269 return typeHandler.addExtension(value);
276 /********************************************************************
277 * FileNameOptionStorage
280 FileNameOptionStorage::FileNameOptionStorage(const FileNameOption &settings)
281 : MyBase(settings), info_(this), filetype_(settings.filetype_),
282 legacyType_(settings.legacyType_),
283 bRead_(settings.bRead_), bWrite_(settings.bWrite_),
284 bLibrary_(settings.bLibrary_)
286 if (settings.defaultBasename_ != NULL)
288 std::string defaultValue =
289 completeFileName(settings.defaultBasename_, filetype_,
291 setDefaultValueIfSet(defaultValue);
294 setDefaultValue(defaultValue);
299 std::string FileNameOptionStorage::typeString() const
301 const FileTypeRegistry ®istry = FileTypeRegistry::instance();
302 const FileTypeHandler &typeHandler = registry.handlerForType(filetype_, legacyType_);
303 const ExtensionList &extensions = typeHandler.extensions();
305 ExtensionList::const_iterator i;
307 for (i = extensions.begin(); count < 2 && i != extensions.end(); ++i, ++count)
309 if (i != extensions.begin())
315 if (i != extensions.end())
317 result.append("/...");
321 if (legacyType_ == efRND)
333 std::string FileNameOptionStorage::formatExtraDescription() const
335 const FileTypeRegistry ®istry = FileTypeRegistry::instance();
336 const FileTypeHandler &typeHandler = registry.handlerForType(filetype_, legacyType_);
337 const ExtensionList &extensions = typeHandler.extensions();
339 if (extensions.size() > 2)
342 ExtensionList::const_iterator i;
343 for (i = extensions.begin(); i != extensions.end(); ++i)
346 result.append((*i) + 1);
352 std::string FileNameOptionStorage::formatSingleValue(const std::string &value) const
357 void FileNameOptionStorage::convertValue(const std::string &value)
359 bool bInput = isInputFile() || isInputOutputFile();
360 addValue(completeFileName(value, filetype_, legacyType_, bInput));
363 /********************************************************************
367 FileNameOptionInfo::FileNameOptionInfo(FileNameOptionStorage *option)
372 const FileNameOptionStorage &FileNameOptionInfo::option() const
374 return static_cast<const FileNameOptionStorage &>(OptionInfo::option());
377 bool FileNameOptionInfo::isInputFile() const
379 return option().isInputFile();
382 bool FileNameOptionInfo::isOutputFile() const
384 return option().isOutputFile();
387 bool FileNameOptionInfo::isInputOutputFile() const
389 return option().isInputOutputFile();
392 bool FileNameOptionInfo::isLibraryFile() const
394 return option().isLibraryFile();
397 /********************************************************************
401 AbstractOptionStoragePointer FileNameOption::createStorage() const
403 return AbstractOptionStoragePointer(new FileNameOptionStorage(*this));