comparison Framework/Plugins/StorageAreaBuffer.cpp @ 194:a51ce147dbe0

refactoring using new class StorageAreaBuffer
author Sebastien Jodogne <s.jodogne@gmail.com>
date Fri, 08 Jan 2021 14:40:03 +0100
parents
children 53bd9022c58b
comparison
equal deleted inserted replaced
193:3236894320d6 194:a51ce147dbe0
1 /**
2 * Orthanc - A Lightweight, RESTful DICOM Store
3 * Copyright (C) 2012-2016 Sebastien Jodogne, Medical Physics
4 * Department, University Hospital of Liege, Belgium
5 * Copyright (C) 2017-2021 Osimis S.A., Belgium
6 *
7 * This program is free software: you can redistribute it and/or
8 * modify it under the terms of the GNU Affero General Public License
9 * as published by the Free Software Foundation, either version 3 of
10 * the License, or (at your option) any later version.
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
15 * Affero General Public License for more details.
16 *
17 * You should have received a copy of the GNU Affero General Public License
18 * along with this program. If not, see <http://www.gnu.org/licenses/>.
19 **/
20
21
22 #include "StorageAreaBuffer.h"
23
24 #include <OrthancException.h>
25
26 #include <limits>
27 #include <string.h>
28
29
30 namespace OrthancDatabases
31 {
32 StorageAreaBuffer::StorageAreaBuffer() :
33 data_(NULL),
34 size_(0)
35 {
36 }
37
38
39 void StorageAreaBuffer::Clear()
40 {
41 if (data_ != NULL)
42 {
43 free(data_);
44 data_ = NULL;
45 size_ = 0;
46 }
47 }
48
49
50 void StorageAreaBuffer::Assign(const std::string& content)
51 {
52 Clear();
53
54 size_ = static_cast<int64_t>(content.size());
55
56 if (static_cast<size_t>(size_) != content.size())
57 {
58 throw Orthanc::OrthancException(Orthanc::ErrorCode_NotEnoughMemory,
59 "File cannot be stored in a 63bit buffer");
60 }
61
62 if (content.empty())
63 {
64 data_ = NULL;
65 }
66 else
67 {
68 data_ = malloc(size_);
69
70 if (data_ == NULL)
71 {
72 throw Orthanc::OrthancException(Orthanc::ErrorCode_NotEnoughMemory);
73 }
74
75 memcpy(data_, content.c_str(), size_);
76 }
77 }
78
79
80 void* StorageAreaBuffer::ReleaseData()
81 {
82 void* result = data_;
83 data_ = NULL;
84 size_ = 0;
85 return result;
86 }
87
88
89 void StorageAreaBuffer::ToString(std::string& target)
90 {
91 target.resize(size_);
92
93 if (size_ != 0)
94 {
95 memcpy(&target[0], data_, size_);
96 }
97 }
98 }