36IValidator_sptr createValidator(
unsigned int action,
const std::vector<std::string> &exts) {
49void addExtension(
const std::string &extension, std::vector<std::string> &extensions) {
50 if (std::find(extensions.begin(), extensions.end(), extension) != extensions.end())
53 extensions.emplace_back(extension);
61const std::string &getHomePath() {
62 static std::string homePath;
63 static bool initialised(
false);
70 char *home = std::getenv(
"HOME");
72 homePath = std::string(home);
76 char *userProfile = std::getenv(
"USERPROFILE");
78 homePath = userProfile ? std::string(userProfile) :
"";
92std::string expandUser(
const std::string &filepath) {
93 auto start = filepath.begin();
94 auto end = filepath.end();
97 if (start == end || *start !=
'~')
101 auto nextSlash = find_if(start, end, [](
const char &c) {
return c ==
'/' || c ==
'\\'; });
104 if (std::distance(start, nextSlash) != 1)
107 return getHomePath() + std::string(nextSlash, end);
115std::string createDirectory(
const std::string &path) {
116 Poco::Path stempath(path);
117 if (stempath.isFile()) {
118 stempath.makeParent();
121 if (!stempath.toString().empty()) {
122 Poco::File stem(stempath);
123 if (!stem.exists()) {
125 stem.createDirectories();
126 }
catch (Poco::Exception &e) {
127 std::stringstream msg;
128 msg <<
"Failed to create directory \"" << stempath.toString() <<
"\": " << e.what();
133 return "Invalid directory.";
153 const std::vector<std::string> &exts,
unsigned int direction)
154 :
PropertyWithValue<
std::string>(name, defaultValue, createValidator(action, exts), direction), m_action(action),
155 m_defaultExt((!exts.empty()) ? exts.front() :
""), m_runFileProp(isLoadProperty() && extsMatchRunFiles()),
156 m_oldLoadPropValue(
""), m_oldLoadFoundFile(
"") {}
168 const std::string &ext,
unsigned int direction)
169 :
FileProperty(name, default_value, action,
std::vector<
std::string>(1, ext), direction) {}
181 std::initializer_list<std::string> exts,
unsigned int direction)
182 :
FileProperty(name, default_value, action,
std::vector<
std::string>(exts), direction) {}
220 if (strippedValue.empty()) {
226 strippedValue = expandUser(strippedValue);
231 if (Poco::Path(strippedValue).isAbsolute()) {
234 error = createDirectory(strippedValue);
242 std::string errorMsg;
261 const std::string &
value = (*this)();
276 return "No file specified.";
290 const std::vector<std::string> facilityExts = facilityInfo.
extensions();
291 auto facilityExtsBegin = facilityExts.cbegin();
292 auto facilityExtsEnd = facilityExts.cend();
293 const std::vector<std::string> allowedExts = this->
allowedValues();
295 for (
const auto &ext : allowedExts) {
296 if (std::find(facilityExtsBegin, facilityExtsEnd, ext) != facilityExtsEnd) {
315 std::string foundFile;
324 if (foundFile.empty()) {
328 std::vector<std::string> exts;
333 addExtension(
lower, exts);
336 addExtension(
upper, exts);
338 for (
auto &ext : allowedExts) {
339 std::string
lower(ext);
340 std::string
upper(ext);
341 std::transform(ext.begin(), ext.end(),
lower.begin(), tolower);
342 std::transform(ext.begin(), ext.end(),
upper.begin(), toupper);
343 addExtension(ext, exts);
344 addExtension(
lower, exts);
345 addExtension(
upper, exts);
357 if (foundFile.empty()) {
370 if (propValue.empty()) {
374 return "Empty filename not allowed.";
376 std::string errorMsg;
383 if (save_path.empty()) {
384 save_dir = Poco::Path(propValue).parent();
387 if (save_dir.toString().empty()) {
388 save_dir = Poco::Path::current();
391 save_dir = Poco::Path(save_path).makeDirectory();
393 errorMsg = createDirectory(save_dir.toString());
394 if (errorMsg.empty()) {
395 std::string fullpath = save_dir.resolve(propValue).toString();
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 via a string.
std::vector< std::string > allowedValues() const override
std::string value() const override
std::string isValid() const override
Overridden function that checks whether the property, if not overriden returns "".
Manage the lifetime of a class intended to be a singleton.
static T & Instance()
Return a reference to the Singleton instance, creating it if it does not already exist Creation is do...
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.
std::shared_ptr< IValidator > IValidator_sptr
A shared_ptr to an IValidator.