Mercurial > hg > orthanc
annotate OrthancFramework/Sources/FileBuffer.cpp @ 4981:d0c34145320c
cppcheck
author | Sebastien Jodogne <s.jodogne@gmail.com> |
---|---|
date | Mon, 25 Apr 2022 17:32:58 +0200 |
parents | 43e613a7756b |
children | 0ea402b4d901 |
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 | |
4870
43e613a7756b
upgrade to year 2022
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4831
diff
changeset
|
5 * Copyright (C) 2017-2022 Osimis S.A., Belgium |
43e613a7756b
upgrade to year 2022
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4831
diff
changeset
|
6 * Copyright (C) 2021-2022 Sebastien Jodogne, ICTEAM UCLouvain, Belgium |
3357 | 7 * |
8 * 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
|
9 * 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
|
10 * 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
|
11 * the License, or (at your option) any later version. |
3357 | 12 * |
13 * This program is distributed in the hope that it will be useful, but | |
14 * WITHOUT ANY WARRANTY; without even the implied warranty of | |
15 * 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
|
16 * Lesser General Public License for more details. |
3357 | 17 * |
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
|
18 * 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
|
19 * 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
|
20 * <http://www.gnu.org/licenses/>. |
3357 | 21 **/ |
22 | |
23 | |
24 #include "PrecompiledHeaders.h" | |
25 #include "FileBuffer.h" | |
26 | |
27 #include "TemporaryFile.h" | |
28 #include "OrthancException.h" | |
29 | |
30 #include <boost/filesystem/fstream.hpp> | |
31 | |
32 | |
33 namespace Orthanc | |
34 { | |
35 class FileBuffer::PImpl | |
36 { | |
37 private: | |
38 TemporaryFile file_; | |
39 boost::filesystem::ofstream stream_; | |
40 bool isWriting_; | |
41 | |
42 public: | |
43 PImpl() : | |
44 isWriting_(true) | |
45 { | |
46 stream_.open(file_.GetPath(), std::ofstream::out | std::ofstream::binary); | |
47 if (!stream_.good()) | |
48 { | |
49 throw OrthancException(ErrorCode_CannotWriteFile); | |
50 } | |
51 } | |
52 | |
53 ~PImpl() | |
54 { | |
55 if (isWriting_) | |
56 { | |
57 stream_.close(); | |
58 } | |
59 } | |
60 | |
61 void Append(const char* buffer, | |
62 size_t size) | |
63 { | |
64 if (!isWriting_) | |
65 { | |
66 throw OrthancException(ErrorCode_BadSequenceOfCalls); | |
67 } | |
68 | |
69 if (size > 0) | |
70 { | |
71 stream_.write(buffer, size); | |
72 if (!stream_.good()) | |
73 { | |
74 stream_.close(); | |
75 throw OrthancException(ErrorCode_FileStorageCannotWrite); | |
76 } | |
77 } | |
78 } | |
79 | |
80 void Read(std::string& target) | |
81 { | |
82 if (isWriting_) | |
83 { | |
84 stream_.close(); | |
85 isWriting_ = false; | |
86 } | |
87 | |
88 file_.Read(target); | |
89 } | |
90 }; | |
91 | |
92 | |
93 FileBuffer::FileBuffer() : | |
94 pimpl_(new PImpl) | |
95 { | |
96 } | |
97 | |
98 | |
99 void FileBuffer::Append(const char* buffer, | |
100 size_t size) | |
101 { | |
102 assert(pimpl_.get() != NULL); | |
103 pimpl_->Append(buffer, size); | |
104 } | |
105 | |
106 | |
107 void FileBuffer::Read(std::string& target) | |
108 { | |
109 assert(pimpl_.get() != NULL); | |
110 pimpl_->Read(target); | |
111 } | |
112 } |