Mercurial > hg > orthanc
annotate OrthancFramework/Sources/FileBuffer.cpp @ 4792:434843934307 storage-cache
Added a StorageCache in the StorageAccessor
author | Alain Mazy <am@osimis.io> |
---|---|
date | Thu, 30 Sep 2021 12:14:19 +0200 |
parents | d9473bd5ed43 |
children | 7053502fbf97 |
rev | line source |
---|---|
3357 | 1 /** |
2 * Orthanc - A Lightweight, RESTful DICOM Store | |
3 * Copyright (C) 2012-2016 Sebastien Jodogne, Medical Physics | |
4 * Department, University Hospital of Liege, Belgium | |
4437
d9473bd5ed43
upgrade to year 2021
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4119
diff
changeset
|
5 * Copyright (C) 2017-2021 Osimis S.A., Belgium |
3357 | 6 * |
7 * This program is free software: you can redistribute it and/or | |
4119
bf7b9edf6b81
re-licensing the OrthancFramework to LGPL, in order to license Stone of Orthanc under LGPL
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4044
diff
changeset
|
8 * modify it under the terms of the GNU Lesser General Public License |
bf7b9edf6b81
re-licensing the OrthancFramework to LGPL, in order to license Stone of Orthanc under LGPL
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4044
diff
changeset
|
9 * as published by the Free Software Foundation, either version 3 of |
bf7b9edf6b81
re-licensing the OrthancFramework to LGPL, in order to license Stone of Orthanc under LGPL
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4044
diff
changeset
|
10 * the License, or (at your option) any later version. |
3357 | 11 * |
12 * This program is distributed in the hope that it will be useful, but | |
13 * WITHOUT ANY WARRANTY; without even the implied warranty of | |
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
4119
bf7b9edf6b81
re-licensing the OrthancFramework to LGPL, in order to license Stone of Orthanc under LGPL
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4044
diff
changeset
|
15 * Lesser General Public License for more details. |
3357 | 16 * |
4119
bf7b9edf6b81
re-licensing the OrthancFramework to LGPL, in order to license Stone of Orthanc under LGPL
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4044
diff
changeset
|
17 * You should have received a copy of the GNU Lesser General Public |
bf7b9edf6b81
re-licensing the OrthancFramework to LGPL, in order to license Stone of Orthanc under LGPL
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4044
diff
changeset
|
18 * License along with this program. If not, see |
bf7b9edf6b81
re-licensing the OrthancFramework to LGPL, in order to license Stone of Orthanc under LGPL
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4044
diff
changeset
|
19 * <http://www.gnu.org/licenses/>. |
3357 | 20 **/ |
21 | |
22 | |
23 #include "PrecompiledHeaders.h" | |
24 #include "FileBuffer.h" | |
25 | |
26 #include "TemporaryFile.h" | |
27 #include "OrthancException.h" | |
28 | |
29 #include <boost/filesystem/fstream.hpp> | |
30 | |
31 | |
32 namespace Orthanc | |
33 { | |
34 class FileBuffer::PImpl | |
35 { | |
36 private: | |
37 TemporaryFile file_; | |
38 boost::filesystem::ofstream stream_; | |
39 bool isWriting_; | |
40 | |
41 public: | |
42 PImpl() : | |
43 isWriting_(true) | |
44 { | |
45 stream_.open(file_.GetPath(), std::ofstream::out | std::ofstream::binary); | |
46 if (!stream_.good()) | |
47 { | |
48 throw OrthancException(ErrorCode_CannotWriteFile); | |
49 } | |
50 } | |
51 | |
52 ~PImpl() | |
53 { | |
54 if (isWriting_) | |
55 { | |
56 stream_.close(); | |
57 } | |
58 } | |
59 | |
60 void Append(const char* buffer, | |
61 size_t size) | |
62 { | |
63 if (!isWriting_) | |
64 { | |
65 throw OrthancException(ErrorCode_BadSequenceOfCalls); | |
66 } | |
67 | |
68 if (size > 0) | |
69 { | |
70 stream_.write(buffer, size); | |
71 if (!stream_.good()) | |
72 { | |
73 stream_.close(); | |
74 throw OrthancException(ErrorCode_FileStorageCannotWrite); | |
75 } | |
76 } | |
77 } | |
78 | |
79 void Read(std::string& target) | |
80 { | |
81 if (isWriting_) | |
82 { | |
83 stream_.close(); | |
84 isWriting_ = false; | |
85 } | |
86 | |
87 file_.Read(target); | |
88 } | |
89 }; | |
90 | |
91 | |
92 FileBuffer::FileBuffer() : | |
93 pimpl_(new PImpl) | |
94 { | |
95 } | |
96 | |
97 | |
98 void FileBuffer::Append(const char* buffer, | |
99 size_t size) | |
100 { | |
101 assert(pimpl_.get() != NULL); | |
102 pimpl_->Append(buffer, size); | |
103 } | |
104 | |
105 | |
106 void FileBuffer::Read(std::string& target) | |
107 { | |
108 assert(pimpl_.get() != NULL); | |
109 pimpl_->Read(target); | |
110 } | |
111 } |