2 * This file is part of the GROMACS molecular simulation package.
4 * Copyright (c) 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.
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
44 #include "filenameoption.h"
51 #include "gromacs/fileio/filenm.h"
52 #include "gromacs/options/filenameoptionmanager.h"
53 #include "gromacs/options/optionmanagercontainer.h"
54 #include "gromacs/utility/arrayref.h"
55 #include "gromacs/utility/exceptions.h"
56 #include "gromacs/utility/gmxassert.h"
57 #include "gromacs/utility/stringutil.h"
59 #include "filenameoptionstorage.h"
67 //! \addtogroup module_options
71 * Mapping from OptionFileType to a file type in filenm.h.
73 struct FileTypeMapping
75 //! OptionFileType value to map.
76 OptionFileType optionType;
77 //! Corresponding file type from filenm.h.
81 //! Mappings from OptionFileType to file types in filenm.h.
82 const FileTypeMapping c_fileTypeMapping[] =
84 { eftTopology, efTPS },
85 { eftTrajectory, efTRX },
89 { eftGenericData, efDAT }
92 /********************************************************************
98 * Handles a single file type known to FileNameOptionStorage.
100 * Methods in this class do not throw, except for a possible std::bad_alloc
101 * when constructing std::string return values.
103 class FileTypeHandler
107 * Returns a handler for a single file type.
109 * \param[in] fileType File type (from filenm.h) to use.
111 explicit FileTypeHandler(int fileType);
113 //! Returns the number of acceptable extensions for this file type.
114 int extensionCount() const;
115 //! Returns the extension with the given index.
116 const char *extension(int i) const;
118 //! Returns whether \p fileType (from filenm.h) is accepted for this type.
119 bool isValidType(int fileType) const;
123 * File type (from filenm.h) represented by this handler.
125 * -1 represents an unknown file type.
128 //! Number of different extensions this type supports.
131 * List of simple file types that are included in this type.
133 * If `fileType_` represents a generic type in filenm.h, i.e., a type
134 * that accepts multiple different types of files, then this is an
135 * array of `extensionCount_` elements, each element specifying one
136 * non-generic file type that this option accepts.
137 * `NULL` for single-extension types.
139 const int *genericTypes_;
142 FileTypeHandler::FileTypeHandler(int fileType)
143 : fileType_(fileType), extensionCount_(0), genericTypes_(NULL)
147 const int genericTypeCount = ftp2generic_count(fileType_);
148 if (genericTypeCount > 0)
150 extensionCount_ = genericTypeCount;
151 genericTypes_ = ftp2generic_list(fileType_);
153 else if (ftp2ext_with_dot(fileType_)[0] != '\0')
160 int FileTypeHandler::extensionCount() const
162 return extensionCount_;
165 const char *FileTypeHandler::extension(int i) const
167 GMX_ASSERT(i >= 0 && i < extensionCount_, "Invalid extension index");
168 if (genericTypes_ != NULL)
170 return ftp2ext_with_dot(genericTypes_[i]);
172 return ftp2ext_with_dot(fileType_);
176 FileTypeHandler::isValidType(int fileType) const
178 if (genericTypes_ != NULL)
180 for (int i = 0; i < extensionCount(); ++i)
182 if (fileType == genericTypes_[i])
191 return fileType == fileType_;
199 /********************************************************************
200 * FileNameOptionStorage
203 FileNameOptionStorage::FileNameOptionStorage(const FileNameOption &settings,
204 FileNameOptionManager *manager)
205 : MyBase(settings), info_(this), manager_(manager), fileType_(-1),
206 defaultExtension_(""), bRead_(settings.bRead_), bWrite_(settings.bWrite_),
207 bLibrary_(settings.bLibrary_), bAllowMissing_(settings.bAllowMissing_)
209 GMX_RELEASE_ASSERT(!hasFlag(efOption_MultipleTimes),
210 "allowMultiple() is not supported for file name options");
211 if (settings.optionType_ == eftUnknown && settings.legacyType_ >= 0)
213 fileType_ = settings.legacyType_;
217 ConstArrayRef<FileTypeMapping> map(c_fileTypeMapping);
218 ConstArrayRef<FileTypeMapping>::const_iterator i;
219 for (i = map.begin(); i != map.end(); ++i)
221 if (i->optionType == settings.optionType_)
223 fileType_ = i->fileType;
228 FileTypeHandler typeHandler(fileType_);
229 if (settings.defaultType_ >= 0 && settings.defaultType_ < efNR)
231 // This also assures that the default type is not a generic type.
232 GMX_RELEASE_ASSERT(typeHandler.isValidType(settings.defaultType_),
233 "Default type for a file option is not an accepted "
234 "type for the option");
235 FileTypeHandler defaultHandler(settings.defaultType_);
236 defaultExtension_ = defaultHandler.extension(0);
238 else if (typeHandler.extensionCount() > 0)
240 defaultExtension_ = typeHandler.extension(0);
242 if (settings.defaultBasename_ != NULL)
244 std::string defaultValue(settings.defaultBasename_);
245 int type = fn2ftp(settings.defaultBasename_);
246 GMX_RELEASE_ASSERT(type == efNR || type == settings.defaultType_,
247 "Default basename has an extension that does not "
248 "match the default type");
251 defaultValue.append(defaultExtension());
253 setDefaultValueIfSet(defaultValue);
254 if (isRequired() || settings.bLegacyOptionalBehavior_)
256 setDefaultValue(defaultValue);
261 std::string FileNameOptionStorage::typeString() const
263 FileTypeHandler typeHandler(fileType_);
266 for (count = 0; count < 2 && count < typeHandler.extensionCount(); ++count)
272 result.append(typeHandler.extension(count));
274 if (count < typeHandler.extensionCount())
276 result.append("/...");
280 if (isDirectoryOption())
292 std::string FileNameOptionStorage::formatExtraDescription() const
294 FileTypeHandler typeHandler(fileType_);
296 if (typeHandler.extensionCount() > 2)
299 for (int i = 0; i < typeHandler.extensionCount(); ++i)
301 result.append(" [REF]");
303 result.append(typeHandler.extension(i) + 1);
304 result.append("[ref]");
310 std::string FileNameOptionStorage::formatSingleValue(const std::string &value) const
315 void FileNameOptionStorage::convertValue(const std::string &value)
317 if (manager_ != NULL)
319 std::string processedValue = manager_->completeFileName(value, info_);
320 if (!processedValue.empty())
322 // If the manager returns a value, use it without further checks,
323 // except for sanity checking.
324 if (!isDirectoryOption())
326 const int fileType = fn2ftp(processedValue.c_str());
327 if (fileType == efNR)
329 // If the manager returned an invalid file name, assume
330 // that it knows what it is doing. But assert that it
331 // only does that for the only case that it is currently
332 // required for: VMD plugins.
333 GMX_ASSERT(isInputFile() && isTrajectoryOption(),
334 "Manager returned an invalid file name");
338 GMX_ASSERT(isValidType(fileType),
339 "Manager returned an invalid file name");
342 addValue(processedValue);
346 // Currently, directory options are simple, and don't need any
347 // special processing.
348 // TODO: Consider splitting them into a separate DirectoryOption.
349 if (isDirectoryOption())
354 const int fileType = fn2ftp(value.c_str());
355 if (fileType == efNR)
358 = formatString("File '%s' cannot be used by GROMACS because it "
359 "does not have a recognizable extension.\n"
360 "The following extensions are possible for this option:\n %s",
361 value.c_str(), joinStrings(extensions(), ", ").c_str());
362 GMX_THROW(InvalidInputError(message));
364 else if (!isValidType(fileType))
367 = formatString("File name '%s' cannot be used for this option.\n"
368 "Only the following extensions are possible:\n %s",
369 value.c_str(), joinStrings(extensions(), ", ").c_str());
370 GMX_THROW(InvalidInputError(message));
375 void FileNameOptionStorage::processAll()
377 if (manager_ != NULL && hasFlag(efOption_HasDefaultValue))
379 ValueList &valueList = values();
380 GMX_RELEASE_ASSERT(valueList.size() == 1,
381 "There should be only one default value");
382 if (!valueList[0].empty())
384 const std::string &oldValue = valueList[0];
385 GMX_ASSERT(endsWith(oldValue, defaultExtension()),
386 "Default value does not have the expected extension");
387 const std::string prefix
388 = stripSuffixIfPresent(oldValue, defaultExtension());
389 const std::string newValue
390 = manager_->completeDefaultFileName(prefix, info_);
391 if (!newValue.empty() && newValue != oldValue)
393 GMX_ASSERT(isValidType(fn2ftp(newValue.c_str())),
394 "Manager returned an invalid default value");
395 valueList[0] = newValue;
402 bool FileNameOptionStorage::isDirectoryOption() const
404 return fileType_ == efRND;
407 bool FileNameOptionStorage::isTrajectoryOption() const
409 return fileType_ == efTRX;
412 const char *FileNameOptionStorage::defaultExtension() const
414 return defaultExtension_;
417 std::vector<const char *> FileNameOptionStorage::extensions() const
419 FileTypeHandler typeHandler(fileType_);
420 std::vector<const char *> result;
421 result.reserve(typeHandler.extensionCount());
422 for (int i = 0; i < typeHandler.extensionCount(); ++i)
424 result.push_back(typeHandler.extension(i));
429 bool FileNameOptionStorage::isValidType(int fileType) const
431 FileTypeHandler typeHandler(fileType_);
432 return typeHandler.isValidType(fileType);
435 ConstArrayRef<int> FileNameOptionStorage::fileTypes() const
439 return ConstArrayRef<int>();
441 const int genericTypeCount = ftp2generic_count(fileType_);
442 if (genericTypeCount > 0)
444 return constArrayRefFromArray<int>(ftp2generic_list(fileType_), genericTypeCount);
446 return constArrayRefFromArray<int>(&fileType_, 1);
449 /********************************************************************
453 FileNameOptionInfo::FileNameOptionInfo(FileNameOptionStorage *option)
458 const FileNameOptionStorage &FileNameOptionInfo::option() const
460 return static_cast<const FileNameOptionStorage &>(OptionInfo::option());
463 bool FileNameOptionInfo::isInputFile() const
465 return option().isInputFile();
468 bool FileNameOptionInfo::isOutputFile() const
470 return option().isOutputFile();
473 bool FileNameOptionInfo::isInputOutputFile() const
475 return option().isInputOutputFile();
478 bool FileNameOptionInfo::isLibraryFile() const
480 return option().isLibraryFile();
483 bool FileNameOptionInfo::allowMissing() const
485 return option().allowMissing();
488 bool FileNameOptionInfo::isDirectoryOption() const
490 return option().isDirectoryOption();
493 bool FileNameOptionInfo::isTrajectoryOption() const
495 return option().isTrajectoryOption();
498 const char *FileNameOptionInfo::defaultExtension() const
500 return option().defaultExtension();
503 FileNameOptionInfo::ExtensionList FileNameOptionInfo::extensions() const
505 return option().extensions();
508 bool FileNameOptionInfo::isValidType(int fileType) const
510 return option().isValidType(fileType);
513 ConstArrayRef<int> FileNameOptionInfo::fileTypes() const
515 return option().fileTypes();
518 /********************************************************************
522 AbstractOptionStorage *
523 FileNameOption::createStorage(const OptionManagerContainer &managers) const
525 return new FileNameOptionStorage(*this, managers.get<FileNameOptionManager>());