Elements  6.2
A C++ base framework for the Euclid Software.
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 
31 #include "ElementsKernel/Logging.h"
32 #include "ElementsKernel/Path.h"
33 
34 using boost::filesystem::temp_directory_path;
35 using std::string;
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() << " 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 
76  return m_path;
77 }
78 
79 string TempPath::motif() const {
80  return m_motif;
81 }
82 
83 TempDir::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 
92 TempFile::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
Elements::Kernel::Path::Item
boost::filesystem::path Item
Definition: Path.h:56
Elements::TempPath::TempPath
TempPath(const std::string &motif=DEFAULT_TMP_MOTIF, const std::string &keep_var=DEFAULT_TMP_KEEP_VAR)
Definition: Temporary.cpp:43
std::string
STL class.
Elements::DEFAULT_TMP_MOTIF
const std::string DEFAULT_TMP_MOTIF
The default random creation motif.
Definition: Temporary.h:40
Path.h
provide functions to retrieve resources pointed by environment variables
Elements::TempFile::TempFile
TempFile(const std::string &motif=DEFAULT_TMP_MOTIF, const std::string &keep_var=DEFAULT_TMP_KEEP_VAR)
Definition: Temporary.cpp:92
Elements::TempPath::motif
std::string motif() const
Definition: Temporary.cpp:79
std::string::find
T find(T... args)
Elements::TempPath::m_path
Path::Item m_path
Definition: Temporary.h:51
Elements::Environment::hasKey
static bool hasKey(const std::string &)
Definition: Environment.cpp:250
Elements::TempPath::path
Path::Item path() const
Definition: Temporary.cpp:75
Elements::TempFile::~TempFile
virtual ~TempFile()
Definition: Temporary.cpp:100
Elements::TempPath::~TempPath
virtual ~TempPath()
Definition: Temporary.cpp:62
Elements::TempPath::m_keep_var
const std::string m_keep_var
Definition: Temporary.h:52
Elements::TempDir::TempDir
TempDir(const std::string &motif=DEFAULT_TMP_MOTIF, const std::string &keep_var=DEFAULT_TMP_KEEP_VAR)
Definition: Temporary.cpp:83
Elements::Examples::log
auto log
Definition: BackTrace.cpp:36
Elements::Logging::getLogger
static Logging getLogger(const std::string &name="")
Definition: Logging.cpp:63
Elements::TempPath
Definition: Temporary.h:42
Environment.h
Defines a class to handle the Environment.
Temporary.h
Handling of temporary files, directories and environments.
Elements::Environment
Python dictionary-like Environment interface.
Definition: Environment.h:44
Logging.h
Logging facility.
Elements::TempDir::~TempDir
virtual ~TempDir()
Definition: Temporary.cpp:90
Elements::TempPath::m_motif
const std::string m_motif
Definition: Temporary.h:50
Elements
Definition: callBackExample.h:35