# HG changeset patch # User Sebastien Jodogne # Date 1506677745 -7200 # Node ID cad393b41bc38504546a1c4c873ac823d6dfad71 # Parent 82d5e305fbd9e1e4a445b347d80e09522c5d2e7a handy shortcuts in DicomMap diff -r 82d5e305fbd9 -r cad393b41bc3 Core/DicomFormat/DicomMap.cpp --- a/Core/DicomFormat/DicomMap.cpp Fri Sep 29 10:16:28 2017 +0200 +++ b/Core/DicomFormat/DicomMap.cpp Fri Sep 29 11:35:45 2017 +0200 @@ -865,4 +865,111 @@ LOG(ERROR) << "Store has failed because required tags (" << s << ") are missing for the following instance: " << t; } } + + + bool DicomMap::CopyToString(std::string& result, + const DicomTag& tag, + bool allowBinary) const + { + const DicomValue* value = TestAndGetValue(tag); + + if (value == NULL) + { + return false; + } + else + { + return value->CopyToString(result, allowBinary); + } + } + + bool DicomMap::ParseInteger32(int32_t& result, + const DicomTag& tag) const + { + const DicomValue* value = TestAndGetValue(tag); + + if (value == NULL) + { + return false; + } + else + { + return value->ParseInteger32(result); + } + } + + bool DicomMap::ParseInteger64(int64_t& result, + const DicomTag& tag) const + { + const DicomValue* value = TestAndGetValue(tag); + + if (value == NULL) + { + return false; + } + else + { + return value->ParseInteger64(result); + } + } + + bool DicomMap::ParseUnsignedInteger32(uint32_t& result, + const DicomTag& tag) const + { + const DicomValue* value = TestAndGetValue(tag); + + if (value == NULL) + { + return false; + } + else + { + return value->ParseUnsignedInteger32(result); + } + } + + bool DicomMap::ParseUnsignedInteger64(uint64_t& result, + const DicomTag& tag) const + { + const DicomValue* value = TestAndGetValue(tag); + + if (value == NULL) + { + return false; + } + else + { + return value->ParseUnsignedInteger64(result); + } + } + + bool DicomMap::ParseFloat(float& result, + const DicomTag& tag) const + { + const DicomValue* value = TestAndGetValue(tag); + + if (value == NULL) + { + return false; + } + else + { + return value->ParseFloat(result); + } + } + + bool DicomMap::ParseDouble(double& result, + const DicomTag& tag) const + { + const DicomValue* value = TestAndGetValue(tag); + + if (value == NULL) + { + return false; + } + else + { + return value->ParseDouble(result); + } + } } diff -r 82d5e305fbd9 -r cad393b41bc3 Core/DicomFormat/DicomMap.h --- a/Core/DicomFormat/DicomMap.h Fri Sep 29 10:16:28 2017 +0200 +++ b/Core/DicomFormat/DicomMap.h Fri Sep 29 11:35:45 2017 +0200 @@ -183,5 +183,27 @@ size_t size); void LogMissingTagsForStore() const; + + bool CopyToString(std::string& result, + const DicomTag& tag, + bool allowBinary) const; + + bool ParseInteger32(int32_t& result, + const DicomTag& tag) const; + + bool ParseInteger64(int64_t& result, + const DicomTag& tag) const; + + bool ParseUnsignedInteger32(uint32_t& result, + const DicomTag& tag) const; + + bool ParseUnsignedInteger64(uint64_t& result, + const DicomTag& tag) const; + + bool ParseFloat(float& result, + const DicomTag& tag) const; + + bool ParseDouble(double& result, + const DicomTag& tag) const; }; } diff -r 82d5e305fbd9 -r cad393b41bc3 Core/DicomFormat/DicomValue.cpp --- a/Core/DicomFormat/DicomValue.cpp Fri Sep 29 10:16:28 2017 +0200 +++ b/Core/DicomFormat/DicomValue.cpp Fri Sep 29 11:35:45 2017 +0200 @@ -175,4 +175,22 @@ { return ParseValue(result, *this); } + + bool DicomValue::CopyToString(std::string& result, + bool allowBinary) const + { + if (IsNull()) + { + return false; + } + else if (IsBinary() && !allowBinary) + { + return false; + } + else + { + result.assign(content_); + return true; + } + } } diff -r 82d5e305fbd9 -r cad393b41bc3 Core/DicomFormat/DicomValue.h --- a/Core/DicomFormat/DicomValue.h Fri Sep 29 10:16:28 2017 +0200 +++ b/Core/DicomFormat/DicomValue.h Fri Sep 29 11:35:45 2017 +0200 @@ -94,6 +94,9 @@ } #endif + bool CopyToString(std::string& result, + bool allowBinary) const; + bool ParseInteger32(int32_t& result) const; bool ParseInteger64(int64_t& result) const; diff -r 82d5e305fbd9 -r cad393b41bc3 Core/Toolbox.cpp --- a/Core/Toolbox.cpp Fri Sep 29 10:16:28 2017 +0200 +++ b/Core/Toolbox.cpp Fri Sep 29 11:35:45 2017 +0200 @@ -397,6 +397,7 @@ #endif +#if ORTHANC_ENABLE_LOCALE == 1 static const char* GetBoostLocaleEncoding(const Encoding sourceEncoding) { switch (sourceEncoding) @@ -463,6 +464,7 @@ throw OrthancException(ErrorCode_NotImplemented); } } +#endif #if ORTHANC_ENABLE_LOCALE == 1 diff -r 82d5e305fbd9 -r cad393b41bc3 UnitTestsSources/DicomMapTests.cpp --- a/UnitTestsSources/DicomMapTests.cpp Fri Sep 29 10:16:28 2017 +0200 +++ b/UnitTestsSources/DicomMapTests.cpp Fri Sep 29 11:35:45 2017 +0200 @@ -231,6 +231,7 @@ int64_t j; uint32_t k; uint64_t l; + std::string s; m.SetValue(DICOM_TAG_PATIENT_NAME, " ", false); // Empty value ASSERT_FALSE(m.GetValue(DICOM_TAG_PATIENT_NAME).ParseFloat(f)); @@ -248,6 +249,11 @@ ASSERT_FALSE(m.GetValue(DICOM_TAG_PATIENT_NAME).ParseUnsignedInteger32(k)); ASSERT_FALSE(m.GetValue(DICOM_TAG_PATIENT_NAME).ParseUnsignedInteger64(l)); + ASSERT_FALSE(m.CopyToString(s, DICOM_TAG_PATIENT_NAME, false)); + ASSERT_TRUE(m.CopyToString(s, DICOM_TAG_PATIENT_NAME, true)); + ASSERT_EQ("0", s); + + // 2**31-1 m.SetValue(DICOM_TAG_PATIENT_NAME, "2147483647", false); ASSERT_TRUE(m.GetValue(DICOM_TAG_PATIENT_NAME).ParseFloat(f)); @@ -262,6 +268,27 @@ ASSERT_EQ(2147483647, j); ASSERT_EQ(2147483647u, k); ASSERT_EQ(2147483647u, l); + + // Test shortcuts + m.SetValue(DICOM_TAG_PATIENT_NAME, "42", false); + ASSERT_TRUE(m.ParseFloat(f, DICOM_TAG_PATIENT_NAME)); + ASSERT_TRUE(m.ParseDouble(d, DICOM_TAG_PATIENT_NAME)); + ASSERT_TRUE(m.ParseInteger32(i, DICOM_TAG_PATIENT_NAME)); + ASSERT_TRUE(m.ParseInteger64(j, DICOM_TAG_PATIENT_NAME)); + ASSERT_TRUE(m.ParseUnsignedInteger32(k, DICOM_TAG_PATIENT_NAME)); + ASSERT_TRUE(m.ParseUnsignedInteger64(l, DICOM_TAG_PATIENT_NAME)); + ASSERT_FLOAT_EQ(42.0f, f); + ASSERT_DOUBLE_EQ(42.0, d); + ASSERT_EQ(42, i); + ASSERT_EQ(42, j); + ASSERT_EQ(42u, k); + ASSERT_EQ(42u, l); + + ASSERT_TRUE(m.CopyToString(s, DICOM_TAG_PATIENT_NAME, false)); + ASSERT_EQ("42", s); + ASSERT_TRUE(m.CopyToString(s, DICOM_TAG_PATIENT_NAME, true)); + ASSERT_EQ("42", s); + // 2**31 m.SetValue(DICOM_TAG_PATIENT_NAME, "2147483648", false);