SourceXtractorPlusPlus  0.8
Please provide a description of the project.
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
TemporaryFile.cpp
Go to the documentation of this file.
1 
17 /*
18  * TemporaryFile.cpp
19  *
20  * Created on: May 23, 2018
21  * Author: aalvarez
22  */
23 
25 #include <boost/filesystem.hpp>
26 
27 namespace SourceXtractor {
28 
29 static std::string generateTemporaryPath(const std::string& dir, const std::string& pattern) {
30  boost::filesystem::path tmp_path;
31  boost::filesystem::path file_name;
32 
33  if (pattern.empty()) {
34  file_name = boost::filesystem::unique_path();
35  }
36  else {
37  file_name = boost::filesystem::unique_path(pattern);
38  }
39  if (dir.empty()) {
40  tmp_path = boost::filesystem::temp_directory_path();
41  }
42  else {
43  tmp_path = dir;
44  }
45 
46  return (tmp_path / file_name).native();
47 }
48 
49 
50 TemporaryFile::TemporaryFile(bool autoremove): TemporaryFile("", "", autoremove) {
51 }
52 
53 
54 TemporaryFile::TemporaryFile(const std::string &pattern, bool autoremove): TemporaryFile("", pattern, autoremove) {
55 }
56 
57 
58 TemporaryFile::TemporaryFile(const std::string &dir, const std::string &pattern, bool autoremove)
59  : m_path(generateTemporaryPath(dir, pattern)), m_autoremove(autoremove) {
60 }
61 
62 
64  if (m_autoremove && boost::filesystem::exists(m_path)) {
65  boost::filesystem::remove(m_path);
66  }
67 }
68 
70  return m_path;
71 }
72 
73 }
T empty(T...args)
STL class.
const std::string & getPath() const
Return the full path of the temporary file.
static std::string generateTemporaryPath(const std::string &dir, const std::string &pattern)
TemporaryFile(bool autoremove=true)