X-Git-Url: https://git.creatis.insa-lyon.fr/pubgit/?a=blobdiff_plain;f=src%2FgdcmUtil.cxx;h=8fa6d9b14a896741213ae16efff9e18f7f01660e;hb=dd9001df5c1b8d5be917f635a6316aa4f0869af7;hp=5f14ec64c8b14e9ecaee2e2e6d2ed1be9580d7bc;hpb=ad84849e95facbb69ee0df9a8509d23c21fddc2b;p=gdcm.git diff --git a/src/gdcmUtil.cxx b/src/gdcmUtil.cxx index 5f14ec64..8fa6d9b1 100644 --- a/src/gdcmUtil.cxx +++ b/src/gdcmUtil.cxx @@ -3,8 +3,8 @@ Program: gdcm Module: $RCSfile: gdcmUtil.cxx,v $ Language: C++ - Date: $Date: 2005/01/25 15:35:15 $ - Version: $Revision: 1.123 $ + Date: $Date: 2005/01/28 17:29:59 $ + Version: $Revision: 1.127 $ Copyright (c) CREATIS (Centre de Recherche et d'Applications en Traitement de l'Image). All rights reserved. See Doc/License.txt or @@ -89,6 +89,9 @@ namespace gdcm { +const std::string Util::GDCM_UID = "1.2.826.0.1.3680043.2.1143"; +std::string Util::RootUID = GDCM_UID; + /** * \brief Provide a better 'c++' approach for sprintf * For example c code is: @@ -665,6 +668,23 @@ int GetMacAddrSys ( unsigned char *addr ) #endif //__sun } +/** + * Mini function to return the last digit from a number express in base 256 + * pre condition data contain an array of 6 unsigned char + * post condition carry contain the last digit + */ +inline int getlastdigit(unsigned char *data) +{ + int extended, carry = 0; + for(int i=0;i<6;i++) + { + extended = (carry << 8) + data[i]; + data[i] = extended / 10; + carry = extended % 10; + } + return carry; +} + /** * \brief Encode the mac address on a fixed lenght string of 15 characters. * we save space this way. @@ -677,24 +697,24 @@ std::string Util::GetMACAddress() // http://groups-beta.google.com/group/comp.unix.solaris/msg/ad36929d783d63be // http://bdn.borland.com/article/0,1410,26040,00.html unsigned char addr[6]; - uint64_t n = 0; - + int stat = GetMacAddrSys(addr); if (stat == 0) { - // Horner evaluation - for(int i=0; i<6; i++) + // We need to convert a 6 digit number from base 256 to base 10, using integer + // would requires a 48bits one. To avoid this we have to reimplement the div + modulo + // with string only + bool zero = false; + int res; + std::string sres; + while(!zero) { - n *= 256; - n += addr[i]; + res = getlastdigit(addr); + sres.insert(sres.begin(), '0' + res); + zero = (addr[0] == 0) && (addr[1] == 0) && (addr[2] == 0) && (addr[3] == 0) && (addr[4] == 0) && (addr[5] == 0); } - // we fit on 15 bytes maximum < 256^6. -#if defined(_MSC_VER) || defined(__BORLANDC__) - return Format("%I64u", n); -#else - return Format("%llu", n); -#endif + return sres; } else { @@ -764,6 +784,37 @@ std::string Util::GetIPAddress() return str; } +unsigned int Util::GetCurrentThreadID() +{ +// FIXME the implementation is far from complete +#if defined(_MSC_VER) || defined(__BORLANDC__) || defined(__MINGW32__) + return (unsigned int)GetCurrentThreadId(); +#endif +#ifdef __linux__ + return 0; + // Doesn't work on fedora, but is in the man page... + //return (unsigned int)gettid(); +#endif +#ifdef __sun + return (unsigned int)thr_self(); +#else + //default implementation + return 0; +#endif +} + +unsigned int Util::GetCurrentProcessID() +{ +#if defined(_MSC_VER) || defined(__BORLANDC__) || defined(__MINGW32__) + // NOTE: There is also a _getpid()... + return (unsigned int)GetCurrentProcessId(); +#else + // get process identification, POSIX + return (unsigned int)getpid(); +#endif + +} + /** * \brief Creates a new UID. As stipulate in the DICOM ref * each time a DICOM image is create it should have @@ -772,16 +823,18 @@ std::string Util::GetIPAddress() */ std::string Util::CreateUniqueUID(const std::string &root) { - std::string prefix = root; + std::string prefix; std::string append; if( root.empty() ) { - // No root was specified use "GDCM" then - // echo "gdcm" | od -b - // 0000000 147 144 143 155 012 - prefix = "147.144.143.155"; // special easter egg + // gdcm UID prefix, as supplied by http://www.medicalconnections.co.uk + prefix = RootUID; } - // else + else + { + prefix = root; + } + // A root was specified use it to forge our new UID: append += "."; append += Util::GetMACAddress(); @@ -804,35 +857,17 @@ std::string Util::CreateUniqueUID(const std::string &root) return prefix + append; } -unsigned int Util::GetCurrentThreadID() +void Util::SetRootUID(const std::string &root) { -// FIXME the implementation is far from complete -#if defined(_MSC_VER) || defined(__BORLANDC__) || defined(__MINGW32__) - return (unsigned int)GetCurrentThreadId(); -#endif -#ifdef __linux__ - return 0; - // Doesn't work on fedora, but is in the man page... - //return (unsigned int)gettid(); -#endif -#ifdef __sun - return (unsigned int)thr_self(); -#else - //default implementation - return 0; -#endif + if( root.empty() ) + RootUID = GDCM_UID; + else + RootUID = root; } -unsigned int Util::GetCurrentProcessID() +const std::string &Util::GetRootUID() { -#if defined(_MSC_VER) || defined(__BORLANDC__) || defined(__MINGW32__) - // NOTE: There is also a _getpid()... - return (unsigned int)GetCurrentProcessId(); -#else - // get process identification, POSIX - return (unsigned int)getpid(); -#endif - + return RootUID; } /**