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 <filesystem>
6#include <string>
7#include "slideio/core/slideio_core_def.hpp"
8
9namespace slideio
10{
11 class SLIDEIO_CORE_EXPORTS TempFile
12 {
13 public:
14 explicit TempFile(std::filesystem::path path) : m_path(path)
15 {
16 if(std::filesystem::exists(path))
17 std::filesystem::remove(path);
18 }
19 TempFile() : TempFile(static_cast<const char*>(nullptr))
20 {
21
22 }
23 explicit TempFile(const char* ext)
24 {
25 std::string pattern("%%%%-%%%%-%%%%-%%%%.");
26 if(ext == nullptr || *ext==0){
27 pattern += "temp";
28 }
29 else{
30 pattern += ext;
31 }
32 m_path = std::filesystem::temp_directory_path();
33 m_path /= unique_path(pattern);
34 }
35 const std::filesystem::path& getPath() const{
36 return m_path;
37 }
38 ~TempFile()
39 {
40 if(std::filesystem::exists(m_path))
41 std::filesystem::remove(m_path);
42 }
43 static std::filesystem::path unique_path(const std::string& model = "%%%%-%%%%-%%%%-%%%%");
44 private:
45 std::filesystem::path m_path;
46 };
47}
Definition: exceptions.hpp:15