Mercurial > hg > orthanc-wsi
comparison Framework/Inputs/Orthanc/DicomDatasetReader.cpp @ 194:e57e6ca5303d
sync
author | Sebastien Jodogne <s.jodogne@gmail.com> |
---|---|
date | Tue, 30 Jun 2020 18:03:23 +0200 |
parents | |
children |
comparison
equal
deleted
inserted
replaced
193:e690c265b315 | 194:e57e6ca5303d |
---|---|
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-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 "DicomDatasetReader.h" | |
23 | |
24 #include <OrthancException.h> | |
25 | |
26 #include <boost/lexical_cast.hpp> | |
27 | |
28 namespace OrthancPlugins | |
29 { | |
30 // This function is copied-pasted from "../../../Core/Toolbox.cpp", | |
31 // in order to avoid the dependency of plugins against the Orthanc core | |
32 static std::string StripSpaces(const std::string& source) | |
33 { | |
34 size_t first = 0; | |
35 | |
36 while (first < source.length() && | |
37 isspace(source[first])) | |
38 { | |
39 first++; | |
40 } | |
41 | |
42 if (first == source.length()) | |
43 { | |
44 // String containing only spaces | |
45 return ""; | |
46 } | |
47 | |
48 size_t last = source.length(); | |
49 while (last > first && | |
50 isspace(source[last - 1])) | |
51 { | |
52 last--; | |
53 } | |
54 | |
55 assert(first <= last); | |
56 return source.substr(first, last - first); | |
57 } | |
58 | |
59 | |
60 DicomDatasetReader::DicomDatasetReader(const IDicomDataset& dataset) : | |
61 dataset_(dataset) | |
62 { | |
63 } | |
64 | |
65 | |
66 std::string DicomDatasetReader::GetStringValue(const DicomPath& path, | |
67 const std::string& defaultValue) const | |
68 { | |
69 std::string s; | |
70 if (dataset_.GetStringValue(s, path)) | |
71 { | |
72 return s; | |
73 } | |
74 else | |
75 { | |
76 return defaultValue; | |
77 } | |
78 } | |
79 | |
80 | |
81 std::string DicomDatasetReader::GetMandatoryStringValue(const DicomPath& path) const | |
82 { | |
83 std::string s; | |
84 if (dataset_.GetStringValue(s, path)) | |
85 { | |
86 return s; | |
87 } | |
88 else | |
89 { | |
90 throw Orthanc::OrthancException(Orthanc::ErrorCode_InexistentTag); | |
91 } | |
92 } | |
93 | |
94 | |
95 template <typename T> | |
96 static bool GetValueInternal(T& target, | |
97 const IDicomDataset& dataset, | |
98 const DicomPath& path) | |
99 { | |
100 try | |
101 { | |
102 std::string s; | |
103 | |
104 if (dataset.GetStringValue(s, path)) | |
105 { | |
106 target = boost::lexical_cast<T>(StripSpaces(s)); | |
107 return true; | |
108 } | |
109 else | |
110 { | |
111 return false; | |
112 } | |
113 } | |
114 catch (boost::bad_lexical_cast&) | |
115 { | |
116 throw Orthanc::OrthancException(Orthanc::ErrorCode_BadFileFormat); | |
117 } | |
118 } | |
119 | |
120 | |
121 bool DicomDatasetReader::GetIntegerValue(int& target, | |
122 const DicomPath& path) const | |
123 { | |
124 return GetValueInternal<int>(target, dataset_, path); | |
125 } | |
126 | |
127 | |
128 bool DicomDatasetReader::GetUnsignedIntegerValue(unsigned int& target, | |
129 const DicomPath& path) const | |
130 { | |
131 int value; | |
132 | |
133 if (!GetIntegerValue(value, path)) | |
134 { | |
135 return false; | |
136 } | |
137 else if (value >= 0) | |
138 { | |
139 target = static_cast<unsigned int>(value); | |
140 return true; | |
141 } | |
142 else | |
143 { | |
144 throw Orthanc::OrthancException(Orthanc::ErrorCode_ParameterOutOfRange); | |
145 } | |
146 } | |
147 | |
148 | |
149 bool DicomDatasetReader::GetFloatValue(float& target, | |
150 const DicomPath& path) const | |
151 { | |
152 return GetValueInternal<float>(target, dataset_, path); | |
153 } | |
154 | |
155 | |
156 bool DicomDatasetReader::GetDoubleValue(double& target, | |
157 const DicomPath& path) const | |
158 { | |
159 return GetValueInternal<double>(target, dataset_, path); | |
160 } | |
161 } |