changeset 3518:a57c8163d9ae

DicomMap::GetStringValue()
author Sebastien Jodogne <s.jodogne@gmail.com>
date Wed, 11 Sep 2019 17:00:59 +0200
parents e481c2b32914
children 77bede920d22
files Core/DicomFormat/DicomArray.h Core/DicomFormat/DicomMap.cpp Core/DicomFormat/DicomMap.h UnitTestsSources/DicomMapTests.cpp UnitTestsSources/FromDcmtkTests.cpp
diffstat 5 files changed, 81 insertions(+), 51 deletions(-) [+]
line wrap: on
line diff
--- a/Core/DicomFormat/DicomArray.h	Wed Sep 11 13:22:07 2019 +0200
+++ b/Core/DicomFormat/DicomArray.h	Wed Sep 11 17:00:59 2019 +0200
@@ -62,6 +62,6 @@
       return *elements_[i];
     }
 
-    void Print(FILE* fp) const;
+    void Print(FILE* fp) const;  // For debugging only
   };
 }
--- a/Core/DicomFormat/DicomMap.cpp	Wed Sep 11 13:22:07 2019 +0200
+++ b/Core/DicomFormat/DicomMap.cpp	Wed Sep 11 17:00:59 2019 +0200
@@ -41,6 +41,7 @@
 #include "../Logging.h"
 #include "../OrthancException.h"
 #include "../Toolbox.h"
+#include "DicomArray.h"
 
 
 namespace Orthanc
@@ -876,9 +877,9 @@
   }
 
 
-  bool DicomMap::CopyToString(std::string& result,
-                              const DicomTag& tag,
-                              bool allowBinary) const
+  bool DicomMap::LookupStringValue(std::string& result,
+                                   const DicomTag& tag,
+                                   bool allowBinary) const
   {
     const DicomValue* value = TestAndGetValue(tag);
 
@@ -1275,4 +1276,27 @@
       }
     }
   }
+
+
+  std::string DicomMap::GetStringValue(const DicomTag& tag,
+                                       const std::string& defaultValue,
+                                       bool allowBinary) const
+  {
+    std::string s;
+    if (LookupStringValue(s, tag, allowBinary))
+    {
+      return s;
+    }
+    else
+    {
+      return defaultValue;
+    }
+  }
+
+
+  void DicomMap::Print(FILE* fp) const
+  {
+    DicomArray a(*this);
+    a.Print(fp);
+  }
 }
--- a/Core/DicomFormat/DicomMap.h	Wed Sep 11 13:22:07 2019 +0200
+++ b/Core/DicomFormat/DicomMap.h	Wed Sep 11 17:00:59 2019 +0200
@@ -198,9 +198,9 @@
 
     void LogMissingTagsForStore() const;
 
-    bool CopyToString(std::string& result,
-                      const DicomTag& tag,
-                      bool allowBinary) const;
+    bool LookupStringValue(std::string& result,
+                           const DicomTag& tag,
+                           bool allowBinary) const;
     
     bool ParseInteger32(int32_t& result,
                         const DicomTag& tag) const;
@@ -233,5 +233,11 @@
     void Unserialize(const Json::Value& source);
 
     void FromDicomWeb(const Json::Value& source);
+
+    std::string GetStringValue(const DicomTag& tag,
+                               const std::string& defaultValue,
+                               bool allowBinary) const;
+
+    void Print(FILE* fp) const;  // For debugging only
   };
 }
--- a/UnitTestsSources/DicomMapTests.cpp	Wed Sep 11 13:22:07 2019 +0200
+++ b/UnitTestsSources/DicomMapTests.cpp	Wed Sep 11 17:00:59 2019 +0200
@@ -257,8 +257,8 @@
   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_FALSE(m.LookupStringValue(s, DICOM_TAG_PATIENT_NAME, false));
+  ASSERT_TRUE(m.LookupStringValue(s, DICOM_TAG_PATIENT_NAME, true));
   ASSERT_EQ("0", s);
                
 
@@ -292,9 +292,9 @@
   ASSERT_EQ(42u, k);
   ASSERT_EQ(42ull, l);
 
-  ASSERT_TRUE(m.CopyToString(s, DICOM_TAG_PATIENT_NAME, false));
+  ASSERT_TRUE(m.LookupStringValue(s, DICOM_TAG_PATIENT_NAME, false));
   ASSERT_EQ("42", s);
