Mercurial > hg > orthanc-stone
annotate OrthancStone/Sources/Toolbox/OrthancDatasets/FullOrthancDataset.cpp @ 1653:2e3b2ed239b9
Fixed usage of object cookie
author | Benjamin Golinvaux <bgo@osimis.io> |
---|---|
date | Mon, 16 Nov 2020 22:17:01 +0100 |
parents | 8563ea5d8ae4 |
children | 9ac2a65d4172 |
rev | line source |
---|---|
1504 | 1 /** |
2 * Stone of Orthanc | |
3 * Copyright (C) 2012-2016 Sebastien Jodogne, Medical Physics | |
4 * Department, University Hospital of Liege, Belgium | |
5 * Copyright (C) 2017-2020 Osimis S.A., Belgium | |
6 * | |
7 * This program is free software: you can redistribute it and/or | |
1598
8563ea5d8ae4
relicensing some files, cf. osimis bm26 and chu agreement on 2020-05-20
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1596
diff
changeset
|
8 * modify it under the terms of the GNU Lesser General Public License |
1504 | 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 | |
1598
8563ea5d8ae4
relicensing some files, cf. osimis bm26 and chu agreement on 2020-05-20
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1596
diff
changeset
|
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
8563ea5d8ae4
relicensing some files, cf. osimis bm26 and chu agreement on 2020-05-20
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1596
diff
changeset
|
15 * Lesser General Public License for more details. |
1596
4fb8fdf03314
removed annoying whitespace
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1512
diff
changeset
|
16 * |
1598
8563ea5d8ae4
relicensing some files, cf. osimis bm26 and chu agreement on 2020-05-20
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1596
diff
changeset
|
17 * You should have received a copy of the GNU Lesser General Public |
8563ea5d8ae4
relicensing some files, cf. osimis bm26 and chu agreement on 2020-05-20
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1596
diff
changeset
|
18 * License along with this program. If not, see |
8563ea5d8ae4
relicensing some files, cf. osimis bm26 and chu agreement on 2020-05-20
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1596
diff
changeset
|
19 * <http://www.gnu.org/licenses/>. |
1504 | 20 **/ |
21 | |
22 | |
23 #include "FullOrthancDataset.h" | |
24 | |
25 #include <OrthancException.h> | |
26 | |
27 #include <stdio.h> | |
28 #include <cassert> | |
29 | |
30 namespace OrthancStone | |
31 { | |
32 static const Json::Value* AccessTag(const Json::Value& dataset, | |
33 const Orthanc::DicomTag& tag) | |
34 { | |
35 if (dataset.type() != Json::objectValue) | |
36 { | |
37 throw Orthanc::OrthancException(Orthanc::ErrorCode_BadFileFormat); | |
38 } | |
39 | |
40 char name[16]; | |
41 sprintf(name, "%04x,%04x", tag.GetGroup(), tag.GetElement()); | |
42 | |
43 if (!dataset.isMember(name)) | |
44 { | |
45 return NULL; | |
46 } | |
47 | |
48 const Json::Value& value = dataset[name]; | |
49 if (value.type() != Json::objectValue || | |
50 !value.isMember("Name") || | |
51 !value.isMember("Type") || | |
52 !value.isMember("Value") || | |
53 value["Name"].type() != Json::stringValue || | |
54 value["Type"].type() != Json::stringValue) | |
55 { | |
56 throw Orthanc::OrthancException(Orthanc::ErrorCode_BadFileFormat); | |
57 } | |
58 | |
59 return &value; | |
60 } | |
61 | |
62 | |
63 static const Json::Value& GetSequenceContent(const Json::Value& sequence) | |
64 { | |
65 assert(sequence.type() == Json::objectValue); | |
66 assert(sequence.isMember("Type")); | |
67 assert(sequence.isMember("Value")); | |
68 | |
69 const Json::Value& value = sequence["Value"]; | |
70 | |
71 if (sequence["Type"].asString() != "Sequence" || | |
72 value.type() != Json::arrayValue) | |
73 { | |
74 throw Orthanc::OrthancException(Orthanc::ErrorCode_BadFileFormat); | |
75 } | |
76 else | |
77 { | |
78 return value; | |
79 } | |
80 } | |
81 | |
82 | |
83 static bool GetStringInternal(std::string& result, | |
84 const Json::Value& tag) | |
85 { | |
86 assert(tag.type() == Json::objectValue); | |
87 assert(tag.isMember("Type")); | |
88 assert(tag.isMember("Value")); | |
89 | |
90 const Json::Value& value = tag["Value"]; | |
91 | |
92 if (tag["Type"].asString() != "String" || | |
93 value.type() != Json::stringValue) | |
94 { | |
95 throw Orthanc::OrthancException(Orthanc::ErrorCode_BadFileFormat); | |
96 } | |
97 else | |
98 { | |
99 result = value.asString(); | |
100 return true; | |
101 } | |
102 } | |
103 | |
104 | |
105 const Json::Value* FullOrthancDataset::LookupPath(const DicomPath& path) const | |
106 { | |
107 const Json::Value* content = &root_; | |
108 | |
109 for (unsigned int depth = 0; depth < path.GetPrefixLength(); depth++) | |
110 { | |
111 const Json::Value* sequence = AccessTag(*content, path.GetPrefixTag(depth)); | |
112 if (sequence == NULL) | |
113 { | |
114 return NULL; | |
115 } | |
116 | |
117 const Json::Value& nextContent = GetSequenceContent(*sequence); | |
118 | |
119 size_t index = path.GetPrefixIndex(depth); | |
120 if (index >= nextContent.size()) | |
121 { | |
122 return NULL; | |
123 } | |
124 else | |
125 { | |
126 content = &nextContent[static_cast<Json::Value::ArrayIndex>(index)]; | |
127 } | |
128 } | |
129 | |
130 return AccessTag(*content, path.GetFinalTag()); | |
131 } | |
132 | |
133 | |
134 void FullOrthancDataset::CheckRoot() const | |
135 { | |
136 if (root_.type() != Json::objectValue) | |
137 { | |
138 throw Orthanc::OrthancException(Orthanc::ErrorCode_BadFileFormat); | |
139 } | |
140 } | |
141 | |
142 | |
143 FullOrthancDataset::FullOrthancDataset(IOrthancConnection& orthanc, | |
144 const std::string& uri) | |
145 { | |
146 IOrthancConnection::RestApiGet(root_, orthanc, uri); | |
147 CheckRoot(); | |
148 } | |
149 | |
150 | |
151 FullOrthancDataset::FullOrthancDataset(const std::string& content) | |
152 { | |
153 IOrthancConnection::ParseJson(root_, content); | |
154 CheckRoot(); | |
155 } | |
156 | |
157 | |
158 FullOrthancDataset::FullOrthancDataset(const void* content, | |
159 size_t size) | |
160 { | |
161 IOrthancConnection::ParseJson(root_, content, size); | |
162 CheckRoot(); | |
163 } | |
164 | |
165 | |
166 FullOrthancDataset::FullOrthancDataset(const Json::Value& root) : | |
167 root_(root) | |
168 { | |
169 CheckRoot(); | |
170 } | |
171 | |
172 | |
173 bool FullOrthancDataset::GetStringValue(std::string& result, | |
174 const DicomPath& path) const | |
175 { | |
176 const Json::Value* value = LookupPath(path); | |
177 | |
178 if (value == NULL) | |
179 { | |
180 return false; | |
181 } | |
182 else | |
183 { | |
184 return GetStringInternal(result, *value); | |
185 } | |
186 } | |
187 | |
188 | |
189 bool FullOrthancDataset::GetSequenceSize(size_t& size, | |
190 const DicomPath& path) const | |
191 { | |
192 const Json::Value* sequence = LookupPath(path); | |
193 | |
194 if (sequence == NULL) | |
195 { | |
196 return false; | |
197 } | |
198 else | |
199 { | |
200 size = GetSequenceContent(*sequence).size(); | |
201 return true; | |
202 } | |
203 } | |
204 } |