Mercurial > hg > orthanc-stone
comparison OrthancStone/Sources/Toolbox/OrthancDatasets/SimplifiedOrthancDataset.cpp @ 1512:244ad1e4e76a
reorganization of folders
author | Sebastien Jodogne <s.jodogne@gmail.com> |
---|---|
date | Tue, 07 Jul 2020 16:21:02 +0200 |
parents | Framework/Toolbox/OrthancDatasets/SimplifiedOrthancDataset.cpp@d8af188ab545 |
children | 85e117739eca |
comparison
equal
deleted
inserted
replaced
1511:9dfeee74c1e6 | 1512:244ad1e4e76a |
---|---|
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 | |
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 "SimplifiedOrthancDataset.h" | |
23 | |
24 namespace OrthancStone | |
25 { | |
26 const Json::Value* SimplifiedOrthancDataset::LookupPath(const DicomPath& path) const | |
27 { | |
28 const Json::Value* content = &root_; | |
29 | |
30 for (unsigned int depth = 0; depth < path.GetPrefixLength(); depth++) | |
31 { | |
32 const char* name = path.GetPrefixTag(depth).GetName(); | |
33 if (content->type() != Json::objectValue) | |
34 { | |
35 throw Orthanc::OrthancException(Orthanc::ErrorCode_BadFileFormat); | |
36 } | |
37 | |
38 if (!content->isMember(name)) | |
39 { | |
40 return NULL; | |
41 } | |
42 | |
43 const Json::Value& sequence = (*content) [name]; | |
44 if (sequence.type() != Json::arrayValue) | |
45 { | |
46 throw Orthanc::OrthancException(Orthanc::ErrorCode_BadFileFormat); | |
47 } | |
48 | |
49 size_t index = path.GetPrefixIndex(depth); | |
50 if (index >= sequence.size()) | |
51 { | |
52 return NULL; | |
53 } | |
54 else | |
55 { | |
56 content = &sequence[static_cast<Json::Value::ArrayIndex>(index)]; | |
57 } | |
58 } | |
59 | |
60 const char* name = path.GetFinalTag().GetName(); | |
61 | |
62 if (content->type() != Json::objectValue) | |
63 { | |
64 throw Orthanc::OrthancException(Orthanc::ErrorCode_BadFileFormat); | |
65 } | |
66 if (!content->isMember(name)) | |
67 { | |
68 return NULL; | |
69 } | |
70 else | |
71 { | |
72 return &((*content) [name]); | |
73 } | |
74 } | |
75 | |
76 | |
77 void SimplifiedOrthancDataset::CheckRoot() const | |
78 { | |
79 if (root_.type() != Json::objectValue) | |
80 { | |
81 throw Orthanc::OrthancException(Orthanc::ErrorCode_BadFileFormat); | |
82 } | |
83 } | |
84 | |
85 | |
86 SimplifiedOrthancDataset::SimplifiedOrthancDataset(IOrthancConnection& orthanc, | |
87 const std::string& uri) | |
88 { | |
89 IOrthancConnection::RestApiGet(root_, orthanc, uri); | |
90 CheckRoot(); | |
91 } | |
92 | |
93 | |
94 SimplifiedOrthancDataset::SimplifiedOrthancDataset(const std::string& content) | |
95 { | |
96 IOrthancConnection::ParseJson(root_, content); | |
97 CheckRoot(); | |
98 } | |
99 | |
100 | |
101 bool SimplifiedOrthancDataset::GetStringValue(std::string& result, | |
102 const DicomPath& path) const | |
103 { | |
104 const Json::Value* value = LookupPath(path); | |
105 | |
106 if (value == NULL) | |
107 { | |
108 return false; | |
109 } | |
110 else if (value->type() != Json::stringValue) | |
111 { | |
112 throw Orthanc::OrthancException(Orthanc::ErrorCode_BadFileFormat); | |
113 } | |
114 else | |
115 { | |
116 result = value->asString(); | |
117 return true; | |
118 } | |
119 } | |
120 | |
121 | |
122 bool SimplifiedOrthancDataset::GetSequenceSize(size_t& size, | |
123 const DicomPath& path) const | |
124 { | |
125 const Json::Value* sequence = LookupPath(path); | |
126 | |
127 if (sequence == NULL) | |
128 { | |
129 // Inexistent path | |
130 return false; | |
131 } | |
132 else if (sequence->type() != Json::arrayValue) | |
133 { | |
134 // Not a sequence | |
135 throw Orthanc::OrthancException(Orthanc::ErrorCode_BadFileFormat); | |
136 } | |
137 else | |
138 { | |
139 size = sequence->size(); | |
140 return true; | |
141 } | |
142 } | |
143 } |