Mantid
Loading...
Searching...
No Matches
FileResource.cpp
Go to the documentation of this file.
1// Mantid Repository : https://github.com/mantidproject/mantid
2//
3// Copyright © 2018 ISIS Rutherford Appleton Laboratory UKRI,
4// NScD Oak Ridge National Laboratory, European Spallation Source,
5// Institut Laue - Langevin & CSNS, Institute of High Energy Physics, CAS
6// SPDX - License - Identifier: GPL - 3.0 +
9#include <filesystem>
10#include <string>
11
12FileResource::FileResource(const std::string &fileName, const bool debugMode, const bool randomize)
13 : m_debugMode(debugMode) {
14
15 const auto temp_dir = std::filesystem::temp_directory_path();
16 auto temp_full_path = temp_dir;
17
18 // add a random prefix to filename to avoid collision
19 // append full path to temp directory to user input file name
20 temp_full_path /= (randomize) ? (Mantid::Kernel::Strings::randomString(12) + "_" + fileName) : fileName;
21
22 // Check proposed location and throw std::invalid argument if file does
23 // not exist. otherwise set m_full_path to location.
24
25 if (std::filesystem::is_directory(temp_dir)) {
26 m_full_path = temp_full_path;
27
28 } else {
29 throw std::invalid_argument("failed to load temp directory: " + temp_dir.generic_string());
30 }
31
32 // if the file already exists and was not cleaned up, remove it
33 if (std::filesystem::is_regular_file(m_full_path)) {
34 std::filesystem::remove(m_full_path);
35 }
36}
37
39 if (std::filesystem::is_regular_file(m_full_path)) {
40 return true;
41 } else {
42 return false;
43 }
44}
45
46void FileResource::setDebugMode(bool mode) { m_debugMode = mode; }
47std::string FileResource::fullPath() const { return m_full_path.generic_string(); }
48
50
51 // file is removed at end of file handle's lifetime
52 if (std::filesystem::is_regular_file(m_full_path)) {
53 if (!m_debugMode)
54 std::filesystem::remove(m_full_path);
55 else
56 std::cout << "Debug file at: " << m_full_path << " not removed. " << std::endl;
57 }
58}
void setDebugMode(bool mode)
std::string fullPath() const
std::filesystem::path m_full_path
FileResource(const std::string &fileName, const bool debugMode=false, const bool randomize=false)
MANTID_KERNEL_DLL std::string randomString(const size_t len)
Generates random alpha-numeric string.
Definition Strings.cpp:1190