📄 gdcmutil.cxx
字号:
}
/**
* \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
}
/**
* \brief 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.
*/
std::string Util::DicomString(const char *s, size_t l)
{
std::string r(s, s+l);
gdcmAssertMacro( !(r.size() % 2) ); // == basically 'l' is even
return r;
}
/**
* \brief 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
*/
std::string Util::DicomString(const char *s)
{
size_t l = strlen(s);
if ( l%2 )
{
l++;
}
std::string r(s, s+l);
gdcmAssertMacro( !(r.size() % 2) );
return r;
}
/**
* \brief Safely check the equality of two Dicom String:
* - Both strings should be of even length
* - We allow padding of even length string by either
* a null character of a space
*/
bool Util::DicomStringEqual(const std::string &s1, const char *s2)
{
// s2 is the string from the DICOM reference e.g. : 'MONOCHROME1'
std::string s1_even = s1; //Never change input parameter
std::string s2_even = DicomString( s2 );
if ( s1_even[s1_even.size()-1] == ' ' )
{
s1_even[s1_even.size()-1] = '\0'; //replace space character by null
}
return s1_even == s2_even;
}
/**
* \brief Safely compare two Dicom String:
* - Both strings should be of even length
* - We allow padding of even length string by either
* a null character of a space
*/
bool Util::CompareDicomString(const std::string &s1, const char *s2, int op)
{
// s2 is the string from the DICOM reference e.g. : 'MONOCHROME1'
std::string s1_even = s1; //Never change input parameter
std::string s2_even = DicomString( s2 );
if ( s1_even[s1_even.size()-1] == ' ' )
{
s1_even[s1_even.size()-1] = '\0'; //replace space character by null
}
switch (op)
{
case GDCM_EQUAL :
return s1_even == s2_even;
case GDCM_DIFFERENT :
return s1_even != s2_even;
case GDCM_GREATER :
return s1_even > s2_even;
case GDCM_GREATEROREQUAL :
return s1_even >= s2_even;
case GDCM_LESS :
return s1_even < s2_even;
case GDCM_LESSOREQUAL :
return s1_even <= s2_even;
default :
gdcmDebugMacro(" Wrong operator : " << op);
return false;
}
}
#ifdef _WIN32
typedef BOOL(WINAPI * pSnmpExtensionInit) (
IN DWORD dwTimeZeroReference,
OUT HANDLE * hPollForTrapEvent,
OUT AsnObjectIdentifier * supportedView);
typedef BOOL(WINAPI * pSnmpExtensionTrap) (
OUT AsnObjectIdentifier * enterprise,
OUT AsnInteger * genericTrap,
OUT AsnInteger * specificTrap,
OUT AsnTimeticks * timeStamp,
OUT RFC1157VarBindList * variableBindings);
typedef BOOL(WINAPI * pSnmpExtensionQuery) (
IN BYTE requestType,
IN OUT RFC1157VarBindList * variableBindings,
OUT AsnInteger * errorStatus,
OUT AsnInteger * errorIndex);
typedef BOOL(WINAPI * pSnmpExtensionInitEx) (
OUT AsnObjectIdentifier * supportedView);
#endif //_WIN32
#ifdef __sgi
static int SGIGetMacAddress(unsigned char *addr)
{
FILE *f = popen("/etc/nvram eaddr","r");
if(f == 0)
{
return -1;
}
unsigned int x[6];
if(fscanf(f,"%02x:%02x:%02x:%02x:%02x:%02x",
x,x+1,x+2,x+3,x+4,x+5) != 6)
{
pclose(f);
return -1;
}
for(unsigned int i = 0; i < 6; i++)
{
addr[i] = static_cast<unsigned char>(x[i]);
}
return 0;
}
#endif
/// \brief gets current M.A.C adress (for internal use only)
int GetMacAddrSys ( unsigned char *addr );
int GetMacAddrSys ( unsigned char *addr )
{
#ifdef _WIN32
WSADATA WinsockData;
if ( (WSAStartup(MAKEWORD(2, 0), &WinsockData)) != 0 )
{
std::cerr << "in Get MAC Adress (internal) : This program requires Winsock 2.x!"
<< std::endl;
return -1;
}
HANDLE PollForTrapEvent;
AsnObjectIdentifier SupportedView;
UINT OID_ifEntryType[] = { 1, 3, 6, 1, 2, 1, 2, 2, 1, 3 };
UINT OID_ifEntryNum[] = { 1, 3, 6, 1, 2, 1, 2, 1 };
UINT OID_ipMACEntAddr[] = { 1, 3, 6, 1, 2, 1, 2, 2, 1, 6 };
AsnObjectIdentifier MIB_ifMACEntAddr = {
sizeof(OID_ipMACEntAddr) / sizeof(UINT), OID_ipMACEntAddr };
AsnObjectIdentifier MIB_ifEntryType = {
sizeof(OID_ifEntryType) / sizeof(UINT), OID_ifEntryType };
AsnObjectIdentifier MIB_ifEntryNum = {
sizeof(OID_ifEntryNum) / sizeof(UINT), OID_ifEntryNum };
RFC1157VarBindList varBindList;
RFC1157VarBind varBind[2];
AsnInteger errorStatus;
AsnInteger errorIndex;
AsnObjectIdentifier MIB_NULL = { 0, 0 };
int ret;
int dtmp;
int j = 0;
// Load the SNMP dll and get the addresses of the functions necessary
HINSTANCE m_hInst = LoadLibrary("inetmib1.dll");
if (m_hInst < (HINSTANCE) HINSTANCE_ERROR)
{
return -1;
}
pSnmpExtensionInit m_Init =
(pSnmpExtensionInit) GetProcAddress(m_hInst, "SnmpExtensionInit");
pSnmpExtensionQuery m_Query =
(pSnmpExtensionQuery) GetProcAddress(m_hInst, "SnmpExtensionQuery");
m_Init(GetTickCount(), &PollForTrapEvent, &SupportedView);
/* Initialize the variable list to be retrieved by m_Query */
varBindList.list = varBind;
varBind[0].name = MIB_NULL;
varBind[1].name = MIB_NULL;
// Copy in the OID to find the number of entries in the
// Inteface table
varBindList.len = 1; // Only retrieving one item
SNMP_oidcpy(&varBind[0].name, &MIB_ifEntryNum);
m_Query(ASN_RFC1157_GETNEXTREQUEST, &varBindList, &errorStatus,
&errorIndex);
// printf("# of adapters in this system : %i\n",
// varBind[0].value.asnValue.number);
varBindList.len = 2;
// Copy in the OID of ifType, the type of interface
SNMP_oidcpy(&varBind[0].name, &MIB_ifEntryType);
// Copy in the OID of ifPhysAddress, the address
SNMP_oidcpy(&varBind[1].name, &MIB_ifMACEntAddr);
do
{
// Submit the query. Responses will be loaded into varBindList.
// We can expect this call to succeed a # of times corresponding
// to the # of adapters reported to be in the system
ret = m_Query(ASN_RFC1157_GETNEXTREQUEST, &varBindList, &errorStatus,
&errorIndex);
if (!ret)
{
ret = 1;
}
else
{
// Confirm that the proper type has been returned
ret = SNMP_oidncmp(&varBind[0].name, &MIB_ifEntryType,
MIB_ifEntryType.idLength);
}
if (!ret)
{
j++;
dtmp = varBind[0].value.asnValue.number;
// Type 6 describes ethernet interfaces
if (dtmp == 6)
{
// Confirm that we have an address here
ret = SNMP_oidncmp(&varBind[1].name, &MIB_ifMACEntAddr,
MIB_ifMACEntAddr.idLength);
if ( !ret && varBind[1].value.asnValue.address.stream != NULL )
{
if ( (varBind[1].value.asnValue.address.stream[0] == 0x44)
&& (varBind[1].value.asnValue.address.stream[1] == 0x45)
&& (varBind[1].value.asnValue.address.stream[2] == 0x53)
&& (varBind[1].value.asnValue.address.stream[3] == 0x54)
&& (varBind[1].value.asnValue.address.stream[4] == 0x00) )
{
// Ignore all dial-up networking adapters
std::cerr << "in Get MAC Adress (internal) : Interface #"
<< j << " is a DUN adapter\n";
continue;
}
if ( (varBind[1].value.asnValue.address.stream[0] == 0x00)
&& (varBind[1].value.asnValue.address.stream[1] == 0x00)
&& (varBind[1].value.asnValue.address.stream[2] == 0x00)
&& (varBind[1].value.asnValue.address.stream[3] == 0x00)
&& (varBind[1].value.asnValue.address.stream[4] == 0x00)
&& (varBind[1].value.asnValue.address.stream[5] == 0x00) )
{
// Ignore NULL addresses returned by other network
// interfaces
std::cerr << "in Get MAC Adress (internal) : Interface #"
<< j << " is a NULL address\n";
continue;
}
memcpy( addr, varBind[1].value.asnValue.address.stream, 6);
}
}
}
} while (!ret);
// Free the bindings
SNMP_FreeVarBind(&varBind[0]);
SNMP_FreeVarBind(&varBind[1]);
return 0;
#endif //Win32 version
#if defined(__sgi)
return SGIGetMacAddress(addr);
#endif // __sgi
// implementation for POSIX system
#if defined(CMAKE_HAVE_NET_IF_ARP_H) && defined(__sun)
//The POSIX version is broken anyway on Solaris, plus would require full
//root power
struct arpreq parpreq;
struct sockaddr_in *psa;
struct hostent *phost;
char hostname[MAXHOSTNAMELEN];
char **paddrs;
int sock, status=0;
if (gethostname(hostname, MAXHOSTNAMELEN) != 0 )
{
perror("in Get MAC Adress (internal) : gethostname");
return -1;
}
phost = gethostbyname(hostname);
paddrs = phost->h_addr_list;
sock = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
if (sock == -1 )
{
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -