SlideIO 2.0.0
Open-source library for reading of medical images
Loading...
Searching...
No Matches
tempfile.hpp
1// This file is part of slideio project.
2// It is subject to the license terms in the LICENSE file found in the top-level directory
3// of this distribution and at http://slideio.com/license.html.
4#pragma once
5#include <boost/filesystem.hpp>
6#include <string>
7
8namespace slideio
9{
10 class TempFile
11 {
12 public:
13 explicit TempFile(boost::filesystem::path path) : m_path(path)
14 {
15 if(boost::filesystem::exists(path))
16 boost::filesystem::remove(path);
17 }
18 TempFile() : TempFile(static_cast<const char*>(nullptr))
19 {
20
21 }
22 explicit TempFile(const char* ext)
23 {
24 std::string pattern("%%%%-%%%%-%%%%-%%%%.");
25 if(ext == nullptr || *ext==0){
26 pattern += "temp";
27 }
28 else{
29 pattern += ext;
30 }
31 m_path = boost::filesystem::temp_directory_path();
32 m_path /= boost::filesystem::unique_path(pattern);
33 }
34 const boost::filesystem::path& getPath() const{
35 return m_path;
36 }
37 ~TempFile()
38 {
39 if(boost::filesystem::exists(m_path))
40 boost::filesystem::remove(m_path);
41 }
42 private:
43 boost::filesystem::path m_path;
44 };
45}
Definition: exceptions.hpp:12