]> Creatis software - gdcm.git/blobdiff - src/gdcmUtil.cxx
BUG: Thanks to JP for bug report.
[gdcm.git] / src / gdcmUtil.cxx
index b551067fad83ba2b2d15d34b399955c074554e6b..05ab21a5d5c91b9944dc927ef16f0f7b944f50a1 100644 (file)
@@ -3,8 +3,8 @@
   Program:   gdcm
   Module:    $RCSfile: gdcmUtil.cxx,v $
   Language:  C++
-  Date:      $Date: 2004/11/16 02:54:35 $
-  Version:   $Revision: 1.64 $
+  Date:      $Date: 2004/12/13 15:12:33 $
+  Version:   $Revision: 1.70 $
                                                                                 
   Copyright (c) CREATIS (Centre de Recherche et d'Applications en Traitement de
   l'Image). All rights reserved. See Doc/License.txt or
 #include <stdarg.h>  //only included in implementation file
 #include <stdio.h>   //only included in implementation file
 
-#include <unistd.h>  // for gethostname
-#include <netdb.h>   // for gethostbyname
+#if defined(_MSC_VER)
+   #include <winsock.h>  // for gethostname & gethostbyname
+   #undef GetCurrentTime
+#else
+#ifndef __BORLANDC__
+   #include <unistd.h>  // for gethostname
+   #include <netdb.h>   // for gethostbyname
+#endif
+#endif
 
 namespace gdcm 
 {
-
 /**
  * \ingroup Globals
  * \brief Provide a better 'c++' approach for sprintf
@@ -88,7 +94,7 @@ int Util::CountSubstring (const std::string& str,
                           const std::string& subStr)
 {
    int count = 0;   // counts how many times it appears
-   unsigned int x = 0;       // The index position in the string
+   std::string::size_type x = 0;       // The index position in the string
 
    do
    {
@@ -114,21 +120,21 @@ std::string Util::CreateCleanString(std::string const & s)
 {
    std::string str = s;
 
-   for(unsigned int i=0;i<str.size();i++)
+   for(unsigned int i=0; i<str.size(); i++)
    {
-      if(!isprint(str[i]))
+      if(!isprint((unsigned char)str[i]))
       {
-         str[i]='.';
+         str[i] = '.';
       }
    }
 
-   if(str.size()>0)
+   if(str.size() > 0)
    {
-      if(!isprint(s[str.size()-1]))
+      if(!isprint((unsigned char)s[str.size()-1]))
       {
-         if(s[str.size()-1]==0)
+         if(s[str.size()-1] == 0)
          {
-            str[str.size()-1]=' ';
+            str[str.size()-1] = ' ';
          }
       }
    }
@@ -139,7 +145,7 @@ std::string Util::CreateCleanString(std::string const & s)
 /**
  * \ingroup Globals
  * \brief   Add a SEPARATOR to the end of the name is necessary
- * @param name file/directory name to normalize 
+ * @param   pathname file/directory name to normalize 
  */
 std::string Util::NormalizePath(std::string const & pathname)
 {
@@ -227,10 +233,12 @@ std::string Util::GetCurrentTime()
 }
 
 /**
- * \ingroup Util
  * \brief Create a /DICOM/ string:
  * It should a of even length (no odd length ever)
- * It can contains as many \0 as you want.
+ * It can contain as many (if you are reading this from your
+ * editor the following character is is backslash followed by zero
+ * that needed to be escaped with an extra backslash for doxygen) \\0
+ * as you want.
  */
 std::string Util::DicomString(const char* s, size_t l)
 {
@@ -243,7 +251,10 @@ std::string Util::DicomString(const char* s, size_t l)
  * \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.
+ * It can contain as many (if you are reading this from your
+ * editor the following character is is backslash followed by zero
+ * that needed to be escaped with an extra backslash for doxygen) \\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
@@ -293,6 +304,19 @@ std::string Util::GetIPAddress()
 #  define HOST_NAME_MAX 255
   // In this case we should maybe check the string was not truncated.
   // But I don't known how to check that...
+#if defined(_MSC_VER) || defined(__BORLANDC__)
+  // with WinSock DLL we need to initialise the WinSock before using gethostname
+  WORD wVersionRequested = MAKEWORD(1,0);
+  WSADATA WSAData;
+  int err = WSAStartup(wVersionRequested,&WSAData);
+  if (err != 0) {
+      /* Tell the user that we could not find a usable */
+      /* WinSock DLL.                                  */
+      WSACleanup();
+      return "127.0.0.1";
+  }
+#endif
+  
 #endif //HOST_NAME_MAX
 
   std::string str;
@@ -314,6 +338,11 @@ std::string Util::GetIPAddress()
             (unsigned int)((unsigned char*)pHost->h_addr_list[i])[j]);
       }
       // str now contains one local IP address 
+
+#if defined(_MSC_VER) || defined(__BORLANDC__)
+  WSACleanup();
+#endif
+  
     }
   }
   // If an error occur r == -1