-  ASSERT_TRUE(m.CopyToString(s, DICOM_TAG_PATIENT_NAME, true));
+  ASSERT_TRUE(m.LookupStringValue(s, DICOM_TAG_PATIENT_NAME, true));
   ASSERT_EQ("42", s);
                
   
@@ -597,12 +597,12 @@
     ASSERT_EQ(3u, m.GetSize());
 
     std::string s;
-    ASSERT_TRUE(m.CopyToString(s, DICOM_TAG_PATIENT_NAME, false));
+    ASSERT_TRUE(m.LookupStringValue(s, DICOM_TAG_PATIENT_NAME, false));
     ASSERT_EQ("SB1^SB2^SB3^SB4^SB5", s);
-    ASSERT_TRUE(m.CopyToString(s, DICOM_TAG_IMAGE_POSITION_PATIENT, false));
+    ASSERT_TRUE(m.LookupStringValue(s, DICOM_TAG_IMAGE_POSITION_PATIENT, false));
     ASSERT_TRUE(s.empty());
 
-    ASSERT_TRUE(m.CopyToString(s, DICOM_TAG_IMAGE_ORIENTATION_PATIENT, false));
+    ASSERT_TRUE(m.LookupStringValue(s, DICOM_TAG_IMAGE_ORIENTATION_PATIENT, false));
 
     std::vector<std::string> v;
     Orthanc::Toolbox::TokenizeString(v, s, '\\');
@@ -647,7 +647,7 @@
     ASSERT_EQ(1u, m.GetSize());
 
     std::string s;
-    ASSERT_TRUE(m.CopyToString(s, DICOM_TAG_IMAGE_ORIENTATION_PATIENT, false));
+    ASSERT_TRUE(m.LookupStringValue(s, DICOM_TAG_IMAGE_ORIENTATION_PATIENT, false));
 
     std::vector<std::string> v;
     Orthanc::Toolbox::TokenizeString(v, s, '\\');
@@ -851,41 +851,41 @@
     ASSERT_EQ(31u, m.GetSize());
 
     std::string s;
