128-bit AVX2 SIMD for AMD Ryzen
[gromacs.git] / src / gromacs / options / filenameoption.cpp
blobe1e431a88cf57e1349568184786af738dcb63d32
1 /*
2 * This file is part of the GROMACS molecular simulation package.
4 * Copyright (c) 2012,2013,2014,2015,2016,2017, 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.
35 /*! \internal \file
36 * \brief
37 * Implements classes in filenameoption.h and filenameoptionstorage.h.
39 * \author Teemu Murtola <teemu.murtola@gmail.com>
40 * \ingroup module_options
42 #include "gmxpre.h"
44 #include "filenameoption.h"
46 #include <cstring>
48 #include <string>
49 #include <vector>
51 #include "gromacs/fileio/filetypes.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"
61 namespace gmx
64 namespace
67 //! \addtogroup module_options
68 //! \{
70 /*! \brief
71 * Mapping from OptionFileType to a file type in filetypes.h.
73 struct FileTypeMapping
75 //! OptionFileType value to map.
76 OptionFileType optionType;
77 //! Corresponding file type from filetypes.h.
78 int fileType;
81 //! Mappings from OptionFileType to file types in filetypes.h.
82 const FileTypeMapping c_fileTypeMapping[] =
84 { eftTopology, efTPS },
85 { eftTrajectory, efTRX },
86 { eftEnergy, efEDR },
87 { eftPDB, efPDB },
88 { eftIndex, efNDX },
89 { eftPlot, efXVG },
90 { eftGenericData, efDAT }
93 /********************************************************************
94 * FileTypeHandler
97 /*! \internal
98 * \brief
99 * Handles a single file type known to FileNameOptionStorage.
101 * Methods in this class do not throw, except for a possible std::bad_alloc
102 * when constructing std::string return values.
104 class FileTypeHandler
106 public:
107 /*! \brief
108 * Returns a handler for a single file type.
110 * \param[in] fileType File type (from filetypes.h) to use.
112 explicit FileTypeHandler(int fileType);
114 //! Returns the number of acceptable extensions for this file type.
115 int extensionCount() const;
116 //! Returns the extension with the given index.
117 const char *extension(int i) const;
119 //! Returns whether \p fileType (from filetypes.h) is accepted for this type.
120 bool isValidType(int fileType) const;
122 private:
123 /*! \brief
124 * File type (from filetypes.h) represented by this handler.
126 * -1 represents an unknown file type.
128 int fileType_;
129 //! Number of different extensions this type supports.
130 int extensionCount_;
131 /*! \brief
132 * List of simple file types that are included in this type.
134 * If `fileType_` represents a generic type in filetypes.h, i.e., a type
135 * that accepts multiple different types of files, then this is an
136 * array of `extensionCount_` elements, each element specifying one
137 * non-generic file type that this option accepts.
138 * `NULL` for single-extension types.
140 const int *genericTypes_;
143 FileTypeHandler::FileTypeHandler(int fileType)
144 : fileType_(fileType), extensionCount_(0), genericTypes_(nullptr)
146 if (fileType_ >= 0)
148 const int genericTypeCount = ftp2generic_count(fileType_);
149 if (genericTypeCount > 0)
151 extensionCount_ = genericTypeCount;
152 genericTypes_ = ftp2generic_list(fileType_);
154 else if (ftp2ext_with_dot(fileType_)[0] != '\0')
156 extensionCount_ = 1;
161 int FileTypeHandler::extensionCount() const
163 return extensionCount_;
166 const char *FileTypeHandler::extension(int i) const
168 GMX_ASSERT(i >= 0 && i < extensionCount_, "Invalid extension index");
169 if (genericTypes_ != nullptr)
171 return ftp2ext_with_dot(genericTypes_[i]);
173 return ftp2ext_with_dot(fileType_);
176 bool
177 FileTypeHandler::isValidType(int fileType) const
179 if (genericTypes_ != nullptr)
181 for (int i = 0; i < extensionCount(); ++i)
183 if (fileType == genericTypes_[i])
185 return true;
188 return false;
190 else
192 return fileType == fileType_;
196 //! \}
198 } // namespace
200 /********************************************************************
201 * FileNameOptionStorage
204 FileNameOptionStorage::FileNameOptionStorage(const FileNameOption &settings,
205 FileNameOptionManager *manager)
206 : MyBase(settings), info_(this), manager_(manager), fileType_(-1),
207 defaultExtension_(""), bRead_(settings.bRead_), bWrite_(settings.bWrite_),
208 bLibrary_(settings.bLibrary_), bAllowMissing_(settings.bAllowMissing_)
210 GMX_RELEASE_ASSERT(!hasFlag(efOption_MultipleTimes),
211 "allowMultiple() is not supported for file name options");
212 if (settings.optionType_ == eftUnknown && settings.legacyType_ >= 0)
214 fileType_ = settings.legacyType_;
216 else
218 ConstArrayRef<FileTypeMapping> map(c_fileTypeMapping);
219 ConstArrayRef<FileTypeMapping>::const_iterator i;
220 for (i = map.begin(); i != map.end(); ++i)
222 if (i->optionType == settings.optionType_)
224 fileType_ = i->fileType;
225 break;
229 FileTypeHandler typeHandler(fileType_);
230 if (settings.defaultType_ >= 0 && settings.defaultType_ < efNR)
232 // This also assures that the default type is not a generic type.
233 GMX_RELEASE_ASSERT(typeHandler.isValidType(settings.defaultType_),
234 "Default type for a file option is not an accepted "
235 "type for the option");
236 FileTypeHandler defaultHandler(settings.defaultType_);
237 defaultExtension_ = defaultHandler.extension(0);
239 else if (typeHandler.extensionCount() > 0)
241 defaultExtension_ = typeHandler.extension(0);
243 if (settings.defaultBasename_ != nullptr)
245 std::string defaultValue(settings.defaultBasename_);
246 int type = fn2ftp(settings.defaultBasename_);
247 GMX_RELEASE_ASSERT(type == efNR || type == settings.defaultType_,
248 "Default basename has an extension that does not "
249 "match the default type");
250 if (type == efNR)
252 defaultValue.append(defaultExtension());
254 setDefaultValueIfSet(defaultValue);
255 if (isRequired() || settings.bLegacyOptionalBehavior_)
257 setDefaultValue(defaultValue);
262 std::string FileNameOptionStorage::typeString() const
264 FileTypeHandler typeHandler(fileType_);
265 std::string result;
266 int count;
267 for (count = 0; count < 2 && count < typeHandler.extensionCount(); ++count)
269 if (count > 0)
271 result.append("/");
273 result.append(typeHandler.extension(count));
275 if (count < typeHandler.extensionCount())
277 result.append("/...");
279 if (result.empty())
281 if (isDirectoryOption())
283 result = "dir";
285 else
287 result = "file";
290 return result;
293 std::string FileNameOptionStorage::formatExtraDescription() const
295 FileTypeHandler typeHandler(fileType_);
296 std::string result;
297 if (typeHandler.extensionCount() > 2)
299 result.append(":");
300 for (int i = 0; i < typeHandler.extensionCount(); ++i)
302 result.append(" [REF]");
303 // Skip the dot.
304 result.append(typeHandler.extension(i) + 1);
305 result.append("[ref]");
308 return result;
311 std::string FileNameOptionStorage::formatSingleValue(const std::string &value) const
313 return value;
316 void FileNameOptionStorage::initConverter(ConverterType * /*converter*/)
320 std::string FileNameOptionStorage::processValue(const std::string &value) const
322 if (manager_ != nullptr)
324 std::string processedValue = manager_->completeFileName(value, info_);
325 if (!processedValue.empty())
327 // If the manager returns a value, use it without further checks,
328 // except for sanity checking.
329 if (!isDirectoryOption())
331 const int fileType = fn2ftp(processedValue.c_str());
332 if (fileType == efNR)
334 // If the manager returned an invalid file name, assume
335 // that it knows what it is doing. But assert that it
336 // only does that for the only case that it is currently
337 // required for: VMD plugins.
338 GMX_ASSERT(isInputFile() && isTrajectoryOption(),
339 "Manager returned an invalid file name");
341 else
343 GMX_ASSERT(isValidType(fileType),
344 "Manager returned an invalid file name");
347 return processedValue;
350 // Currently, directory options are simple, and don't need any
351 // special processing.
352 // TODO: Consider splitting them into a separate DirectoryOption.
353 if (isDirectoryOption())
355 return value;
357 const int fileType = fn2ftp(value.c_str());
358 if (fileType == efNR)
360 std::string message
361 = formatString("File '%s' cannot be used by GROMACS because it "
362 "does not have a recognizable extension.\n"
363 "The following extensions are possible for this option:\n %s",
364 value.c_str(), joinStrings(extensions(), ", ").c_str());
365 GMX_THROW(InvalidInputError(message));
367 else if (!isValidType(fileType))
369 std::string message
370 = formatString("File name '%s' cannot be used for this option.\n"
371 "Only the following extensions are possible:\n %s",
372 value.c_str(), joinStrings(extensions(), ", ").c_str());
373 GMX_THROW(InvalidInputError(message));
375 return value;
378 void FileNameOptionStorage::processAll()
380 if (manager_ != nullptr && hasFlag(efOption_HasDefaultValue))
382 ArrayRef<std::string> valueList = values();
383 GMX_RELEASE_ASSERT(valueList.size() == 1,
384 "There should be only one default value");
385 if (!valueList[0].empty())
387 const std::string &oldValue = valueList[0];
388 GMX_ASSERT(endsWith(oldValue, defaultExtension()),
389 "Default value does not have the expected extension");
390 const std::string prefix
391 = stripSuffixIfPresent(oldValue, defaultExtension());
392 const std::string newValue
393 = manager_->completeDefaultFileName(prefix, info_);
394 if (!newValue.empty() && newValue != oldValue)
396 GMX_ASSERT(isValidType(fn2ftp(newValue.c_str())),
397 "Manager returned an invalid default value");
398 valueList[0] = newValue;
404 bool FileNameOptionStorage::isDirectoryOption() const
406 return fileType_ == efRND;
409 bool FileNameOptionStorage::isTrajectoryOption() const
411 return fileType_ == efTRX;
414 const char *FileNameOptionStorage::defaultExtension() const
416 return defaultExtension_;
419 std::vector<const char *> FileNameOptionStorage::extensions() const
421 FileTypeHandler typeHandler(fileType_);
422 std::vector<const char *> result;
423 result.reserve(typeHandler.extensionCount());
424 for (int i = 0; i < typeHandler.extensionCount(); ++i)
426 result.push_back(typeHandler.extension(i));
428 return result;
431 bool FileNameOptionStorage::isValidType(int fileType) const
433 FileTypeHandler typeHandler(fileType_);
434 return typeHandler.isValidType(fileType);
437 ConstArrayRef<int> FileNameOptionStorage::fileTypes() const
439 if (fileType_ < 0)
441 return ConstArrayRef<int>();
443 const int genericTypeCount = ftp2generic_count(fileType_);
444 if (genericTypeCount > 0)
446 return constArrayRefFromArray<int>(ftp2generic_list(fileType_), genericTypeCount);
448 return constArrayRefFromArray<int>(&fileType_, 1);
451 /********************************************************************
452 * FileNameOptionInfo
455 FileNameOptionInfo::FileNameOptionInfo(FileNameOptionStorage *option)
456 : OptionInfo(option)
460 const FileNameOptionStorage &FileNameOptionInfo::option() const
462 return static_cast<const FileNameOptionStorage &>(OptionInfo::option());
465 bool FileNameOptionInfo::isInputFile() const
467 return option().isInputFile();
470 bool FileNameOptionInfo::isOutputFile() const
472 return option().isOutputFile();
475 bool FileNameOptionInfo::isInputOutputFile() const
477 return option().isInputOutputFile();
480 bool FileNameOptionInfo::isLibraryFile() const
482 return option().isLibraryFile();
485 bool FileNameOptionInfo::allowMissing() const
487 return option().allowMissing();
490 bool FileNameOptionInfo::isDirectoryOption() const
492 return option().isDirectoryOption();
495 bool FileNameOptionInfo::isTrajectoryOption() const
497 return option().isTrajectoryOption();
500 const char *FileNameOptionInfo::defaultExtension() const
502 return option().defaultExtension();
505 FileNameOptionInfo::ExtensionList FileNameOptionInfo::extensions() const
507 return option().extensions();
510 bool FileNameOptionInfo::isValidType(int fileType) const
512 return option().isValidType(fileType);
515 ConstArrayRef<int> FileNameOptionInfo::fileTypes() const
517 return option().fileTypes();
520 /********************************************************************
521 * FileNameOption
524 AbstractOptionStorage *
525 FileNameOption::createStorage(const OptionManagerContainer &managers) const
527 return new FileNameOptionStorage(*this, managers.get<FileNameOptionManager>());
530 } // namespace gmx