annotate OrthancFramework/Sources/FileBuffer.cpp @ 5171:6b07c1a14d55

cpp wrapper
author Alain Mazy <am@osimis.io>
date Thu, 02 Mar 2023 15:08:02 +0100
parents 43e613a7756b
children 0ea402b4d901
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
3357
c0aa5f1cf2f5 new class: FileBuffer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
1 /**
c0aa5f1cf2f5 new class: FileBuffer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
2 * Orthanc - A Lightweight, RESTful DICOM Store
c0aa5f1cf2f5 new class: FileBuffer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
3 * Copyright (C) 2012-2016 Sebastien Jodogne, Medical Physics
c0aa5f1cf2f5 new class: FileBuffer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
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
c0aa5f1cf2f5 new class: FileBuffer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
7 *
c0aa5f1cf2f5 new class: FileBuffer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
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
c0aa5f1cf2f5 new class: FileBuffer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
12 *
c0aa5f1cf2f5 new class: FileBuffer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
13 * This program is distributed in the hope that it will be useful, but
c0aa5f1cf2f5 new class: FileBuffer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
14 * WITHOUT ANY WARRANTY; without even the implied warranty of
c0aa5f1cf2f5 new class: FileBuffer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
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
c0aa5f1cf2f5 new class: FileBuffer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
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
c0aa5f1cf2f5 new class: FileBuffer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
21 **/
c0aa5f1cf2f5 new class: FileBuffer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
22
c0aa5f1cf2f5 new class: FileBuffer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
23
c0aa5f1cf2f5 new class: FileBuffer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
24 #include "PrecompiledHeaders.h"
c0aa5f1cf2f5 new class: FileBuffer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
25 #include "FileBuffer.h"
c0aa5f1cf2f5 new class: FileBuffer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
26
c0aa5f1cf2f5 new class: FileBuffer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
27 #include "TemporaryFile.h"
c0aa5f1cf2f5 new class: FileBuffer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
28 #include "OrthancException.h"
c0aa5f1cf2f5 new class: FileBuffer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
29
c0aa5f1cf2f5 new class: FileBuffer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
30 #include <boost/filesystem/fstream.hpp>
c0aa5f1cf2f5 new class: FileBuffer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
31
c0aa5f1cf2f5 new class: FileBuffer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
32
c0aa5f1cf2f5 new class: FileBuffer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
33 namespace Orthanc
c0aa5f1cf2f5 new class: FileBuffer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
34 {
c0aa5f1cf2f5 new class: FileBuffer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
35 class FileBuffer::PImpl
c0aa5f1cf2f5 new class: FileBuffer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
36 {
c0aa5f1cf2f5 new class: FileBuffer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
37 private:
c0aa5f1cf2f5 new class: FileBuffer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
38 TemporaryFile file_;
c0aa5f1cf2f5 new class: FileBuffer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
39 boost::filesystem::ofstream stream_;
c0aa5f1cf2f5 new class: FileBuffer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
40 bool isWriting_;
c0aa5f1cf2f5 new class: FileBuffer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
41
c0aa5f1cf2f5 new class: FileBuffer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
42 public:
c0aa5f1cf2f5 new class: FileBuffer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
43 PImpl() :
c0aa5f1cf2f5 new class: FileBuffer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
44 isWriting_(true)
c0aa5f1cf2f5 new class: FileBuffer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
45 {
c0aa5f1cf2f5 new class: FileBuffer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
46 stream_.open(file_.GetPath(), std::ofstream::out | std::ofstream::binary);
c0aa5f1cf2f5 new class: FileBuffer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
47 if (!stream_.good())
c0aa5f1cf2f5 new class: FileBuffer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
48 {
c0aa5f1cf2f5 new class: FileBuffer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
49 throw OrthancException(ErrorCode_CannotWriteFile);
c0aa5f1cf2f5 new class: FileBuffer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
50 }
c0aa5f1cf2f5 new class: FileBuffer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
51 }
c0aa5f1cf2f5 new class: FileBuffer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
52
c0aa5f1cf2f5 new class: FileBuffer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
53 ~PImpl()
c0aa5f1cf2f5 new class: FileBuffer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
54 {
c0aa5f1cf2f5 new class: FileBuffer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
55 if (isWriting_)
c0aa5f1cf2f5 new class: FileBuffer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
56 {
c0aa5f1cf2f5 new class: FileBuffer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
57 stream_.close();
c0aa5f1cf2f5 new class: FileBuffer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
58 }
c0aa5f1cf2f5 new class: FileBuffer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
59 }
c0aa5f1cf2f5 new class: FileBuffer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
60
c0aa5f1cf2f5 new class: FileBuffer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
61 void Append(const char* buffer,
c0aa5f1cf2f5 new class: FileBuffer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
62 size_t size)
c0aa5f1cf2f5 new class: FileBuffer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
63 {
c0aa5f1cf2f5 new class: FileBuffer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
64 if (!isWriting_)
c0aa5f1cf2f5 new class: FileBuffer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
65 {
c0aa5f1cf2f5 new class: FileBuffer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
66 throw OrthancException(ErrorCode_BadSequenceOfCalls);
c0aa5f1cf2f5 new class: FileBuffer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
67 }
c0aa5f1cf2f5 new class: FileBuffer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
68
c0aa5f1cf2f5 new class: FileBuffer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
69 if (size > 0)
c0aa5f1cf2f5 new class: FileBuffer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
70 {
c0aa5f1cf2f5 new class: FileBuffer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
71 stream_.write(buffer, size);
c0aa5f1cf2f5 new class: FileBuffer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
72 if (!stream_.good())
c0aa5f1cf2f5 new class: FileBuffer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
73 {
c0aa5f1cf2f5 new class: FileBuffer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
74 stream_.close();
c0aa5f1cf2f5 new class: FileBuffer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
75 throw OrthancException(ErrorCode_FileStorageCannotWrite);
c0aa5f1cf2f5 new class: FileBuffer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
76 }
c0aa5f1cf2f5 new class: FileBuffer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
77 }
c0aa5f1cf2f5 new class: FileBuffer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
78 }
c0aa5f1cf2f5 new class: FileBuffer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
79
c0aa5f1cf2f5 new class: FileBuffer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
80 void Read(std::string& target)
c0aa5f1cf2f5 new class: FileBuffer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
81 {
c0aa5f1cf2f5 new class: FileBuffer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
82 if (isWriting_)
c0aa5f1cf2f5 new class: FileBuffer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
83 {
c0aa5f1cf2f5 new class: FileBuffer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
84 stream_.close();
c0aa5f1cf2f5 new class: FileBuffer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
85 isWriting_ = false;
c0aa5f1cf2f5 new class: FileBuffer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
86 }
c0aa5f1cf2f5 new class: FileBuffer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
87
c0aa5f1cf2f5 new class: FileBuffer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
88 file_.Read(target);
c0aa5f1cf2f5 new class: FileBuffer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
89 }
c0aa5f1cf2f5 new class: FileBuffer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
90 };
c0aa5f1cf2f5 new class: FileBuffer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
91
c0aa5f1cf2f5 new class: FileBuffer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
92
c0aa5f1cf2f5 new class: FileBuffer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
93 FileBuffer::FileBuffer() :
c0aa5f1cf2f5 new class: FileBuffer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
94 pimpl_(new PImpl)
c0aa5f1cf2f5 new class: FileBuffer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
95 {
c0aa5f1cf2f5 new class: FileBuffer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
96 }
c0aa5f1cf2f5 new class: FileBuffer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
97
c0aa5f1cf2f5 new class: FileBuffer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
98
c0aa5f1cf2f5 new class: FileBuffer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
99 void FileBuffer::Append(const char* buffer,
c0aa5f1cf2f5 new class: FileBuffer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
100 size_t size)
c0aa5f1cf2f5 new class: FileBuffer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
101 {
c0aa5f1cf2f5 new class: FileBuffer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
102 assert(pimpl_.get() != NULL);
c0aa5f1cf2f5 new class: FileBuffer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
103 pimpl_->Append(buffer, size);
c0aa5f1cf2f5 new class: FileBuffer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
104 }
c0aa5f1cf2f5 new class: FileBuffer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
105
c0aa5f1cf2f5 new class: FileBuffer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
106
c0aa5f1cf2f5 new class: FileBuffer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
107 void FileBuffer::Read(std::string& target)
c0aa5f1cf2f5 new class: FileBuffer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
108 {
c0aa5f1cf2f5 new class: FileBuffer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
109 assert(pimpl_.get() != NULL);
c0aa5f1cf2f5 new class: FileBuffer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
110 pimpl_->Read(target);
c0aa5f1cf2f5 new class: FileBuffer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
111 }
c0aa5f1cf2f5 new class: FileBuffer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
112 }