-    ASSERT_TRUE(m.CopyToString(s, DicomTag(0x0002, 0x0002), false));  ASSERT_EQ("UI", s);
-    ASSERT_TRUE(m.CopyToString(s, DicomTag(0x0040, 0x0241), false));  ASSERT_EQ("AE", s);
-    ASSERT_TRUE(m.CopyToString(s, DicomTag(0x0010, 0x1010), false));  ASSERT_EQ("AS", s);
-    ASSERT_TRUE(m.CopyToString(s, DicomTag(0x0020, 0x9165), false));  ASSERT_EQ("00100020", s);
-    ASSERT_TRUE(m.CopyToString(s, DicomTag(0x0008, 0x0052), false));  ASSERT_EQ("CS", s);
-    ASSERT_TRUE(m.CopyToString(s, DicomTag(0x0008, 0x0012), false));  ASSERT_EQ("DA", s);
-    ASSERT_TRUE(m.CopyToString(s, DicomTag(0x0010, 0x1020), false));  ASSERT_EQ("42", s);
-    ASSERT_TRUE(m.CopyToString(s, DicomTag(0x0008, 0x002a), false));  ASSERT_EQ("DT", s);
-    ASSERT_TRUE(m.CopyToString(s, DicomTag(0x0010, 0x9431), false));  ASSERT_EQ("43", s);
-    ASSERT_TRUE(m.CopyToString(s, DicomTag(0x0008, 0x1163), false));  ASSERT_EQ("44", s);
-    ASSERT_TRUE(m.CopyToString(s, DicomTag(0x0008, 0x1160), false));  ASSERT_EQ("45", s);
-    ASSERT_TRUE(m.CopyToString(s, DicomTag(0x0008, 0x0070), false));  ASSERT_EQ("LO", s);
-    ASSERT_TRUE(m.CopyToString(s, DicomTag(0x0010, 0x4000), false));  ASSERT_EQ("LT", s);
-    ASSERT_TRUE(m.CopyToString(s, DicomTag(0x0028, 0x2000), true));   ASSERT_EQ("OB", s);
-    ASSERT_TRUE(m.CopyToString(s, DicomTag(0x7fe0, 0x0009), true));   ASSERT_EQ("OD", s);
-    ASSERT_TRUE(m.CopyToString(s, DicomTag(0x0064, 0x0009), true));   ASSERT_EQ("OF", s);
-    ASSERT_TRUE(m.CopyToString(s, DicomTag(0x0028, 0x1201), true));   ASSERT_EQ("OWOW", s);
-    ASSERT_TRUE(m.CopyToString(s, DicomTag(0x0010, 0x0010), false));  ASSERT_EQ("PN", s);
-    ASSERT_TRUE(m.CopyToString(s, DicomTag(0x0008, 0x0050), false));  ASSERT_EQ("SH", s);
-    ASSERT_TRUE(m.CopyToString(s, DicomTag(0x0018, 0x6020), false));  ASSERT_EQ("-15", s);
-    ASSERT_TRUE(m.CopyToString(s, DicomTag(0x0018, 0x9219), false));  ASSERT_EQ("-16", s);
-    ASSERT_TRUE(m.CopyToString(s, DicomTag(0x0008, 0x0081), false));  ASSERT_EQ("ST", s);
-    ASSERT_TRUE(m.CopyToString(s, DicomTag(0x0008, 0x0013), false));  ASSERT_EQ("TM", s);
-    ASSERT_TRUE(m.CopyToString(s, DicomTag(0x0008, 0x0119), false));  ASSERT_EQ("UC", s);
-    ASSERT_TRUE(m.CopyToString(s, DicomTag(0x0008, 0x0016), false));  ASSERT_EQ("UI", s);
-    ASSERT_TRUE(m.CopyToString(s, DicomTag(0x0008, 0x1161), false));  ASSERT_EQ("128", s);
-    ASSERT_TRUE(m.CopyToString(s, DicomTag(0x4342, 0x1234), true));   ASSERT_EQ("UN", s);
-    ASSERT_TRUE(m.CopyToString(s, DicomTag(0x0008, 0x0120), false));  ASSERT_EQ("UR", s);
-    ASSERT_TRUE(m.CopyToString(s, DicomTag(0x0008, 0x0301), false));  ASSERT_EQ("17", s);
-    ASSERT_TRUE(m.CopyToString(s, DicomTag(0x0040, 0x0031), false));  ASSERT_EQ("UT", s);
+    ASSERT_TRUE(m.LookupStringValue(s, DicomTag(0x0002, 0x0002), false));  ASSERT_EQ("UI", s);
+    ASSERT_TRUE(m.LookupStringValue(s, DicomTag(0x0040, 0x0241), false));  ASSERT_EQ("AE", s);
+    ASSERT_TRUE(m.LookupStringValue(s, DicomTag(0x0010, 0x1010), false));  ASSERT_EQ("AS", s);
+    ASSERT_TRUE(m.LookupStringValue(s, DicomTag(0x0020, 0x9165), false));  ASSERT_EQ("00100020", s);
+    ASSERT_TRUE(m.LookupStringValue(s, DicomTag(0x0008, 0x0052), false));  ASSERT_EQ("CS", s);
+    ASSERT_TRUE(m.LookupStringValue(s, DicomTag(0x0008, 0x0012), false));  ASSERT_EQ("DA", s);
+    ASSERT_TRUE(m.LookupStringValue(s, DicomTag(0x0010, 0x1020), false));  ASSERT_EQ("42", s);
+    ASSERT_TRUE(m.LookupStringValue(s, DicomTag(0x0008, 0x002a), false));  ASSERT_EQ("DT", s);
+    ASSERT_TRUE(m.LookupStringValue(s, DicomTag(0x0010, 0x9431), false));  ASSERT_EQ("43", s);
+    ASSERT_TRUE(m.LookupStringValue(s, DicomTag(0x0008, 0x1163), false));  ASSERT_EQ("44", s);
+    ASSERT_TRUE(m.LookupStringValue(s, DicomTag(0x0008, 0x1160), false));  ASSERT_EQ("45", s);
+    ASSERT_TRUE(m.LookupStringValue(s, DicomTag(0x0008, 0x0070), false));  ASSERT_EQ("LO", s);
+    ASSERT_TRUE(m.LookupStringValue(s, DicomTag(0x0010, 0x4000), false));  ASSERT_EQ("LT", s);
+    ASSERT_TRUE(m.LookupStringValue(s, DicomTag(0x0028, 0x2000), true));   ASSERT_EQ("OB", s);
+    ASSERT_TRUE(m.LookupStringValue(s, DicomTag(0x7fe0, 0x0009), true));   ASSERT_EQ("OD", s);
+    ASSERT_TRUE(m.LookupStringValue(s, DicomTag(0x0064, 0x0009), true));   ASSERT_EQ("OF", s);
+    ASSERT_TRUE(m.LookupStringValue(s, DicomTag(0x0028, 0x1201), true));   ASSERT_EQ("OWOW", s);
+    ASSERT_TRUE(m.LookupStringValue(s, DicomTag(0x0010, 0x0010), false));  ASSERT_EQ("PN", s);
+    ASSERT_TRUE(m.LookupStringValue(s, DicomTag(0x0008, 0x0050), false));  ASSERT_EQ("SH", s);
+    ASSERT_TRUE(m.LookupStringValue(s, DicomTag(0x0018, 0x6020), false));  ASSERT_EQ("-15", s);
+    ASSERT_TRUE(m.LookupStringValue(s, DicomTag(0x0018, 0x9219), false));  ASSERT_EQ("-16", s);
+    ASSERT_TRUE(m.LookupStringValue(s, DicomTag(0x0008, 0x0081), false));  ASSERT_EQ("ST", s);
+    ASSERT_TRUE(m.LookupStringValue(s, DicomTag(0x0008, 0x0013), false));  ASSERT_EQ("TM", s);
+    ASSERT_TRUE(m.LookupStringValue(s, DicomTag(0x0008, 0x0119), false));  ASSERT_EQ("UC", s);
+    ASSERT_TRUE(m.LookupStringValue(s, DicomTag(0x0008, 0x0016), false));  ASSERT_EQ("UI", s);
+    ASSERT_TRUE(m.LookupStringValue(s, DicomTag(0x0008, 0x1161), false));  ASSERT_EQ("128", s);
+    ASSERT_TRUE(m.LookupStringValue(s, DicomTag(0x4342, 0x1234), true));   ASSERT_EQ("UN", s);
+    ASSERT_TRUE(m.LookupStringValue(s, DicomTag(0x0008, 0x0120), false));  ASSERT_EQ("UR", s);
+    ASSERT_TRUE(m.LookupStringValue(s, DicomTag(0x0008, 0x0301), false));  ASSERT_EQ("17", s);
+    ASSERT_TRUE(m.LookupStringValue(s, DicomTag(0x0040, 0x0031), false));  ASSERT_EQ("UT", s);
 
 #if DCMTK_VERSION_NUMBER == 361
