]> Creatis software - gdcm.git/blobdiff - src/gdcmDataEntry.cxx
Thx to Jean-Michel Rouet for reporting compile error.
[gdcm.git] / src / gdcmDataEntry.cxx
index 416fccf350f128d0234784a95d8cf2cc5123cee3..07603e1b35b44df1b21cbf1ee27db0b15b81fbb9 100644 (file)
@@ -3,8 +3,8 @@
   Program:   gdcm
   Module:    $RCSfile: gdcmDataEntry.cxx,v $
   Language:  C++
-  Date:      $Date: 2008/02/13 11:44:09 $
-  Version:   $Revision: 1.52 $
+  Date:      $Date: 2010/07/09 09:20:20 $
+  Version:   $Revision: 1.56 $
                                                                                 
   Copyright (c) CREATIS (Centre de Recherche et d'Applications en Traitement de
   l'Image). All rights reserved. See Doc/License.txt or
@@ -30,6 +30,8 @@
  #include <stdlib.h> // for atof
  #include <ctype.h>  // for isdigit
 #endif
+#include <string.h> // memcpy
+#include <stdlib.h> // atof
 
 // Could be defined like MAX_SIZE_LOAD_ELEMENT_VALUE
 #define GDCM_MAX_LENGTH_TO_CONVERT_TO_HEXA 8
@@ -71,7 +73,7 @@ DataEntry::DataEntry(DocEntry *e)
 {
    Flag = FLAG_NONE;
    BinArea = 0;
-   
+
    SelfArea = true;
 
    Copy(e);
@@ -83,7 +85,6 @@ DataEntry::DataEntry(DocEntry *e)
 DataEntry::~DataEntry ()
 {
    DeleteBinArea();
-   
 }
 
 //-----------------------------------------------------------------------------
@@ -128,6 +129,99 @@ void DataEntry::CopyBinArea( uint8_t *area, uint32_t length )
    }
 }
 
+/**
+ * \brief Checks wether the current DataEntry contains number(s)
+ */
+
+bool DataEntry::IsNumerical()
+{
+   const VRKey &vr = GetVR();
+
+   return 
+          vr == "DS" ||
+          vr == "FL" ||
+          vr == "FD" ||
+          vr == "IS" || 
+          vr == "SH" ||
+          vr == "SL" ||
+          vr == "SS" ||
+          vr == "UI" ||
+          vr == "UL" ||
+          vr == "US" ;      
+}
+
+/**
+ * \brief Gets a std::vector of 'double' holding the value(s) of any 'numerical' DataEntry
+ * @param valueVector std::vector double of value(s)
+ * \return false if VR not "a 'numerical" one
+ */
+ bool DataEntry::GetNumerical(std::vector <double> &valueVector)
+ {
+    valueVector.clear();
+    
+    if (!IsNumerical()) // never trust a user !
+       return false;    
+
+    const VRKey &vr = GetVR();
+    int loop;
+    if (vr == "IS" || vr == "DS")
+    {
+       /// \todo rewrite the whole method, in order *not to use* std::string !
+       std::vector<std::string> tokens;
+    
+       Util::Tokenize ( GetString().c_str(), tokens, "\\" );
+
+       int nbValues= tokens.size();
+       if (nbValues == 0)
+          return false;
+
+       if (vr == "DS")        
+          for (loop=0; loop<nbValues; loop++) 
+             valueVector.push_back(atof(tokens[loop].c_str()));
+       else
+          for (loop=0; loop<nbValues; loop++) 
+             valueVector.push_back(atoi(tokens[loop].c_str()));
+      
+       return true;     
+    }    
+
+    uint32_t nbValues = GetValueCount();
+    if (nbValues == 0)
+       return false;
+  
+    if( vr == "US") {
+       for (loop=0; loop<nbValues; loop++)
+             valueVector.push_back(((uint16_t *)BinArea)[loop]);  
+       return true;
+    }
+    if( vr == "SS" ) {
+       for (loop=0; loop<nbValues; loop++)
+             valueVector.push_back(((int16_t *)BinArea)[loop]);  
+       return true;
+    }
+    if( vr == "UL") {
+       for (loop=0; loop<nbValues; loop++) 
+             valueVector.push_back(((uint32_t *)BinArea)[loop]);      
+       return true;
+    }
+    if(vr == "SL" ) {
+       for (loop=0; loop<nbValues; loop++) 
+             valueVector.push_back(((int32_t *)BinArea)[loop]);      
+       return true;
+    }       
+    if( vr == "FL" ) {
+       for (loop=0; loop<nbValues; loop++) 
+             valueVector.push_back(((float *)BinArea)[loop]);     
+       return true;
+    }
+    if( vr == "FD" ) {
+       for (loop=0; loop<nbValues; loop++) 
+             valueVector.push_back(((double *)BinArea)[loop]);
+       return true;
+    }     
+    return false;   // ?!? 
+ }
+
 /**
  * \brief Inserts the elementary (non string) value into the current DataEntry
  * @param id index of the elementary value to be set
@@ -427,10 +521,12 @@ void DataEntry::SetString(std::string const &value)
       NewBinArea();
       memcpy(BinArea, value.c_str(), l);
       if (l%2) // padded with blank except for UI
-         if ( vr == "UI" ) 
-            BinArea[l] = '\0';
-         else
-            BinArea[l] = ' ';                
+        {
+        if ( vr == "UI" )
+          BinArea[l] = '\0';
+        else
+          BinArea[l] = ' ';
+        }
    }
    State = STATE_LOADED;
 }
@@ -598,25 +694,39 @@ std::string const &DataEntry::GetHexaRepresentation() const
   }
   else if( vr == "FL" )
   {
-     float *data=(float *)BinArea;
+     unsigned char *toto=(unsigned char *)BinArea;
      for (unsigned int i=0; i < GetValueCount(); i++)
      {
-        s2  << std::setw( 4 ) << std::setfill( '0' );
+         s2.str("");
          if( i!=0 )
            s2 << '\\';
-        s2 << data[i];
+         unsigned int a4;
+         for(int iif=0; iif<4; iif++)
+         {
+            a4=toto[iif];
+            s2 << a4; 
+         }
      }
      *StrHexaArea=s2.str();
   }
   else if( vr == "FD" )
   {
-     double *data=(double *)BinArea;
+     //double *data=(double *)BinArea;
+     unsigned char *toto=(unsigned char *)BinArea;
      for (unsigned int i=0; i < GetValueCount(); i++)
      {
-        s2  << std::setw( 8 ) << std::setfill( '0' );
-         if( i!=0 )
+        s2.str("");
+        if( i!=0 )
            s2 << '\\';
-        s2 << data[i];
+        //s2 << data[i];
+
+         unsigned int a4;
+         for(int iid=0; iid<8; iid++)
+         {
+            a4=toto[iid];
+            s2 << a4; 
+         }
+
      }
      *StrHexaArea=s2.str();
   }