]> Creatis software - gdcm.git/blobdiff - src/gdcmUtil.cxx
Comments
[gdcm.git] / src / gdcmUtil.cxx
index 6551051bf31cb213d096f681f0ca95faf5c0b65c..5267b38d7626e361b0e70be21fb4b8f52ffd8577 100644 (file)
@@ -3,8 +3,8 @@
   Program:   gdcm
   Module:    $RCSfile: gdcmUtil.cxx,v $
   Language:  C++
-  Date:      $Date: 2005/07/18 10:20:20 $
-  Version:   $Revision: 1.157 $
+  Date:      $Date: 2005/10/28 15:52:46 $
+  Version:   $Revision: 1.172 $
                                                                                 
   Copyright (c) CREATIS (Centre de Recherche et d'Applications en Traitement de
   l'Image). All rights reserved. See Doc/License.txt or
@@ -95,7 +95,13 @@ namespace gdcm
 //-------------------------------------------------------------------------
 const std::string Util::GDCM_UID = "1.2.826.0.1.3680043.2.1143";
 std::string Util::RootUID        = GDCM_UID;
-const uint16_t Util::FMIV = 0x0001;
+/*
+ * File Meta Information Version (0002,0001) shall contain a two byte OB 
+ * value consisting of a 0x00 byte, followed by 0x01 byte, and not the 
+ * value 0x0001 encoded as a little endian 16 bit short value, 
+ * which would be the other way around...
+ */
+const uint16_t Util::FMIV = 0x0100;
 uint8_t *Util::FileMetaInformationVersion = (uint8_t *)&FMIV;
 std::string Util::GDCM_MAC_ADRESS = GetMACAddress();
 
@@ -179,10 +185,45 @@ int Util::CountSubstring (const std::string &str,
    return count;
 }
 
+/**
+ * \brief  Checks whether a 'string' is printable or not (in order
+ *         to avoid corrupting the terminal of invocation when printing)
+ * @param s string to check
+ */
+bool Util::IsCleanString(std::string const &s)
+{
+   //std::cout<< std::endl << s << std::endl;
+   for(unsigned int i=0; i<s.size(); i++)
+   {
+      if (!isprint((unsigned char)s[i]) )
+      {
+         return false;
+      }
+   }
+   return true;   
+}
+
+/**
+ * \brief  Checks whether an 'area' is printable or not (in order
+ *         to avoid corrupting the terminal of invocation when printing)
+ * @param s area to check (uint8_t is just for prototyping. feel free to cast)
+ * @param l area length to check
+ */
+bool Util::IsCleanArea(uint8_t *s, int l)
+{
+   for( int i=0; i<l; i++)
+   {
+      if (!isprint((unsigned char)s[i]) )
+      {
+         return false;
+      }
+   }
+   return true;   
+}
 /**
  * \brief  Weed out a string from the non-printable characters (in order
  *         to avoid corrupting the terminal of invocation when printing)
- * @param s string to remove non printable characters from
+ * @param s string to check (uint8_t is just for prototyping. feel free to cast)
  */
 std::string Util::CreateCleanString(std::string const &s)
 {
@@ -210,6 +251,30 @@ std::string Util::CreateCleanString(std::string const &s)
    return str;
 }
 
+/**
+ * \brief  Weed out a string from the non-printable characters (in order
+ *         to avoid corrupting the terminal of invocation when printing)
+ * @param s area to process (uint8_t is just for prototyping. feel free to cast)
+ * @param l area length to check
+ */
+std::string Util::CreateCleanString(uint8_t *s, int l)
+{
+   std::string str;
+
+   for( int i=0; i<l; i++)
+   {
+      if (!isprint((unsigned char)s[i]) )
+      {
+         str = str + '.';
+      }
+   else
+      {
+         str = str + (char )s[i];
+      }
+   }
+
+   return str;
+}
 /**
  * \brief   Add a SEPARATOR to the end of the name is necessary
  * @param   pathname file/directory name to normalize 
@@ -747,7 +812,7 @@ int GetMacAddrSys ( unsigned char *addr )
    }
    close(sd);
 #endif
-   // Not implemented platforms
+   // Not implemented platforms (or no cable !)
    perror("in Get MAC Adress (internal) : There was a configuration problem on your plateform");
    memset(addr,0,6);
    return -1;
@@ -836,7 +901,7 @@ std::string Util::CreateUniqueUID(const std::string &root)
    append += Util::GDCM_MAC_ADRESS;
    append += ".";
    append += Util::GetCurrentDateTime();
-
+   append += ".";
    //Also add a mini random number just in case:
    char tmp[10];
    int r = (int) (100.0*rand()/RAND_MAX);
@@ -873,15 +938,13 @@ const std::string &Util::GetRootUID()
 /**
  * \brief binary_write binary_write
  * @param os ostream to write to 
- * @param val val
+ * @param val 16 bits value to write
  */ 
 std::ostream &binary_write(std::ostream &os, const uint16_t &val)
 {
 #if defined(GDCM_WORDS_BIGENDIAN) || defined(GDCM_FORCE_BIGENDIAN_EMULATION)
    uint16_t swap;
-   //swap = ((( val << 8 ) & 0xff00 ) | (( val >> 8 ) & 0x00ff ) );
-   //save CPU time
-   swap = ( val << 8 |  val >> 8  );
+   swap = ( val << 8 | val >> 8 );
 
    return os.write(reinterpret_cast<const char*>(&swap), 2);
 #else
@@ -892,15 +955,12 @@ std::ostream &binary_write(std::ostream &os, const uint16_t &val)
 /**
  * \brief binary_write binary_write
  * @param os ostream to write to
- * @param val val
+ * @param val 32 bits value to write
  */ 
 std::ostream &binary_write(std::ostream &os, const uint32_t &val)
 {
 #if defined(GDCM_WORDS_BIGENDIAN) || defined(GDCM_FORCE_BIGENDIAN_EMULATION)
    uint32_t swap;
-//   swap = ( ((val<<24) & 0xff000000) | ((val<<8)  & 0x00ff0000) | 
-//            ((val>>8)  & 0x0000ff00) | ((val>>24) & 0x000000ff) );
-// save CPU time
    swap = (  (val<<24)               | ((val<<8)  & 0x00ff0000) | 
             ((val>>8)  & 0x0000ff00) |  (val>>24)               );
    return os.write(reinterpret_cast<const char*>(&swap), 4);
@@ -909,10 +969,39 @@ std::ostream &binary_write(std::ostream &os, const uint32_t &val)
 #endif //GDCM_WORDS_BIGENDIAN
 }
 
+
+/**
+ * \brief binary_write binary_write
+ * @param os ostream to write to
+ * @param val double (64 bits) value to write
+ */ 
+std::ostream &binary_write(std::ostream &os, const double &val)
+{
+#if defined(GDCM_WORDS_BIGENDIAN) || defined(GDCM_FORCE_BIGENDIAN_EMULATION)    
+   double swap;
+   
+   char *beg = (char *)&swap;
+   char *end = beg + 7;
+   char t;
+   for (unsigned int i = 0; i<7; i++)
+   {
+      t    = *beg;
+      *beg = *end;
+      *end = t;
+      beg++,
+      end--;  
+   }  
+   return os.write(reinterpret_cast<const char*>(&swap), 8);
+#else
+   return os.write(reinterpret_cast<const char*>(&val), 8);
+#endif //GDCM_WORDS_BIGENDIAN
+}
+
+
 /**
  * \brief  binary_write binary_write
  * @param os ostream to write to
- * @param val val
+ * @param val 8 bits characters aray to write
  */ 
 std::ostream &binary_write(std::ostream &os, const char *val)
 {
@@ -920,9 +1009,9 @@ std::ostream &binary_write(std::ostream &os, const char *val)
 }
 
 /**
- * \brief
+ * \brief  binary_write binary_write
  * @param os ostream to write to
- * @param val val
+ * @param val std::string value to write
  */ 
 std::ostream &binary_write(std::ostream &os, std::string const &val)
 {
@@ -932,7 +1021,7 @@ std::ostream &binary_write(std::ostream &os, std::string const &val)
 /**
  * \brief  binary_write binary_write
  * @param os ostream to write to
- * @param val value
+ * @param val 8 bits 'characters' aray to write
  * @param len length of the 'value' to be written
  */ 
 std::ostream &binary_write(std::ostream &os, const uint8_t *val, size_t len)
@@ -944,8 +1033,8 @@ std::ostream &binary_write(std::ostream &os, const uint8_t *val, size_t len)
 /**
  * \brief  binary_write binary_write
  * @param os ostream to write to
- * @param val val
- * @param len length of the 'value' to be written 
+ * @param val 16 bits words aray to write
+ * @param len length (in bytes) of the 'value' to be written 
  */ 
 std::ostream &binary_write(std::ostream &os, const uint16_t *val, size_t len)
 {