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 +
8#include <filesystem>
9#include <string>
10
11FileResource::FileResource(const std::string &fileName, bool debugMode) : m_debugMode(debugMode) {
12
13 const auto temp_dir = std::filesystem::temp_directory_path();
14 auto temp_full_path = temp_dir;
15 // append full path to temp directory to user input file name
16 temp_full_path /= fileName;
17
18 // Check proposed location and throw std::invalid argument if file does
19 // not exist. otherwise set m_full_path to location.
20
21 if (std::filesystem::is_directory(temp_dir)) {
22 m_full_path = temp_full_path;
23
24 } else {
25 throw std::invalid_argument("failed to load temp directory: " + temp_dir.generic_string());
26 }
27
28 // if the file already exists and was not cleaned up, remove it
29 if (std::filesystem::is_regular_file(m_full_path)) {
30 std::filesystem::remove(m_full_path);
31 }
32}
33
35 if (std::filesystem::is_regular_file(m_full_path)) {
36 return true;
37 } else {
38 return false;
39 }
40}
41
42void FileResource::setDebugMode(bool mode) { m_debugMode = mode; }
43std::string FileResource::fullPath() const { return m_full_path.generic_string(); }
44
46
47 // file is removed at end of file handle's lifetime
48 if (std::filesystem::is_regular_file(m_full_path)) {
49 if (!m_debugMode)
50 std::filesystem::remove(m_full_path);
51 else
52 std::cout << "Debug file at: " << m_full_path << " not removed. " << std::endl;
53 }
54}
void setDebugMode(bool mode)
std::string fullPath() const
FileResource(const std::string &fileName, bool debugMode=false)
std::filesystem::path m_full_path