Elements  5.12.0
A C++ base framework for the Euclid Software.
Temporary.cpp
Go to the documentation of this file.
1 
23 
24 #include <string>
25 #include <iostream>
26 
27 #include <boost/filesystem/operations.hpp>
28 #include <boost/filesystem/fstream.hpp>
29 
30 #include "ElementsKernel/Logging.h"
32 #include "ElementsKernel/Path.h"
33 
34 using std::string;
35 using boost::filesystem::temp_directory_path;
36 
37 namespace Elements {
38 
39 namespace {
40  auto log = Logging::getLogger();
41 }
42 
43 TempPath::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()
68  << " temporary path";
69  const auto file_number = boost::filesystem::remove_all(m_path);
70  log.debug() << "Number of files removed: " << file_number;
71  } else {
72  log.info() << m_keep_var << " set: I do not remove the "
73  << m_path.string() << " temporary path";
74  }
75 
76 }
77 
79  return m_path;
80 }
81 
82 string TempPath::motif() const {
83  return m_motif;
84 }
85 
86 TempDir::TempDir(const string& arg_motif, const string& keep_var) :
87  TempPath(arg_motif, keep_var) {
88 
89  log.debug() << "Creation of the " << path() << " temporary directory";
90 
91  boost::filesystem::create_directory(path());
92 
93 }
94 
96 }
97 
98 TempFile::TempFile(const string& arg_motif, const string& keep_var) :
99  TempPath(arg_motif, keep_var) {
100 
101  log.debug() << "Creation of the " << path() << " temporary file";
102 
103  boost::filesystem::ofstream ofs(path());
104  ofs.close();
105 
106 }
107 
109 }
110 
111 } // 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.
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:86
virtual ~TempDir()
Definition: Temporary.cpp:95
TempFile(const std::string &motif=DEFAULT_TMP_MOTIF, const std::string &keep_var=DEFAULT_TMP_KEEP_VAR)
Definition: Temporary.cpp:98
virtual ~TempFile()
Definition: Temporary.cpp:108
Path::Item path() const
Definition: Temporary.cpp:78
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:82
T find(T... args)
T log(T... args)
boost::filesystem::path Item
Definition: Path.h:61
const std::string DEFAULT_TMP_MOTIF
The default random creation motif.
Definition: Temporary.h:40