Mercurial > hg > orthanc
diff OrthancFramework/Sources/DicomFormat/DicomValue.cpp @ 4775:add0337b928a
refactoring parsing of numbers
author | Sebastien Jodogne <s.jodogne@gmail.com> |
---|---|
date | Mon, 30 Aug 2021 10:24:36 +0200 |
parents | d9473bd5ed43 |
children | 7053502fbf97 |
line wrap: on
line diff
--- a/OrthancFramework/Sources/DicomFormat/DicomValue.cpp Wed Aug 25 17:20:21 2021 +0200 +++ b/OrthancFramework/Sources/DicomFormat/DicomValue.cpp Mon Aug 30 10:24:36 2021 +0200 @@ -104,102 +104,110 @@ } #endif - // same as ParseValue but in case the value actually contains a sequence, - // it will return the first value - // this has been introduced to support invalid "width/height" DICOM tags in some US - // images where the width is stored as "800\0" ! - template <typename T, - bool allowSigned> - static bool ParseFirstValue(T& result, - const DicomValue& source) + bool DicomValue::ParseInteger32(int32_t& result) const + { + if (IsBinary() || + IsNull()) + { + return false; + } + else + { + return SerializationToolbox::ParseInteger32(result, GetContent()); + } + } + + bool DicomValue::ParseInteger64(int64_t& result) const { - if (source.IsBinary() || - source.IsNull()) + if (IsBinary() || + IsNull()) + { + return false; + } + else + { + return SerializationToolbox::ParseInteger64(result, GetContent()); + } + } + + bool DicomValue::ParseUnsignedInteger32(uint32_t& result) const + { + if (IsBinary() || + IsNull()) + { + return false; + } + else + { + return SerializationToolbox::ParseUnsignedInteger32(result, GetContent()); + } + } + + bool DicomValue::ParseUnsignedInteger64(uint64_t& result) const + { + if (IsBinary() || + IsNull()) { return false; } - - try + else { - std::string value = Toolbox::StripSpaces(source.GetContent()); - if (value.empty()) - { - return false; - } - - if (!allowSigned && - value[0] == '-') - { - return false; - } + return SerializationToolbox::ParseUnsignedInteger64(result, GetContent()); + } + } - if (value.find("\\") == std::string::npos) - { - result = boost::lexical_cast<T>(value); - return true; - } - else - { - std::vector<std::string> tokens; - Toolbox::TokenizeString(tokens, value, '\\'); - - if (tokens.size() >= 1) - { - result = boost::lexical_cast<T>(tokens[0]); - return true; - } - - return false; - } - } - catch (boost::bad_lexical_cast&) + bool DicomValue::ParseFloat(float& result) const + { + if (IsBinary() || + IsNull()) { return false; } + else + { + return SerializationToolbox::ParseFloat(result, GetContent()); + } } + bool DicomValue::ParseDouble(double& result) const + { + if (IsBinary() || + IsNull()) + { + return false; + } + else + { + return SerializationToolbox::ParseDouble(result, GetContent()); + } + } - template <typename T, - bool allowSigned> - static bool ParseValue(T& result, - const DicomValue& source) + bool DicomValue::ParseFirstFloat(float& result) const { - if (source.IsBinary() || - source.IsNull()) + if (IsBinary() || + IsNull()) { return false; } - - try + else { - std::string value = Toolbox::StripSpaces(source.GetContent()); - if (value.empty()) - { - return false; - } + return SerializationToolbox::ParseFirstFloat(result, GetContent()); + } + } - if (!allowSigned && - value[0] == '-') - { - return false; - } - - result = boost::lexical_cast<T>(value); - return true; - } - catch (boost::bad_lexical_cast&) + bool DicomValue::ParseFirstUnsignedInteger(unsigned int& result) const + { + uint64_t value; + + if (IsBinary() || + IsNull()) { return false; } - } - - bool DicomValue::ParseInteger32(int32_t& result) const - { - int64_t tmp; - if (ParseValue<int64_t, true>(tmp, *this)) + else if (SerializationToolbox::ParseFirstUnsignedInteger64(value, GetContent())) { - result = static_cast<int32_t>(tmp); - return (tmp == static_cast<int64_t>(result)); // Check no overflow occurs + result = static_cast<unsigned int>(value); + return (static_cast<uint64_t>(result) == value); // Check no overflow } else { @@ -207,50 +215,6 @@ } } - bool DicomValue::ParseInteger64(int64_t& result) const - { - return ParseValue<int64_t, true>(result, *this); - } - - bool DicomValue::ParseUnsignedInteger32(uint32_t& result) const - { - uint64_t tmp; - if (ParseValue<uint64_t, false>(tmp, *this)) - { - result = static_cast<uint32_t>(tmp); - return (tmp == static_cast<uint64_t>(result)); // Check no overflow occurs - } - else - { - return false; - } - } - - bool DicomValue::ParseUnsignedInteger64(uint64_t& result) const - { - return ParseValue<uint64_t, false>(result, *this); - } - - bool DicomValue::ParseFloat(float& result) const - { - return ParseValue<float, true>(result, *this); - } - - bool DicomValue::ParseDouble(double& result) const - { - return ParseValue<double, true>(result, *this); - } - - bool DicomValue::ParseFirstFloat(float& result) const - { - return ParseFirstValue<float, true>(result, *this); - } - - bool DicomValue::ParseFirstUnsignedInteger(unsigned int& result) const - { - return ParseFirstValue<unsigned int, true>(result, *this); - } - bool DicomValue::CopyToString(std::string& result, bool allowBinary) const {