#include <gdcmUtil.h>
Static Public Member Functions | |
static std::string | Format (const char *format,...) |
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);. | |
static void | Tokenize (const std::string &str, std::vector< std::string > &tokens, const std::string &delimiters=" ") |
Because not available in C++ (?). | |
static 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. | |
static 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). | |
static std::string | CreateCleanString (uint8_t *s, int l) |
Weed out a string from the non-printable characters (in order to avoid corrupting the terminal of invocation when printing). | |
static bool | IsCleanString (std::string const &s) |
Checks whether a 'string' is printable or not (in order to avoid corrupting the terminal of invocation when printing). | |
static bool | IsCleanArea (uint8_t *s, int l) |
Checks whether an 'area' is printable or not (in order to avoid corrupting the terminal of invocation when printing). | |
static std::string | NormalizePath (std::string const &name) |
Add a SEPARATOR to the end of the name is necessary. | |
static std::string | GetPath (std::string const &fullName) |
Get the (directory) path from a full path file name. | |
static std::string | GetName (std::string const &fullName) |
Get the (last) name of a full path file name. | |
static std::string | GetCurrentDate () |
Get the current date of the system in a dicom string. | |
static std::string | GetCurrentTime () |
Get the current time of the system in a dicom string. | |
static std::string | GetCurrentDateTime () |
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. | |
static std::string | GetVersion () |
Provides a simple static GetVersion() function. | |
static unsigned int | GetCurrentThreadID () |
static unsigned int | GetCurrentProcessID () |
static bool | IsCurrentProcessorBigEndian () |
tells us whether the processor we are working with is BigEndian or not | |
static 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 backslash followed by zero that needed to be escaped with an extra backslash for doxygen) \0 as you want. | |
static 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 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. | |
static bool | DicomStringEqual (const std::string &s1, const char *s2) |
Safely check the equality of two Dicom String:
| |
static bool | CompareDicomString (const std::string &s1, const char *s2, int op) |
Safely compare two Dicom String:
| |
static std::string | GetMACAddress () |
Encode the mac address on a fixed lenght string of 15 characters. we save space this way. | |
static std::string | CreateUniqueUID (const std::string &root="") |
Creates a new UID. As stipulated in the DICOM ref each time a DICOM image is created it should have a unique identifier (URI). | |
static void | SetRootUID (const std::string &root="") |
static const std::string & | GetRootUID () |
static const uint8_t * | GetFileMetaInformationVersion () |
static void | SetFileMetaInformationVersion (uint16_t fmiv) |
static void | hifpswap (double *a, double *b) |
static void | hNoifpswap (double *a, double *b) |
Static Private Member Functions | |
static std::string | GetIPAddress () |
Return the IP adress of the machine writting the DICOM image. | |
Static Private Attributes | |
static std::string | RootUID = GDCM_UID |
static const std::string | GDCM_UID = "1.2.826.0.1.3680043.2.1143" |
static uint8_t * | FileMetaInformationVersion = (uint8_t *)&FMIV |
static const uint16_t | FMIV = 0x0100 |
static std::string | GDCM_MAC_ADRESS = GetMACAddress() |
Definition at line 36 of file gdcmUtil.h.
|
Safely compare two Dicom String:
Definition at line 505 of file gdcmUtil.cxx. References DicomString(), gdcm::GDCM_DIFFERENT, gdcm::GDCM_EQUAL, gdcm::GDCM_GREATER, gdcm::GDCM_GREATEROREQUAL, gdcm::GDCM_LESS, gdcm::GDCM_LESSOREQUAL, and gdcmDebugMacro. Referenced by gdcm::SerieHelper::AddFileName(). 00506 { 00507 // s2 is the string from the DICOM reference e.g. : 'MONOCHROME1' 00508 std::string s1_even = s1; //Never change input parameter 00509 std::string s2_even = DicomString( s2 ); 00510 if ( s1_even[s1_even.size()-1] == ' ' ) 00511 { 00512 s1_even[s1_even.size()-1] = '\0'; //replace space character by null 00513 } 00514 switch (op) 00515 { 00516 case GDCM_EQUAL : 00517 return s1_even == s2_even; 00518 case GDCM_DIFFERENT : 00519 return s1_even != s2_even; 00520 case GDCM_GREATER : 00521 return s1_even > s2_even; 00522 case GDCM_GREATEROREQUAL : 00523 return s1_even >= s2_even; 00524 case GDCM_LESS : 00525 return s1_even < s2_even; 00526 case GDCM_LESSOREQUAL : 00527 return s1_even <= s2_even; 00528 default : 00529 gdcmDebugMacro(" Wrong operator : " << op); 00530 return false; 00531 } 00532 }
|
|
Because not available in C++ (?) Counts the number of occurences of a substring within a string.
Definition at line 168 of file gdcmUtil.cxx. Referenced by gdcm::CheckVM(). 00170 { 00171 int count = 0; // counts how many times it appears 00172 std::string::size_type x = 0; // The index position in the string 00173 00174 do 00175 { 00176 x = str.find(subStr,x); // Find the substring 00177 if (x != std::string::npos) // If present 00178 { 00179 count++; // increase the count 00180 x += subStr.length(); // Skip this word 00181 } 00182 } 00183 while (x != std::string::npos);// Carry on until not present 00184 00185 return count; 00186 }
|
|
Weed out a string from the non-printable characters (in order to avoid corrupting the terminal of invocation when printing).
Definition at line 260 of file gdcmUtil.cxx. 00261 { 00262 std::string str; 00263 00264 for( int i=0; i<l; i++) 00265 { 00266 if (!isprint((unsigned char)s[i]) ) 00267 { 00268 str = str + '.'; 00269 } 00270 else 00271 { 00272 str = str + (char )s[i]; 00273 } 00274 } 00275 00276 return str; 00277 }
|
|
Weed out a string from the non-printable characters (in order to avoid corrupting the terminal of invocation when printing).
Definition at line 228 of file gdcmUtil.cxx. Referenced by gdcm::DocEntrySet::GetEntryForcedAsciiValue(), and gdcm::DataEntry::Print(). 00229 { 00230 std::string str = s; 00231 00232 for(unsigned int i=0; i<str.size(); i++) 00233 { 00234 if (!isprint((unsigned char)str[i]) ) 00235 { 00236 str[i] = '.'; 00237 } 00238 } 00239 00240 if (str.size() > 0 ) 00241 { 00242 if (!isprint((unsigned char)s[str.size()-1]) ) 00243 { 00244 if (s[str.size()-1] == 0 ) 00245 { 00246 str[str.size()-1] = ' '; 00247 } 00248 } 00249 } 00250 00251 return str; 00252 }
|
|
Creates a new UID. As stipulated in the DICOM ref each time a DICOM image is created it should have a unique identifier (URI).
Definition at line 884 of file gdcmUtil.cxx. References GDCM_MAC_ADRESS, gdcmErrorMacro, GetCurrentDateTime(), and RootUID. Referenced by gdcm::FileHelper::CheckMandatoryElements(), and gdcm::DicomDirMeta::WriteContent(). 00885 { 00886 std::string prefix; 00887 std::string append; 00888 if ( root.empty() ) 00889 { 00890 // gdcm UID prefix, as supplied by http://www.medicalconnections.co.uk 00891 prefix = RootUID; 00892 } 00893 else 00894 { 00895 prefix = root; 00896 } 00897 00898 // A root was specified use it to forge our new UID: 00899 append += "."; 00900 //append += Util::GetMACAddress(); // to save CPU time 00901 append += Util::GDCM_MAC_ADRESS; 00902 append += "."; 00903 append += Util::GetCurrentDateTime(); 00904 append += "."; 00905 //Also add a mini random number just in case: 00906 char tmp[10]; 00907 int r = (int) (100.0*rand()/RAND_MAX); 00908 // Don't use Util::Format to accelerate the execution 00909 sprintf(tmp,"%02d", r); 00910 append += tmp; 00911 00912 // If append is too long we need to rehash it 00913 if ( (prefix + append).size() > 64 ) 00914 { 00915 gdcmErrorMacro( "Size of UID is too long." ); 00916 // we need a hash function to truncate this number 00917 // if only md5 was cross plateform 00918 // MD5(append); 00919 } 00920 00921 return prefix + append; 00922 }
|
|
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 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 469 of file gdcmUtil.cxx. References gdcmAssertMacro. 00470 { 00471 size_t l = strlen(s); 00472 if ( l%2 ) 00473 { 00474 l++; 00475 } 00476 std::string r(s, s+l); 00477 gdcmAssertMacro( !(r.size() % 2) ); 00478 return r; 00479 }
|
|
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 backslash followed by zero that needed to be escaped with an extra backslash for doxygen) \0 as you want.
Definition at line 451 of file gdcmUtil.cxx. References gdcmAssertMacro. Referenced by CompareDicomString(), DicomStringEqual(), gdcm::FileHelper::SetWriteFileTypeToExplicitVR(), gdcm::FileHelper::SetWriteFileTypeToImplicitVR(), gdcm::FileHelper::SetWriteFileTypeToJPEG(), and gdcm::File::Write(). 00452 { 00453 std::string r(s, s+l); 00454 gdcmAssertMacro( !(r.size() % 2) ); // == basically 'l' is even 00455 return r; 00456 }
|
|
Safely check the equality of two Dicom String:
Definition at line 487 of file gdcmUtil.cxx. References DicomString(). Referenced by gdcm::SerieHelper::AddGdcmFile(), gdcm::File::GetNumberOfScalarComponents(), gdcm::File::IsMonochrome(), and gdcm::File::IsMonochrome1(). 00488 { 00489 // s2 is the string from the DICOM reference e.g. : 'MONOCHROME1' 00490 std::string s1_even = s1; //Never change input parameter 00491 std::string s2_even = DicomString( s2 ); 00492 if ( s1_even[s1_even.size()-1] == ' ' ) 00493 { 00494 s1_even[s1_even.size()-1] = '\0'; //replace space character by null 00495 } 00496 return s1_even == s2_even; 00497 }
|
|
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); Definition at line 127 of file gdcmUtil.cxx. Referenced by GetIPAddress(), gdcm::Document::OpenFile(), gdcm::DocEntry::Print(), gdcm::Document::ReadNextDocEntry(), and gdcm::File::Write(). 00128 { 00129 char buffer[2048]; // hope 2048 is enough 00130 va_list args; 00131 va_start(args, format); 00132 vsprintf(buffer, format, args); //might be a security flaw 00133 va_end(args); // Each invocation of va_start should be matched 00134 // by a corresponding invocation of va_end 00135 // args is then 'undefined' 00136 return buffer; 00137 }
|
|
Get the current date of the system in a dicom string.
Definition at line 342 of file gdcmUtil.cxx. Referenced by gdcm::FileHelper::CheckMandatoryElements(). 00343 { 00344 char tmp[512]; 00345 time_t tloc; 00346 time (&tloc); 00347 strftime(tmp,512,"%Y%m%d", localtime(&tloc) ); 00348 return tmp; 00349 }
|
|
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.
Definition at line 367 of file gdcmUtil.cxx. Referenced by CreateUniqueUID(). 00368 { 00369 char tmp[40]; 00370 long milliseconds; 00371 time_t timep; 00372 00373 // We need implementation specific functions to obtain millisecond precision 00374 #if defined(_MSC_VER) || defined(__BORLANDC__) || defined(__MINGW32__) 00375 struct timeb tb; 00376 ::ftime(&tb); 00377 timep = tb.time; 00378 milliseconds = tb.millitm; 00379 #else 00380 struct timeval tv; 00381 gettimeofday (&tv, NULL); 00382 timep = tv.tv_sec; 00383 // Compute milliseconds from microseconds. 00384 milliseconds = tv.tv_usec / 1000; 00385 #endif 00386 // Obtain the time of day, and convert it to a tm struct. 00387 struct tm *ptm = localtime (&timep); 00388 // Format the date and time, down to a single second. 00389 strftime (tmp, sizeof (tmp), "%Y%m%d%H%M%S", ptm); 00390 00391 // Add milliseconds 00392 // Don't use Util::Format to accelerate execution of code 00393 char tmpAll[80]; 00394 sprintf(tmpAll,"%s%03ld",tmp,milliseconds); 00395 00396 return tmpAll; 00397 }
|
|
Definition at line 420 of file gdcmUtil.cxx. 00421 { 00422 #if defined(_MSC_VER) || defined(__BORLANDC__) || defined(__MINGW32__) 00423 // NOTE: There is also a _getpid()... 00424 return (unsigned int)GetCurrentProcessId(); 00425 #else 00426 // get process identification, POSIX 00427 return (unsigned int)getpid(); 00428 #endif 00429 }
|
|
Definition at line 399 of file gdcmUtil.cxx. 00400 { 00401 // FIXME the implementation is far from complete 00402 #if defined(_MSC_VER) || defined(__BORLANDC__) || defined(__MINGW32__) 00403 return (unsigned int)GetCurrentThreadId(); 00404 #else 00405 #ifdef __linux__ 00406 return 0; 00407 // Doesn't work on fedora, but is in the man page... 00408 //return (unsigned int)gettid(); 00409 #else 00410 #ifdef __sun 00411 return (unsigned int)thr_self(); 00412 #else 00413 //default implementation 00414 return 0; 00415 #endif // __sun 00416 #endif // __linux__ 00417 #endif // Win32 00418 }
|
|
Get the current time of the system in a dicom string.
Definition at line 354 of file gdcmUtil.cxx. Referenced by gdcm::FileHelper::CheckMandatoryElements(). 00355 { 00356 char tmp[512]; 00357 time_t tloc; 00358 time (&tloc); 00359 strftime(tmp,512,"%H%M%S", localtime(&tloc) ); 00360 return tmp; 00361 }
|
|
Definition at line 74 of file gdcmUtil.h. Referenced by gdcm::FileHelper::CheckMandatoryElements(). 00075 { return FileMetaInformationVersion;}
|
|
Return the IP adress of the machine writting the DICOM image.
Definition at line 1090 of file gdcmUtil.cxx. References Format(), and HOST_NAME_MAX. 01091 { 01092 // This is a rip from 01093 // http://www.codeguru.com/Cpp/I-N/internet/network/article.php/c3445/ 01094 #ifndef HOST_NAME_MAX 01095 // SUSv2 guarantees that `Host names are limited to 255 bytes'. 01096 // POSIX 1003.1-2001 guarantees that `Host names (not including the 01097 // terminating NUL) are limited to HOST_NAME_MAX bytes'. 01098 #define HOST_NAME_MAX 255 01099 // In this case we should maybe check the string was not truncated. 01100 // But I don't known how to check that... 01101 #if defined(_MSC_VER) || defined(__BORLANDC__) || defined(__MINGW32__) 01102 // with WinSock DLL we need to initialize the WinSock before using gethostname 01103 WORD wVersionRequested = MAKEWORD(1,0); 01104 WSADATA WSAData; 01105 int err = WSAStartup(wVersionRequested,&WSAData); 01106 if (err != 0) 01107 { 01108 // Tell the user that we could not find a usable 01109 // WinSock DLL. 01110 WSACleanup(); 01111 return "127.0.0.1"; 01112 } 01113 #endif 01114 01115 #endif //HOST_NAME_MAX 01116 01117 std::string str; 01118 char szHostName[HOST_NAME_MAX+1]; 01119 int r = gethostname(szHostName, HOST_NAME_MAX); 01120 01121 if ( r == 0 ) 01122 { 01123 // Get host adresses 01124 struct hostent *pHost = gethostbyname(szHostName); 01125 01126 for( int i = 0; pHost!= NULL && pHost->h_addr_list[i]!= NULL; i++ ) 01127 { 01128 for( int j = 0; j<pHost->h_length; j++ ) 01129 { 01130 if ( j > 0 ) str += "."; 01131 01132 str += Util::Format("%u", 01133 (unsigned int)((unsigned char*)pHost->h_addr_list[i])[j]); 01134 } 01135 // str now contains one local IP address 01136 01137 #if defined(_MSC_VER) || defined(__BORLANDC__) || defined(__MINGW32__) 01138 WSACleanup(); 01139 #endif 01140 01141 } 01142 } 01143 // If an error occur r == -1 01144 // Most of the time it will return 127.0.0.1... 01145 return str; 01146 }
|
|
Encode the mac address on a fixed lenght string of 15 characters. we save space this way.
Definition at line 843 of file gdcmUtil.cxx. References gdcmWarningMacro, gdcm::getlastdigit(), and gdcm::GetMacAddrSys(). 00844 { 00845 // This code is the result of a long internet search to find something 00846 // as compact as possible (not OS independant). We only have to separate 00847 // 3 OS: Win32, SunOS and 'real' POSIX 00848 // http://groups-beta.google.com/group/comp.unix.solaris/msg/ad36929d783d63be 00849 // http://bdn.borland.com/article/0,1410,26040,00.html 00850 unsigned char addr[6]; 00851 00852 int stat = GetMacAddrSys(addr); 00853 if (stat == 0) 00854 { 00855 // We need to convert a 6 digit number from base 256 to base 10, using integer 00856 // would requires a 48bits one. To avoid this we have to reimplement the div + modulo 00857 // with string only 00858 bool zero = false; 00859 int res; 00860 std::string sres; 00861 while(!zero) 00862 { 00863 res = getlastdigit(addr); 00864 sres.insert(sres.begin(), '0' + res); 00865 zero = (addr[0] == 0) && (addr[1] == 0) && (addr[2] == 0) 00866 && (addr[3] == 0) && (addr[4] == 0) && (addr[5] == 0); 00867 } 00868 00869 return sres; 00870 } 00871 else 00872 { 00873 gdcmWarningMacro("Problem in finding the MAC Address"); 00874 return ""; 00875 } 00876 }
|
|
Get the (last) name of a full path file name.
Definition at line 322 of file gdcmUtil.cxx. Referenced by gdcm::DicomDir::SetElement(). 00323 { 00324 std::string filename = fullName; 00325 00326 std::string::size_type slash_pos = filename.rfind("/"); 00327 std::string::size_type backslash_pos = filename.rfind("\\"); 00328 slash_pos = slash_pos > backslash_pos ? slash_pos : backslash_pos; 00329 if (slash_pos != std::string::npos ) 00330 { 00331 return filename.substr(slash_pos + 1); 00332 } 00333 else 00334 { 00335 return filename; 00336 } 00337 }
|
|
Get the (directory) path from a full path file name.
Definition at line 301 of file gdcmUtil.cxx. 00302 { 00303 std::string res = fullName; 00304 int pos1 = res.rfind("/"); 00305 int pos2 = res.rfind("\\"); 00306 if ( pos1 > pos2 ) 00307 { 00308 res.resize(pos1); 00309 } 00310 else 00311 { 00312 res.resize(pos2); 00313 } 00314 00315 return res; 00316 }
|
|
Definition at line 932 of file gdcmUtil.cxx. References RootUID. 00933 { 00934 return RootUID; 00935 }
|
|
Provides a simple static GetVersion() function.
Definition at line 57 of file gdcmUtil.h. Referenced by gdcm::FileHelper::CheckMandatoryElements().
|
|
Definition at line 85 of file gdcmUtil.h.
|
|
Definition at line 92 of file gdcmUtil.h.
|
|
Checks whether an 'area' is printable or not (in order to avoid corrupting the terminal of invocation when printing).
Definition at line 212 of file gdcmUtil.cxx. Referenced by gdcm::DocEntrySet::GetEntryForcedAsciiValue(), and gdcm::DataEntry::Print(). 00213 { 00214 for( int i=0; i<l; i++) 00215 { 00216 if (!isprint((unsigned char)s[i]) ) 00217 { 00218 return false; 00219 } 00220 } 00221 return true; 00222 }
|
|
Checks whether a 'string' is printable or not (in order to avoid corrupting the terminal of invocation when printing).
Definition at line 193 of file gdcmUtil.cxx. 00194 { 00195 //std::cout<< std::endl << s << std::endl; 00196 for(unsigned int i=0; i<s.size(); i++) 00197 { 00198 if (!isprint((unsigned char)s[i]) ) 00199 { 00200 return false; 00201 } 00202 } 00203 return true; 00204 }
|
|
tells us whether the processor we are working with is BigEndian or not
Definition at line 434 of file gdcmUtil.cxx. Referenced by gdcm::Document::CheckSwap(), and gdcm::RLEFramesInfo::ConvertRLE16BitsFromRLE8Bits(). 00435 { 00436 #if defined(GDCM_WORDS_BIGENDIAN) 00437 return true; 00438 #else 00439 return false; 00440 #endif 00441 }
|
|
Add a SEPARATOR to the end of the name is necessary.
Definition at line 282 of file gdcmUtil.cxx. Referenced by gdcm::DirList::Explore(). 00283 { 00284 const char SEPARATOR_X = '/'; 00285 const char SEPARATOR_WIN = '\\'; 00286 const std::string SEPARATOR = "/"; 00287 std::string name = pathname; 00288 int size = name.size(); 00289 00290 if ( name[size-1] != SEPARATOR_X && name[size-1] != SEPARATOR_WIN ) 00291 { 00292 name += SEPARATOR; 00293 } 00294 return name; 00295 }
|
|
Definition at line 76 of file gdcmUtil.h. 00077 { FileMetaInformationVersion = (uint8_t *)&fmiv; }
|
|
Definition at line 924 of file gdcmUtil.cxx. References GDCM_UID, and RootUID. 00925 { 00926 if ( root.empty() ) 00927 RootUID = GDCM_UID; 00928 else 00929 RootUID = root; 00930 }
|
|
Because not available in C++ (?).
Definition at line 147 of file gdcmUtil.cxx. Referenced by gdcm::File::GetLUTNbits(), and gdcm::DataEntry::SetString(). 00150 { 00151 std::string::size_type lastPos = str.find_first_not_of(delimiters,0); 00152 std::string::size_type pos = str.find_first_of (delimiters,lastPos); 00153 while (std::string::npos != pos || std::string::npos != lastPos) 00154 { 00155 tokens.push_back(str.substr(lastPos, pos - lastPos)); 00156 lastPos = str.find_first_not_of(delimiters, pos); 00157 pos = str.find_first_of (delimiters, lastPos); 00158 } 00159 }
|
|
Definition at line 105 of file gdcmUtil.h. |
|
Definition at line 106 of file gdcmUtil.h. |
|
Definition at line 107 of file gdcmUtil.h. Referenced by CreateUniqueUID(). |
|
Definition at line 104 of file gdcmUtil.h. Referenced by SetRootUID(). |
|
Definition at line 103 of file gdcmUtil.h. Referenced by CreateUniqueUID(), GetRootUID(), and SetRootUID(). |