+// For GetCurrentDate, GetCurrentTime
+#include <time.h>
+#include <sys/types.h>
+#include <sys/stat.h>
+
+#if defined(_MSC_VER) || defined(__BORLANDC__) || defined(__MINGW32__)
+#include <sys/timeb.h>
+#else
+#include <sys/time.h>
+#endif
+
+#include <stdarg.h> //only included in implementation file
+#include <stdio.h> //only included in implementation file
+
+#if defined(_MSC_VER) || defined(__BORLANDC__) || defined(__MINGW32__)
+ #include <winsock.h> // for gethostname and gethostbyname and GetTickCount...
+// I haven't find a way to determine wether we need to under GetCurrentTime or not...
+// I think the best solution would simply to get rid of this problematic function
+// and use a 'less' common name...
+#if !defined(__BORLANDC__) || (__BORLANDC__ >= 0x0560)
+ #undef GetCurrentTime
+#endif
+#else
+ #include <unistd.h> // for gethostname
+ #include <netdb.h> // for gethostbyname
+#endif
+
+// For GetMACAddress
+#ifdef _WIN32
+ #include <snmp.h>
+ #include <conio.h>
+#else
+ #include <unistd.h>
+ #include <stdlib.h>
+ #include <string.h>
+ #include <sys/types.h>
+#endif
+
+#ifdef CMAKE_HAVE_SYS_IOCTL_H
+ #include <sys/ioctl.h> // For SIOCGIFCONF on Linux
+#endif
+#ifdef CMAKE_HAVE_SYS_SOCKET_H
+ #include <sys/socket.h>
+#endif
+#ifdef CMAKE_HAVE_SYS_SOCKIO_H
+ #include <sys/sockio.h> // For SIOCGIFCONF on SunOS
+#endif
+#ifdef CMAKE_HAVE_NET_IF_H
+ #include <net/if.h>
+#endif
+#ifdef CMAKE_HAVE_NETINET_IN_H
+ #include <netinet/in.h> //For IPPROTO_IP
+#endif
+#ifdef CMAKE_HAVE_NET_IF_DL_H
+ #include <net/if_dl.h>
+#endif
+#if defined(CMAKE_HAVE_NET_IF_ARP_H) && defined(__sun)
+ // This is absolutely necessary on SunOS
+ #include <net/if_arp.h>
+#endif
+
+// For GetCurrentThreadID()
+#ifdef __linux__
+ #include <sys/types.h>
+ #include <linux/unistd.h>
+#endif
+#ifdef __sun
+ #include <thread.h>
+#endif
+
+namespace gdcm
+{
+//-------------------------------------------------------------------------
+const std::string Util::GDCM_UID = "1.2.826.0.1.3680043.2.1143";
+std::string Util::RootUID = GDCM_UID;
+/*
+ * File Meta Information Version (0002,0001) shall contain a two byte OB
+ * value consisting of a 0x00 byte, followed by 0x01 byte, and not the
+ * value 0x0001 encoded as a little endian 16 bit short value,
+ * which would be the other way around...
+ */
+const uint16_t Util::FMIV = 0x0100;
+uint8_t *Util::FileMetaInformationVersion = (uint8_t *)&FMIV;
+std::string Util::GDCM_MAC_ADRESS = GetMACAddress();
+
+//-------------------------------------------------------------------------
+// Public
+/**
+ * \brief Provide a better 'c++' approach for sprintf
+ * For example c code is:
+ * char result[2048]; // hope 2048 is enough
+ * sprintf(result, "%04x|%04x", group , elem);
+ *
+ * c++ code is
+ * std::ostringstream buf;
+ * buf << std::right << std::setw(4) << std::setfill('0') << std::hex
+ * << group << "|" << std::right << std::setw(4) << std::setfill('0')
+ * << std::hex << elem;
+ * buf.str();
+ *
+ * gdcm style code is
+ * string result;
+ * result = gdcm::Util::Format("%04x|%04x", group , elem);
+ */
+std::string Util::Format(const char *format, ...)
+{
+ char buffer[2048]; // hope 2048 is enough
+ va_list args;
+ va_start(args, format);
+ vsprintf(buffer, format, args); //might be a security flaw
+ va_end(args); // Each invocation of va_start should be matched
+ // by a corresponding invocation of va_end
+ // args is then 'undefined'
+ return buffer;
+}
+
+
+/**
+ * \brief Because not available in C++ (?)
+ * @param str string to check
+ * @param tokens std::vector to receive the tokenized substrings
+ * @param delimiters string containing the character delimitors
+
+ */
+void Util::Tokenize (const std::string &str,
+ std::vector<std::string> &tokens,
+ const std::string &delimiters)
+{
+ std::string::size_type lastPos = str.find_first_not_of(delimiters,0);
+ std::string::size_type pos = str.find_first_of (delimiters,lastPos);
+ while (std::string::npos != pos || std::string::npos != lastPos)
+ {
+ tokens.push_back(str.substr(lastPos, pos - lastPos));
+ lastPos = str.find_first_not_of(delimiters, pos);
+ pos = str.find_first_of (delimiters, lastPos);
+ }
+}
+
+/**
+ * \brief Because not available in C++ (?)
+ * Counts the number of occurences of a substring within a string
+ * @param str string to check
+ * @param subStr substring to count
+ */
+
+int Util::CountSubstring (const std::string &str,
+ const std::string &subStr)
+{
+ int count = 0; // counts how many times it appears
+ std::string::size_type x = 0; // The index position in the string
+
+ do
+ {
+ x = str.find(subStr,x); // Find the substring
+ if (x != std::string::npos) // If present
+ {
+ count++; // increase the count
+ x += subStr.length(); // Skip this word
+ }
+ }
+ while (x != std::string::npos);// Carry on until not present
+
+ return count;
+}
+
+/**
+ * \brief Checks whether a 'string' is printable or not (in order
+ * to avoid corrupting the terminal of invocation when printing)
+ * @param s string to check
+ */
+bool Util::IsCleanString(std::string const &s)
+{
+ //std::cout<< std::endl << s << std::endl;
+ for(unsigned int i=0; i<s.size(); i++)
+ {
+ if (!isprint((unsigned char)s[i]) )
+ {
+ return false;
+ }
+ }
+ return true;
+}
+
+/**
+ * \brief Checks whether an 'area' is printable or not (in order
+ * to avoid corrupting the terminal of invocation when printing)
+ * @param s area to check (uint8_t is just for prototyping. feel free to cast)
+ * @param l area length to check
+ */
+bool Util::IsCleanArea(uint8_t *s, int l)
+{
+ for( int i=0; i<l; i++)
+ {
+ if (!isprint((unsigned char)s[i]) )
+ {
+ return false;
+ }
+ }
+ return true;
+}
+/**
+ * \brief Weed out a string from the non-printable characters (in order
+ * to avoid corrupting the terminal of invocation when printing)
+ * @param s string to check (uint8_t is just for prototyping. feel free to cast)
+ */
+std::string Util::CreateCleanString(std::string const &s)
+{
+ std::string str = s;
+
+ for(unsigned int i=0; i<str.size(); i++)
+ {
+ if (!isprint((unsigned char)str[i]) )
+ {
+ str[i] = '.';
+ }
+ }
+
+ if (str.size() > 0 )
+ {
+ if (!isprint((unsigned char)s[str.size()-1]) )
+ {
+ if (s[str.size()-1] == 0 )
+ {
+ str[str.size()-1] = ' ';
+ }
+ }
+ }
+
+ return str;
+}
+
+/**
+ * \brief Weed out a string from the non-printable characters (in order
+ * to avoid corrupting the terminal of invocation when printing)
+ * @param s area to process (uint8_t is just for prototyping. feel free to cast)
+ * @param l area length to check
+ */
+std::string Util::CreateCleanString(uint8_t *s, int l)
+{
+ std::string str;
+
+ for( int i=0; i<l; i++)
+ {
+ if (!isprint((unsigned char)s[i]) )
+ {
+ str = str + '.';
+ }
+ else
+ {
+ str = str + (char )s[i];
+ }
+ }
+
+ return str;
+}
+/**
+ * \brief Add a SEPARATOR to the end of the name is necessary
+ * @param pathname file/directory name to normalize
+ */
+std::string Util::NormalizePath(std::string const &pathname)
+{
+ const char SEPARATOR_X = '/';
+ const char SEPARATOR_WIN = '\\';
+ const std::string SEPARATOR = "/";
+ std::string name = pathname;
+ int size = name.size();
+
+ if ( name[size-1] != SEPARATOR_X && name[size-1] != SEPARATOR_WIN )
+ {
+ name += SEPARATOR;
+ }
+ return name;
+}
+
+/**
+ * \brief Get the (directory) path from a full path file name
+ * @param fullName file/directory name to extract Path from
+ */
+std::string Util::GetPath(std::string const &fullName)
+{
+ std::string res = fullName;
+ int pos1 = res.rfind("/");
+ int pos2 = res.rfind("\\");
+ if ( pos1 > pos2 )
+ {
+ res.resize(pos1);
+ }
+ else
+ {
+ res.resize(pos2);
+ }
+
+ return res;
+}
+
+/**
+ * \brief Get the (last) name of a full path file name
+ * @param fullName file/directory name to extract end name from
+ */
+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;
+ if (slash_pos != std::string::npos )
+ {
+ return filename.substr(slash_pos + 1);
+ }
+ else
+ {
+ return filename;
+ }
+}
+
+/**
+ * \brief Get the current date of the system in a dicom string
+ */
+std::string Util::GetCurrentDate()
+{
+ char tmp[512];
+ time_t tloc;
+ time (&tloc);
+ strftime(tmp,512,"%Y%m%d", localtime(&tloc) );
+ return tmp;
+}
+
+/**
+ * \brief Get the current time of the system in a dicom string
+ */
+std::string Util::GetCurrentTime()
+{
+ char tmp[512];
+ time_t tloc;
+ time (&tloc);
+ strftime(tmp,512,"%H%M%S", localtime(&tloc) );
+ return tmp;
+}
+
+/**
+ * \brief Get both the date and time at the same time to avoid problem
+ * around midnight where the two calls could be before and after midnight
+ */
+std::string Util::GetCurrentDateTime()
+{
+ char tmp[40];
+ long milliseconds;
+ time_t timep;
+
+ // We need implementation specific functions to obtain millisecond precision
+#if defined(_MSC_VER) || defined(__BORLANDC__) || defined(__MINGW32__)
+ struct timeb tb;
+ ::ftime(&tb);
+ timep = tb.time;
+ milliseconds = tb.millitm;
+#else
+ struct timeval tv;
+ gettimeofday (&tv, NULL);
+ timep = tv.tv_sec;
+ // Compute milliseconds from microseconds.
+ milliseconds = tv.tv_usec / 1000;
+#endif
+ // Obtain the time of day, and convert it to a tm struct.
+ struct tm *ptm = localtime (&timep);
+ // Format the date and time, down to a single second.
+ strftime (tmp, sizeof (tmp), "%Y%m%d%H%M%S", ptm);
+
+ // Add milliseconds
+ // Don't use Util::Format to accelerate execution of code
+ char tmpAll[80];
+ sprintf(tmpAll,"%s%03ld",tmp,milliseconds);
+
+ return tmpAll;
+}
+
+unsigned int Util::GetCurrentThreadID()
+{
+// FIXME the implementation is far from complete
+#if defined(_MSC_VER) || defined(__BORLANDC__) || defined(__MINGW32__)
+ return (unsigned int)GetCurrentThreadId();
+#else
+#ifdef __linux__
+ return 0;
+ // Doesn't work on fedora, but is in the man page...
+ //return (unsigned int)gettid();
+#else
+#ifdef __sun
+ return (unsigned int)thr_self();
+#else
+ //default implementation
+ return 0;
+#endif // __sun
+#endif // __linux__
+#endif // Win32
+}
+
+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 tells us whether the processor we are working with is BigEndian or not
+ */
+bool Util::IsCurrentProcessorBigEndian()
+{
+#if defined(GDCM_WORDS_BIGENDIAN)
+ return true;
+#else
+ return false;
+#endif