Files
Jungfraujoch/common/CheckPath.h
T

26 lines
1.1 KiB
C++

// SPDX-FileCopyrightText: 2024 Filip Leonarski, Paul Scherrer Institute <filip.leonarski@psi.ch>
// SPDX-License-Identifier: GPL-3.0-only
#ifndef JFJOCH_CHECKPATH_H
#define JFJOCH_CHECKPATH_H
#include <string>
#include "JFJochException.h"
inline void CheckPath(const std::string &s) {
// CheckPath looks validates that s can be a valid file name
// (in practice with an added suffix, .e.g. _master.h5 or .tiff)
// It avoids paths going outside of a given directory
if (!s.empty() && (s.front() == '/' || s.back() != '/'))
throw JFJochException(JFJochExceptionCategory::InputParameterInvalid,
"Path cannot start with slash");
if (s.size() >= 3 && 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 //JFJOCH_CHECKPATH_H