Elements  5.8
A C++ base framework for the Euclid Software.
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
Temporary.cpp
Go to the documentation of this file.
1 
23 
24 #include <string>
25 #include <iostream>
26 
27 #include <boost/filesystem.hpp>
28 #include <boost/filesystem/fstream.hpp>
29 
30 #include "ElementsKernel/Logging.h"
32 
33 using std::string;
34 
35 namespace Elements {
36 
37 namespace {
38  auto log = Logging::getLogger();
39 }
40 
41 TempPath::TempPath(const string& motif, const string& keep_var) :
42  m_motif(motif), m_keep_var(keep_var) {
43 
44  using boost::filesystem::temp_directory_path;
45  using boost::filesystem::unique_path;
46 
47  if (m_motif.find('%') == string::npos) {
48  log.warn() << "The '" << m_motif << "' motif is not random";
49  }
50 
51  if (m_motif != "") {
52  m_path = temp_directory_path() / unique_path(m_motif);
53  } else {
54  m_path = temp_directory_path() / unique_path();
55  }
56 }
57 
59 
60  Environment current;
61 
62  if (not current.hasKey(m_keep_var)) {
63  log.debug() << "Automatic destruction of the " << path()
64  << " temporary path";
65  boost::filesystem::remove_all(m_path);
66  } else {
67  log.info() << m_keep_var << " set: I do not remove the "
68  << m_path.string() << " temporary path";
69  }
70 
71 }
72 
74  return m_path;
75 }
76 
77 string TempPath::motif() const {
78  return m_motif;
79 }
80 
81 TempDir::TempDir(const string& motif, const string& keep_var) :
82  TempPath(motif, keep_var) {
83 
84  log.debug() << "Creation of the " << path() << " temporary directory";
85 
86  boost::filesystem::create_directory(path());
87 
88 }
89 
91 }
92 
93 TempFile::TempFile(const string& motif, const string& keep_var) :
94  TempPath(motif, keep_var) {
95 
96  log.debug() << "Creation of the " << path() << " temporary file";
97 
98  boost::filesystem::ofstream ofs(path());
99  ofs.close();
100 
101 }
102 
104 }
105 
106 } // namespace Elements
static bool hasKey(const std::string &)
Logging facility.
virtual ~TempPath()
Definition: Temporary.cpp:58
Handling of temporary files, directories and environments.
T log(T...args)
virtual ~TempDir()
Definition: Temporary.cpp:90
const std::string m_keep_var
Definition: Temporary.h:52
STL class.
TempDir(const std::string &motif=DEFAULT_TMP_MOTIF, const std::string &keep_var=DEFAULT_TMP_KEEP_VAR)
Definition: Temporary.cpp:81
const std::string m_motif
Definition: Temporary.h:50
boost::filesystem::path m_path
Definition: Temporary.h:51
boost::filesystem::path path() const
Definition: Temporary.cpp:73
boost::filesystem::path path
Definition: DataSyncUtils.h:33
TempPath(const std::string &motif=DEFAULT_TMP_MOTIF, const std::string &keep_var=DEFAULT_TMP_KEEP_VAR)
Definition: Temporary.cpp:41
std::string motif() const
Definition: Temporary.cpp:77
virtual ~TempFile()
Definition: Temporary.cpp:103
TempFile(const std::string &motif=DEFAULT_TMP_MOTIF, const std::string &keep_var=DEFAULT_TMP_KEEP_VAR)
Definition: Temporary.cpp:93
T find(T...args)
Defines a class to handle the Environment.
static Logging getLogger(const std::string &name="")
Definition: Logging.cpp:63