-    ASSERT_TRUE(m.CopyToString(s, DicomTag(0x0066, 0x0040), false));
+    ASSERT_TRUE(m.LookupStringValue(s, DicomTag(0x0066, 0x0040), false));
 #else
-    ASSERT_TRUE(m.CopyToString(s, DicomTag(0x0066, 0x0040), true));
+    ASSERT_TRUE(m.LookupStringValue(s, DicomTag(0x0066, 0x0040), true));
 #endif
     ASSERT_EQ("46", s);
   }
--- a/UnitTestsSources/FromDcmtkTests.cpp	Wed Sep 11 13:22:07 2019 +0200
+++ b/UnitTestsSources/FromDcmtkTests.cpp	Wed Sep 11 17:00:59 2019 +0200
@@ -1615,10 +1615,10 @@
     ASSERT_EQ(2u, m.GetSize());
 
     std::string s;
-    ASSERT_TRUE(m.CopyToString(s, DICOM_TAG_SPECIFIC_CHARACTER_SET, false));
+    ASSERT_TRUE(m.LookupStringValue(s, DICOM_TAG_SPECIFIC_CHARACTER_SET, false));
     ASSERT_EQ("ISO 2022 IR 149", s);
 
-    ASSERT_TRUE(m.CopyToString(s, DICOM_TAG_PATIENT_NAME, false));
+    ASSERT_TRUE(m.LookupStringValue(s, DICOM_TAG_PATIENT_NAME, false));
     std::vector<std::string> v;
     Toolbox::TokenizeString(v, s, '=');
     ASSERT_EQ(3u, v.size());
@@ -1712,10 +1712,10 @@
     ASSERT_EQ(2u, m.GetSize());
 
     std::string s;
-    ASSERT_TRUE(m.CopyToString(s, DICOM_TAG_SPECIFIC_CHARACTER_SET, false));
+    ASSERT_TRUE(m.LookupStringValue(s, DICOM_TAG_SPECIFIC_CHARACTER_SET, false));
     ASSERT_EQ("ISO 2022 IR 87", s);
 
-    ASSERT_TRUE(m.CopyToString(s, DICOM_TAG_PATIENT_NAME, false));
+    ASSERT_TRUE(m.LookupStringValue(s, DICOM_TAG_PATIENT_NAME, false));
     std::vector<std::string> v;
     Toolbox::TokenizeString(v, s, '=');
     ASSERT_EQ(3u, v.size());