Mercurial > hg > orthanc
annotate Core/TemporaryFile.cpp @ 2247:521adccdbfa0
todo
author | Sebastien Jodogne <s.jodogne@gmail.com> |
---|---|
date | Mon, 09 Jan 2017 17:13:01 +0100 |
parents | a3a65de1840f |
children | 878b59270859 |
rev | line source |
---|---|
0 | 1 /** |
59 | 2 * Orthanc - A Lightweight, RESTful DICOM Store |
1900 | 3 * Copyright (C) 2012-2016 Sebastien Jodogne, Medical Physics |
1288
6e7e5ed91c2d
upgrade to year 2015
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
824
diff
changeset
|
4 * Department, University Hospital of Liege, Belgium |
2244
a3a65de1840f
shared copyright with osimis
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2172
diff
changeset
|
5 * Copyright (C) 2017 Osimis, Belgium |
0 | 6 * |
7 * This program is free software: you can redistribute it and/or | |
8 * modify it under the terms of the GNU General Public License as | |
9 * published by the Free Software Foundation, either version 3 of the | |
10 * License, or (at your option) any later version. | |
136 | 11 * |
12 * In addition, as a special exception, the copyright holders of this | |
13 * program give permission to link the code of its release with the | |
14 * OpenSSL project's "OpenSSL" library (or with modified versions of it | |
15 * that use the same license as the "OpenSSL" library), and distribute | |
16 * the linked executables. You must obey the GNU General Public License | |
17 * in all respects for all of the code used other than "OpenSSL". If you | |
18 * modify file(s) with this exception, you may extend this exception to | |
19 * your version of the file(s), but you are not obligated to do so. If | |
20 * you do not wish to do so, delete this exception statement from your | |
21 * version. If you delete this exception statement from all source files | |
22 * in the program, then also delete it here. | |
0 | 23 * |
24 * This program is distributed in the hope that it will be useful, but | |
25 * WITHOUT ANY WARRANTY; without even the implied warranty of | |
26 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
27 * General Public License for more details. | |
28 * | |
29 * You should have received a copy of the GNU General Public License | |
30 * along with this program. If not, see <http://www.gnu.org/licenses/>. | |
31 **/ | |
32 | |
33 | |
824
a811bdf8b8eb
precompiled headers
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
689
diff
changeset
|
34 #include "PrecompiledHeaders.h" |
2142 | 35 #include "TemporaryFile.h" |
0 | 36 |
2143
fd5875662670
creation of namespace SystemToolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2142
diff
changeset
|
37 #include "SystemToolbox.h" |
fd5875662670
creation of namespace SystemToolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2142
diff
changeset
|
38 #include "Toolbox.h" |
fd5875662670
creation of namespace SystemToolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2142
diff
changeset
|
39 |
82 | 40 #include <boost/filesystem.hpp> |
41 | |
59 | 42 namespace Orthanc |
0 | 43 { |
2140 | 44 static std::string CreateTemporaryPath(const char* extension) |
45 { | |
110
fd7b0a3e6260
support of boost 1.42 for debian
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
82
diff
changeset
|
46 #if BOOST_HAS_FILESYSTEM_V3 == 1 |
2140 | 47 boost::filesystem::path tmpDir = boost::filesystem::temp_directory_path(); |
110
fd7b0a3e6260
support of boost 1.42 for debian
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
82
diff
changeset
|
48 #elif defined(__linux__) |
2140 | 49 boost::filesystem::path tmpDir("/tmp"); |
110
fd7b0a3e6260
support of boost 1.42 for debian
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
82
diff
changeset
|
50 #else |
fd7b0a3e6260
support of boost 1.42 for debian
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
82
diff
changeset
|
51 #error Support your platform here |
fd7b0a3e6260
support of boost 1.42 for debian
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
82
diff
changeset
|
52 #endif |
fd7b0a3e6260
support of boost 1.42 for debian
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
82
diff
changeset
|
53 |
2140 | 54 // We use UUID to create unique path to temporary files |
2172
84d1d392a9ab
GenerateUuid() not available in sandboxed environments
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2143
diff
changeset
|
55 std::string filename = "Orthanc-" + Orthanc::SystemToolbox::GenerateUuid(); |
366
68fd4de63eae
create temporary files file a given extension
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
136
diff
changeset
|
56 |
2140 | 57 if (extension != NULL) |
58 { | |
59 filename.append(extension); | |
60 } | |
366
68fd4de63eae
create temporary files file a given extension
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
136
diff
changeset
|
61 |
2140 | 62 tmpDir /= filename; |
63 return tmpDir.string(); | |
64 } | |
366
68fd4de63eae
create temporary files file a given extension
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
136
diff
changeset
|
65 |
68fd4de63eae
create temporary files file a given extension
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
136
diff
changeset
|
66 |
2140 | 67 TemporaryFile::TemporaryFile() : |
68 path_(CreateTemporaryPath(NULL)) | |
69 { | |
70 } | |
366
68fd4de63eae
create temporary files file a given extension
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
136
diff
changeset
|
71 |
68fd4de63eae
create temporary files file a given extension
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
136
diff
changeset
|
72 |
2140 | 73 TemporaryFile::TemporaryFile(const char* extension) : |
74 path_(CreateTemporaryPath(extension)) | |
75 { | |
76 } | |
82 | 77 |
78 | |
2140 | 79 TemporaryFile::~TemporaryFile() |
80 { | |
81 boost::filesystem::remove(path_); | |
2143
fd5875662670
creation of namespace SystemToolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2142
diff
changeset
|
82 } |
fd5875662670
creation of namespace SystemToolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2142
diff
changeset
|
83 |
fd5875662670
creation of namespace SystemToolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2142
diff
changeset
|
84 |
fd5875662670
creation of namespace SystemToolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2142
diff
changeset
|
85 void TemporaryFile::Write(const std::string& content) |
fd5875662670
creation of namespace SystemToolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2142
diff
changeset
|
86 { |
fd5875662670
creation of namespace SystemToolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2142
diff
changeset
|
87 SystemToolbox::WriteFile(content, path_); |
fd5875662670
creation of namespace SystemToolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2142
diff
changeset
|
88 } |
fd5875662670
creation of namespace SystemToolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2142
diff
changeset
|
89 |
fd5875662670
creation of namespace SystemToolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2142
diff
changeset
|
90 |
fd5875662670
creation of namespace SystemToolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2142
diff
changeset
|
91 void TemporaryFile::Read(std::string& content) const |
fd5875662670
creation of namespace SystemToolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2142
diff
changeset
|
92 { |
fd5875662670
creation of namespace SystemToolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2142
diff
changeset
|
93 SystemToolbox::ReadFile(content, path_); |
fd5875662670
creation of namespace SystemToolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2142
diff
changeset
|
94 } |
0 | 95 } |