35IValidator_sptr createValidator(
unsigned int action,
const std::vector<std::string> &exts) {
48void addExtension(
const std::string &extension, std::vector<std::string> &extensions) {
49 if (std::find(extensions.begin(), extensions.end(), extension) != extensions.end())
52 extensions.emplace_back(extension);
60const std::string &getHomePath() {
61 static std::string homePath;
62 static bool initialised(
false);
69 char *home = std::getenv(
"HOME");
71 homePath = std::string(home);
75 char *userProfile = std::getenv(
"USERPROFILE");
77 homePath = userProfile ? std::string(userProfile) :
"";
91std::string expandUser(
const std::string &filepath) {
96 auto const start = filepath.begin();
97 auto const end = filepath.end();
104 auto nextSlash = find_if(start, end, [](
const char &c) {
return c ==
'/' || c ==
'\\'; });
107 if (std::distance(start, nextSlash) != 1)
110 return getHomePath() + std::string(nextSlash, end);
116std::string resolveCurrentDirectory(
const std::string &filepath) {
117 if (filepath.empty())
120 if (filepath[0] !=
'.')
122 if (filepath.size() > 1 && !(filepath.starts_with(
"./"))) {
126 std::filesystem::path path(filepath);
127 if (!path.is_absolute()) {
128 std::filesystem::path currentPath = std::filesystem::current_path();
129 path = currentPath / path;
131 return path.string();
141std::string createDirectory(
const std::filesystem::path &path) {
142 std::filesystem::path stempath(path);
145 if (std::filesystem::is_regular_file(stempath)) {
146 stempath = stempath.parent_path();
149 if (!stempath.empty()) {
150 if (!std::filesystem::exists(stempath)) {
152 std::filesystem::create_directories(stempath);
153 }
catch (
const std::exception &e) {
154 std::stringstream msg;
155 msg <<
"Failed to create directory \"" << stempath <<
"\": " << e.what();
160 return "Invalid directory.";
180 const std::vector<std::string> &exts,
unsigned int direction)
182 m_defaultExt((!exts.empty()) ? exts.front() :
""), m_runFileProp(isLoadProperty() && extsMatchRunFiles()),
183 m_oldLoadPropValue(
""), m_oldLoadFoundFile(
"") {}
195 const std::string &ext,
unsigned int direction)
208 std::initializer_list<std::string> exts,
unsigned int direction)
247 if (strippedValue.empty()) {
253 strippedValue = expandUser(strippedValue);
254 strippedValue = resolveCurrentDirectory(strippedValue);
255 std::filesystem::path strippedPath(strippedValue);
260 if (strippedPath.is_absolute()) {
262 std::string
error = createDirectory(strippedPath.parent_path());
270 std::string errorMsg;
289 const std::string &
value = (*this)();
304 return "No file specified.";
318 const std::vector<std::string> facilityExts = facilityInfo.
extensions();
319 const std::vector<std::string> allowedExts = this->
allowedValues();
320 match = std::any_of(allowedExts.cbegin(), allowedExts.cend(), [&facilityExts](
const auto &ext) {
321 return std::find(facilityExts.cbegin(), facilityExts.cend(), ext) != facilityExts.cend();
338 std::string foundFile;
347 if (foundFile.empty()) {
351 std::vector<std::string> exts;
356 addExtension(
lower, exts);
359 addExtension(
upper, exts);
361 for (
auto &ext : allowedExts) {
362 std::string
lower(ext);
363 std::string
upper(ext);
364 std::transform(ext.begin(), ext.end(),
lower.begin(), tolower);
365 std::transform(ext.begin(), ext.end(),
upper.begin(), toupper);
366 addExtension(ext, exts);
367 addExtension(
lower, exts);
368 addExtension(
upper, exts);
370 foundFile = FileFinder::Instance().findRun(propValue, exts).result();
373 foundFile = FileFinder::Instance().getFullPath(propValue);
380 if (foundFile.empty()) {
393 if (propValue.empty()) {
397 return "Empty filename not allowed.";
399 std::string errorMsg;
403 std::string save_path = ConfigService::Instance().getString(
"defaultsave.directory");
404 std::filesystem::path save_dir;
405 if (save_path.empty()) {
406 save_dir = std::filesystem::path(propValue).parent_path();
408 if (save_dir.empty()) {
409 save_dir = std::filesystem::current_path();
412 save_dir = std::filesystem::path(save_path);
414 errorMsg = createDirectory(save_dir);
415 if (errorMsg.empty()) {
416 std::string fullpath = (save_dir / propValue).
string();
double lower
lower and upper bounds on the multiplier, if known
A specialized class for dealing with file properties.
unsigned int m_action
The action type of this property, i.e. load/save.
bool isLoadProperty() const
Check if this is a load type property.
std::string setValue(const std::string &propValue) override
Overridden setValue method.
bool isOptional() const
Check if this property is optional.
bool isDirectoryProperty() const
Check if this is a directory type property.
std::string isEmptyValueValid() const
Returns a string depending on whether an empty value is valid.
std::string setLoadProperty(const std::string &propValue)
Handles the filename if this is a save property.
std::string m_oldLoadPropValue
Last value of propValue used in FileProperty::setLoadProperty.
std::string setSaveProperty(const std::string &propValue)
Handles the filename if this is a save property.
bool m_runFileProp
Is this property for run files?
bool isSaveProperty() const
Check if this is a save type property.
std::string m_oldLoadFoundFile
Last value of foundFile used in FileProperty::setLoadProperty.
FileProperty(const std::string &name, const std::string &defaultValue, unsigned int action, const std::vector< std::string > &exts=std::vector< std::string >(), unsigned int direction=Kernel::Direction::Input)
Constructor taking a list of extensions as a vector.
bool extsMatchRunFiles()
Do the allowed values match the facility preference extensions for run files.
std::string m_defaultExt
The default file extension associated with the type of file this property will handle.
std::string isValid() const override
Returns an empty string if the property is valid, otherwise contains an error message.
@ Directory
to specify a directory that must exist
@ OptionalSave
to specify a file to write to but an empty string is
@ OptionalLoad
to specify a file to read but the file doesn't have to exist
@ OptionalDirectory
to specify a directory that does not have to exist
@ Save
to specify a file to write to, the file may or may not exist
@ Load
allowed here which will be passed to the algorithm
Loads a workspace from a data file.
DirectoryValidator is a validator that checks that a directory path is valid.
Exception for when an item is not found in a collection.
A class that holds information about a facility.
const std::vector< std::string > & extensions() const
Returns a list of file extensions.
FileValidator is a validator that checks that a filepath is valid.
The concrete, templated class for properties.
std::string setValue(const std::string &value) override
Set the value of the property from a string representation.
std::vector< std::string > allowedValues() const override
Returns the set of valid values for this property, if such a set exists.
std::string value() const override
Get the value of the property as a string.
std::string isValid() const override
Check the value chosen for the property is OK, unless overidden it just calls the validator's isValid...
DLLExport QString addExtension(const QString &filename, const QString &selectedFilter)
For file dialogs.
MANTID_KERNEL_DLL std::string toLower(const std::string &input)
Converts string to all lowercase.
MANTID_KERNEL_DLL std::string strip(const std::string &A)
strip pre/post spaces
MANTID_KERNEL_DLL std::string toUpper(const std::string &input)
Converts string to all uppercase.
Mantid::Kernel::SingletonHolder< ConfigServiceImpl > ConfigService
std::shared_ptr< IValidator > IValidator_sptr
A shared_ptr to an IValidator.