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
44 #include "filenameoption.h"
45 #include "filenameoptionstorage.h"
52 #include "gromacs/fileio/filenm.h"
53 #include "gromacs/options/filenameoptionmanager.h"
54 #include "gromacs/options/optionmanagercontainer.h"
55 #include "gromacs/utility/arrayref.h"
56 #include "gromacs/utility/exceptions.h"
57 #include "gromacs/utility/gmxassert.h"
58 #include "gromacs/utility/stringutil.h"
66 //! \addtogroup module_options
70 * Mapping from OptionFileType to a file type in filenm.h.
72 struct FileTypeMapping
74 //! OptionFileType value to map.
75 OptionFileType optionType;
76 //! Corresponding file type from filenm.h.
80 //! Mappings from OptionFileType to file types in filenm.h.
81 const FileTypeMapping c_fileTypeMapping[] =
83 { eftTopology, efTPS },
84 { eftTrajectory, efTRX },
88 { eftGenericData, efDAT }
91 /********************************************************************
97 * Handles a single file type known to FileNameOptionStorage.
99 * Methods in this class do not throw, except for a possible std::bad_alloc
100 * when constructing std::string return values.
102 class FileTypeHandler
106 * Returns a handler for a single file type.
108 * \param[in] fileType File type (from filenm.h) to use.
110 explicit FileTypeHandler(int fileType);
112 //! Returns the number of acceptable extensions for this file type.
113 int extensionCount() const;
114 //! Returns the extension with the given index.
115 const char *extension(int i) const;
117 //! Returns whether \p fileType (from filenm.h) is accepted for this type.
118 bool isValidType(int fileType) const;
122 * File type (from filenm.h) represented by this handler.
124 * -1 represents an unknown file type.
127 //! Number of different extensions this type supports.
130 * List of simple file types that are included in this type.
132 * If `fileType_` represents a generic type in filenm.h, i.e., a type
133 * that accepts multiple different types of files, then this is an
134 * array of `extensionCount_` elements, each element specifying one
135 * non-generic file type that this option accepts.
136 * `NULL` for single-extension types.
138 const int *genericTypes_;
141 FileTypeHandler::FileTypeHandler(int fileType)
142 : fileType_(fileType), extensionCount_(0), genericTypes_(NULL)
146 const int genericTypeCount = ftp2generic_count(fileType_);
147 if (genericTypeCount > 0)
149 extensionCount_ = genericTypeCount;
150 genericTypes_ = ftp2generic_list(fileType_);
152 else if (ftp2ext_with_dot(fileType_)[0] != '\0')
159 int FileTypeHandler::extensionCount() const
161 return extensionCount_;
164 const char *FileTypeHandler::extension(int i) const
166 GMX_ASSERT(i >= 0 && i < extensionCount_, "Invalid extension index");
167 if (genericTypes_ != NULL)
169 return ftp2ext_with_dot(genericTypes_[i]);
171 return ftp2ext_with_dot(fileType_);
175 FileTypeHandler::isValidType(int fileType) const
177 if (genericTypes_ != NULL)
179 for (int i = 0; i < extensionCount(); ++i)
181 if (fileType == genericTypes_[i])
190 return fileType == fileType_;
198 /********************************************************************
199 * FileNameOptionStorage
202 FileNameOptionStorage::FileNameOptionStorage(const FileNameOption &settings,
203 FileNameOptionManager *manager)
204 : MyBase(settings), info_(this), manager_(manager), fileType_(-1),
205 bRead_(settings.bRead_), bWrite_(settings.bWrite_),
206 bLibrary_(settings.bLibrary_)
208 GMX_RELEASE_ASSERT(!hasFlag(efOption_MultipleTimes),
209 "allowMultiple() is not supported for file name options");
210 if (settings.optionType_ == eftUnknown && settings.legacyType_ >= 0)
212 fileType_ = settings.legacyType_;
216 ConstArrayRef<FileTypeMapping> map(c_fileTypeMapping);
217 ConstArrayRef<FileTypeMapping>::const_iterator i;
218 for (i = map.begin(); i != map.end(); ++i)
220 if (i->optionType == settings.optionType_)
222 fileType_ = i->fileType;
227 if (settings.defaultBasename_ != NULL)
229 std::string defaultValue(settings.defaultBasename_);
230 defaultValue.append(defaultExtension());
231 setDefaultValueIfSet(defaultValue);
232 if (isRequired() || settings.bLegacyOptionalBehavior_)
234 setDefaultValue(defaultValue);
239 std::string FileNameOptionStorage::typeString() const
241 FileTypeHandler typeHandler(fileType_);
244 for (count = 0; count < 2 && count < typeHandler.extensionCount(); ++count)
250 result.append(typeHandler.extension(count));
252 if (count < typeHandler.extensionCount())
254 result.append("/...");
258 if (isDirectoryOption())
270 std::string FileNameOptionStorage::formatExtraDescription() const
272 FileTypeHandler typeHandler(fileType_);
274 if (typeHandler.extensionCount() > 2)
277 for (int i = 0; i < typeHandler.extensionCount(); ++i)
281 result.append(typeHandler.extension(i) + 1);
287 std::string FileNameOptionStorage::formatSingleValue(const std::string &value) const
292 void FileNameOptionStorage::convertValue(const std::string &value)
294 if (manager_ != NULL)
296 std::string processedValue = manager_->completeFileName(value, info_);
297 if (!processedValue.empty())
299 // If the manager returns a value, use it without further checks,
300 // except for sanity checking.
301 if (!isDirectoryOption())
303 const int fileType = fn2ftp(processedValue.c_str());
304 if (fileType == efNR)
306 // If the manager returned an invalid file name, assume
307 // that it knows what it is doing. But assert that it
308 // only does that for the only case that it is currently
309 // required for: VMD plugins.
310 GMX_ASSERT(isInputFile() && isTrajectoryOption(),
311 "Manager returned an invalid file name");
315 GMX_ASSERT(isValidType(fileType),
316 "Manager returned an invalid file name");
319 addValue(processedValue);
323 // Currently, directory options are simple, and don't need any
324 // special processing.
325 // TODO: Consider splitting them into a separate DirectoryOption.
326 if (isDirectoryOption())
331 const int fileType = fn2ftp(value.c_str());
332 if (fileType == efNR)
335 = formatString("File '%s' cannot be used by GROMACS because it "
336 "does not have a recognizable extension.\n"
337 "The following extensions are possible for this option:\n %s",
338 value.c_str(), joinStrings(extensions(), ", ").c_str());
339 GMX_THROW(InvalidInputError(message));
341 else if (!isValidType(fileType))
344 = formatString("File name '%s' cannot be used for this option.\n"
345 "Only the following extensions are possible:\n %s",
346 value.c_str(), joinStrings(extensions(), ", ").c_str());
347 GMX_THROW(InvalidInputError(message));
352 void FileNameOptionStorage::processAll()
354 if (manager_ != NULL && hasFlag(efOption_HasDefaultValue))
356 ValueList &valueList = values();
357 GMX_RELEASE_ASSERT(valueList.size() == 1,
358 "There should be only one default value");
359 if (!valueList[0].empty())
361 const std::string &oldValue = valueList[0];
362 GMX_ASSERT(endsWith(oldValue, defaultExtension()),
363 "Default value does not have the expected extension");
364 const std::string prefix
365 = stripSuffixIfPresent(oldValue, defaultExtension());
366 const std::string newValue
367 = manager_->completeDefaultFileName(prefix, info_);
368 if (!newValue.empty() && newValue != oldValue)
370 GMX_ASSERT(isValidType(fn2ftp(newValue.c_str())),
371 "Manager returned an invalid default value");
372 valueList[0] = newValue;
379 bool FileNameOptionStorage::isDirectoryOption() const
381 return fileType_ == efRND;
384 bool FileNameOptionStorage::isTrajectoryOption() const
386 return fileType_ == efTRX;
389 const char *FileNameOptionStorage::defaultExtension() const
391 FileTypeHandler typeHandler(fileType_);
392 if (typeHandler.extensionCount() == 0)
396 return typeHandler.extension(0);
399 std::vector<const char *> FileNameOptionStorage::extensions() const
401 FileTypeHandler typeHandler(fileType_);
402 std::vector<const char *> result;
403 result.reserve(typeHandler.extensionCount());
404 for (int i = 0; i < typeHandler.extensionCount(); ++i)
406 result.push_back(typeHandler.extension(i));
411 bool FileNameOptionStorage::isValidType(int fileType) const
413 FileTypeHandler typeHandler(fileType_);
414 return typeHandler.isValidType(fileType);
417 ConstArrayRef<int> FileNameOptionStorage::fileTypes() const
421 return ConstArrayRef<int>();
423 const int genericTypeCount = ftp2generic_count(fileType_);
424 if (genericTypeCount > 0)
426 return constArrayRefFromArray<int>(ftp2generic_list(fileType_), genericTypeCount);
428 return constArrayRefFromArray<int>(&fileType_, 1);
431 /********************************************************************
435 FileNameOptionInfo::FileNameOptionInfo(FileNameOptionStorage *option)
440 const FileNameOptionStorage &FileNameOptionInfo::option() const
442 return static_cast<const FileNameOptionStorage &>(OptionInfo::option());
445 bool FileNameOptionInfo::isInputFile() const
447 return option().isInputFile();
450 bool FileNameOptionInfo::isOutputFile() const
452 return option().isOutputFile();
455 bool FileNameOptionInfo::isInputOutputFile() const
457 return option().isInputOutputFile();
460 bool FileNameOptionInfo::isLibraryFile() const
462 return option().isLibraryFile();
465 bool FileNameOptionInfo::isDirectoryOption() const
467 return option().isDirectoryOption();
470 bool FileNameOptionInfo::isTrajectoryOption() const
472 return option().isTrajectoryOption();
475 const char *FileNameOptionInfo::defaultExtension() const
477 return option().defaultExtension();
480 FileNameOptionInfo::ExtensionList FileNameOptionInfo::extensions() const
482 return option().extensions();
485 bool FileNameOptionInfo::isValidType(int fileType) const
487 return option().isValidType(fileType);
490 ConstArrayRef<int> FileNameOptionInfo::fileTypes() const
492 return option().fileTypes();
495 /********************************************************************
499 AbstractOptionStorage *
500 FileNameOption::createStorage(const OptionManagerContainer &managers) const
502 return new FileNameOptionStorage(*this, managers.get<FileNameOptionManager>());