comparison Core/DicomFormat/DicomValue.cpp @ 2410:3590c936e56f

parsing numbers in DicomValue
author Sebastien Jodogne <s.jodogne@gmail.com>
date Fri, 29 Sep 2017 10:13:52 +0200
parents a3a65de1840f
children cad393b41bc3
comparison
equal deleted inserted replaced
2409:e4045b3c9772 2410:3590c936e56f
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 }
94 } 178 }