33 lines
1.2 KiB
C++
33 lines
1.2 KiB
C++
// Copyright (2019-2023) Paul Scherrer Institute
|
|
|
|
#ifndef JUNGFRAUJOCH_MAKEDIRECTORY_H
|
|
#define JUNGFRAUJOCH_MAKEDIRECTORY_H
|
|
|
|
#include <filesystem>
|
|
#include "../common/JFJochException.h"
|
|
|
|
inline void MakeDirectory(const std::string &input) {
|
|
try {
|
|
std::filesystem::path path(input);
|
|
if (path.has_parent_path())
|
|
std::filesystem::create_directories(path.parent_path());
|
|
} catch (const std::filesystem::filesystem_error &err) {
|
|
throw JFJochException(JFJochExceptionCategory::FileWriteError,
|
|
"Cannot create subdirectory for file " + input + ": " + std::string(err.what()));
|
|
}
|
|
}
|
|
|
|
inline void CheckPath(const std::string &s) {
|
|
if (s.front() == '/')
|
|
throw JFJochException(JFJochExceptionCategory::InputParameterInvalid,
|
|
"Path cannot start with slash");
|
|
if (s.substr(0,3) == "../")
|
|
throw JFJochException(JFJochExceptionCategory::InputParameterInvalid,
|
|
"Path cannot start with ../");
|
|
if (s.find("/../") != std::string::npos)
|
|
throw JFJochException(JFJochExceptionCategory::InputParameterInvalid,
|
|
"Path cannot contain /../");
|
|
}
|
|
|
|
#endif //JUNGFRAUJOCH_MAKEDIRECTORY_H
|