You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
149 lines
4.4 KiB
149 lines
4.4 KiB
#include <limits>
|
|
|
|
#include "utils.hpp"
|
|
#include "globals.hpp"
|
|
|
|
using namespace std ;
|
|
|
|
// ---------------------------------------------------------------------
|
|
|
|
const char*
|
|
enumString( const EnumStringInfo* pEnumStringTable , int val )
|
|
{
|
|
// return the enum value's display string
|
|
for ( const EnumStringInfo* pInfo=pEnumStringTable ; pInfo->mpName != NULL ; ++pInfo )
|
|
{
|
|
if ( pInfo->mVal == val )
|
|
return pInfo->mpName ;
|
|
}
|
|
assert( false ) ;
|
|
return "???" ;
|
|
}
|
|
|
|
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
|
|
|
|
string
|
|
bitFlagsString( const BitFlagsInfo* pBitFlagsTable , int val , char sep )
|
|
{
|
|
// convert the bit flags to a string
|
|
vector<const char*> bitFlags ;
|
|
for ( const BitFlagsInfo* pInfo=pBitFlagsTable ; pInfo->mpName != NULL ; ++pInfo )
|
|
{
|
|
if ( val & pInfo->mFlag )
|
|
bitFlags.push_back( pInfo->mpName ) ;
|
|
}
|
|
stringstream buf ;
|
|
for ( size_t i=0 ; i < bitFlags.size() ; ++i )
|
|
{
|
|
if ( i > 0 )
|
|
buf << sep ;
|
|
buf << bitFlags[i] ;
|
|
}
|
|
return buf.str() ;
|
|
}
|
|
|
|
// ---------------------------------------------------------------------
|
|
|
|
string
|
|
getErrorString( int errorCode )
|
|
{
|
|
// get the WIN32 error message
|
|
char buf[ 4*1024 ] = "" ;
|
|
if ( errorCode != 0 )
|
|
{
|
|
DWORD nBytes = FormatMessageA(
|
|
FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_MAX_WIDTH_MASK ,
|
|
NULL ,
|
|
errorCode ,
|
|
MAKELANGID(LANG_NEUTRAL,SUBLANG_DEFAULT) ,
|
|
buf , sizeof(buf) ,
|
|
NULL
|
|
) ;
|
|
}
|
|
if ( buf[0] != '\0' )
|
|
return buf ;
|
|
|
|
// just return the WIN32 error code
|
|
sprintf_s( buf , sizeof(buf) , "WIN32 ERROR: %08lX" , errorCode ) ;
|
|
return buf ;
|
|
}
|
|
|
|
string getLastErrorString() { return getErrorString( GetLastError() ) ; }
|
|
|
|
// ---------------------------------------------------------------------
|
|
|
|
string
|
|
toUtf8( const wchar_t* pStr , int len )
|
|
{
|
|
if ( pStr == NULL || len == 0 )
|
|
return "" ;
|
|
assert( len > 0 || len == -1 ) ;
|
|
|
|
// figure out how many UTF-8 characters we are going to get
|
|
int nChars = WideCharToMultiByte( CP_UTF8 , 0 , pStr , len , NULL , 0 , NULL , NULL ) ;
|
|
if ( len == -1 )
|
|
-- nChars ;
|
|
if ( nChars == 0 )
|
|
return "" ;
|
|
|
|
// convert the string to UTF-8
|
|
// nb: slightly naughty to write directly into the string like this
|
|
string buf ;
|
|
buf.resize( nChars ) ;
|
|
WideCharToMultiByte( CP_UTF8 , 0 , pStr , len , const_cast<char*>(buf.c_str()) , nChars , NULL , NULL ) ;
|
|
|
|
return buf ;
|
|
}
|
|
|
|
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
|
|
|
|
wstring
|
|
fromUtf8( const char* pStr , int len )
|
|
{
|
|
if ( pStr == NULL || len == 0 )
|
|
return L"" ;
|
|
assert( len > 0 || len == -1 ) ;
|
|
|
|
// figure out how many wide characters we are going to get
|
|
int nChars = MultiByteToWideChar( CP_UTF8 , 0 , pStr , len , NULL , 0 ) ;
|
|
if ( len == -1 )
|
|
-- nChars ;
|
|
if ( nChars == 0 )
|
|
return L"" ;
|
|
|
|
// convert the string from UTF-8
|
|
// nb: slightly naughty to write directly into the string like this
|
|
wstring buf ;
|
|
buf.resize( nChars ) ;
|
|
MultiByteToWideChar( CP_UTF8 , 0 , pStr , len , const_cast<wchar_t*>(buf.c_str()) , nChars ) ;
|
|
|
|
return buf ;
|
|
}
|
|
|
|
// ---------------------------------------------------------------------
|
|
|
|
template<class T>
|
|
string
|
|
hexString( T val , int fieldWidth )
|
|
{
|
|
// convert the numeric value to a hex string
|
|
stringstream buf ;
|
|
if ( fieldWidth < 0 )
|
|
fieldWidth = (numeric_limits<T>::digits + 3) / 4 ;
|
|
buf.width( fieldWidth ) ;
|
|
buf.fill( '0' ) ;
|
|
buf << hex << val ;
|
|
string bufVal = buf.str() ;
|
|
char* p = const_cast<char*>( bufVal.c_str() ) ;
|
|
for ( ; *p != '\0' ; ++p )
|
|
*p = toupper( *p ) ;
|
|
return bufVal ;
|
|
}
|
|
|
|
template string hexString<int>( int , int ) ; // nb: instantiate the template
|
|
template string hexString<unsigned int>( unsigned int , int ) ; // nb: instantiate the template
|
|
template string hexString<short>( short , int ) ; // nb: instantiate the template
|
|
template string hexString<unsigned short>( unsigned short , int ) ; // nb: instantiate the template
|
|
template string hexString<long>( long , int ) ; // nb: instantiate the template
|
|
template string hexString<unsigned long>( unsigned long , int ) ; // nb: instantiate the template
|
|
template string hexString<void*>( void* , int ) ; // nb: instantiate the template
|
|
|