/*
* This file is part of the GROMACS molecular simulation package.
*
- * Copyright (c) 2014, by the GROMACS development team, led by
+ * Copyright (c) 2014,2015, by the GROMACS development team, led by
* Mark Abraham, David van der Spoel, Berk Hess, and Erik Lindahl,
* and including many others, as listed in the AUTHORS file in the
* top-level source directory and at http://www.gromacs.org.
* \author Teemu Murtola <teemu.murtola@gmail.com>
* \ingroup module_options
*/
+#include "gmxpre.h"
+
#include "filenameoptionmanager.h"
#include <cstring>
#include "gromacs/options/options.h"
#include "gromacs/utility/arrayref.h"
#include "gromacs/utility/exceptions.h"
-#include "gromacs/utility/file.h"
+#include "gromacs/utility/fileredirector.h"
#include "gromacs/utility/path.h"
#include "gromacs/utility/stringutil.h"
* The first match is returned.
* Returns an empty string if no existing file is found.
*/
-std::string findExistingExtension(const std::string &prefix,
- const FileNameOptionInfo &option)
+std::string findExistingExtension(const std::string &prefix,
+ const FileNameOptionInfo &option,
+ const IFileInputRedirector *redirector)
{
ConstArrayRef<int> types = option.fileTypes();
ConstArrayRef<int>::const_iterator i;
for (i = types.begin(); i != types.end(); ++i)
{
std::string testFilename(prefix + ftp2ext_with_dot(*i));
- if (File::exists(testFilename))
+ if (redirector->fileExists(testFilename))
{
return testFilename;
}
class FileNameOptionManager::Impl
{
public:
- Impl() : bInputCheckingDisabled_(false) {}
+ Impl()
+ : redirector_(&defaultFileInputRedirector()),
+ bInputCheckingDisabled_(false)
+ {
+ }
+ //! Redirector for file existence checks.
+ const IFileInputRedirector *redirector_;
//! Global default file name, if set.
- std::string defaultFileName_;
+ std::string defaultFileName_;
//! Whether input option processing has been disabled.
- bool bInputCheckingDisabled_;
+ bool bInputCheckingDisabled_;
};
/********************************************************************
{
}
+void FileNameOptionManager::setInputRedirector(
+ const IFileInputRedirector *redirector)
+{
+ impl_->redirector_ = redirector;
+}
+
void FileNameOptionManager::disableInputOptionChecking(bool bDisable)
{
impl_->bInputCheckingDisabled_ = bDisable;
std::string FileNameOptionManager::completeFileName(
const std::string &value, const FileNameOptionInfo &option)
{
- const bool bInput = option.isInputFile() || option.isInputOutputFile();
+ const bool bAllowMissing = option.allowMissing();
+ const bool bInput
+ = option.isInputFile() || option.isInputOutputFile();
// Currently, directory options are simple, and don't need any
// special processing.
// TODO: Consider splitting them into a separate DirectoryOption.
if (option.isDirectoryOption())
{
- if (!impl_->bInputCheckingDisabled_ && bInput && !Directory::exists(value))
+ if (!impl_->bInputCheckingDisabled_ && bInput && !bAllowMissing
+ && !Directory::exists(value))
{
std::string message
= formatString("Directory '%s' does not exist or is not accessible.",
const int fileType = fn2ftp(value.c_str());
if (bInput && !impl_->bInputCheckingDisabled_)
{
- if (fileType == efNR && File::exists(value))
+ if (fileType == efNR && impl_->redirector_->fileExists(value))
{
ConstArrayRef<const char *> compressedExtensions(c_compressedExtensions);
ConstArrayRef<const char *>::const_iterator ext;
}
else if (fileType == efNR)
{
- std::string processedValue = findExistingExtension(value, option);
+ const std::string processedValue
+ = findExistingExtension(value, option, impl_->redirector_);
if (!processedValue.empty())
{
return processedValue;
}
- if (option.isLibraryFile())
+ if (bAllowMissing)
+ {
+ return value + option.defaultExtension();
+ }
+ else if (option.isLibraryFile())
{
// TODO: Treat also library files here.
return value + option.defaultExtension();
{
// TODO: Treat also library files.
}
- else if (!File::exists(value))
+ else if (!bAllowMissing && !impl_->redirector_->fileExists(value))
{
std::string message
= formatString("File '%s' does not exist or is not accessible.",
const bool bInput = option.isInputFile() || option.isInputOutputFile();
const std::string realPrefix
= !impl_->defaultFileName_.empty() ? impl_->defaultFileName_ : prefix;
+ const bool bAllowMissing = option.allowMissing();
if (bInput)
{
- std::string completedName = findExistingExtension(realPrefix, option);
+ const std::string completedName
+ = findExistingExtension(realPrefix, option, impl_->redirector_);
if (!completedName.empty())
{
return completedName;
}
- if (option.isLibraryFile())
+ if (bAllowMissing)
+ {
+ return realPrefix + option.defaultExtension();
+ }
+ else if (option.isLibraryFile())
{
// TODO: Treat also library files here.
return realPrefix + option.defaultExtension();