/
DirectoryValidator.cpp
66 lines (56 loc) · 1.73 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
64
65
66
#include "MantidKernel/DirectoryValidator.h"
#include <algorithm>
#include <Poco/File.h>
#include <Poco/Path.h>
#include <iostream>
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;
}
/// Destructor
DirectoryValidator::~DirectoryValidator() {}
/// 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 boost::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 )
{
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";
}
//Otherwise we are okay, file extensions are just a suggestion so no validation on them is necessary
return "";
}
} // namespace Kernel
} // namespace Mantid