Elements 6.1.2
A C++ base framework for the Euclid Software.
Loading...
Searching...
No Matches
Temporary.cpp
Go to the documentation of this file.
1
23
24#include <iostream>
25#include <string>
26
27#include <boost/filesystem/fstream.hpp>
28#include <boost/filesystem/operations.hpp>
29
32#include "ElementsKernel/Path.h"
33
34using boost::filesystem::temp_directory_path;
35using std::string;
36
37namespace Elements {
38
39namespace {
40auto log = Logging::getLogger();
41}
42
43TempPath::TempPath(const string& arg_motif, const string& keep_var)
44 : m_motif(arg_motif), m_path(temp_directory_path()), m_keep_var(keep_var) {
45
46 using boost::filesystem::unique_path;
47
48 if (m_motif.find('%') == string::npos) {
49 log.error() << "The '" << m_motif << "' motif is not random";
50 }
51
52 auto pattern = m_motif;
53
54 if (pattern.empty()) {
55 log.warn() << "The motif has been replaced by \"" << DEFAULT_TMP_MOTIF << "\"";
56 pattern = DEFAULT_TMP_MOTIF;
57 }
58
59 m_path /= unique_path(pattern);
60}
61
63
64 Environment current;
65
66 if (not current.hasKey(m_keep_var)) {
67 log.debug() << "Automatic destruction of the " << path() << " temporary path";
68 const auto file_number = boost::filesystem::remove_all(m_path);
69 log.debug() << "Number of files removed: " << file_number;
70 } else {
71 log.info() << m_keep_var << " set: I do not remove the " << m_path.string() << " temporary path";
72 }
73}
74
75Path::Item TempPath::path() const {
76 return m_path;
77}
78
79string TempPath::motif() const {
80 return m_motif;
81}
82
83TempDir::TempDir(const string& arg_motif, const string& keep_var) : TempPath(arg_motif, keep_var) {
84
85 log.debug() << "Creation of the " << path() << " temporary directory";
86
87 boost::filesystem::create_directory(path());
88}
89
91
92TempFile::TempFile(const string& arg_motif, const string& keep_var) : TempPath(arg_motif, keep_var) {
93
94 log.debug() << "Creation of the " << path() << " temporary file";
95
96 boost::filesystem::ofstream ofs(path());
97 ofs.close();
98}
99
101
102} // namespace Elements
Defines a class to handle the Environment.
Logging facility.
provide functions to retrieve resources pointed by environment variables
Handling of temporary files, directories and environments.
Python dictionary-like Environment interface.
Definition: Environment.h:44
static bool hasKey(const std::string &)
static Logging getLogger(const std::string &name="")
Definition: Logging.cpp:63
TempDir(const std::string &motif=DEFAULT_TMP_MOTIF, const std::string &keep_var=DEFAULT_TMP_KEEP_VAR)
Definition: Temporary.cpp:83
virtual ~TempDir()
Definition: Temporary.cpp:90
TempFile(const std::string &motif=DEFAULT_TMP_MOTIF, const std::string &keep_var=DEFAULT_TMP_KEEP_VAR)
Definition: Temporary.cpp:92
virtual ~TempFile()
Definition: Temporary.cpp:100
Path::Item path() const
Definition: Temporary.cpp:75
virtual ~TempPath()
Definition: Temporary.cpp:62
const std::string m_keep_var
Definition: Temporary.h:52
const std::string m_motif
Definition: Temporary.h:50
Path::Item m_path
Definition: Temporary.h:51
TempPath(const std::string &motif=DEFAULT_TMP_MOTIF, const std::string &keep_var=DEFAULT_TMP_KEEP_VAR)
Definition: Temporary.cpp:43
std::string motif() const
Definition: Temporary.cpp:79
T find(T... args)
const std::string DEFAULT_TMP_MOTIF
The default random creation motif.
Definition: Temporary.h:40