Program: gdcm
Module: $RCSfile: gdcmUtil.cxx,v $
Language: C++
- Date: $Date: 2005/08/22 15:38:05 $
- Version: $Revision: 1.158 $
+ Date: $Date: 2005/11/29 13:02:46 $
+ Version: $Revision: 1.179 $
Copyright (c) CREATIS (Centre de Recherche et d'Applications en Traitement de
l'Image). All rights reserved. See Doc/License.txt or
#include "gdcmUtil.h"
#include "gdcmDebug.h"
+
#include <iostream>
+#include <stdarg.h> // for va_list
// For GetCurrentDate, GetCurrentTime
#include <time.h>
//-------------------------------------------------------------------------
const std::string Util::GDCM_UID = "1.2.826.0.1.3680043.2.1143";
std::string Util::RootUID = GDCM_UID;
-const uint16_t Util::FMIV = 0x0001;
+/*
+ * 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();
/**
* \brief Provide a better 'c++' approach for sprintf
* For example c code is:
- * char result[200]; // hope 200 is enough
+ * char result[2048]; // hope 2048 is enough
* sprintf(result, "%04x|%04x", group , elem);
*
* c++ code is
*/
std::string Util::Format(const char *format, ...)
{
- char buffer[2048];
+ char buffer[2048]; // hope 2048 is enough
va_list args;
va_start(args, format);
vsprintf(buffer, format, args); //might be a security flaw
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 remove non printable characters from
+ * @param s string to check (uint8_t is just for prototyping. feel free to cast)
*/
std::string Util::CreateCleanString(std::string const &s)
{
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
return tmpAll;
}
-/*
- * \brief Provide a simple static GetVersion() function
- */
-std::string Util::GetVersion()
-{
- return GDCM_VERSION;
-}
-
unsigned int Util::GetCurrentThreadID()
{
// FIXME the implementation is far from complete
}
close(sd);
#endif
- // Not implemented platforms
- perror("in Get MAC Adress (internal) : There was a configuration problem on your plateform");
+ // Not implemented platforms (or no cable !)
+ perror("in Get MAC Adress (internal) : There was a configuration problem (or no cable !) on your plateform");
memset(addr,0,6);
return -1;
#endif //__sun
}
/**
- * \brief Encode the mac address on a fixed lenght string of 15 characters.
+ * \brief Encode the mac address on a fixed length string of 15 characters.
* we save space this way.
*/
std::string Util::GetMACAddress()
}
else
{
- gdcmWarningMacro("Problem in finding the MAC Address");
+ gdcmStaticWarningMacro("Problem in finding the MAC Address");
return "";
}
}
/**
- * \brief Creates a new UID. As stipulate in the DICOM ref
- * each time a DICOM image is create it should have
+ * \brief Creates a new UID. As stipulated in the DICOM ref
+ * each time a DICOM image is created it should have
* a unique identifier (URI)
* @param root is the DICOM prefix assigned by IOS group
*/
append += Util::GDCM_MAC_ADRESS;
append += ".";
append += Util::GetCurrentDateTime();
-
+ append += ".";
//Also add a mini random number just in case:
char tmp[10];
int r = (int) (100.0*rand()/RAND_MAX);
// If append is too long we need to rehash it
if ( (prefix + append).size() > 64 )
{
- gdcmErrorMacro( "Size of UID is too long." );
+ gdcmStaticErrorMacro( "Size of UID is too long." );
// we need a hash function to truncate this number
// if only md5 was cross plateform
// MD5(append);
/**
* \brief binary_write binary_write
* @param os ostream to write to
- * @param val val
+ * @param val 16 bits value to write
*/
std::ostream &binary_write(std::ostream &os, const uint16_t &val)
{
#if defined(GDCM_WORDS_BIGENDIAN) || defined(GDCM_FORCE_BIGENDIAN_EMULATION)
uint16_t swap;
- //swap = ((( val << 8 ) & 0xff00 ) | (( val >> 8 ) & 0x00ff ) );
- //save CPU time
- swap = ( val << 8 | val >> 8 );
+ swap = ( val << 8 | val >> 8 );
return os.write(reinterpret_cast<const char*>(&swap), 2);
#else
/**
* \brief binary_write binary_write
* @param os ostream to write to
- * @param val val
+ * @param val 32 bits value to write
*/
std::ostream &binary_write(std::ostream &os, const uint32_t &val)
{
#if defined(GDCM_WORDS_BIGENDIAN) || defined(GDCM_FORCE_BIGENDIAN_EMULATION)
uint32_t swap;
-// swap = ( ((val<<24) & 0xff000000) | ((val<<8) & 0x00ff0000) |
-// ((val>>8) & 0x0000ff00) | ((val>>24) & 0x000000ff) );
-// save CPU time
swap = ( (val<<24) | ((val<<8) & 0x00ff0000) |
((val>>8) & 0x0000ff00) | (val>>24) );
return os.write(reinterpret_cast<const char*>(&swap), 4);
#endif //GDCM_WORDS_BIGENDIAN
}
+
+/**
+ * \brief binary_write binary_write
+ * @param os ostream to write to
+ * @param val double (64 bits) value to write
+ */
+std::ostream &binary_write(std::ostream &os, const double &val)
+{
+#if defined(GDCM_WORDS_BIGENDIAN) || defined(GDCM_FORCE_BIGENDIAN_EMULATION)
+ double swap = val;
+
+ char *beg = (char *)&swap;
+ char *end = beg + 7;
+ char t;
+ for (unsigned int i = 0; i<7; i++)
+ {
+ t = *beg;
+ *beg = *end;
+ *end = t;
+ beg++,
+ end--;
+ }
+ return os.write(reinterpret_cast<const char*>(&swap), 8);
+#else
+ return os.write(reinterpret_cast<const char*>(&val), 8);
+#endif //GDCM_WORDS_BIGENDIAN
+}
+
+
/**
* \brief binary_write binary_write
* @param os ostream to write to
- * @param val val
+ * @param val 8 bits characters aray to write
*/
std::ostream &binary_write(std::ostream &os, const char *val)
{
}
/**
- * \brief
+ * \brief binary_write binary_write
* @param os ostream to write to
- * @param val val
+ * @param val std::string value to write
*/
std::ostream &binary_write(std::ostream &os, std::string const &val)
{
/**
* \brief binary_write binary_write
* @param os ostream to write to
- * @param val value
+ * @param val 8 bits 'characters' aray to write
* @param len length of the 'value' to be written
*/
std::ostream &binary_write(std::ostream &os, const uint8_t *val, size_t len)
/**
* \brief binary_write binary_write
* @param os ostream to write to
- * @param val val
- * @param len length of the 'value' to be written
+ * @param val 16 bits words aray to write
+ * @param len length (in bytes) of the 'value' to be written
*/
std::ostream &binary_write(std::ostream &os, const uint16_t *val, size_t len)
{
return str;
}
+void Util::hfpswap(double *a, double *b)
+{
+ double tmp;
+ tmp=*a;
+ *a=*b;
+ *b=tmp;
+}
+
//-------------------------------------------------------------------------
} // end namespace gdcm