#include <gdcmUtil.h>
Static Public Member Functions | |
std::string | Format (const char *format,...) |
Provide a better 'c++' approach for sprintf For example c code is: sprintf(trash, "%04x|%04x", group , elem);. | |
void | Tokenize (const std::string &str, std::vector< std::string > &tokens, const std::string &delimiters="") |
Because not available in C++ (?). | |
int | CountSubstring (const std::string &str, const std::string &subStr) |
Because not available in C++ (?) Counts the number of occurences of a substring within a string. | |
std::string | CreateCleanString (std::string const &s) |
Weed out a string from the non-printable characters (in order to avoid corrupting the terminal of invocation when printing). | |
std::string | NormalizePath (std::string const &name) |
Add a SEPARATOR to the end of the name is necessary. | |
std::string | GetPath (std::string const &fullName) |
Get the (directory) path from a full path file name. | |
std::string | GetName (std::string const &fullName) |
Get the (last) name of a full path file name. | |
std::string | GetCurrentDate () |
Get the current date of the system in a dicom string. | |
std::string | GetCurrentTime () |
Get the current time of the system in a dicom string. | |
std::string | GetCurrentDateTime () |
Get both the date and time at the same time to avoid problem around midnight where two call could be before and after midnight. | |
unsigned int | GetCurrentThreadID () |
unsigned int | GetCurrentProcessID () |
bool | IsCurrentProcessorBigEndian () |
tells us if the processor we are working with is BigEndian or not | |
std::string | DicomString (const char *s, size_t l) |
Create a /DICOM/ string: It should a of even length (no odd length ever) 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 | DicomString (const char *s) |
Create a /DICOM/ string: It should a of even length (no odd length ever) 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 length. It only pad with a null character if length is odd. | |
bool | DicomStringEqual (const std::string &s1, const char *s2) |
Safely compare two Dicom String: -Both string should be of even length -We allow padding of even length string by either a null character of a space. | |
std::string | GetMACAddress () |
Encode the mac address on a fixed lenght string of 15 characters. we save space this way. | |
std::string | CreateUniqueUID (const std::string &root="") |
Creates a new UID. As stipulate in the DICOM ref each time a DICOM image is create it should have a unique identifier (URI). | |
void | SetRootUID (const std::string &root="") |
const std::string & | GetRootUID () |
Static Private Member Functions | |
std::string | GetIPAddress () |
Return the IP adress of the machine writting the DICOM image. | |
Static Private Attributes | |
std::string | RootUID = GDCM_UID |
const std::string | GDCM_UID = "1.2.826.0.1.3680043.2.1143" |
Definition at line 36 of file gdcmUtil.h.
|
Because not available in C++ (?) Counts the number of occurences of a substring within a string.
Definition at line 148 of file gdcmUtil.cxx.
00150 { 00151 int count = 0; // counts how many times it appears 00152 std::string::size_type x = 0; // The index position in the string 00153 00154 do 00155 { 00156 x = str.find(subStr,x); // Find the substring 00157 if (x != std::string::npos) // If present 00158 { 00159 count++; // increase the count 00160 x += subStr.length(); // Skip this word 00161 } 00162 } 00163 while (x != std::string::npos); // Carry on until not present 00164 00165 return count; 00166 } |
|
Weed out a string from the non-printable characters (in order to avoid corrupting the terminal of invocation when printing).
Definition at line 173 of file gdcmUtil.cxx.
00174 { 00175 std::string str = s; 00176 00177 for(unsigned int i=0; i<str.size(); i++) 00178 { 00179 if(!isprint((unsigned char)str[i])) 00180 { 00181 str[i] = '.'; 00182 } 00183 } 00184 00185 if(str.size() > 0) 00186 { 00187 if(!isprint((unsigned char)s[str.size()-1])) 00188 { 00189 if(s[str.size()-1] == 0) 00190 { 00191 str[str.size()-1] = ' '; 00192 } 00193 } 00194 } 00195 00196 return str; 00197 } |
|
Creates a new UID. As stipulate in the DICOM ref each time a DICOM image is create it should have a unique identifier (URI).
Definition at line 762 of file gdcmUtil.cxx. References Format(), gdcmErrorMacro, and RootUID. Referenced by vtkGdcmWriter::WriteDcmFile().
00763 { 00764 std::string prefix; 00765 std::string append; 00766 if( root.empty() ) 00767 { 00768 // gdcm UID prefix, as supplied by http://www.medicalconnections.co.uk 00769 prefix = RootUID; 00770 } 00771 else 00772 { 00773 prefix = root; 00774 } 00775 00776 // A root was specified use it to forge our new UID: 00777 append += "."; 00778 append += Util::GetMACAddress(); 00779 append += "."; 00780 append += Util::GetCurrentDateTime(); 00781 00782 //Also add a mini random number just in case: 00783 int r = (int) (100.0*rand()/RAND_MAX); 00784 append += Format("%02d", r); 00785 00786 // If append is too long we need to rehash it 00787 if( (prefix + append).size() > 64 ) 00788 { 00789 gdcmErrorMacro( "Size of UID is too long." ); 00790 // we need a hash function to truncate this number 00791 // if only md5 was cross plateform 00792 // MD5(append); 00793 } 00794 00795 return prefix + append; 00796 } |
|
Create a /DICOM/ string: It should a of even length (no odd length ever) 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 length. It only pad with a null character if length is odd.
Definition at line 387 of file gdcmUtil.cxx. References gdcmAssertMacro.
00388 { 00389 size_t l = strlen(s); 00390 if( l%2 ) 00391 { 00392 l++; 00393 } 00394 std::string r(s, s+l); 00395 gdcmAssertMacro( !(r.size() % 2) ); 00396 return r; 00397 } |
|
Create a /DICOM/ string: It should a of even length (no odd length ever) 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.
Definition at line 369 of file gdcmUtil.cxx. References gdcmAssertMacro. Referenced by DicomStringEqual().
00370 { 00371 std::string r(s, s+l); 00372 gdcmAssertMacro( !(r.size() % 2) ); // == basically 'l' is even 00373 return r; 00374 } |
|
Safely compare two Dicom String: -Both string should be of even length -We allow padding of even length string by either a null character of a space.
Definition at line 405 of file gdcmUtil.cxx. References DicomString().
00406 { 00407 // s2 is the string from the DICOM reference: 'MONOCHROME1' 00408 std::string s1_even = s1; //Never change input parameter 00409 std::string s2_even = DicomString( s2 ); 00410 if( s1_even[s1_even.size()-1] == ' ') 00411 { 00412 s1_even[s1_even.size()-1] = '\0'; //replace space character by null 00413 } 00414 return s1_even == s2_even; 00415 } |
|
Provide a better 'c++' approach for sprintf For example c code is: sprintf(trash, "%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 Format("%04x|%04x", group , elem); Definition at line 113 of file gdcmUtil.cxx. Referenced by CreateUniqueUID(), and GetCurrentDateTime().
00114 { 00115 char buffer[2048]; 00116 va_list args; 00117 va_start(args, format); 00118 vsprintf(buffer, format, args); //might be a security flaw 00119 va_end(args); // Each invocation of va_start should be matched 00120 // by a corresponding invocation of va_end 00121 // args is then 'undefined' 00122 return buffer; 00123 } |
|
Get the current date of the system in a dicom string.
Definition at line 263 of file gdcmUtil.cxx.
00264 { 00265 char tmp[512]; 00266 time_t tloc; 00267 time (&tloc); 00268 strftime(tmp,512,"%Y%m%d", localtime(&tloc) ); 00269 return tmp; 00270 } |
|
Get both the date and time at the same time to avoid problem around midnight where two call could be before and after midnight.
Definition at line 288 of file gdcmUtil.cxx. References Format().
00289 { 00290 char tmp[40]; 00291 long milliseconds; 00292 time_t timep; 00293 00294 // We need implementation specific functions to obtain millisecond precision 00295 #if defined(_MSC_VER) || defined(__BORLANDC__) || defined(__MINGW32__) 00296 struct timeb tb; 00297 ::ftime(&tb); 00298 timep = tb.time; 00299 milliseconds = tb.millitm; 00300 #else 00301 struct timeval tv; 00302 gettimeofday (&tv, NULL); 00303 timep = tv.tv_sec; 00304 // Compute milliseconds from microseconds. 00305 milliseconds = tv.tv_usec / 1000; 00306 #endif 00307 // Obtain the time of day, and convert it to a tm struct. 00308 struct tm *ptm = localtime (&timep); 00309 // Format the date and time, down to a single second. 00310 strftime (tmp, sizeof (tmp), "%Y%m%d%H%M%S", ptm); 00311 00312 // Add milliseconds 00313 std::string r = tmp; 00314 r += Format("%03ld", milliseconds); 00315 00316 return r; 00317 } |
|
Definition at line 338 of file gdcmUtil.cxx.
00339 { 00340 #if defined(_MSC_VER) || defined(__BORLANDC__) || defined(__MINGW32__) 00341 // NOTE: There is also a _getpid()... 00342 return (unsigned int)GetCurrentProcessId(); 00343 #else 00344 // get process identification, POSIX 00345 return (unsigned int)getpid(); 00346 #endif 00347 } |
|
Definition at line 319 of file gdcmUtil.cxx.
00320 { 00321 // FIXME the implementation is far from complete 00322 #if defined(_MSC_VER) || defined(__BORLANDC__) || defined(__MINGW32__) 00323 return (unsigned int)GetCurrentThreadId(); 00324 #endif 00325 #ifdef __linux__ 00326 return 0; 00327 // Doesn't work on fedora, but is in the man page... 00328 //return (unsigned int)gettid(); 00329 #endif 00330 #ifdef __sun 00331 return (unsigned int)thr_self(); 00332 #else 00333 //default implementation 00334 return 0; 00335 #endif 00336 } |
|
Get the current time of the system in a dicom string.
Definition at line 275 of file gdcmUtil.cxx.
00276 { 00277 char tmp[512]; 00278 time_t tloc; 00279 time (&tloc); 00280 strftime(tmp,512,"%H%M%S", localtime(&tloc) ); 00281 return tmp; 00282 } |
|
Return the IP adress of the machine writting the DICOM image.
Definition at line 884 of file gdcmUtil.cxx. References HOST_NAME_MAX.
00885 { 00886 // This is a rip from 00887 // http://www.codeguru.com/Cpp/I-N/internet/network/article.php/c3445/ 00888 #ifndef HOST_NAME_MAX 00889 // SUSv2 guarantees that `Host names are limited to 255 bytes'. 00890 // POSIX 1003.1-2001 guarantees that `Host names (not including the 00891 // terminating NUL) are limited to HOST_NAME_MAX bytes'. 00892 #define HOST_NAME_MAX 255 00893 // In this case we should maybe check the string was not truncated. 00894 // But I don't known how to check that... 00895 #if defined(_MSC_VER) || defined(__BORLANDC__) || defined(__MINGW32__) 00896 // with WinSock DLL we need to initialize the WinSock before using gethostname 00897 WORD wVersionRequested = MAKEWORD(1,0); 00898 WSADATA WSAData; 00899 int err = WSAStartup(wVersionRequested,&WSAData); 00900 if (err != 0) 00901 { 00902 // Tell the user that we could not find a usable 00903 // WinSock DLL. 00904 WSACleanup(); 00905 return "127.0.0.1"; 00906 } 00907 #endif 00908 00909 #endif //HOST_NAME_MAX 00910 00911 std::string str; 00912 char szHostName[HOST_NAME_MAX+1]; 00913 int r = gethostname(szHostName, HOST_NAME_MAX); 00914 00915 if( r == 0 ) 00916 { 00917 // Get host adresses 00918 struct hostent *pHost = gethostbyname(szHostName); 00919 00920 for( int i = 0; pHost!= NULL && pHost->h_addr_list[i]!= NULL; i++ ) 00921 { 00922 for( int j = 0; j<pHost->h_length; j++ ) 00923 { 00924 if( j > 0 ) str += "."; 00925 00926 str += Util::Format("%u", 00927 (unsigned int)((unsigned char*)pHost->h_addr_list[i])[j]); 00928 } 00929 // str now contains one local IP address 00930 00931 #if defined(_MSC_VER) || defined(__BORLANDC__) || defined(__MINGW32__) 00932 WSACleanup(); 00933 #endif 00934 00935 } 00936 } 00937 // If an error occur r == -1 00938 // Most of the time it will return 127.0.0.1... 00939 return str; 00940 } |
|
Encode the mac address on a fixed lenght string of 15 characters. we save space this way.
Definition at line 722 of file gdcmUtil.cxx. References gdcmWarningMacro, gdcm::getlastdigit(), and gdcm::GetMacAddrSys().
00723 { 00724 // This code is the result of a long internet search to find something 00725 // as compact as possible (not OS independant). We only have to separate 00726 // 3 OS: Win32, SunOS and 'real' POSIX 00727 // http://groups-beta.google.com/group/comp.unix.solaris/msg/ad36929d783d63be 00728 // http://bdn.borland.com/article/0,1410,26040,00.html 00729 unsigned char addr[6]; 00730 00731 int stat = GetMacAddrSys(addr); 00732 if (stat == 0) 00733 { 00734 // We need to convert a 6 digit number from base 256 to base 10, using integer 00735 // would requires a 48bits one. To avoid this we have to reimplement the div + modulo 00736 // with string only 00737 bool zero = false; 00738 int res; 00739 std::string sres; 00740 while(!zero) 00741 { 00742 res = getlastdigit(addr); 00743 sres.insert(sres.begin(), '0' + res); 00744 zero = (addr[0] == 0) && (addr[1] == 0) && (addr[2] == 0) && (addr[3] == 0) && (addr[4] == 0) && (addr[5] == 0); 00745 } 00746 00747 return sres; 00748 } 00749 else 00750 { 00751 gdcmWarningMacro("Problem in finding the MAC Address"); 00752 return ""; 00753 } 00754 } |
|
Get the (last) name of a full path file name.
Definition at line 243 of file gdcmUtil.cxx.
00244 { 00245 std::string filename = fullName; 00246 00247 std::string::size_type slash_pos = filename.rfind("/"); 00248 std::string::size_type backslash_pos = filename.rfind("\\"); 00249 slash_pos = slash_pos > backslash_pos ? slash_pos : backslash_pos; 00250 if(slash_pos != std::string::npos) 00251 { 00252 return filename.substr(slash_pos + 1); 00253 } 00254 else 00255 { 00256 return filename; 00257 } 00258 } |
|
Get the (directory) path from a full path file name.
Definition at line 222 of file gdcmUtil.cxx.
00223 { 00224 std::string res = fullName; 00225 int pos1 = res.rfind("/"); 00226 int pos2 = res.rfind("\\"); 00227 if( pos1 > pos2) 00228 { 00229 res.resize(pos1); 00230 } 00231 else 00232 { 00233 res.resize(pos2); 00234 } 00235 00236 return res; 00237 } |
|
Definition at line 806 of file gdcmUtil.cxx. References RootUID.
00807 { 00808 return RootUID; 00809 } |
|
tells us if the processor we are working with is BigEndian or not
Definition at line 352 of file gdcmUtil.cxx.
00353 { 00354 #ifdef GDCM_WORDS_BIGENDIAN 00355 return true; 00356 #else 00357 return false; 00358 #endif 00359 } |
|
Add a SEPARATOR to the end of the name is necessary.
Definition at line 203 of file gdcmUtil.cxx.
00204 { 00205 const char SEPARATOR_X = '/'; 00206 const char SEPARATOR_WIN = '\\'; 00207 const std::string SEPARATOR = "/"; 00208 std::string name = pathname; 00209 int size = name.size(); 00210 00211 if( name[size-1] != SEPARATOR_X && name[size-1] != SEPARATOR_WIN ) 00212 { 00213 name += SEPARATOR; 00214 } 00215 return name; 00216 } |
|
Definition at line 798 of file gdcmUtil.cxx. References GDCM_UID, and RootUID.
|
|
Because not available in C++ (?).
Definition at line 129 of file gdcmUtil.cxx.
00132 {
00133 std::string::size_type lastPos = str.find_first_not_of(delimiters,0);
00134 std::string::size_type pos = str.find_first_of (delimiters,lastPos);
00135 while (std::string::npos != pos || std::string::npos != lastPos)
00136 {
00137 tokens.push_back(str.substr(lastPos, pos - lastPos));
00138 lastPos = str.find_first_not_of(delimiters, pos);
00139 pos = str.find_first_of (delimiters, lastPos);
00140 }
00141 }
|
|
Definition at line 93 of file gdcmUtil.cxx. Referenced by SetRootUID(). |
|
Definition at line 94 of file gdcmUtil.cxx. Referenced by CreateUniqueUID(), GetRootUID(), and SetRootUID(). |