00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019 #include "gdcmUtil.h"
00020 #include "gdcmDebug.h"
00021
00022 #include <iostream>
00023 #include <stdarg.h>
00024
00025
00026 #include <time.h>
00027 #include <sys/types.h>
00028 #include <sys/stat.h>
00029
00030 #if defined(_MSC_VER) || defined(__BORLANDC__) || defined(__MINGW32__)
00031 #include <sys/timeb.h>
00032 #else
00033 #include <sys/time.h>
00034 #endif
00035
00036 #include <stdarg.h>
00037 #include <stdio.h>
00038
00039 #if defined(_MSC_VER) || defined(__BORLANDC__) || defined(__MINGW32__)
00040 #include <winsock.h>
00041
00042
00043
00044 #if !defined(__BORLANDC__) || (__BORLANDC__ >= 0x0560)
00045 #undef GetCurrentTime
00046 #endif
00047 #else
00048 #include <unistd.h>
00049 #include <netdb.h>
00050 #endif
00051
00052
00053 #ifdef _WIN32
00054 #include <snmp.h>
00055 #include <conio.h>
00056 #else
00057 #include <unistd.h>
00058 #include <stdlib.h>
00059 #include <string.h>
00060 #include <sys/types.h>
00061 #endif
00062
00063 #ifdef CMAKE_HAVE_SYS_IOCTL_H
00064 #include <sys/ioctl.h>
00065 #endif
00066 #ifdef CMAKE_HAVE_SYS_SOCKET_H
00067 #include <sys/socket.h>
00068 #endif
00069 #ifdef CMAKE_HAVE_SYS_SOCKIO_H
00070 #include <sys/sockio.h>
00071 #endif
00072 #ifdef CMAKE_HAVE_NET_IF_H
00073 #include <net/if.h>
00074 #endif
00075 #ifdef CMAKE_HAVE_NETINET_IN_H
00076 #include <netinet/in.h>
00077 #endif
00078 #ifdef CMAKE_HAVE_NET_IF_DL_H
00079 #include <net/if_dl.h>
00080 #endif
00081 #if defined(CMAKE_HAVE_NET_IF_ARP_H) && defined(__sun)
00082
00083 #include <net/if_arp.h>
00084 #endif
00085
00086
00087 #ifdef __linux__
00088 #include <sys/types.h>
00089 #include <linux/unistd.h>
00090 #endif
00091 #ifdef __sun
00092 #include <thread.h>
00093 #endif
00094
00095 namespace gdcm
00096 {
00097
00098 const std::string Util::GDCM_UID = "1.2.826.0.1.3680043.2.1143";
00099 std::string Util::RootUID = GDCM_UID;
00100
00101
00102
00103
00104
00105
00106 const uint16_t Util::FMIV = 0x0100;
00107 uint8_t *Util::FileMetaInformationVersion = (uint8_t *)&FMIV;
00108 std::string Util::GDCM_MAC_ADRESS = GetMACAddress();
00109
00110
00111
00129 std::string Util::Format(const char *format, ...)
00130 {
00131 char buffer[2048];
00132 va_list args;
00133 va_start(args, format);
00134 vsprintf(buffer, format, args);
00135 va_end(args);
00136
00137
00138 return buffer;
00139 }
00140
00141
00149 void Util::Tokenize (const std::string &str,
00150 std::vector<std::string> &tokens,
00151 const std::string &delimiters)
00152 {
00153 std::string::size_type lastPos = str.find_first_not_of(delimiters,0);
00154 std::string::size_type pos = str.find_first_of (delimiters,lastPos);
00155 while (std::string::npos != pos || std::string::npos != lastPos)
00156 {
00157 tokens.push_back(str.substr(lastPos, pos - lastPos));
00158 lastPos = str.find_first_not_of(delimiters, pos);
00159 pos = str.find_first_of (delimiters, lastPos);
00160 }
00161 }
00162
00170 int Util::CountSubstring (const std::string &str,
00171 const std::string &subStr)
00172 {
00173 int count = 0;
00174 std::string::size_type x = 0;
00175
00176 do
00177 {
00178 x = str.find(subStr,x);
00179 if (x != std::string::npos)
00180 {
00181 count++;
00182 x += subStr.length();
00183 }
00184 }
00185 while (x != std::string::npos);
00186
00187 return count;
00188 }
00189
00195 bool Util::IsCleanString(std::string const &s)
00196 {
00197 for(unsigned int i=0; i<s.size(); i++)
00198 {
00199 if (!isprint((unsigned char)s[i]) )
00200 {
00201 return false;
00202 }
00203 }
00204 return true;
00205 }
00206
00213 bool Util::IsCleanArea(uint8_t *s, int l)
00214 {
00215 for( int i=0; i<l; i++)
00216 {
00217 if (!isprint((unsigned char)s[i]) )
00218 {
00219 return false;
00220 }
00221 }
00222 return true;
00223 }
00229 std::string Util::CreateCleanString(std::string const &s)
00230 {
00231 std::string str = s;
00232
00233 for(unsigned int i=0; i<str.size(); i++)
00234 {
00235 if (!isprint((unsigned char)str[i]) )
00236 {
00237 str[i] = '.';
00238 }
00239 }
00240
00241 if (str.size() > 0 )
00242 {
00243 if (!isprint((unsigned char)s[str.size()-1]) )
00244 {
00245 if (s[str.size()-1] == 0 )
00246 {
00247 str[str.size()-1] = ' ';
00248 }
00249 }
00250 }
00251
00252 return str;
00253 }
00254
00261 std::string Util::CreateCleanString(uint8_t *s, int l)
00262 {
00263 std::string str;
00264
00265 for( int i=0; i<l; i++)
00266 {
00267 if (!isprint((unsigned char)s[i]) )
00268 {
00269 str = str + '.';
00270 }
00271 else
00272 {
00273 str = str + (char )s[i];
00274 }
00275 }
00276
00277 return str;
00278 }
00283 std::string Util::NormalizePath(std::string const &pathname)
00284 {
00285
00286
00287
00288
00289
00290
00291
00292
00293
00294 std::string name = pathname;
00295 int size = name.size();
00296
00297
00298 if ( name[size-1] != GDCM_FILESEPARATOR )
00299 {
00300 name += GDCM_FILESEPARATOR;
00301 }
00302 return name;
00303 }
00304
00309 std::string Util::GetPath(std::string const &fullName)
00310 {
00311 std::string res = fullName;
00312
00313
00314
00315
00316
00317
00318
00319
00320
00321
00322
00323
00324
00325 int pos = res.rfind(GDCM_FILESEPARATOR);
00326 res.resize(pos);
00327 return res;
00328 }
00329
00334 std::string Util::GetName(std::string const &fullName)
00335 {
00336 std::string filename = fullName;
00337
00338
00339
00340
00341
00342
00343
00344 std::string::size_type slash_pos = filename.rfind(GDCM_FILESEPARATOR);
00345 if (slash_pos != std::string::npos )
00346 {
00347 return filename.substr(slash_pos + 1);
00348 }
00349 else
00350 {
00351 return filename;
00352 }
00353 }
00354
00358 std::string Util::GetCurrentDate()
00359 {
00360 char tmp[512];
00361 time_t tloc;
00362 time (&tloc);
00363 strftime(tmp,512,"%Y%m%d", localtime(&tloc) );
00364 return tmp;
00365 }
00366
00370 std::string Util::GetCurrentTime()
00371 {
00372 char tmp[512];
00373 time_t tloc;
00374 time (&tloc);
00375 strftime(tmp,512,"%H%M%S", localtime(&tloc) );
00376 return tmp;
00377 }
00378
00383 std::string Util::GetCurrentDateTime()
00384 {
00385 char tmp[40];
00386 long milliseconds;
00387 time_t timep;
00388
00389
00390 #if defined(_MSC_VER) || defined(__BORLANDC__) || defined(__MINGW32__)
00391 struct timeb tb;
00392 ::ftime(&tb);
00393 timep = tb.time;
00394 milliseconds = tb.millitm;
00395 #else
00396 struct timeval tv;
00397 gettimeofday (&tv, NULL);
00398 timep = tv.tv_sec;
00399
00400 milliseconds = tv.tv_usec / 1000;
00401 #endif
00402
00403 struct tm *ptm = localtime (&timep);
00404
00405 strftime (tmp, sizeof (tmp), "%Y%m%d%H%M%S", ptm);
00406
00407
00408
00409 char tmpAll[80];
00410 sprintf(tmpAll,"%s%03ld",tmp,milliseconds);
00411
00412 return tmpAll;
00413 }
00414
00415 unsigned int Util::GetCurrentThreadID()
00416 {
00417
00418 #if defined(_MSC_VER) || defined(__BORLANDC__) || defined(__MINGW32__)
00419 return (unsigned int)GetCurrentThreadId();
00420 #else
00421 #ifdef __linux__
00422 return 0;
00423
00424
00425 #else
00426 #ifdef __sun
00427 return (unsigned int)thr_self();
00428 #else
00429
00430 return 0;
00431 #endif // __sun
00432 #endif // __linux__
00433 #endif // Win32
00434 }
00435
00436 unsigned int Util::GetCurrentProcessID()
00437 {
00438 #if defined(_MSC_VER) || defined(__BORLANDC__) || defined(__MINGW32__)
00439
00440 return (unsigned int)GetCurrentProcessId();
00441 #else
00442
00443 return (unsigned int)getpid();
00444 #endif
00445 }
00446
00450 bool Util::IsCurrentProcessorBigEndian()
00451 {
00452 #if defined(GDCM_WORDS_BIGENDIAN)
00453 return true;
00454 #else
00455 return false;
00456 #endif
00457 }
00458
00467 std::string Util::DicomString(const char *s, size_t l)
00468 {
00469 std::string r(s, s+l);
00470 gdcmAssertMacro( !(r.size() % 2) );
00471 return r;
00472 }
00473
00485 std::string Util::DicomString(const char *s)
00486 {
00487 size_t l = strlen(s);
00488 if ( l%2 )
00489 {
00490 l++;
00491 }
00492 std::string r(s, s+l);
00493 gdcmAssertMacro( !(r.size() % 2) );
00494 return r;
00495 }
00496
00503 bool Util::DicomStringEqual(const std::string &s1, const char *s2)
00504 {
00505
00506 std::string s1_even = s1;
00507 std::string s2_even = DicomString( s2 );
00508 if ( s1_even[s1_even.size()-1] == ' ' )
00509 {
00510 s1_even[s1_even.size()-1] = '\0';
00511 }
00512 return s1_even == s2_even;
00513 }
00514
00521 bool Util::CompareDicomString(const std::string &s1, const char *s2, int op)
00522 {
00523
00524 std::string s1_even = s1;
00525 std::string s2_even = DicomString( s2 );
00526 if ( s1_even[s1_even.size()-1] == ' ' )
00527 {
00528 s1_even[s1_even.size()-1] = '\0';
00529 }
00530 switch (op)
00531 {
00532 case GDCM_EQUAL :
00533 return s1_even == s2_even;
00534 case GDCM_DIFFERENT :
00535 return s1_even != s2_even;
00536 case GDCM_GREATER :
00537 return s1_even > s2_even;
00538 case GDCM_GREATEROREQUAL :
00539 return s1_even >= s2_even;
00540 case GDCM_LESS :
00541 return s1_even < s2_even;
00542 case GDCM_LESSOREQUAL :
00543 return s1_even <= s2_even;
00544 default :
00545 gdcmDebugMacro(" Wrong operator : " << op);
00546 return false;
00547 }
00548 }
00549
00550 #ifdef _WIN32
00551 typedef BOOL(WINAPI * pSnmpExtensionInit) (
00552 IN DWORD dwTimeZeroReference,
00553 OUT HANDLE * hPollForTrapEvent,
00554 OUT AsnObjectIdentifier * supportedView);
00555
00556 typedef BOOL(WINAPI * pSnmpExtensionTrap) (
00557 OUT AsnObjectIdentifier * enterprise,
00558 OUT AsnInteger * genericTrap,
00559 OUT AsnInteger * specificTrap,
00560 OUT AsnTimeticks * timeStamp,
00561 OUT RFC1157VarBindList * variableBindings);
00562
00563 typedef BOOL(WINAPI * pSnmpExtensionQuery) (
00564 IN BYTE requestType,
00565 IN OUT RFC1157VarBindList * variableBindings,
00566 OUT AsnInteger * errorStatus,
00567 OUT AsnInteger * errorIndex);
00568
00569 typedef BOOL(WINAPI * pSnmpExtensionInitEx) (
00570 OUT AsnObjectIdentifier * supportedView);
00571 #endif //_WIN32
00572
00574 int GetMacAddrSys ( unsigned char *addr );
00575 int GetMacAddrSys ( unsigned char *addr )
00576 {
00577 #ifdef _WIN32
00578 WSADATA WinsockData;
00579 if ( (WSAStartup(MAKEWORD(2, 0), &WinsockData)) != 0 )
00580 {
00581 std::cerr << "in Get MAC Adress (internal) : This program requires Winsock 2.x!"
00582 << std::endl;
00583 return -1;
00584 }
00585
00586 HANDLE PollForTrapEvent;
00587 AsnObjectIdentifier SupportedView;
00588 UINT OID_ifEntryType[] = { 1, 3, 6, 1, 2, 1, 2, 2, 1, 3 };
00589 UINT OID_ifEntryNum[] = { 1, 3, 6, 1, 2, 1, 2, 1 };
00590 UINT OID_ipMACEntAddr[] = { 1, 3, 6, 1, 2, 1, 2, 2, 1, 6 };
00591 AsnObjectIdentifier MIB_ifMACEntAddr = {
00592 sizeof(OID_ipMACEntAddr) / sizeof(UINT), OID_ipMACEntAddr };
00593 AsnObjectIdentifier MIB_ifEntryType = {
00594 sizeof(OID_ifEntryType) / sizeof(UINT), OID_ifEntryType };
00595 AsnObjectIdentifier MIB_ifEntryNum = {
00596 sizeof(OID_ifEntryNum) / sizeof(UINT), OID_ifEntryNum };
00597 RFC1157VarBindList varBindList;
00598 RFC1157VarBind varBind[2];
00599 AsnInteger errorStatus;
00600 AsnInteger errorIndex;
00601 AsnObjectIdentifier MIB_NULL = { 0, 0 };
00602 int ret;
00603 int dtmp;
00604 int j = 0;
00605
00606
00607 HINSTANCE m_hInst = LoadLibrary("inetmib1.dll");
00608 if (m_hInst < (HINSTANCE) HINSTANCE_ERROR)
00609 {
00610 return -1;
00611 }
00612 pSnmpExtensionInit m_Init =
00613 (pSnmpExtensionInit) GetProcAddress(m_hInst, "SnmpExtensionInit");
00614 pSnmpExtensionQuery m_Query =
00615 (pSnmpExtensionQuery) GetProcAddress(m_hInst, "SnmpExtensionQuery");
00616 m_Init(GetTickCount(), &PollForTrapEvent, &SupportedView);
00617
00618
00619 varBindList.list = varBind;
00620 varBind[0].name = MIB_NULL;
00621 varBind[1].name = MIB_NULL;
00622
00623
00624
00625 varBindList.len = 1;
00626 SNMP_oidcpy(&varBind[0].name, &MIB_ifEntryNum);
00627 m_Query(ASN_RFC1157_GETNEXTREQUEST, &varBindList, &errorStatus,
00628 &errorIndex);
00629
00630
00631 varBindList.len = 2;
00632
00633
00634 SNMP_oidcpy(&varBind[0].name, &MIB_ifEntryType);
00635
00636
00637 SNMP_oidcpy(&varBind[1].name, &MIB_ifMACEntAddr);
00638
00639 do
00640 {
00641
00642
00643
00644 ret = m_Query(ASN_RFC1157_GETNEXTREQUEST, &varBindList, &errorStatus,
00645 &errorIndex);
00646 if (!ret)
00647 {
00648 ret = 1;
00649 }
00650 else
00651 {
00652
00653 ret = SNMP_oidncmp(&varBind[0].name, &MIB_ifEntryType,
00654 MIB_ifEntryType.idLength);
00655 }
00656 if (!ret)
00657 {
00658 j++;
00659 dtmp = varBind[0].value.asnValue.number;
00660
00661
00662 if (dtmp == 6)
00663 {
00664
00665 ret = SNMP_oidncmp(&varBind[1].name, &MIB_ifMACEntAddr,
00666 MIB_ifMACEntAddr.idLength);
00667 if ( !ret && varBind[1].value.asnValue.address.stream != NULL )
00668 {
00669 if ( (varBind[1].value.asnValue.address.stream[0] == 0x44)
00670 && (varBind[1].value.asnValue.address.stream[1] == 0x45)
00671 && (varBind[1].value.asnValue.address.stream[2] == 0x53)
00672 && (varBind[1].value.asnValue.address.stream[3] == 0x54)
00673 && (varBind[1].value.asnValue.address.stream[4] == 0x00) )
00674 {
00675
00676 std::cerr << "in Get MAC Adress (internal) : Interface #"
00677 << j << " is a DUN adapter\n";
00678 continue;
00679 }
00680 if ( (varBind[1].value.asnValue.address.stream[0] == 0x00)
00681 && (varBind[1].value.asnValue.address.stream[1] == 0x00)
00682 && (varBind[1].value.asnValue.address.stream[2] == 0x00)
00683 && (varBind[1].value.asnValue.address.stream[3] == 0x00)
00684 && (varBind[1].value.asnValue.address.stream[4] == 0x00)
00685 && (varBind[1].value.asnValue.address.stream[5] == 0x00) )
00686 {
00687
00688
00689 std::cerr << "in Get MAC Adress (internal) : Interface #"
00690 << j << " is a NULL address\n";
00691 continue;
00692 }
00693 memcpy( addr, varBind[1].value.asnValue.address.stream, 6);
00694 }
00695 }
00696 }
00697 } while (!ret);
00698
00699
00700 SNMP_FreeVarBind(&varBind[0]);
00701 SNMP_FreeVarBind(&varBind[1]);
00702 return 0;
00703 #endif //Win32 version
00704
00705
00706
00707 #if defined(CMAKE_HAVE_NET_IF_ARP_H) && defined(__sun)
00708
00709
00710 struct arpreq parpreq;
00711 struct sockaddr_in *psa;
00712 struct hostent *phost;
00713 char hostname[MAXHOSTNAMELEN];
00714 char **paddrs;
00715 int sock, status=0;
00716
00717 if (gethostname(hostname, MAXHOSTNAMELEN) != 0 )
00718 {
00719 perror("in Get MAC Adress (internal) : gethostname");
00720 return -1;
00721 }
00722 phost = gethostbyname(hostname);
00723 paddrs = phost->h_addr_list;
00724
00725 sock = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
00726 if (sock == -1 )
00727 {
00728 perror("in Get MAC Adress (internal) : sock");
00729 return -1;
00730 }
00731 memset(&parpreq, 0, sizeof(struct arpreq));
00732 psa = (struct sockaddr_in *) &parpreq.arp_pa;
00733
00734 memset(psa, 0, sizeof(struct sockaddr_in));
00735 psa->sin_family = AF_INET;
00736 memcpy(&psa->sin_addr, *paddrs, sizeof(struct in_addr));
00737
00738 status = ioctl(sock, SIOCGARP, &parpreq);
00739 if (status == -1 )
00740 {
00741 perror("in Get MAC Adress (internal) : SIOCGARP");
00742 return -1;
00743 }
00744 memcpy(addr, parpreq.arp_ha.sa_data, 6);
00745
00746 return 0;
00747 #else
00748 #ifdef CMAKE_HAVE_NET_IF_H
00749 int sd;
00750 struct ifreq ifr, *ifrp;
00751 struct ifconf ifc;
00752 char buf[1024];
00753 int n, i;
00754 unsigned char *a;
00755 #if defined(AF_LINK) && (!defined(SIOCGIFHWADDR) && !defined(SIOCGENADDR))
00756 struct sockaddr_dl *sdlp;
00757 #endif
00758
00759
00760
00761
00762
00763
00764
00765
00766 #ifdef HAVE_SA_LEN
00767 #ifndef max
00768 #define max(a,b) ((a) > (b) ? (a) : (b))
00769 #endif
00770 #define ifreq_size(i) max(sizeof(struct ifreq),\
00771 sizeof((i).ifr_name)+(i).ifr_addr.sa_len)
00772 #else
00773 #define ifreq_size(i) sizeof(struct ifreq)
00774 #endif // HAVE_SA_LEN
00775
00776 if ( (sd = socket(AF_INET, SOCK_DGRAM, IPPROTO_IP)) < 0 )
00777 {
00778 return -1;
00779 }
00780 memset(buf, 0, sizeof(buf));
00781 ifc.ifc_len = sizeof(buf);
00782 ifc.ifc_buf = buf;
00783 if (ioctl (sd, SIOCGIFCONF, (char *)&ifc) < 0)
00784 {
00785 close(sd);
00786 return -1;
00787 }
00788 n = ifc.ifc_len;
00789 for (i = 0; i < n; i+= ifreq_size(*ifrp) )
00790 {
00791 ifrp = (struct ifreq *)((char *) ifc.ifc_buf+i);
00792 strncpy(ifr.ifr_name, ifrp->ifr_name, IFNAMSIZ);
00793 #ifdef SIOCGIFHWADDR
00794 if (ioctl(sd, SIOCGIFHWADDR, &ifr) < 0)
00795 continue;
00796 a = (unsigned char *) &ifr.ifr_hwaddr.sa_data;
00797 #else
00798 #ifdef SIOCGENADDR
00799
00800
00801
00802
00803
00804 if (ioctl(sd, SIOCGENADDR, &ifr) < 0)
00805 continue;
00806 a = (unsigned char *) ifr.ifr_enaddr;
00807 #else
00808 #ifdef AF_LINK
00809 sdlp = (struct sockaddr_dl *) &ifrp->ifr_addr;
00810 if ((sdlp->sdl_family != AF_LINK) || (sdlp->sdl_alen != 6))
00811 continue;
00812 a = (unsigned char *) &sdlp->sdl_data[sdlp->sdl_nlen];
00813 #else
00814 perror("in Get MAC Adress (internal) : No way to access hardware");
00815 close(sd);
00816 return -1;
00817 #endif // AF_LINK
00818 #endif // SIOCGENADDR
00819 #endif // SIOCGIFHWADDR
00820 if (!a[0] && !a[1] && !a[2] && !a[3] && !a[4] && !a[5]) continue;
00821
00822 if (addr)
00823 {
00824 memcpy(addr, a, 6);
00825 close(sd);
00826 return 0;
00827 }
00828 }
00829 close(sd);
00830 #endif
00831
00832 perror("in Get MAC Adress (internal) : There was a configuration problem (or no cable !) on your plateform");
00833 memset(addr,0,6);
00834 return -1;
00835 #endif //__sun
00836 }
00837
00843 inline int getlastdigit(unsigned char *data)
00844 {
00845 int extended, carry = 0;
00846 for(int i=0;i<6;i++)
00847 {
00848 extended = (carry << 8) + data[i];
00849 data[i] = extended / 10;
00850 carry = extended % 10;
00851 }
00852 return carry;
00853 }
00854
00859 std::string Util::GetMACAddress()
00860 {
00861
00862
00863
00864
00865
00866 unsigned char addr[6];
00867
00868 int stat = GetMacAddrSys(addr);
00869 if (stat == 0)
00870 {
00871
00872
00873
00874 bool zero = false;
00875 int res;
00876 std::string sres;
00877 while(!zero)
00878 {
00879 res = getlastdigit(addr);
00880 sres.insert(sres.begin(), '0' + res);
00881 zero = (addr[0] == 0) && (addr[1] == 0) && (addr[2] == 0)
00882 && (addr[3] == 0) && (addr[4] == 0) && (addr[5] == 0);
00883 }
00884
00885 return sres;
00886 }
00887 else
00888 {
00889 gdcmStaticWarningMacro("Problem in finding the MAC Address");
00890 return "";
00891 }
00892 }
00893
00900 std::string Util::CreateUniqueUID(const std::string &root)
00901 {
00902 std::string prefix;
00903 std::string append;
00904 if ( root.empty() )
00905 {
00906
00907 prefix = RootUID;
00908 }
00909 else
00910 {
00911 prefix = root;
00912 }
00913
00914
00915 append += ".";
00916
00917 append += Util::GDCM_MAC_ADRESS;
00918 append += ".";
00919 append += Util::GetCurrentDateTime();
00920 append += ".";
00921
00922 char tmp[10];
00923 int r = (int) (100.0*rand()/RAND_MAX);
00924
00925 sprintf(tmp,"%02d", r);
00926 append += tmp;
00927
00928
00929 if ( (prefix + append).size() > 64 )
00930 {
00931 gdcmStaticErrorMacro( "Size of UID is too long." );
00932
00933
00934
00935 }
00936
00937 return prefix + append;
00938 }
00939
00940 void Util::SetRootUID(const std::string &root)
00941 {
00942 if ( root.empty() )
00943 RootUID = GDCM_UID;
00944 else
00945 RootUID = root;
00946 }
00947
00948 const std::string &Util::GetRootUID()
00949 {
00950 return RootUID;
00951 }
00952
00953
00959 std::ostream &binary_write(std::ostream &os, const uint16_t &val)
00960 {
00961 #if defined(GDCM_WORDS_BIGENDIAN) || defined(GDCM_FORCE_BIGENDIAN_EMULATION)
00962 uint16_t swap;
00963 swap = ( val << 8 | val >> 8 );
00964
00965 return os.write(reinterpret_cast<const char*>(&swap), 2);
00966 #else
00967 return os.write(reinterpret_cast<const char*>(&val), 2);
00968 #endif //GDCM_WORDS_BIGENDIAN
00969 }
00970
00976 std::ostream &binary_write(std::ostream &os, const uint32_t &val)
00977 {
00978 #if defined(GDCM_WORDS_BIGENDIAN) || defined(GDCM_FORCE_BIGENDIAN_EMULATION)
00979 uint32_t swap;
00980 swap = ( (val<<24) | ((val<<8) & 0x00ff0000) |
00981 ((val>>8) & 0x0000ff00) | (val>>24) );
00982 return os.write(reinterpret_cast<const char*>(&swap), 4);
00983 #else
00984 return os.write(reinterpret_cast<const char*>(&val), 4);
00985 #endif //GDCM_WORDS_BIGENDIAN
00986 }
00987
00988
00994 std::ostream &binary_write(std::ostream &os, const double &val)
00995 {
00996 #if defined(GDCM_WORDS_BIGENDIAN) || defined(GDCM_FORCE_BIGENDIAN_EMULATION)
00997 double swap = val;
00998
00999 char *beg = (char *)&swap;
01000 char *end = beg + 7;
01001 char t;
01002 for (unsigned int i = 0; i<7; i++)
01003 {
01004 t = *beg;
01005 *beg = *end;
01006 *end = t;
01007 beg++,
01008 end--;
01009 }
01010 return os.write(reinterpret_cast<const char*>(&swap), 8);
01011 #else
01012 return os.write(reinterpret_cast<const char*>(&val), 8);
01013 #endif //GDCM_WORDS_BIGENDIAN
01014 }
01015
01016
01022 std::ostream &binary_write(std::ostream &os, const char *val)
01023 {
01024 return os.write(val, strlen(val));
01025 }
01026
01032 std::ostream &binary_write(std::ostream &os, std::string const &val)
01033 {
01034 return os.write(val.c_str(), val.size());
01035 }
01036
01043 std::ostream &binary_write(std::ostream &os, const uint8_t *val, size_t len)
01044 {
01045
01046 return os.write(reinterpret_cast<const char*>(val), len);
01047 }
01048
01055 std::ostream &binary_write(std::ostream &os, const uint16_t *val, size_t len)
01056 {
01057
01058
01059
01060
01061 #if defined(GDCM_WORDS_BIGENDIAN) || defined(GDCM_FORCE_BIGENDIAN_EMULATION)
01062 const int BUFFER_SIZE = 4096;
01063 static char buffer[BUFFER_SIZE];
01064 uint16_t *binArea16 = (uint16_t*)val;
01065
01066
01067 int nbPieces = len/BUFFER_SIZE;
01068 int remainingSize = len%BUFFER_SIZE;
01069
01070 for (int j=0;j<nbPieces;j++)
01071 {
01072 uint16_t *pbuffer = (uint16_t*)buffer;
01073 for (int i = 0; i < BUFFER_SIZE/2; i++)
01074 {
01075 *pbuffer = *binArea16 >> 8 | *binArea16 << 8;
01076 pbuffer++;
01077 binArea16++;
01078 }
01079 os.write ( buffer, BUFFER_SIZE );
01080 }
01081 if ( remainingSize > 0)
01082 {
01083 uint16_t *pbuffer = (uint16_t*)buffer;
01084 for (int i = 0; i < remainingSize/2; i++)
01085 {
01086 *pbuffer = *binArea16 >> 8 | *binArea16 << 8;
01087 pbuffer++;
01088 binArea16++;
01089 }
01090 os.write ( buffer, remainingSize );
01091 }
01092 return os;
01093 #else
01094 return os.write(reinterpret_cast<const char*>(val), len);
01095 #endif
01096 }
01097
01098
01099
01100
01101
01102
01106 std::string Util::GetIPAddress()
01107 {
01108
01109
01110 #ifndef HOST_NAME_MAX
01111
01112
01113
01114 #define HOST_NAME_MAX 255
01115
01116
01117 #if defined(_MSC_VER) || defined(__BORLANDC__) || defined(__MINGW32__)
01118
01119 WORD wVersionRequested = MAKEWORD(1,0);
01120 WSADATA WSAData;
01121 int err = WSAStartup(wVersionRequested,&WSAData);
01122 if (err != 0)
01123 {
01124
01125
01126 WSACleanup();
01127 return "127.0.0.1";
01128 }
01129 #endif
01130
01131 #endif //HOST_NAME_MAX
01132
01133 std::string str;
01134 char szHostName[HOST_NAME_MAX+1];
01135 int r = gethostname(szHostName, HOST_NAME_MAX);
01136
01137 if ( r == 0 )
01138 {
01139
01140 struct hostent *pHost = gethostbyname(szHostName);
01141
01142 for( int i = 0; pHost!= NULL && pHost->h_addr_list[i]!= NULL; i++ )
01143 {
01144 for( int j = 0; j<pHost->h_length; j++ )
01145 {
01146 if ( j > 0 ) str += ".";
01147
01148 str += Util::Format("%u",
01149 (unsigned int)((unsigned char*)pHost->h_addr_list[i])[j]);
01150 }
01151
01152
01153 #if defined(_MSC_VER) || defined(__BORLANDC__) || defined(__MINGW32__)
01154 WSACleanup();
01155 #endif
01156
01157 }
01158 }
01159
01160
01161 return str;
01162 }
01163
01164 void Util::hfpswap(double *a, double *b)
01165 {
01166 double tmp;
01167 tmp=*a;
01168 *a=*b;
01169 *b=tmp;
01170 }
01171
01172
01173 }
01174