]> Creatis software - gdcm.git/blobdiff - src/gdcmUtil.cxx
ENH: Saving temp code until I have time for Win32 version. This will return the MAC...
[gdcm.git] / src / gdcmUtil.cxx
index edaee708673a42246819c89f80ecad4069a6ec0a..3cd372ab39d34d5e10f7a0eaf88275b8a2c65f44 100644 (file)
@@ -3,8 +3,8 @@
   Program:   gdcm
   Module:    $RCSfile: gdcmUtil.cxx,v $
   Language:  C++
-  Date:      $Date: 2004/11/16 10:37:54 $
-  Version:   $Revision: 1.66 $
+  Date:      $Date: 2005/01/06 18:46:02 $
+  Version:   $Revision: 1.72 $
                                                                                 
   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
 
-#if defined _MSC_VER || defined(__BORLANDC__)
+#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 
 {
@@ -92,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
    {
@@ -118,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] = ' ';
          }
       }
    }
@@ -288,6 +290,180 @@ bool Util::DicomStringEqual(const std::string& s1, const char *s2)
   return s1_even == s2_even;
 }
 
+
+
+/**
+ * \ingroup Util
+ * \brief   tells us if the processor we are working with is BigEndian or not
+ */
+bool Util::IsCurrentProcessorBigEndian()
+{
+   uint16_t intVal = 1;
+   uint8_t bigEndianRepr[4] = { 0x00, 0x00, 0x00, 0x01 };
+   int res = memcmp(reinterpret_cast<const void*>(&intVal),
+                    reinterpret_cast<const void*>(bigEndianRepr), 4);
+   if (res == 0)
+      return true;
+   else
+      return false;
+}
+
+
+#include <fcntl.h>
+#include <stdlib.h>
+#include <string.h>
+#include <strings.h> //for bzero
+#include <unistd.h>
+
+#ifdef __linux__
+#include <sys/ioctl.h>
+#include <sys/types.h>
+#include <sys/socket.h>
+#include <netinet/in.h>
+#include <linux/if.h>
+#endif
+
+#ifdef __HP_aCC
+#include <netio.h>
+#endif
+
+#ifdef _AIX
+#include <sys/ndd_var.h>
+#include <sys/kinfo.h>
+#endif
+
+long GetMacAddrSys ( u_char *addr)
+{
+/* implementation for Linux */
+#ifdef __linux__
+    struct ifreq ifr;
+    struct ifreq *IFR;
+    struct ifconf ifc;
+    char buf[1024];
+    int s, i;
+    int ok = 0;
+
+    s = socket(AF_INET, SOCK_DGRAM, 0);
+    if (s==-1) {
+        return -1;
+    }
+
+    ifc.ifc_len = sizeof(buf);
+    ifc.ifc_buf = buf;
+    ioctl(s, SIOCGIFCONF, &ifc);
+    IFR = ifc.ifc_req;
+    for (i = ifc.ifc_len / sizeof(struct ifreq); --i >= 0; IFR++) {
+
+        strcpy(ifr.ifr_name, IFR->ifr_name);
+        if (ioctl(s, SIOCGIFFLAGS, &ifr) == 0) {
+            if (! (ifr.ifr_flags & IFF_LOOPBACK)) {
+                if (ioctl(s, SIOCGIFHWADDR, &ifr) == 0) {
+                    ok = 1;
+                    break;
+                }
+            }
+        }
+    }
+
+    close(s);
+    if (ok) {
+        bcopy( ifr.ifr_hwaddr.sa_data, addr, 6);
+    }
+    else {
+        return -1;
+    }
+    return 0;
+#endif
+
+/* implementation for HP-UX */
+#ifdef __HP_aCC
+
+#define LAN_DEV0 "/dev/lan0"
+
+    int fd;
+    struct fis iocnt_block;
+    int i;
+    char net_buf[sizeof(LAN_DEV0)+1];
+    char *p;
+
+    (void)sprintf(net_buf, "%s", LAN_DEV0);
+    p = net_buf + strlen(net_buf) - 1;
+
+    /* 
+     * Get 802.3 address from card by opening the driver and interrogating it.
+     */
+    for (i = 0; i < 10; i++, (*p)++) {
+        if ((fd = open (net_buf, O_RDONLY)) != -1) {
+      iocnt_block.reqtype = LOCAL_ADDRESS;
+      ioctl (fd, NETSTAT, &iocnt_block);
+      close (fd);
+
+            if (iocnt_block.vtype == 6)
+                break;
+        }
+    }
+
+    if (fd == -1 || iocnt_block.vtype != 6) {
+        return -1;
+    }
+
+  bcopy( &iocnt_block.value.s[0], addr, 6);
+  return 0;
+
+#endif /* HPUX */
+
+/* implementation for AIX */
+#ifdef _AIX
+
+    int size;
+    struct kinfo_ndd *nddp;
+
+    size = getkerninfo(KINFO_NDD, 0, 0, 0);
+    if (size <= 0) {
+        return -1;
+    }
+    nddp = (struct kinfo_ndd *)malloc(size);
+          
+    if (!nddp) {
+        return -1;
+    }
+    if (getkerninfo(KINFO_NDD, nddp, &size, 0) < 0) {
+        free(nddp);
+        return -1;
+    }
+    bcopy(nddp->ndd_addr, addr, 6);
+    free(nddp);
+    return 0;
+#endif //_AIX
+
+/* Not implemented platforms */
+  return -1;
+}
+
+//std::string Util::GetMACAddress()
+//{
+//   // This is a rip from: http://cplus.kompf.de/macaddr.html for Linux, HPUX and AIX 
+//   // and http://tangentsoft.net/wskfaq/examples/src/snmpmac.cpp for windows version
+//   long stat;
+//   int i;
+//   u_char addr[6];
+// 
+//   stat = mac_addr_sys( addr);
+//   if (0 == stat)
+//   {
+//      printf( "MAC address = ");
+//      for (i=0; i<6; ++i) {
+//           printf("%2.2x", addr[i]);
+//         }
+//         printf( "\n");
+//     }
+//     else {
+//         fprintf( stderr, "can't get MAC address\n");
+//         exit( 1);
+//     }
+//}
+
 /**
  * \ingroup Util
  * \brief   Return the IP adress of the machine writting the DICOM image
@@ -302,6 +478,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;
@@ -323,6 +512,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