comparison Resources/Orthanc/Core/DicomFormat/DicomValue.cpp @ 116:a18bfe1fdd62

sync
author Sebastien Jodogne <s.jodogne@gmail.com>
date Thu, 04 Jan 2018 10:49:34 +0100
parents ff0ef01c332c
children
comparison
equal deleted inserted replaced
115:a51dee6a1515 116:a18bfe1fdd62
1 /** 1 /**
2 * Orthanc - A Lightweight, RESTful DICOM Store 2 * Orthanc - A Lightweight, RESTful DICOM Store
3 * Copyright (C) 2012-2016 Sebastien Jodogne, Medical Physics 3 * Copyright (C) 2012-2016 Sebastien Jodogne, Medical Physics
4 * Department, University Hospital of Liege, Belgium 4 * Department, University Hospital of Liege, Belgium
5 * Copyright (C) 2017 Osimis, Belgium 5 * Copyright (C) 2017-2018 Osimis S.A., Belgium
6 * 6 *
7 * This program is free software: you can redistribute it and/or 7 * This program is free software: you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License as 8 * modify it under the terms of the GNU General Public License as
9 * published by the Free Software Foundation, either version 3 of the 9 * published by the Free Software Foundation, either version 3 of the
10 * License, or (at your option) any later version. 10 * License, or (at your option) any later version.
34 #include "../PrecompiledHeaders.h" 34 #include "../PrecompiledHeaders.h"
35 #include "DicomValue.h" 35 #include "DicomValue.h"
36 36
37 #include "../OrthancException.h" 37 #include "../OrthancException.h"
38 #include "../Toolbox.h" 38 #include "../Toolbox.h"
39
40 #include <boost/lexical_cast.hpp>
39 41
40 namespace Orthanc 42 namespace Orthanc
41 { 43 {
42 DicomValue::DicomValue(const DicomValue& other) : 44 DicomValue::DicomValue(const DicomValue& other) :
43 type_(other.type_), 45 type_(other.type_),
89 Toolbox::EncodeBase64(target, GetContent()); 91 Toolbox::EncodeBase64(target, GetContent());
90 target.insert(0, "data:" + mime + ";base64,"); 92 target.insert(0, "data:" + mime + ";base64,");
91 } 93 }
92 #endif 94 #endif
93 95
96
97 template <typename T,
98 bool allowSigned>
99 static bool ParseValue(T& result,
100 const DicomValue& source)
101 {
102 if (source.IsBinary() ||
103 source.IsNull())
104 {
105 return false;
106 }
107
108 try
109 {
110 std::string value = Toolbox::StripSpaces(source.GetContent());
111 if (value.empty())
112 {
113 return false;
114 }
115
116 if (!allowSigned &&
117 value[0] == '-')
118 {
119 return false;
120 }
121
122 result = boost::lexical_cast<T>(value);
123 return true;
124 }
125 catch (boost::bad_lexical_cast&)
126 {
127 return false;
128 }
129 }
130
131 bool DicomValue::ParseInteger32(int32_t& result) const
132 {
133 int64_t tmp;
134 if (ParseValue<int64_t, true>(tmp, *this))
135 {
136 result = static_cast<int32_t>(tmp);
137 return (tmp == static_cast<int64_t>(result)); // Check no overflow occurs
138 }
139 else
140 {
141 return false;
142 }
143 }
144
145 bool DicomValue::ParseInteger64(int64_t& result) const
146 {
147 return ParseValue<int64_t, true>(result, *this);
148 }
149
150 bool DicomValue::ParseUnsignedInteger32(uint32_t& result) const
151 {
152 uint64_t tmp;
153 if (ParseValue<uint64_t, false>(tmp, *this))
154 {
155 result = static_cast<uint32_t>(tmp);
156 return (tmp == static_cast<uint64_t>(result)); // Check no overflow occurs
157 }
158 else
159 {
160 return false;
161 }
162 }
163
164 bool DicomValue::ParseUnsignedInteger64(uint64_t& result) const
165 {
166 return ParseValue<uint64_t, false>(result, *this);
167 }
168
169 bool DicomValue::ParseFloat(float& result) const
170 {
171 return ParseValue<float, true>(result, *this);
172 }
173
174 bool DicomValue::ParseDouble(double& result) const
175 {
176 return ParseValue<double, true>(result, *this);
177 }
178
179 bool DicomValue::CopyToString(std::string& result,
180 bool allowBinary) const
181 {
182 if (IsNull())
183 {
184 return false;
185 }
186 else if (IsBinary() && !allowBinary)
187 {
188 return false;
189 }
190 else
191 {
192 result.assign(content_);
193 return true;
194 }
195 }
94 } 196 }