Mantid
Loading...
Searching...
No Matches
DirectoryValidator.cpp
Go to the documentation of this file.
1// Mantid Repository : https://github.com/mantidproject/mantid
2//
3// Copyright © 2018 ISIS Rutherford Appleton Laboratory UKRI,
4// NScD Oak Ridge National Laboratory, European Spallation Source,
5// Institut Laue - Langevin & CSNS, Institute of High Energy Physics, CAS
6// SPDX - License - Identifier: GPL - 3.0 +
9#include <filesystem>
10#include <memory>
11
12namespace Mantid::Kernel {
13
19 this->m_testExist = testDirectoryExists;
20}
21
23std::vector<std::string> DirectoryValidator::allowedValues() const { return std::vector<std::string>(); }
24
29IValidator_sptr DirectoryValidator::clone() const { return std::make_shared<DirectoryValidator>(*this); }
30
36std::string DirectoryValidator::checkValidity(const std::string &value) const {
37 // Check if the path is syntactically valid
38 try {
39 std::filesystem::path testPath(value);
40 // Just creating a path object validates syntax in most cases
41 } catch (const std::exception &) {
42 return "Error in path syntax: \"" + value + "\".";
43 }
44
45 // If the path is required to exist check it is there
46 if (m_testExist) {
47 try {
48 if (value.empty() || !std::filesystem::exists(value))
49 return "Directory \"" + value + "\" not found";
50 if (!std::filesystem::is_directory(value))
51 return "Directory \"" + value + "\" specified is actually a file";
52 } catch (const std::filesystem::filesystem_error &) {
53 return "Error accessing directory \"" + value + "\"";
54 }
55 }
56
57 // Otherwise we are okay, file extensions are just a suggestion so no
58 // validation on them is necessary
59 return "";
60}
61
62} // namespace Mantid::Kernel
double value
The value of the point.
Definition FitMW.cpp:51
IValidator_sptr clone() const override
Clone the validator.
DirectoryValidator(bool testDirectoryExists=true)
Constructor.
std::vector< std::string > allowedValues() const override
Returns the set of valid values.
std::string checkValidity(const std::string &value) const override
If m_fullTest=true if checks that the files exists, otherwise just that path syntax looks valid.
FileValidator is a validator that checks that a filepath is valid.
bool m_testExist
Flag indicating whether to test for existence of filename.
std::shared_ptr< IValidator > IValidator_sptr
A shared_ptr to an IValidator.
Definition IValidator.h:26