forked from npshub/mantid
-
Notifications
You must be signed in to change notification settings - Fork 0
/
DirectoryValidator.cpp
63 lines (55 loc) · 2.14 KB
/
DirectoryValidator.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
// Mantid Repository : https://github.com/mantidproject/mantid
//
// Copyright © 2018 ISIS Rutherford Appleton Laboratory UKRI,
// NScD Oak Ridge National Laboratory, European Spallation Source,
// Institut Laue - Langevin & CSNS, Institute of High Energy Physics, CAS
// SPDX - License - Identifier: GPL - 3.0 +
#include "MantidKernel/DirectoryValidator.h"
#include "MantidKernel/IValidator.h"
#include <Poco/Exception.h>
#include <Poco/File.h>
#include <Poco/Path.h>
#include <memory>
namespace Mantid {
namespace Kernel {
/** Constructor
* @param testDirectoryExists :: Flag indicating whether to test for existence
* of directory (default: yes)
*/
DirectoryValidator::DirectoryValidator(bool testDirectoryExists) : FileValidator() {
this->m_testExist = testDirectoryExists;
}
/// Returns the set of valid values
std::vector<std::string> DirectoryValidator::allowedValues() const { return std::vector<std::string>(); }
/**
* Clone the validator
* @returns A pointer to a new validator with the same properties as this one
*/
IValidator_sptr DirectoryValidator::clone() const { return std::make_shared<DirectoryValidator>(*this); }
/** If m_fullTest=true if checks that the files exists, otherwise just that path
* syntax looks valid
* @param value :: file name
* @returns An error message to display to users or an empty string on no error
*/
std::string DirectoryValidator::checkValidity(const std::string &value) const {
// Check if the path is syntactically valid
if (!Poco::Path().tryParse(value)) {
return "Error in path syntax: \"" + value + "\".";
}
// If the path is required to exist check it is there
if (m_testExist) {
try {
if (value.empty() || !Poco::File(value).exists())
return "Directory \"" + value + "\" not found";
if (!Poco::File(value).isDirectory())
return "Directory \"" + value + "\" specified is actually a file";
} catch (Poco::FileException &) {
return "Error accessing directory \"" + value + "\"";
}
}
// Otherwise we are okay, file extensions are just a suggestion so no
// validation on them is necessary
return "";
}
} // namespace Kernel
} // namespace Mantid