]> Creatis software - gdcm.git/commitdiff
ENH: Apply first patch toward better string comparison when dealing with broken DICOM...
authormalaterre <malaterre>
Tue, 16 Nov 2004 02:04:00 +0000 (02:04 +0000)
committermalaterre <malaterre>
Tue, 16 Nov 2004 02:04:00 +0000 (02:04 +0000)
Testing/CMakeLists.txt
Testing/TestUtil.cxx [new file with mode: 0644]
src/gdcmUtil.cxx
src/gdcmUtil.h

index 37e5ccd92f97d7e76fd75246dd77bbc349a515a4..d53cbc71e940dd1b6961f58993d87ce8e359ea1d 100644 (file)
@@ -12,6 +12,7 @@ SET(TEST_SOURCES
   TestHash.cxx
   TestTS.cxx
   TestVR.cxx
+  TestUtil.cxx
   TestDicomString.cxx
 )
 
diff --git a/Testing/TestUtil.cxx b/Testing/TestUtil.cxx
new file mode 100644 (file)
index 0000000..fb9cb97
--- /dev/null
@@ -0,0 +1,21 @@
+// This test should test everything in Util, since I didn't know any other 
+// way to test this class.
+
+#include "gdcm.h"
+
+int TestUtil(int , char * [])
+{
+   const char ref[] = "MONOCHROME1";
+   std::string a = "MONOCHROME1";
+   a += '\0';
+   std::string b = "MONOCHROME1 ";
+   std::string c = gdcm::Util::DicomString("MONOCHROME1");
+   std::string d = "MONOCHROME1";
+
+   if( !gdcm::Util::DicomStringEqual(a,ref) ) return 1;
+   if( !gdcm::Util::DicomStringEqual(b,ref) ) return 1;
+   if( !gdcm::Util::DicomStringEqual(c,ref) ) return 1;
+   if(  gdcm::Util::DicomStringEqual(d,ref) ) return 1;
+
+   return 0;
+}
index 9c096bea306a086fe87f5bb02d083aee0c0c69a0..30adaed290979a982ad03da1ca56290d6ff57374 100644 (file)
@@ -3,8 +3,8 @@
   Program:   gdcm
   Module:    $RCSfile: gdcmUtil.cxx,v $
   Language:  C++
-  Date:      $Date: 2004/11/14 00:53:10 $
-  Version:   $Revision: 1.62 $
+  Date:      $Date: 2004/11/16 02:04:00 $
+  Version:   $Revision: 1.63 $
                                                                                 
   Copyright (c) CREATIS (Centre de Recherche et d'Applications en Traitement de
   l'Image). All rights reserved. See Doc/License.txt or
@@ -197,27 +197,6 @@ std::string Util::GetName(std::string const & fullName)
     }
 } 
 
-/**
- * \ingroup Util
- * \brief Create a /DICOM/ string:
- * It should a of even lenght (no odd length ever)
- * It can contains as many \0 as you want.
- * This function is similar to DicomString(const char*), 
- * except it doesn't take a lenght. 
- * It only pad with a null character if length is odd
- */
-std::string Util::DicomString(const char* s)
-{
-   size_t l = strlen(s);
-   if( l%2 )
-   {
-      l++;
-   }
-   std::string r(s, s+l);
-   assert( !(r.size() % 2) );
-   return r;
-}
-
 /**
  * \ingroup Util
  * \brief   Get the current date of the system in a dicom string
@@ -252,11 +231,51 @@ std::string Util::GetCurrentTime()
  */
 std::string Util::DicomString(const char* s, size_t l)
 {
+   std::string r(s, s+l);
+   assert( !(r.size() % 2) ); // == basically 'l' is even
+   return r;
+}
+
+/**
+ * \ingroup Util
+ * \brief Create a /DICOM/ string:
+ * It should a of even lenght (no odd length ever)
+ * It can contains as many \0 as you want.
+ * This function is similar to DicomString(const char*), 
+ * except it doesn't take a lenght. 
+ * It only pad with a null character if length is odd
+ */
+std::string Util::DicomString(const char* s)
+{
+   size_t l = strlen(s);
+   if( l%2 )
+   {
+      l++;
+   }
    std::string r(s, s+l);
    assert( !(r.size() % 2) );
    return r;
 }
 
+/**
+ * \ingroup Util
+ * \brief Safely compare two Dicom String:
+ *        - Both string should be of even lenght
+ *        - We allow padding of even lenght string by either a null 
+ *          character of a space
+ */
+bool Util::DicomStringEqual(const std::string& s1, const char *s2)
+{
+  // s2 is the string from the DICOM reference: 'MONOCHROME1'
+  std::string s1_even = s1; //Never directly change input parameter
+  std::string s2_even = DicomString( s2 );
+  if( s1_even[s1_even.size()-1] == ' ')
+  {
+    s1_even[s1_even.size()-1] = '\0'; //replace space character by null
+  }
+  return s1_even == s2_even;
+}
+
 template <class T>
 std::ostream& binary_write(std::ostream& os, const T& val)
 {
index 764369364e85697963deff4fe3e2f88341273bfe..99cb255520818004e01cbbd0680fae3e59aaba40 100644 (file)
@@ -3,8 +3,8 @@
   Program:   gdcm
   Module:    $RCSfile: gdcmUtil.h,v $
   Language:  C++
-  Date:      $Date: 2004/11/10 18:27:24 $
-  Version:   $Revision: 1.42 $
+  Date:      $Date: 2004/11/16 02:04:00 $
+  Version:   $Revision: 1.43 $
                                                                                 
   Copyright (c) CREATIS (Centre de Recherche et d'Applications en Traitement de
   l'Image). All rights reserved. See Doc/License.txt or
@@ -53,6 +53,7 @@ public:
 
    static std::string DicomString(const char* s, size_t l);
    static std::string DicomString(const char* s);
+   static bool        DicomStringEqual(const std::string& s1, const char *s2);
 };
 
    template <class T>