annotate OrthancStone/Sources/Toolbox/OrthancDatasets/SimplifiedOrthancDataset.cpp @ 1582:cdd1932c530c

fix cppcheck
author Sebastien Jodogne <s.jodogne@gmail.com>
date Fri, 16 Oct 2020 17:34:53 +0200
parents 85e117739eca
children 4fb8fdf03314
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
1504
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
1 /**
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
2 * Stone of Orthanc
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
3 * Copyright (C) 2012-2016 Sebastien Jodogne, Medical Physics
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
4 * Department, University Hospital of Liege, Belgium
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
5 * Copyright (C) 2017-2020 Osimis S.A., Belgium
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
6 *
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
7 * This program is free software: you can redistribute it and/or
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
8 * modify it under the terms of the GNU Affero General Public License
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
9 * as published by the Free Software Foundation, either version 3 of
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
10 * the License, or (at your option) any later version.
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
11 *
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
12 * This program is distributed in the hope that it will be useful, but
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
13 * WITHOUT ANY WARRANTY; without even the implied warranty of
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
15 * Affero General Public License for more details.
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
16 *
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
17 * You should have received a copy of the GNU Affero General Public License
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
18 * along with this program. If not, see <http://www.gnu.org/licenses/>.
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
19 **/
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
20
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
21
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
22 #include "SimplifiedOrthancDataset.h"
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
23
1571
85e117739eca cppcheck
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1512
diff changeset
24 #include <OrthancException.h>
85e117739eca cppcheck
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1512
diff changeset
25 #include <DicomParsing/FromDcmtkBridge.h>
85e117739eca cppcheck
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1512
diff changeset
26
1504
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
27 namespace OrthancStone
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
28 {
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
29 const Json::Value* SimplifiedOrthancDataset::LookupPath(const DicomPath& path) const
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
30 {
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
31 const Json::Value* content = &root_;
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
32
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
33 for (unsigned int depth = 0; depth < path.GetPrefixLength(); depth++)
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
34 {
1571
85e117739eca cppcheck
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1512
diff changeset
35 const std::string name = Orthanc::FromDcmtkBridge::GetTagName(
85e117739eca cppcheck
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1512
diff changeset
36 path.GetPrefixTag(depth), "" /* no private creator */);
1504
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
37 if (content->type() != Json::objectValue)
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
38 {
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
39 throw Orthanc::OrthancException(Orthanc::ErrorCode_BadFileFormat);
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
40 }
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
41
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
42 if (!content->isMember(name))
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
43 {
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
44 return NULL;
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
45 }
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
46
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
47 const Json::Value& sequence = (*content) [name];
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
48 if (sequence.type() != Json::arrayValue)
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
49 {
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
50 throw Orthanc::OrthancException(Orthanc::ErrorCode_BadFileFormat);
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
51 }
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
52
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
53 size_t index = path.GetPrefixIndex(depth);
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
54 if (index >= sequence.size())
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
55 {
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
56 return NULL;
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
57 }
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
58 else
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
59 {
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
60 content = &sequence[static_cast<Json::Value::ArrayIndex>(index)];
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
61 }
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
62 }
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
63
1571
85e117739eca cppcheck
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1512
diff changeset
64 const std::string name = Orthanc::FromDcmtkBridge::GetTagName(
85e117739eca cppcheck
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1512
diff changeset
65 path.GetFinalTag(), "" /* no private creator */);
1504
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
66
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
67 if (content->type() != Json::objectValue)
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
68 {
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
69 throw Orthanc::OrthancException(Orthanc::ErrorCode_BadFileFormat);
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
70 }
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
71 if (!content->isMember(name))
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
72 {
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
73 return NULL;
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
74 }
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
75 else
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
76 {
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
77 return &((*content) [name]);
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
78 }
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
79 }
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
80
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
81
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
82 void SimplifiedOrthancDataset::CheckRoot() const
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
83 {
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
84 if (root_.type() != Json::objectValue)
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
85 {
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
86 throw Orthanc::OrthancException(Orthanc::ErrorCode_BadFileFormat);
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
87 }
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
88 }
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
89
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
90
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
91 SimplifiedOrthancDataset::SimplifiedOrthancDataset(IOrthancConnection& orthanc,
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
92 const std::string& uri)
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
93 {
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
94 IOrthancConnection::RestApiGet(root_, orthanc, uri);
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
95 CheckRoot();
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
96 }
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
97
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
98
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
99 SimplifiedOrthancDataset::SimplifiedOrthancDataset(const std::string& content)
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
100 {
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
101 IOrthancConnection::ParseJson(root_, content);
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
102 CheckRoot();
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
103 }
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
104
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
105
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
106 bool SimplifiedOrthancDataset::GetStringValue(std::string& result,
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
107 const DicomPath& path) const
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
108 {
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
109 const Json::Value* value = LookupPath(path);
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
110
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
111 if (value == NULL)
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
112 {
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
113 return false;
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
114 }
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
115 else if (value->type() != Json::stringValue)
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
116 {
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
117 throw Orthanc::OrthancException(Orthanc::ErrorCode_BadFileFormat);
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
118 }
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
119 else
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
120 {
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
121 result = value->asString();
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
122 return true;
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
123 }
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
124 }
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
125
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
126
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
127 bool SimplifiedOrthancDataset::GetSequenceSize(size_t& size,
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
128 const DicomPath& path) const
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
129 {
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
130 const Json::Value* sequence = LookupPath(path);
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
131
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
132 if (sequence == NULL)
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
133 {
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
134 // Inexistent path
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
135 return false;
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
136 }
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
137 else if (sequence->type() != Json::arrayValue)
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
138 {
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
139 // Not a sequence
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
140 throw Orthanc::OrthancException(Orthanc::ErrorCode_BadFileFormat);
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
141 }
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
142 else
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
143 {
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
144 size = sequence->size();
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
145 return true;
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
146 }
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
147 }
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
148 }