Mercurial > hg > orthanc
annotate OrthancFramework/Sources/FileStorage/MemoryStorageArea.cpp @ 4356:18c94a82f3d4
cppcheck
author | Sebastien Jodogne <s.jodogne@gmail.com> |
---|---|
date | Tue, 08 Dec 2020 12:55:32 +0100 |
parents | bf7b9edf6b81 |
children | d9473bd5ed43 |
rev | line source |
---|---|
2653 | 1 /** |
2 * Orthanc - A Lightweight, RESTful DICOM Store | |
3 * Copyright (C) 2012-2016 Sebastien Jodogne, Medical Physics | |
4 * Department, University Hospital of Liege, Belgium | |
3640
94f4a18a79cc
upgrade to year 2020
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3060
diff
changeset
|
5 * Copyright (C) 2017-2020 Osimis S.A., Belgium |
2653 | 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. |
2653 | 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. |
2653 | 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/>. |
2653 | 20 **/ |
21 | |
22 | |
23 #include "../PrecompiledHeaders.h" | |
24 #include "MemoryStorageArea.h" | |
25 | |
26 #include "../OrthancException.h" | |
2826
c277e0421200
unit testing of overwriting
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2653
diff
changeset
|
27 #include "../Logging.h" |
2653 | 28 |
29 namespace Orthanc | |
30 { | |
31 MemoryStorageArea::~MemoryStorageArea() | |
32 { | |
33 for (Content::iterator it = content_.begin(); it != content_.end(); ++it) | |
34 { | |
35 if (it->second != NULL) | |
36 { | |
37 delete it->second; | |
38 } | |
39 } | |
40 } | |
41 | |
42 void MemoryStorageArea::Create(const std::string& uuid, | |
43 const void* content, | |
44 size_t size, | |
45 FileContentType type) | |
46 { | |
2826
c277e0421200
unit testing of overwriting
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2653
diff
changeset
|
47 LOG(INFO) << "Creating attachment \"" << uuid << "\" of \"" << static_cast<int>(type) |
c277e0421200
unit testing of overwriting
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2653
diff
changeset
|
48 << "\" type (size: " << (size / (1024 * 1024) + 1) << "MB)"; |
c277e0421200
unit testing of overwriting
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2653
diff
changeset
|
49 |
2653 | 50 boost::mutex::scoped_lock lock(mutex_); |
51 | |
52 if (size != 0 && | |
53 content == NULL) | |
54 { | |
55 throw OrthancException(ErrorCode_NullPointer); | |
56 } | |
57 else if (content_.find(uuid) != content_.end()) | |
58 { | |
59 throw OrthancException(ErrorCode_InternalError); | |
60 } | |
61 else | |
62 { | |
63 content_[uuid] = new std::string(reinterpret_cast<const char*>(content), size); | |
64 } | |
65 } | |
66 | |
67 | |
68 void MemoryStorageArea::Read(std::string& content, | |
69 const std::string& uuid, | |
70 FileContentType type) | |
71 { | |
2826
c277e0421200
unit testing of overwriting
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2653
diff
changeset
|
72 LOG(INFO) << "Reading attachment \"" << uuid << "\" of \"" |
c277e0421200
unit testing of overwriting
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2653
diff
changeset
|
73 << static_cast<int>(type) << "\" content type"; |
c277e0421200
unit testing of overwriting
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2653
diff
changeset
|
74 |
2653 | 75 boost::mutex::scoped_lock lock(mutex_); |
76 | |
77 Content::const_iterator found = content_.find(uuid); | |
78 | |
79 if (found == content_.end()) | |
80 { | |
81 throw OrthancException(ErrorCode_InexistentFile); | |
82 } | |
83 else if (found->second == NULL) | |
84 { | |
85 throw OrthancException(ErrorCode_InternalError); | |
86 } | |
87 else | |
88 { | |
89 content.assign(*found->second); | |
90 } | |
91 } | |
92 | |
93 | |
94 void MemoryStorageArea::Remove(const std::string& uuid, | |
95 FileContentType type) | |
96 { | |
2826
c277e0421200
unit testing of overwriting
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2653
diff
changeset
|
97 LOG(INFO) << "Deleting attachment \"" << uuid << "\" of type " << static_cast<int>(type); |
c277e0421200
unit testing of overwriting
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2653
diff
changeset
|
98 |
2653 | 99 boost::mutex::scoped_lock lock(mutex_); |
100 | |
101 Content::iterator found = content_.find(uuid); | |
102 | |
103 if (found == content_.end()) | |
104 { | |
105 // Ignore second removal | |
106 } | |
107 else if (found->second == NULL) | |
108 { | |
109 throw OrthancException(ErrorCode_InternalError); | |
110 } | |
111 else | |
112 { | |
113 delete found->second; | |
114 content_.erase(found); | |
115 } | |
116 } | |
117 } |