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"
50 #include "gromacs/fileio/filenm.h"
51 #include "gromacs/options/filenameoptionmanager.h"
52 #include "gromacs/options/optionmanagercontainer.h"
53 #include "gromacs/utility/arrayref.h"
54 #include "gromacs/utility/exceptions.h"
55 #include "gromacs/utility/gmxassert.h"
56 #include "gromacs/utility/stringutil.h"
64 //! \addtogroup module_options
68 * Mapping from OptionFileType to a file type in filenm.h.
70 struct FileTypeMapping
72 //! OptionFileType value to map.
73 OptionFileType optionType;
74 //! Corresponding file type from filenm.h.
78 //! Mappings from OptionFileType to file types in filenm.h.
79 const FileTypeMapping c_fileTypeMapping[] =
81 { eftTopology, efTPS },
82 { eftTrajectory, efTRX },
86 { eftGenericData, efDAT }
89 /********************************************************************
95 * Handles a single file type known to FileNameOptionStorage.
97 * Methods in this class do not throw, except for a possible std::bad_alloc
98 * when constructing std::string return values.
100 class FileTypeHandler
104 * Returns a handler for a single file type.
106 * \param[in] fileType File type (from filenm.h) to use.
108 explicit FileTypeHandler(int fileType);
110 //! Returns the number of acceptable extensions for this file type.
111 int extensionCount() const;
112 //! Returns the extension with the given index.
113 const char *extension(int i) const;
115 //! Returns whether \p fileType (from filenm.h) is accepted for this type.
116 bool isValidType(int fileType) const;
120 * File type (from filenm.h) represented by this handler.
122 * -1 represents an unknown file type.
125 //! Number of different extensions this type supports.
128 * List of simple file types that are included in this type.
130 * If `fileType_` represents a generic type in filenm.h, i.e., a type
131 * that accepts multiple different types of files, then this is an
132 * array of `extensionCount_` elements, each element specifying one
133 * non-generic file type that this option accepts.
134 * `NULL` for single-extension types.
136 const int *genericTypes_;
139 FileTypeHandler::FileTypeHandler(int fileType)
140 : fileType_(fileType), extensionCount_(0), genericTypes_(NULL)
144 const int genericTypeCount = ftp2generic_count(fileType_);
145 if (genericTypeCount > 0)
147 extensionCount_ = genericTypeCount;
148 genericTypes_ = ftp2generic_list(fileType_);
150 else if (ftp2ext_with_dot(fileType_)[0] != '\0')
157 int FileTypeHandler::extensionCount() const
159 return extensionCount_;
162 const char *FileTypeHandler::extension(int i) const
164 GMX_ASSERT(i >= 0 && i < extensionCount_, "Invalid extension index");
165 if (genericTypes_ != NULL)
167 return ftp2ext_with_dot(genericTypes_[i]);
169 return ftp2ext_with_dot(fileType_);
173 FileTypeHandler::isValidType(int fileType) const
175 if (genericTypes_ != NULL)
177 for (int i = 0; i < extensionCount(); ++i)
179 if (fileType == genericTypes_[i])
188 return fileType == fileType_;
196 /********************************************************************
197 * FileNameOptionStorage
200 FileNameOptionStorage::FileNameOptionStorage(const FileNameOption &settings,
201 FileNameOptionManager *manager)
202 : MyBase(settings), info_(this), manager_(manager), fileType_(-1),
203 bRead_(settings.bRead_), bWrite_(settings.bWrite_),
204 bLibrary_(settings.bLibrary_)
206 GMX_RELEASE_ASSERT(!hasFlag(efOption_MultipleTimes),
207 "allowMultiple() is not supported for file name options");
208 if (settings.optionType_ == eftUnknown && settings.legacyType_ >= 0)
210 fileType_ = settings.legacyType_;
214 ConstArrayRef<FileTypeMapping> map(c_fileTypeMapping);
215 ConstArrayRef<FileTypeMapping>::const_iterator i;
216 for (i = map.begin(); i != map.end(); ++i)
218 if (i->optionType == settings.optionType_)
220 fileType_ = i->fileType;
225 if (settings.defaultBasename_ != NULL)
227 std::string defaultValue(settings.defaultBasename_);
228 defaultValue.append(defaultExtension());
229 setDefaultValueIfSet(defaultValue);
230 if (isRequired() || settings.bLegacyOptionalBehavior_)
232 setDefaultValue(defaultValue);
237 std::string FileNameOptionStorage::typeString() const
239 FileTypeHandler typeHandler(fileType_);
242 for (count = 0; count < 2 && count < typeHandler.extensionCount(); ++count)
248 result.append(typeHandler.extension(count));
250 if (count < typeHandler.extensionCount())
252 result.append("/...");
256 if (isDirectoryOption())
268 std::string FileNameOptionStorage::formatExtraDescription() const
270 FileTypeHandler typeHandler(fileType_);
272 if (typeHandler.extensionCount() > 2)
275 for (int i = 0; i < typeHandler.extensionCount(); ++i)
279 result.append(typeHandler.extension(i) + 1);
285 std::string FileNameOptionStorage::formatSingleValue(const std::string &value) const
290 void FileNameOptionStorage::convertValue(const std::string &value)
292 if (manager_ != NULL)
294 std::string processedValue = manager_->completeFileName(value, info_);
295 if (!processedValue.empty())
297 // If the manager returns a value, use it without further checks,
298 // except for sanity checking.
299 if (!isDirectoryOption())
301 const int fileType = fn2ftp(processedValue.c_str());
302 if (fileType == efNR)
304 // If the manager returned an invalid file name, assume
305 // that it knows what it is doing. But assert that it
306 // only does that for the only case that it is currently
307 // required for: VMD plugins.
308 GMX_ASSERT(isInputFile() && isTrajectoryOption(),
309 "Manager returned an invalid file name");
313 GMX_ASSERT(isValidType(fileType),
314 "Manager returned an invalid file name");
317 addValue(processedValue);
321 // Currently, directory options are simple, and don't need any
322 // special processing.
323 // TODO: Consider splitting them into a separate DirectoryOption.
324 if (isDirectoryOption())
329 const int fileType = fn2ftp(value.c_str());
330 if (fileType == efNR)
333 = formatString("File '%s' cannot be used by GROMACS because it "
334 "does not have a recognizable extension.\n"
335 "The following extensions are possible for this option:\n %s",
336 value.c_str(), joinStrings(extensions(), ", ").c_str());
337 GMX_THROW(InvalidInputError(message));
339 else if (!isValidType(fileType))
342 = formatString("File name '%s' cannot be used for this option.\n"
343 "Only the following extensions are possible:\n %s",
344 value.c_str(), joinStrings(extensions(), ", ").c_str());
345 GMX_THROW(InvalidInputError(message));
350 void FileNameOptionStorage::processAll()
352 if (manager_ != NULL && hasFlag(efOption_HasDefaultValue))
354 ValueList &valueList = values();
355 GMX_RELEASE_ASSERT(valueList.size() == 1,
356 "There should be only one default value");
357 if (!valueList[0].empty())
359 const std::string &oldValue = valueList[0];
360 GMX_ASSERT(endsWith(oldValue, defaultExtension()),
361 "Default value does not have the expected extension");
362 const std::string prefix
363 = stripSuffixIfPresent(oldValue, defaultExtension());
364 const std::string newValue
365 = manager_->completeDefaultFileName(prefix, info_);
366 if (!newValue.empty() && newValue != oldValue)
368 GMX_ASSERT(isValidType(fn2ftp(newValue.c_str())),
369 "Manager returned an invalid default value");
370 valueList[0] = newValue;
377 bool FileNameOptionStorage::isDirectoryOption() const
379 return fileType_ == efRND;
382 bool FileNameOptionStorage::isTrajectoryOption() const
384 return fileType_ == efTRX;
387 const char *FileNameOptionStorage::defaultExtension() const
389 FileTypeHandler typeHandler(fileType_);
390 if (typeHandler.extensionCount() == 0)
394 return typeHandler.extension(0);
397 std::vector<const char *> FileNameOptionStorage::extensions() const
399 FileTypeHandler typeHandler(fileType_);
400 std::vector<const char *> result;
401 result.reserve(typeHandler.extensionCount());
402 for (int i = 0; i < typeHandler.extensionCount(); ++i)
404 result.push_back(typeHandler.extension(i));
409 bool FileNameOptionStorage::isValidType(int fileType) const
411 FileTypeHandler typeHandler(fileType_);
412 return typeHandler.isValidType(fileType);
415 ConstArrayRef<int> FileNameOptionStorage::fileTypes() const
419 return ConstArrayRef<int>();
421 const int genericTypeCount = ftp2generic_count(fileType_);
422 if (genericTypeCount > 0)
424 return constArrayRefFromArray<int>(ftp2generic_list(fileType_), genericTypeCount);
426 return constArrayRefFromArray<int>(&fileType_, 1);
429 /********************************************************************
433 FileNameOptionInfo::FileNameOptionInfo(FileNameOptionStorage *option)
438 const FileNameOptionStorage &FileNameOptionInfo::option() const
440 return static_cast<const FileNameOptionStorage &>(OptionInfo::option());
443 bool FileNameOptionInfo::isInputFile() const
445 return option().isInputFile();
448 bool FileNameOptionInfo::isOutputFile() const
450 return option().isOutputFile();
453 bool FileNameOptionInfo::isInputOutputFile() const
455 return option().isInputOutputFile();
458 bool FileNameOptionInfo::isLibraryFile() const
460 return option().isLibraryFile();
463 bool FileNameOptionInfo::isDirectoryOption() const
465 return option().isDirectoryOption();
468 bool FileNameOptionInfo::isTrajectoryOption() const
470 return option().isTrajectoryOption();
473 const char *FileNameOptionInfo::defaultExtension() const
475 return option().defaultExtension();
478 FileNameOptionInfo::ExtensionList FileNameOptionInfo::extensions() const
480 return option().extensions();
483 bool FileNameOptionInfo::isValidType(int fileType) const
485 return option().isValidType(fileType);
488 ConstArrayRef<int> FileNameOptionInfo::fileTypes() const
490 return option().fileTypes();
493 /********************************************************************
497 AbstractOptionStorage *
498 FileNameOption::createStorage(const OptionManagerContainer &managers) const
500 return new FileNameOptionStorage(*this, managers.get<FileNameOptionManager>());