]> Creatis software - gdcm.git/blobdiff - src/gdcmUtil.cxx
ENH: Try to sync gdcm CVS and gdcm 1.2. ~2000 lines of changes, please be gentle...
[gdcm.git] / src / gdcmUtil.cxx
index 7ba0e2bf83a000d014f8008d83ed062cd18a0f98..6d474c2e5c83b3584c00db80685ea166956e8f87 100644 (file)
@@ -3,8 +3,8 @@
   Program:   gdcm
   Module:    $RCSfile: gdcmUtil.cxx,v $
   Language:  C++
-  Date:      $Date: 2005/10/24 21:31:11 $
-  Version:   $Revision: 1.167 $
+  Date:      $Date: 2006/02/16 20:06:15 $
+  Version:   $Revision: 1.183 $
                                                                                 
   Copyright (c) CREATIS (Centre de Recherche et d'Applications en Traitement de
   l'Image). All rights reserved. See Doc/License.txt or
 
 #include "gdcmUtil.h"
 #include "gdcmDebug.h"
+
 #include <iostream>
+#include <stdarg.h> // for va_list
 
 // For GetCurrentDate, GetCurrentTime
 #include <time.h>
 #include <sys/types.h>
 #include <sys/stat.h>
 
-// Two approaches, *NIX or Win32
-#ifdef CMAKE_HAVE_SYS_TIMEB_H
+#if defined(_MSC_VER) || defined(__BORLANDC__) || defined(__MINGW32__)
 #include <sys/timeb.h>
-#endif
-#ifdef CMAKE_HAVE_SYS_TIMES_H
+#else
 #include <sys/time.h>
 #endif
 
@@ -112,7 +112,7 @@ std::string Util::GDCM_MAC_ADRESS = GetMACAddress();
 /**
  * \brief Provide a better 'c++' approach for sprintf
  * For example c code is:
- * char result[200]; // hope 200 is enough
+ * char result[2048]; // hope 2048 is enough
  * sprintf(result, "%04x|%04x", group , elem);
  *
  * c++ code is 
@@ -128,7 +128,7 @@ std::string Util::GDCM_MAC_ADRESS = GetMACAddress();
  */
 std::string Util::Format(const char *format, ...)
 {
-   char buffer[2048];
+   char buffer[2048]; // hope 2048 is enough
    va_list args;
    va_start(args, format);
    vsprintf(buffer, format, args);  //might be a security flaw
@@ -194,10 +194,8 @@ int Util::CountSubstring (const std::string &str,
  */
 bool Util::IsCleanString(std::string const &s)
 {
-   std::cout<< std::endl << s << std::endl;
    for(unsigned int i=0; i<s.size(); i++)
    {
-      //std::cout<< std::endl << i << " : " << (unsigned char)s[i] << std::endl;
       if (!isprint((unsigned char)s[i]) )
       {
          return false;
@@ -279,20 +277,27 @@ std::string Util::CreateCleanString(uint8_t *s, int l)
    return str;
 }
 /**
- * \brief   Add a SEPARATOR to the end of the name is necessary
+ * \brief   Add a SEPARATOR to the end of the name if necessary
  * @param   pathname file/directory name to normalize 
  */
 std::string Util::NormalizePath(std::string const &pathname)
 {
+/*
    const char SEPARATOR_X      = '/';
    const char SEPARATOR_WIN    = '\\';
+#ifdef _WIN32
+   const std::string SEPARATOR = "\\";
+#else
    const std::string SEPARATOR = "/";
+#endif
+*/
    std::string name = pathname;
    int size = name.size();
 
-   if ( name[size-1] != SEPARATOR_X && name[size-1] != SEPARATOR_WIN )
+//   if ( name[size-1] != SEPARATOR_X && name[size-1] != SEPARATOR_WIN )
+   if ( name[size-1] != GDCM_FILESEPARATOR )
    {
-      name += SEPARATOR;
+      name += GDCM_FILESEPARATOR;
    }
    return name;
 }
@@ -304,6 +309,8 @@ std::string Util::NormalizePath(std::string const &pathname)
 std::string Util::GetPath(std::string const &fullName)
 {
    std::string res = fullName;
+/*
+   
    int pos1 = res.rfind("/");
    int pos2 = res.rfind("\\");
    if ( pos1 > pos2 )
@@ -314,7 +321,9 @@ std::string Util::GetPath(std::string const &fullName)
    {
       res.resize(pos2);
    }
-
+*/
+   int pos = res.rfind(GDCM_FILESEPARATOR);
+   res.resize(pos);
    return res;
 }
 
@@ -325,10 +334,14 @@ std::string Util::GetPath(std::string const &fullName)
 std::string Util::GetName(std::string const &fullName)
 {   
   std::string filename = fullName;
-
+/*
   std::string::size_type slash_pos = filename.rfind("/");
   std::string::size_type backslash_pos = filename.rfind("\\");
-  slash_pos = slash_pos > backslash_pos ? slash_pos : backslash_pos;
+  // At least with my gcc4.0.1, unfound char results in pos =4294967295 ...
+  //slash_pos = slash_pos > backslash_pos ? slash_pos : backslash_pos;  
+  slash_pos = slash_pos < backslash_pos ? slash_pos : backslash_pos;
+*/
+  std::string::size_type slash_pos = filename.rfind(GDCM_FILESEPARATOR);
   if (slash_pos != std::string::npos )
     {
     return filename.substr(slash_pos + 1);
@@ -484,8 +497,8 @@ std::string Util::DicomString(const char *s)
 /**
  * \brief Safely check the equality of two Dicom String:
  *        - Both strings should be of even length
- *        - We allow padding of even length string by either a null 
- *          character of a space
+ *        - We allow padding of even length string by either
+ *          a null character of a space
  */
 bool Util::DicomStringEqual(const std::string &s1, const char *s2)
 {
@@ -502,8 +515,8 @@ bool Util::DicomStringEqual(const std::string &s1, const char *s2)
 /**
  * \brief Safely compare two Dicom String:
  *        - Both strings should be of even length
- *        - We allow padding of even length string by either a null 
- *          character of a space
+ *        - We allow padding of even length string by either
+ *          a null character of a space
  */
 bool Util::CompareDicomString(const std::string &s1, const char *s2, int op)
 {
@@ -557,6 +570,29 @@ bool Util::CompareDicomString(const std::string &s1, const char *s2, int op)
            OUT AsnObjectIdentifier * supportedView);
 #endif //_WIN32
 
+#ifdef __sgi
+static int SGIGetMacAddress(unsigned char *addr)
+{
+  FILE *f = popen("/etc/nvram eaddr","r");
+  if(f == 0)
+    {
+    return -1;
+    }
+  unsigned int x[6];
+  if(fscanf(f,"%02x:%02x:%02x:%02x:%02x:%02x",
+            x,x+1,x+2,x+3,x+4,x+5) != 6)
+    {
+    pclose(f);
+    return -1;
+    }
+  for(unsigned int i = 0; i < 6; i++)
+    {
+    addr[i] = static_cast<unsigned char>(x[i]);
+    }
+  return 0;
+}
+#endif
+
 /// \brief gets current M.A.C adress (for internal use only)
 int GetMacAddrSys ( unsigned char *addr );
 int GetMacAddrSys ( unsigned char *addr )
@@ -689,6 +725,10 @@ int GetMacAddrSys ( unsigned char *addr )
    return 0;
 #endif //Win32 version
 
+#if defined(__sgi)
+   return SGIGetMacAddress(addr);
+#endif // __sgi
+
 
 // implementation for POSIX system
 #if defined(CMAKE_HAVE_NET_IF_ARP_H) && defined(__sun)
@@ -815,8 +855,8 @@ int GetMacAddrSys ( unsigned char *addr )
    }
    close(sd);
 #endif
-   // Not implemented platforms
-   perror("in Get MAC Adress (internal) : There was a configuration problem on your plateform");
+   // Not implemented platforms (or no cable !)
+   perror("in Get MAC Adress (internal) : There was a configuration problem (or no cable !) on your plateform");
    memset(addr,0,6);
    return -1;
 #endif //__sun
@@ -840,7 +880,7 @@ inline int getlastdigit(unsigned char *data)
 }
 
 /**
- * \brief Encode the mac address on a fixed lenght string of 15 characters.
+ * \brief Encode the mac address on a fixed length string of 15 characters.
  * we save space this way.
  */
 std::string Util::GetMACAddress()
@@ -873,14 +913,14 @@ std::string Util::GetMACAddress()
    }
    else
    {
-      gdcmWarningMacro("Problem in finding the MAC Address");
+      gdcmStaticWarningMacro("Problem in finding the MAC Address");
       return "";
    }
 }
 
 /**
- * \brief Creates a new UID. As stipulate in the DICOM ref
- *        each time a DICOM image is create it should have 
+ * \brief Creates a new UID. As stipulated in the DICOM ref
+ *        each time a DICOM image is created it should have 
  *        a unique identifier (URI)
  * @param root is the DICOM prefix assigned by IOS group
  */
@@ -915,7 +955,7 @@ std::string Util::CreateUniqueUID(const std::string &root)
    // If append is too long we need to rehash it
    if ( (prefix + append).size() > 64 )
    {
-      gdcmErrorMacro( "Size of UID is too long." );
+      gdcmStaticErrorMacro( "Size of UID is too long." );
       // we need a hash function to truncate this number
       // if only md5 was cross plateform
       // MD5(append);
@@ -941,7 +981,7 @@ 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)
 {
@@ -958,7 +998,7 @@ 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)
 {
@@ -972,10 +1012,37 @@ 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 = val;
+   
+   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)
 {
@@ -983,9 +1050,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)
 {
@@ -995,7 +1062,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)
@@ -1007,8 +1074,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)
 {
@@ -1119,6 +1186,14 @@ std::string Util::GetIPAddress()
    return str;
 }
 
+void Util::hfpswap(double *a, double *b)
+{
+   double tmp;
+   tmp=*a;
+   *a=*b;
+   *b=tmp;
+}
+
 //-------------------------------------------------------------------------
 } // end namespace gdcm