stringutils.cpp
来自「这是VCF框架的代码」· C++ 代码 · 共 2,672 行 · 第 1/5 页
CPP
2,672 行
#endif return String( tmp );#endif}VCF::String StringUtils::toString( const double& value ){#ifdef VCF_OSX CFTextString cfTmp; CFStringRef s = CFStringCreateWithFormat( NULL, NULL, CFSTR( "%.5f" ), value ); cfTmp = s; CFRelease( s ); return String( cfTmp );#else VCFChar tmp[TO_STRING_TXT_SIZE]; memset( tmp, 0, TO_STRING_TXT_SIZE * sizeof(VCFChar) ); #if defined(VCF_POSIX) || defined(VCF_CW_W32) || defined(VCF_DMC) swprintf( tmp, sizeof(tmp)/sizeof(VCFChar)-1, L"%.5f", value ); #else swprintf( tmp, L"%.5f", value ); #endif return String( tmp );#endif}VCF::String StringUtils::toString( const char& value ){#ifdef VCF_OSX CFTextString cfTmp; CFStringRef s = CFStringCreateWithFormat( NULL, NULL, CFSTR( STR_CHAR_CONVERSION ), value ); cfTmp = s; CFRelease( s ); return String( cfTmp );#else VCFChar tmp[TO_STRING_TXT_SIZE]; memset( tmp, 0, TO_STRING_TXT_SIZE * sizeof(VCFChar) ); #if defined(VCF_POSIX) || defined(VCF_CW_W32) || defined(VCF_DMC) swprintf( tmp, sizeof(tmp)/sizeof(VCFChar)-1, W_STR_CHAR_CONVERSION, value ); #else swprintf( tmp, W_STR_CHAR_CONVERSION, value ); #endif return String( tmp );#endif}VCF::String StringUtils::toString( const bool& value ){ return ( value == true ) ? L"true" : L"false";}VCF::String StringUtils::newUUID(){ VCF::String result = "";#ifdef WIN32 UUID id; if ( RPC_S_OK == ::UuidCreate( &id ) ){ if ( System::isUnicodeEnabled() ) { WideChar* tmpid = NULL; RPC_STATUS rpcresult = UuidToStringW( &id, reinterpret_cast<unsigned short**>(&tmpid) ); if ( RPC_S_OUT_OF_MEMORY != rpcresult ) { result = VCF::String( tmpid ); RpcStringFreeW( reinterpret_cast<unsigned short**>(&tmpid) ); } } else { char* tmpid = NULL; RPC_STATUS rpcresult = UuidToStringA( &id, (unsigned char**)&tmpid ); if ( RPC_S_OUT_OF_MEMORY != rpcresult ) { result = VCF::String( tmpid ); RpcStringFreeA( (unsigned char**)&tmpid ); } } }#elif defined(VCF_OSX) CFUUIDRef uuidRef = CFUUIDCreate( kCFAllocatorDefault ); CFTextString tmp; CFStringRef s = CFUUIDCreateString( kCFAllocatorDefault, uuidRef ); tmp = s; CFRelease( s ); result = tmp; CFRelease( uuidRef );#elif defined(VCF_POSIX) const uint32 uuidgenLength = 36; char buffer[uuidgenLength+1]; memset(buffer, '\0', sizeof(buffer)); uuid_t uuid; uuid_generate(uuid); uuid_unparse(uuid, buffer); uuid_clear(uuid); result = buffer;// FILE* uuidgenFile;// uuidgenFile = popen("uuidgen", "r");// if(uuidgenFile != NULL) {// int charsRead = fread(buffer, sizeof(char), uuidgenLength, uuidgenFile);// if(charsRead == uuidgenLength) {// result = buffer;// }// else {// What should we do? Throw an exception!// }// pclose(uuidgenFile);// }#endif return result;}VCF::String StringUtils::format( const Format& formatter ){ return formatter;}/*VCF::String StringUtils::format( VCF::String formatText, ... ){ //StringUtils::trace( String("WARNING: Using deprecated function!!! - StringUtils::format(...)\n") ); // MP- VCF::String result = ""; formatText = StringUtils::convertFormatString( formatText ); va_list argList; va_start( argList, formatText ); // Initialize variable arguments. VCFChar* buf = new VCFChar[MAX_TRACE_STRING]; memset( buf, 0, MAX_TRACE_STRING*sizeof(VCFChar) );#ifdef VCF_OSX CFMutableStringRef fmt = CFStringCreateMutable( NULL, 0 ); CFStringAppendCharacters( fmt, formatText.c_str(), formatText.size() ); CFStringRef res = CFStringCreateWithFormatAndArguments( NULL, NULL, fmt, argList ); int length = minVal<uint32>( MAX_TRACE_STRING-1, CFStringGetLength( res ) ); CFRange range = {0, length }; CFStringGetCharacters( res, range, buf ); CFRelease( res ); CFRelease( fmt );#elif defined(VCF_POSIX) || defined(VCF_CW_W32) vswprintf( buf, MAX_TRACE_STRING, formatText.c_str(), argList );#else _vsnwprintf( buf, MAX_TRACE_STRING, formatText.c_str(), argList );#endif va_end( argList ); // Reset variable arguments. result = buf; delete [] buf; return result;}*/VCF::String StringUtils::toString( const std::type_info& typeInfo ){#if defined(WIN32) && !defined(VCF_MINGW) return StringUtils::getClassNameFromTypeInfo( typeInfo );#elif defined(VCF_GCC) String result; int status = 0; char* nameBuf; const char* c_name = 0; nameBuf = abi::__cxa_demangle( typeInfo.name(), 0, 0, &status ); c_name = nameBuf; if ( NULL == c_name ) { //try typeinfo.name() without the C++ de-mangler c_name = typeInfo.name(); if ( -2 == status && (strlen(c_name) == 1) ) { //built-in type switch (c_name[0]) { case 'v': c_name = "void"; break; case 'w': c_name = "wchar_t"; break; case 'b': c_name = "bool"; break; case 'c': c_name = "char"; break; case 'a': c_name = "signed char"; break; case 'h': c_name = "unsigned char"; break; case 's': c_name = "short"; break; case 't': c_name = "unsigned short"; break; case 'i': c_name = "int"; break; case 'j': c_name = "unsigned int"; break; case 'l': c_name = "long"; break; case 'm': c_name = "unsigned long"; break; case 'x': c_name = "long long"; break; case 'y': c_name = "unsigned long long"; break; case 'n': c_name = "__int128"; break; case 'o': c_name = "unsigned __int128"; break; case 'f': c_name = "float"; break; case 'd': c_name = "double"; break; case 'e': c_name = "long double"; break; case 'g': c_name = "__float128"; break; case 'z': c_name = "..."; break; } } } if ( NULL != c_name ) { result = c_name; } if ( NULL != nameBuf ) { ::free( nameBuf ); } return result;#else return typeInfo.name();#endif }VCF::String StringUtils::getClassNameFromTypeInfo( const std::type_info& typeInfo ){ VCF::String result = "";#if defined(VCF_WIN32) && !defined(VCF_MINGW) //don't know if we really need this here std::string tmp = typeInfo.name(); //put back in when we find typeid if ( tmp != "void *" ) {//void* is a special case! //strip out the preceding "class" or "enum" or whatever std::string::size_type idx = tmp.find( "class " ); if ( idx != tmp.npos ) { tmp = tmp.substr( idx+std::string("class ").size() ); } else { idx = tmp.find( "enum " ); if ( idx != tmp.npos ) { tmp = tmp.substr( idx+std::string("enum ").size() ); } else { idx = tmp.find( "struct " ); if ( idx != tmp.npos ) { tmp = tmp.substr( idx+std::string("struct ").size() ); } } } } #ifndef KEEP_NAMESPACE_IN_CLASSNAME std::string::size_type idx = tmp.find( "::" ); if ( idx == tmp.npos ) { result = tmp; // not part of a namespace } else { result = tmp.substr( idx + 2 ); // strip namespace off from string } #else result = tmp; #endif#elif defined(VCF_OSX) || defined(VCF_MINGW) || defined(VCF_POSIX) int status = 0; char* nameBuf; const char* c_name = 0; nameBuf = abi::__cxa_demangle( typeInfo.name(), 0, 0, &status ); c_name = nameBuf; if ( NULL == c_name ) { //try typeinfo.name() without the C++ de-mangler c_name = typeInfo.name(); if ( -2 == status && (strlen(c_name) == 1) ) { //built-in type switch (c_name[0]) { case 'v': c_name = "void"; break; case 'w': c_name = "wchar_t"; break; case 'b': c_name = "bool"; break; case 'c': c_name = "char"; break; case 'a': c_name = "signed char"; break; case 'h': c_name = "unsigned char"; break; case 's': c_name = "short"; break; case 't': c_name = "unsigned short"; break; case 'i': c_name = "int"; break; case 'j': c_name = "unsigned int"; break; case 'l': c_name = "long"; break; case 'm': c_name = "unsigned long"; break; case 'x': c_name = "long long"; break; case 'y': c_name = "unsigned long long"; break; case 'n': c_name = "__int128"; break; case 'o': c_name = "unsigned __int128"; break; case 'f': c_name = "float"; break; case 'd': c_name = "double"; break; case 'e': c_name = "long double"; break; case 'g': c_name = "__float128"; break; case 'z': c_name = "..."; break; } } } if ( NULL != c_name ) { result = c_name; } if ( NULL != nameBuf ) { ::free( nameBuf ); }#else result = typeInfo.name();#endif return result;}#ifdef VCF_OSX// routine to check if it is a real error// if your Mac code is brocken, please fix this !// What about using sscanf ?/*void check_true_error( const VCF::String& value ){ if ( 0 == value ) { bool error = false; int len = value.size(); if ( 0 == len ) { error = true; } else { VCFChar c = value[0]; if ( CFSTR( "0" ) != c ) { if ( CFSTR( "+" ) == c || CFSTR( "-" ) == c ) { if ( CFSTR( "0" ) != value[1] ) { error = true; } } else { error = true; } } } if ( error ) { throw BasicException( L"Unable to convert: " + value ); } }}*/#endifint StringUtils::fromStringAsHexNumber( const VCF::String& value ){ int result = 0; String::size_type size = value.size();#ifdef VCF_OSX // is the string beginning with "0x" ? const UnicodeString::AnsiChar* p = value.ansi_c_str(); if ( 2 < size ) { if ( ( '0' == p[0] ) && ( 'x' == p[1] ) ) { p += 2; } } int ret = sscanf( p, "%X", &result ); if ( ret != 1 ) { throw BasicException( L"Unable to convert: " + value ); } #else // is the string beginning with "0x" ? const UnicodeString::UniChar* p = value.c_str(); if ( 2 < size ) { if ( ( '0' == p[0] ) && ( 'x' == p[1] ) ) { p += 2; } } int ret = swscanf( p, L"%X", &result ); if ( ret != 1 ) { throw BasicException( L"Unable to convert: " + value ); } #endif return result;}int StringUtils::fromStringAsInt( const VCF::String& value ){ int result = 0; #ifdef VCF_OSX CFTextString tmp; tmp = value; result = CFStringGetIntValue( tmp ); if ( 0 == result ) { //check_true_error( tmp ); } else if ( INT_MIN == result || INT_MAX == result ) { throw BasicException( L"Overflow - Unable to convert: " + value ); } #else #ifdef VCF_MSC result = _wtoi( value.c_str() ); if ( 0 == result && ( value[0] != '0' ) && ( -1 != swscanf( value.c_str(), W_STR_INT_CONVERSION, &result ) ) ) { throw BasicException( L"Unable to convert: " + value ); } #else int ret = swscanf( value.c_str(), W_STR_INT_CONVERSION, &result ); if ( ret != 1 ) { throw BasicException( L"Unable to convert: " + value ); } #endif #endif return result;}VCF::uint32 StringUtils::fromStringAsUInt( const VCF::String& value ){ uint32 result = 0; #ifdef VCF_OSX CFTextString tmp; tmp = value; result = CFStringGetIntValue( tmp ); if ( 0 == result ) { //check_true_error( tmp ); } else if ( INT_MIN == result || INT_MAX == result ) { throw BasicException( L"StringUtils::fromStringAsUInt() Overflow - Unable to convert: " + value ); } #else #ifdef VCF_MSC /* unfortunately there is no _wtoui function provided so we use _wtoi64 to avoid overflow */ result = _wtoi64( value.c_str() ); if ( 0 == result && ( value[0] != '0' ) && ( -1 != swscanf( value.c_str(), W_STR_UINT_CONVERSION, &result ) ) ) { throw BasicException( L"StringUtils::fromStringAsUInt() Unable to convert: " + value ); } #else int ret = swscanf( value.c_str(), W_STR_UINT_CONVERSION, &result ); if ( ret != 1 ) { throw BasicException( L"StringUtils::fromStringAsUInt() Unable to convert: " + value ); } #endif #endif return result;}long StringUtils::fromStringAsLong( const VCF::String& value ){ uint32 result = 0; #ifdef VCF_OSX CFTextString tmp; tmp = value; result = CFStringGetIntValue( tmp ); if ( 0 == result ) { //check_true_error( tmp ); } else if ( INT_MIN == result || INT_MAX == result ) { throw BasicException( L"StringUtils::fromStringAsLong() Overflow - Unable to convert: " + value ); } #else #ifdef _MSC_VER result = _wtol( value.c_str() ); if ( 0 == result && ( value[0] != '0' ) && ( -1 != swscanf( value.c_str(), W_STR_LONG_CONVERSION, &result ) ) ) { throw BasicException( L"StringUtils::fromStringAsLong() Unable to convert: " + value ); } #else int ret = swscanf( value.c_str(), W_STR_LONG_CONVERSION, &result ); if ( ret != 1 ) { throw BasicException( L"StringUtils::fromStringAsLong() Unable to convert: " + value ); } #endif #endif return result;}VCF::ulong32 StringUtils::fromStringAsULong( const VCF::String& value ){ uint32 result = 0; #ifdef VCF_OSX CFTextString tmp; tmp = value; result = CFStringGetIntValue( tmp ); if ( 0 == result ) { //check_true_error( tmp ); } else if ( INT_MIN == result || INT_MAX == result ) { throw BasicException( L"StringUtils::fromStringAsULong() Overflow - Unable to convert: " + value ); } #else #ifdef VCF_MSC result = _wtoi64( value.c_str() ); if ( 0 == result && ( value[0] != '0' ) && ( -1 != swscanf( value.c_str(), W_STR_ULONG_CONVERSION, &result ) ) ) { throw BasicException( L"StringUtils::fromStringAsULong() Unable to convert: " + value ); } #else int ret = swscanf( value.c_str(), W_STR_ULONG_CONVERSION, &result ); if ( ret != 1 ) { throw BasicException( L"StringUtils::fromStringAsULong() Unable to convert: " + value ); } #endif #endif return result;}VCF::long64 StringUtils::fromStringAsLong64( const VCF::String& value ){ long64 result = 0; #ifdef VCF_OSX CFTextString tmp; tmp = value; result = CFStringGetIntValue( tmp ); /*if ( result == 0 ) { //check_true_error( tmp ); } else */ if ( (INT_MIN == (int)result) || (INT_MAX == (int)result) ) { throw BasicException( L"StringUtils::fromStringAsLong64() Overflow - Unable to convert: " + value ); } #else #ifdef VCF_MSC result = _wtoi64( value.c_str() ); if ( (long64)0 == result && ( value[0] != '0' ) && ( -1 != swscanf( value.c_str(), L"%I64d", &result ) ) ) { throw BasicException( L"StringUtils::fromStringAsLong64() Unable to convert: " + value ); } #else int ret = swscanf( value.c_str(), L"%I64d", &result ); if ( ret != 1 ) { throw BasicException( L"StringUtils::fromStringAsLong64() Unable to convert: " + value ); }
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?