ExtMfcSafeNativeTCHAR.h
上传用户:sesekoo
上传日期:2020-07-18
资源大小:21543k
文件大小:143k
源码类别:
界面编程
开发平台:
Visual C++
- // This is part of the Professional User Interface Suite library.
- // Copyright (C) 2001-2009 FOSS Software, Inc.
- // All rights reserved.
- //
- // http://www.prof-uis.com
- // mailto:support@prof-uis.com
- //
- // This source code can be used, modified and redistributed
- // under the terms of the license agreement that is included
- // in the Professional User Interface Suite package.
- //
- // Warranties and Disclaimers:
- // THIS SOFTWARE IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND
- // INCLUDING, BUT NOT LIMITED TO, WARRANTIES OF MERCHANTABILITY,
- // FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.
- // IN NO EVENT WILL FOSS SOFTWARE INC. BE LIABLE FOR ANY DIRECT,
- // INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY OR CONSEQUENTIAL DAMAGES,
- // INCLUDING DAMAGES FOR LOSS OF PROFITS, LOSS OR INACCURACY OF DATA,
- // INCURRED BY ANY PERSON FROM SUCH PERSON'S USAGE OF THIS SOFTWARE
- // EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGES.
- #if (!defined __ExtMfcSafeNativeTCHAR_H)
- #define __ExtMfcSafeNativeTCHAR_H
- #if _MFC_VER >= 0x800
- // disable warning 4201
- #pragma warning( push )
- #pragma warning ( disable : 4996 )
- #endif // _MFC_VER >= 0x800
- //
- //
- //
- // Prof-UIS native unicode character type support for VC++ .NET 2002 or later
- //
- //
- //
- // enable template-based chars for unicode builds under Visual C++ .NET
- // (if commented then automatic conversion between "unsigned short" and
- // native "__wchar_t" is off)
- //
- // #define __EXT_MFC_ENABLE_TEMPLATED_CHARS
- //
- #if _MFC_VER >= 0x700
- #if (defined _UNICODE) && (defined _WCHAR_T_DEFINED)
- #if (defined __EXT_MFC_ENABLE_TEMPLATED_CHARS)
- // this should be configured by user
- // (determines whether Prof-UIS/UNICODE uses
- // native "__wchar_t" or not)
- // #define __EXT_MFC_COMPILED_WITH_NATIVE_WCHAR_T
- // signal we are using extra-safe and compatible chars
- #define __EXT_MFC_TEMPLATED_CHARS_IMPLEMENTED
- #if (defined _NATIVE_WCHAR_T_DEFINED)
- #pragma message(" Prof-UIS native unicode character type support:")
- #pragma message(" adding automatic conversion from unsigned short to __wchar_t")
- // if we compiling the Prof-UIS library or client project
- // withing native unicode character type,
- // then we need to include additonal exports for the clients
- // without _NATIVE_WCHAR_T_DEFINED
- #if (defined __PROF_UIS_IMPL__)
- #if (!defined __EXT_MFC_COMPILED_WITH_NATIVE_WCHAR_T)
- #error Prof-UIS native unicode character type support: the __EXT_MFC_COMPILED_WITH_NATIVE_WCHAR_T symbol should be definded to provide client programs about valid Prof-UIS types
- #endif // (!defined __EXT_MFC_COMPILED_WITH_NATIVE_WCHAR_T)
- #pragma message(" Prof-UIS real wchar_t type is:")
- #pragma message(" native __wchar_t")
- #endif // (defined __PROF_UIS_IMPL__)
- #else // if - (defined _NATIVE_WCHAR_T_DEFINED)
- #pragma message(" Prof-UIS native unicode character type support:")
- #pragma message(" adding automatic conversion from __wchar_t to unsigned short")
- // if we compiling the Prof-UIS library or client project
- // without native unicode character type, then we need
- // to include additonal exports for the clients
- // which need _NATIVE_WCHAR_T_DEFINED
- #if (defined __PROF_UIS_IMPL__)
- #if (defined __EXT_MFC_COMPILED_WITH_NATIVE_WCHAR_T)
- #error Prof-UIS native unicode character type support: the __EXT_MFC_COMPILED_WITH_NATIVE_WCHAR_T symbol should NOT be definded to provide client programs about valid Prof-UIS types
- #endif // (defined __EXT_MFC_COMPILED_WITH_NATIVE_WCHAR_T)
- #pragma message(" Prof-UIS real wchar_t type is:")
- #pragma message(" unsigned short")
- #endif // (defined __PROF_UIS_IMPL__)
- #endif // if not - (defined _NATIVE_WCHAR_T_DEFINED)
- #if (!defined __PROF_UIS_IMPL__)
- #if (defined __EXT_MFC_COMPILED_WITH_NATIVE_WCHAR_T)
- #pragma message(" Prof-UIS real wchar_t type is:")
- #pragma message(" native __wchar_t")
- #else // if - (defined __EXT_MFC_COMPILED_WITH_NATIVE_WCHAR_T)
- #pragma message(" Prof-UIS real wchar_t type is:")
- #pragma message(" unsigned short")
- #endif // if not - (defined __EXT_MFC_COMPILED_WITH_NATIVE_WCHAR_T)
- #if (defined _NATIVE_WCHAR_T_DEFINED)
- #pragma message(" Client project real wchar_t type is:")
- #pragma message(" native __wchar_t")
- #if (defined __EXT_MFC_COMPILED_WITH_NATIVE_WCHAR_T)
- #pragma message(" (Prof-UIS native wchar_t conversion system is not needed)")
- #endif // (defined __EXT_MFC_COMPILED_WITH_NATIVE_WCHAR_T)
- #else // if - (defined _NATIVE_WCHAR_T_DEFINED)
- #pragma message(" Client project real wchar_t type is:")
- #pragma message(" unsigned short")
- #if (!defined __EXT_MFC_COMPILED_WITH_NATIVE_WCHAR_T)
- #pragma message(" (Prof-UIS native wchar_t conversion system is not needed)")
- #endif // (!defined __EXT_MFC_COMPILED_WITH_NATIVE_WCHAR_T)
- #endif // if not - (defined _NATIVE_WCHAR_T_DEFINED)
- #endif // (!defined __PROF_UIS_IMPL__)
- #if (defined __EXT_MFC_COMPILED_WITH_NATIVE_WCHAR_T)
- typedef __wchar_t __prof_uis_used_wchar_t;
- typedef unsigned short __prof_uis_converted_wchar_t;
- #else // if - (defined __EXT_MFC_COMPILED_WITH_NATIVE_WCHAR_T)
- typedef unsigned short __prof_uis_used_wchar_t;
- typedef __wchar_t __prof_uis_converted_wchar_t;
- #endif // if - not (defined __EXT_MFC_COMPILED_WITH_NATIVE_WCHAR_T)
- template < typename BaseType = char >
- class CExtSafeChTraitsBase
- {
- public:
- typedef char XCHAR;
- typedef char * PXSTR;
- typedef const char * PCXSTR;
- typedef __wchar_t YCHAR;
- typedef __wchar_t * PYSTR;
- typedef const __wchar_t * PCYSTR;
- }; // class CExtSafeChTraitsBase
- template<>
- class CExtSafeChTraitsBase< __wchar_t >
- {
- public:
- typedef __wchar_t XCHAR;
- typedef __wchar_t * PXSTR;
- typedef const __wchar_t * PCXSTR;
- typedef char YCHAR;
- typedef char * PYSTR;
- typedef const char * PCYSTR;
- }; // class CExtSafeChTraitsBase
- template<>
- class CExtSafeChTraitsBase< unsigned short >
- {
- public:
- typedef unsigned short XCHAR;
- typedef unsigned short * PXSTR;
- typedef const unsigned short * PCXSTR;
- typedef char YCHAR;
- typedef char * PYSTR;
- typedef const char * PCYSTR;
- }; // class CExtSafeChTraitsBase
- template < typename _CharType = char >
- class CExtSafeChTraitsCRT : public CExtSafeChTraitsBase < _CharType >
- {
- public:
- static char * CharNext( const char * p ) throw()
- {
- return
- reinterpret_cast
- < char * >
- (
- _mbsinc(
- reinterpret_cast
- < const unsigned char * >
- ( p )
- )
- );
- }
- static int IsDigit( char ch ) throw()
- {
- return _ismbcdigit( ch );
- }
- static int IsSpace( char ch ) throw()
- {
- return _ismbcspace( ch );
- }
- static int StringCompare( LPCSTR pszA, LPCSTR pszB ) throw()
- {
- return
- _mbscmp(
- reinterpret_cast < const unsigned char * > ( pszA ),
- reinterpret_cast < const unsigned char * > ( pszB )
- );
- }
- static int StringCompareIgnore( LPCSTR pszA, LPCSTR pszB ) throw()
- {
- return
- _mbsicmp(
- reinterpret_cast < const unsigned char * > ( pszA ),
- reinterpret_cast < const unsigned char * > ( pszB )
- );
- }
- static int StringCollate( LPCSTR pszA, LPCSTR pszB ) throw()
- {
- return
- _mbscoll(
- reinterpret_cast < const unsigned char * > ( pszA ),
- reinterpret_cast < const unsigned char * > ( pszB )
- );
- }
- static int StringCollateIgnore( LPCSTR pszA, LPCSTR pszB ) throw()
- {
- return
- _mbsicoll(
- reinterpret_cast < const unsigned char * > ( pszA ),
- reinterpret_cast < const unsigned char * > ( pszB )
- );
- }
- static LPCSTR StringFindString( LPCSTR pszBlock, LPCSTR pszMatch ) throw()
- {
- return
- reinterpret_cast < LPCSTR >
- (
- _mbsstr(
- reinterpret_cast < const unsigned char * > ( pszBlock ),
- reinterpret_cast < const unsigned char * > ( pszMatch )
- )
- );
- }
- static LPSTR StringFindString( LPSTR pszBlock, LPCSTR pszMatch ) throw()
- {
- return
- (
- const_cast < LPSTR >
- (
- StringFindString(
- const_cast < LPCSTR > ( pszBlock ),
- pszMatch
- )
- )
- );
- }
- static LPCSTR StringFindChar( LPCSTR pszBlock, char chMatch ) throw()
- {
- return
- reinterpret_cast < LPCSTR >
- (
- _mbschr(
- reinterpret_cast < const unsigned char * > ( pszBlock ),
- chMatch
- )
- );
- }
- static LPCSTR StringFindCharRev( LPCSTR psz, char ch ) throw()
- {
- return
- reinterpret_cast < LPSTR >
- (
- _mbsrchr(
- reinterpret_cast < const unsigned char * > ( psz ),
- ch
- )
- );
- }
- static LPCSTR StringScanSet( LPCSTR pszBlock, LPCSTR pszMatch ) throw()
- {
- return
- reinterpret_cast < LPCSTR >
- (
- _mbspbrk(
- reinterpret_cast < const unsigned char * > ( pszBlock ),
- reinterpret_cast < const unsigned char * > ( pszMatch )
- )
- );
- }
- static int StringSpanIncluding( LPCSTR pszBlock, LPCSTR pszSet ) throw()
- {
- return
- (int) _mbsspn(
- reinterpret_cast < const unsigned char * > ( pszBlock ),
- reinterpret_cast < const unsigned char * > ( pszSet )
- );
- }
- static int StringSpanExcluding( LPCSTR pszBlock, LPCSTR pszSet ) throw()
- {
- return
- (int) _mbscspn(
- reinterpret_cast < const unsigned char * > ( pszBlock ),
- reinterpret_cast < const unsigned char * > ( pszSet )
- );
- }
- static LPSTR StringUppercase( LPSTR psz ) throw()
- {
- return
- reinterpret_cast < LPSTR >
- (
- _mbsupr(
- reinterpret_cast < unsigned char * > ( psz )
- )
- );
- }
- static LPSTR StringLowercase( LPSTR psz ) throw()
- {
- return
- reinterpret_cast < LPSTR >
- (
- _mbslwr(
- reinterpret_cast < unsigned char * > ( psz )
- )
- );
- }
- static LPSTR StringReverse( LPSTR psz ) throw()
- {
- return
- reinterpret_cast < LPSTR >
- (
- _mbsrev(
- reinterpret_cast < unsigned char * > ( psz )
- )
- );
- }
- static int GetFormattedLength( LPCSTR pszFormat, va_list args ) throw()
- {
- return _vscprintf( pszFormat, args );
- }
- static int Format( LPSTR pszBuffer, LPCSTR pszFormat, va_list args ) throw()
- {
- return vsprintf( pszBuffer, pszFormat, args );
- }
- static int GetBaseTypeLength( LPCSTR pszSrc ) throw()
- {
- return int( strlen( pszSrc ) );
- }
- static int GetBaseTypeLength( LPCSTR pszSrc, int nLength ) throw()
- {
- (void)pszSrc;
- return nLength;
- }
- static int GetBaseTypeLength( LPCWSTR pszSource ) throw()
- {
- return
- ::WideCharToMultiByte(
- _AtlGetConversionACP(),
- 0,
- pszSource,
- -1,
- NULL,
- 0,
- NULL,
- NULL
- ) - 1;
- }
- static int GetBaseTypeLength( LPCWSTR pszSource, int nLength ) throw()
- {
- return
- ::WideCharToMultiByte(
- _AtlGetConversionACP(),
- 0,
- pszSource,
- nLength,
- NULL,
- 0,
- NULL,
- NULL
- );
- }
- static void ConvertToBaseType(
- LPSTR pszDest,
- int nDestLength,
- LPCSTR pszSrc,
- int nSrcLength = -1
- ) throw()
- {
- (void)nSrcLength;
- __EXT_MFC_MEMCPY(
- pszDest,
- nDestLength * sizeof(char),
- pszSrc,
- nDestLength * sizeof(char)
- );
- }
- static void ConvertToBaseType(
- LPSTR pszDest,
- int nDestLength,
- LPCWSTR pszSrc,
- int nSrcLength = -1
- ) throw()
- {
- ::WideCharToMultiByte(
- _AtlGetConversionACP(),
- 0,
- pszSrc,
- nSrcLength,
- pszDest,
- nDestLength,
- NULL,
- NULL
- );
- }
- static void ConvertToOem( LPSTR psz ) throw()
- {
- ::AnsiToOem( psz, psz );
- }
- static void ConvertToAnsi( LPSTR psz ) throw()
- {
- ::OemToAnsi( psz, psz );
- }
- static void FloodCharacters( char ch, int nLength, char * pch ) throw()
- {
- memset( pch, ch, nLength );
- }
- static BSTR AllocSysString( const char * pchData, int nDataLength ) throw()
- {
- int nLen =
- ::MultiByteToWideChar(
- _AtlGetConversionACP(),
- 0,
- pchData,
- nDataLength,
- NULL,
- NULL
- );
- BSTR bstr = ::SysAllocStringLen( NULL, nLen );
- if( bstr != NULL )
- {
- ::MultiByteToWideChar(
- _AtlGetConversionACP(),
- 0,
- pchData,
- nDataLength,
- bstr,
- nLen
- );
- }
- return bstr;
- }
- static BOOL ReAllocSysString( const char * pchData, BSTR* pbstr, int nDataLength ) throw()
- {
- int nLen =
- ::MultiByteToWideChar(
- _AtlGetConversionACP(),
- 0,
- pchData,
- nDataLength,
- NULL,
- NULL
- );
- BOOL bSuccess = ::SysReAllocStringLen( pbstr, NULL, nLen );
- if( bSuccess )
- {
- ::MultiByteToWideChar(
- _AtlGetConversionACP(),
- 0,
- pchData,
- nDataLength,
- *pbstr,
- nLen
- );
- }
- return bSuccess;
- }
- static DWORD FormatMessage(
- DWORD dwFlags,
- LPCVOID pSource,
- DWORD dwMessageID,
- DWORD dwLanguageID,
- LPSTR pszBuffer,
- DWORD nSize,
- va_list * pArguments
- ) throw()
- {
- return
- ::FormatMessageA(
- dwFlags,
- pSource,
- dwMessageID,
- dwLanguageID,
- pszBuffer,
- nSize,
- pArguments
- );
- }
- static int SafeStringLen( LPCSTR psz ) throw()
- {
- return (psz != NULL) ? int( strlen( psz ) ) : 0;
- }
- static int SafeStringLen( LPCWSTR psz ) throw()
- {
- return (psz != NULL) ? int( wcslen( psz ) ) : 0;
- }
- static int GetCharLen( const __wchar_t * pch ) throw()
- {
- (void)pch;
- return 1;
- }
- static int GetCharLen( const char* pch ) throw()
- {
- return int(
- _mbclen(
- reinterpret_cast < const unsigned char * > ( pch )
- )
- );
- }
- static DWORD GetEnvironmentVariable(
- LPCSTR pszVar,
- LPSTR pszBuffer,
- DWORD dwSize
- ) throw()
- {
- return ::GetEnvironmentVariableA( pszVar, pszBuffer, dwSize );
- }
- }; // class CExtSafeChTraitsCRT
- template<>
- class CExtSafeChTraitsCRT < __wchar_t > : public ChTraitsBase < __wchar_t >
- {
- #if defined(_UNICODE) && !defined(_CSTRING_ALWAYS_THUNK)
- static DWORD _GetEnvironmentVariableW(
- const __wchar_t * pszName,
- __wchar_t * pszBuffer,
- DWORD nSize
- ) throw()
- {
- return
- ::GetEnvironmentVariableW(
- (LPCWSTR)pszName,
- (LPWSTR)pszBuffer,
- nSize
- );
- }
- #else
- static DWORD WINAPI _GetEnvironmentVariableW(
- const __wchar_t * pszName,
- __wchar_t * pszBuffer,
- DWORD nSize
- ) throw()
- {
- return
- _strthunks.pfnGetEnvironmentVariableW(
- (LPCWSTR)pszName,
- (LPWSTR)pszBuffer,
- nSize
- );
- }
- #endif
- public:
- static __wchar_t * CharNext( const __wchar_t * psz ) throw()
- {
- return const_cast < __wchar_t * > ( psz+1 );
- }
- static int IsDigit( __wchar_t ch ) throw()
- {
- return iswdigit( ch );
- }
- static int IsSpace( __wchar_t ch ) throw()
- {
- return iswspace( ch );
- }
- static int StringCompare( const __wchar_t * pszA, const __wchar_t * pszB ) throw()
- {
- return wcscmp( (wchar_t *) pszA, (wchar_t *) pszB );
- }
- static int StringCompareIgnore( const __wchar_t * pszA, const __wchar_t * pszB ) throw()
- {
- return _wcsicmp( (wchar_t *)pszA, (wchar_t *)pszB );
- }
- static int StringCollate( const __wchar_t * pszA, const __wchar_t * pszB ) throw()
- {
- return wcscoll( (wchar_t *)pszA, (wchar_t *)pszB );
- }
- static int StringCollateIgnore( const __wchar_t * pszA, const __wchar_t * pszB ) throw()
- {
- return _wcsicoll( (wchar_t *)pszA, (wchar_t *)pszB );
- }
- static const __wchar_t * StringFindString( const __wchar_t * pszBlock, const __wchar_t * pszMatch ) throw()
- {
- return (__wchar_t *)wcsstr( (wchar_t *)pszBlock, (wchar_t *)pszMatch );
- }
- static __wchar_t * StringFindString( __wchar_t * pszBlock, const __wchar_t * pszMatch ) throw()
- {
- return ( const_cast < __wchar_t * > ( StringFindString( const_cast < const __wchar_t * > ( pszBlock ), pszMatch ) ) );
- }
- static const __wchar_t * StringFindChar( const __wchar_t * pszBlock, __wchar_t chMatch ) throw()
- {
- return (__wchar_t *)wcschr( (wchar_t *)pszBlock, (wchar_t)chMatch );
- }
- static const __wchar_t * StringFindCharRev( const __wchar_t * psz, __wchar_t ch ) throw()
- {
- return (__wchar_t *)wcsrchr( (wchar_t *)psz, (wchar_t)ch );
- }
- static const __wchar_t * StringScanSet( const __wchar_t * pszBlock, const __wchar_t * pszMatch ) throw()
- {
- return (__wchar_t *)wcspbrk( (wchar_t *)pszBlock, (wchar_t *)pszMatch );
- }
- static int StringSpanIncluding( const __wchar_t * pszBlock, const __wchar_t * pszSet ) throw()
- {
- return (int)wcsspn( (wchar_t *)pszBlock, (wchar_t *)pszSet );
- }
- static int StringSpanExcluding( const __wchar_t * pszBlock, const __wchar_t * pszSet ) throw()
- {
- return (int)wcscspn( (wchar_t *)pszBlock, (wchar_t *)pszSet );
- }
- static __wchar_t * StringUppercase( __wchar_t * psz ) throw()
- {
- return (__wchar_t *)_wcsupr( (wchar_t *)psz );
- }
- static __wchar_t * StringLowercase( __wchar_t * psz ) throw()
- {
- return (__wchar_t *)_wcslwr( (wchar_t *)psz );
- }
- static __wchar_t * StringReverse( __wchar_t * psz ) throw()
- {
- return (__wchar_t *)_wcsrev( (wchar_t *)psz );
- }
- static int GetFormattedLength( const __wchar_t * pszFormat, va_list args) throw()
- {
- return _vscwprintf( (wchar_t *)pszFormat, args );
- }
- static int Format( __wchar_t * pszBuffer, const __wchar_t * pszFormat, va_list args) throw()
- {
- return vswprintf( (wchar_t *)pszBuffer, (wchar_t *)pszFormat, args );
- }
- static int GetBaseTypeLength( LPCSTR pszSrc ) throw()
- {
- return
- ::MultiByteToWideChar(
- _AtlGetConversionACP(),
- 0,
- pszSrc,
- -1,
- NULL,
- 0
- ) - 1;
- }
- static int GetBaseTypeLength( LPCSTR pszSrc, int nLength ) throw()
- {
- return
- ::MultiByteToWideChar(
- _AtlGetConversionACP(),
- 0,
- pszSrc,
- nLength,
- NULL,
- 0
- );
- }
- static int GetBaseTypeLength( const __wchar_t * pszSrc ) throw()
- {
- return (int)wcslen( (wchar_t *)pszSrc );
- }
- static int GetBaseTypeLength( const __wchar_t * pszSrc, int nLength ) throw()
- {
- (void)pszSrc;
- return nLength;
- }
- static void ConvertToBaseType(
- __wchar_t * pszDest,
- int nDestLength,
- LPCSTR pszSrc,
- int nSrcLength = -1
- ) throw()
- {
- ::MultiByteToWideChar(
- _AtlGetConversionACP(),
- 0,
- pszSrc,
- nSrcLength,
- (LPWSTR)pszDest,
- nDestLength
- );
- }
- static void ConvertToBaseType(
- __wchar_t * pszDest,
- int nDestLength,
- const __wchar_t * pszSrc,
- int nSrcLength = -1
- ) throw()
- {
- (void)nSrcLength;
- __EXT_MFC_MEMCPY(
- pszDest,
- nDestLength * sizeof(__wchar_t),
- pszSrc,
- nDestLength * sizeof(__wchar_t)
- );
- }
- static void FloodCharacters( __wchar_t ch, int nLength, __wchar_t * psz ) throw()
- {
- for( int i = 0; i < nLength; i++ )
- {
- psz[i] = ch;
- }
- }
- static BSTR AllocSysString( const __wchar_t * pchData, int nDataLength ) throw()
- {
- return ::SysAllocStringLen( (const OLECHAR *)pchData, nDataLength );
- }
- static BOOL ReAllocSysString( const __wchar_t * pchData, BSTR* pbstr, int nDataLength ) throw()
- {
- return ::SysReAllocStringLen( pbstr, (const OLECHAR *)pchData, nDataLength );
- }
- #ifdef _UNICODE
- static DWORD FormatMessage(
- DWORD dwFlags,
- LPCVOID pSource,
- DWORD dwMessageID,
- DWORD dwLanguageID,
- __wchar_t * pszBuffer,
- DWORD nSize,
- va_list * pArguments
- ) throw()
- {
- return
- ::FormatMessageW(
- dwFlags,
- pSource,
- dwMessageID,
- dwLanguageID,
- (LPWSTR)pszBuffer,
- nSize,
- pArguments
- );
- }
- #endif
- static int SafeStringLen( LPCSTR psz ) throw()
- {
- return (psz != NULL) ? (int)strlen( psz ) : 0;
- }
- static int SafeStringLen( const __wchar_t * psz ) throw()
- {
- return (psz != NULL) ? (int)wcslen( (wchar_t *)psz ) : 0;
- }
- static int GetCharLen( const __wchar_t * pch ) throw()
- {
- (void)pch;
- return 1;
- }
- static int GetCharLen( const char * pch ) throw()
- {
- return (int)(
- _mbclen(
- reinterpret_cast < const unsigned char * > ( pch )
- )
- );
- }
- static DWORD GetEnvironmentVariable( const __wchar_t * pszVar, __wchar_t * pszBuffer, DWORD dwSize ) throw()
- {
- return _GetEnvironmentVariableW( pszVar, pszBuffer, dwSize );
- }
- }; // class CExtSafeChTraitsCRT
- template<>
- class CExtSafeChTraitsCRT < unsigned short > : public ChTraitsBase < unsigned short >
- {
- #if defined(_UNICODE) && !defined(_CSTRING_ALWAYS_THUNK)
- static DWORD _GetEnvironmentVariableW(
- const unsigned short * pszName,
- unsigned short * pszBuffer,
- DWORD nSize
- ) throw()
- {
- return
- ::GetEnvironmentVariableW(
- (LPCWSTR)pszName,
- (LPWSTR)pszBuffer,
- nSize
- );
- }
- #else
- static DWORD WINAPI _GetEnvironmentVariableW(
- const unsigned short * pszName,
- unsigned short * pszBuffer,
- DWORD nSize
- ) throw()
- {
- return
- _strthunks.pfnGetEnvironmentVariableW(
- (LPCWSTR)pszName,
- (LPWSTR)pszBuffer,
- nSize
- );
- }
- #endif
- public:
- static unsigned short * CharNext( const unsigned short * psz ) throw()
- {
- return const_cast < unsigned short * > ( psz+1 );
- }
- static int IsDigit( unsigned short ch ) throw()
- {
- return iswdigit( ch );
- }
- static int IsSpace( unsigned short ch ) throw()
- {
- return iswspace( ch );
- }
- static int StringCompare( const unsigned short * pszA, const unsigned short * pszB ) throw()
- {
- return wcscmp( (wchar_t *)pszA, (wchar_t *)pszB );
- }
- static int StringCompareIgnore( const unsigned short * pszA, const unsigned short * pszB ) throw()
- {
- return _wcsicmp( (wchar_t *)pszA, (wchar_t *)pszB );
- }
- static int StringCollate( const unsigned short * pszA, const unsigned short * pszB ) throw()
- {
- return wcscoll( (wchar_t *)pszA, (wchar_t *)pszB );
- }
- static int StringCollateIgnore( const unsigned short * pszA, const unsigned short * pszB ) throw()
- {
- return _wcsicoll( (wchar_t *)pszA, (wchar_t *)pszB );
- }
- static const unsigned short * StringFindString( const unsigned short * pszBlock, const unsigned short * pszMatch ) throw()
- {
- return (unsigned short *)wcsstr( (wchar_t *)pszBlock, (wchar_t *)pszMatch );
- }
- static unsigned short * StringFindString( unsigned short * pszBlock, const unsigned short * pszMatch ) throw()
- {
- return ( const_cast < unsigned short * > ( StringFindString( const_cast < const unsigned short * > ( pszBlock ), pszMatch ) ) );
- }
- static const unsigned short * StringFindChar( const unsigned short * pszBlock, unsigned short chMatch ) throw()
- {
- return (unsigned short *)wcschr( (wchar_t *)pszBlock, (wchar_t)chMatch );
- }
- static const unsigned short * StringFindCharRev( const unsigned short * psz, unsigned short ch ) throw()
- {
- return (unsigned short *)wcsrchr( (wchar_t *)psz, (wchar_t)ch );
- }
- static const unsigned short * StringScanSet( const unsigned short * pszBlock, const unsigned short * pszMatch ) throw()
- {
- return (unsigned short *)wcspbrk( (wchar_t *)pszBlock, (wchar_t *)pszMatch );
- }
- static int StringSpanIncluding( const unsigned short * pszBlock, const unsigned short * pszSet ) throw()
- {
- return (int)wcsspn( (wchar_t *)pszBlock, (wchar_t *)pszSet );
- }
- static int StringSpanExcluding( const unsigned short * pszBlock, const unsigned short * pszSet ) throw()
- {
- return (int)wcscspn( (wchar_t *)pszBlock, (wchar_t *)pszSet );
- }
- static unsigned short * StringUppercase( unsigned short * psz ) throw()
- {
- return (unsigned short *)_wcsupr( (wchar_t *)psz );
- }
- static unsigned short * StringLowercase( unsigned short * psz ) throw()
- {
- return (unsigned short *)_wcslwr( (wchar_t *)psz );
- }
- static unsigned short * StringReverse( unsigned short * psz ) throw()
- {
- return (unsigned short *)_wcsrev( (wchar_t *)psz );
- }
- static int GetFormattedLength( const unsigned short * pszFormat, va_list args) throw()
- {
- return _vscwprintf( (wchar_t *)pszFormat, args );
- }
- static int Format( unsigned short * pszBuffer, const unsigned short * pszFormat, va_list args) throw()
- {
- return vswprintf( (wchar_t *)pszBuffer, (wchar_t *)pszFormat, args );
- }
- static int GetBaseTypeLength( LPCSTR pszSrc ) throw()
- {
- return
- ::MultiByteToWideChar(
- _AtlGetConversionACP(),
- 0,
- pszSrc,
- -1,
- NULL,
- 0
- ) - 1;
- }
- static int GetBaseTypeLength( LPCSTR pszSrc, int nLength ) throw()
- {
- return
- ::MultiByteToWideChar(
- _AtlGetConversionACP(),
- 0,
- pszSrc,
- nLength,
- NULL,
- 0
- );
- }
- static int GetBaseTypeLength( const unsigned short * pszSrc ) throw()
- {
- return (int)wcslen( (wchar_t *)pszSrc );
- }
- static int GetBaseTypeLength( const unsigned short * pszSrc, int nLength ) throw()
- {
- (void)pszSrc;
- return nLength;
- }
- static void ConvertToBaseType(
- unsigned short * pszDest,
- int nDestLength,
- LPCSTR pszSrc,
- int nSrcLength = -1
- ) throw()
- {
- ::MultiByteToWideChar(
- _AtlGetConversionACP(),
- 0,
- pszSrc,
- nSrcLength,
- (LPWSTR)pszDest,
- nDestLength
- );
- }
- static void ConvertToBaseType(
- unsigned short * pszDest,
- int nDestLength,
- const unsigned short * pszSrc,
- int nSrcLength = -1
- ) throw()
- {
- (void)nSrcLength;
- __EXT_MFC_MEMCPY(
- pszDest,
- nDestLength * sizeof(unsigned short),
- pszSrc,
- nDestLength * sizeof(unsigned short)
- );
- }
- static void FloodCharacters( unsigned short ch, int nLength, unsigned short * psz ) throw()
- {
- for( int i = 0; i < nLength; i++ )
- {
- psz[i] = ch;
- }
- }
- static BSTR AllocSysString( const unsigned short* pchData, int nDataLength ) throw()
- {
- return ::SysAllocStringLen( (const OLECHAR *)pchData, nDataLength );
- }
- static BOOL ReAllocSysString( const unsigned short* pchData, BSTR* pbstr, int nDataLength ) throw()
- {
- return ::SysReAllocStringLen( pbstr, (const OLECHAR *)pchData, nDataLength );
- }
- #ifdef _UNICODE
- static DWORD FormatMessage(
- DWORD dwFlags,
- LPCVOID pSource,
- DWORD dwMessageID,
- DWORD dwLanguageID,
- unsigned short * pszBuffer,
- DWORD nSize,
- va_list * pArguments
- ) throw()
- {
- return
- ::FormatMessageW(
- dwFlags,
- pSource,
- dwMessageID,
- dwLanguageID,
- (LPWSTR)pszBuffer,
- nSize,
- pArguments
- );
- }
- #endif
- static int SafeStringLen( LPCSTR psz ) throw()
- {
- return (psz != NULL) ? (int)strlen( psz ) : 0;
- }
- static int SafeStringLen( const unsigned short * psz ) throw()
- {
- return (psz != NULL) ? (int)wcslen( (wchar_t *)psz ) : 0;
- }
- static int GetCharLen( const unsigned short * pch ) throw()
- {
- (void)pch;
- return 1;
- }
- static int GetCharLen( const char * pch ) throw()
- {
- return (int)(
- _mbclen(
- reinterpret_cast < const unsigned char * > ( pch )
- )
- );
- }
- static DWORD GetEnvironmentVariable(
- const unsigned short * pszVar,
- unsigned short * pszBuffer,
- DWORD dwSize
- ) throw()
- {
- return _GetEnvironmentVariableW( pszVar, pszBuffer, dwSize );
- }
- }; // class CExtSafeChTraitsCRT
- template <
- typename _CharType = char,
- class StringIterator = CExtSafeChTraitsCRT < char >
- >
- class CExtSafeStrTraitMFC : public StringIterator
- {
- public:
- static HINSTANCE FindStringResourceInstance( UINT nID ) throw()
- {
- return ( AfxFindStringResourceHandle( nID ) );
- }
- static ATL::IAtlStringMgr * GetDefaultManager() throw()
- {
- return ( AfxGetStringManager() );
- }
- static char* CharNext( const char * p ) throw()
- {
- return reinterpret_cast < char* > ( _mbsinc( reinterpret_cast < const unsigned char* > ( p ) ) );
- }
- static int IsDigit( char ch ) throw()
- {
- return _ismbcdigit( ch );
- }
- static int IsSpace( char ch ) throw()
- {
- return _ismbcspace( ch );
- }
- static int StringCompare( LPCSTR pszA, LPCSTR pszB ) throw()
- {
- return
- _mbscmp(
- reinterpret_cast < const unsigned char * > ( pszA ),
- reinterpret_cast < const unsigned char * > ( pszB )
- );
- }
- static int StringCompareIgnore( LPCSTR pszA, LPCSTR pszB ) throw()
- {
- return
- _mbsicmp(
- reinterpret_cast < const unsigned char * > ( pszA ),
- reinterpret_cast < const unsigned char * > ( pszB )
- );
- }
- static int StringCollate( LPCSTR pszA, LPCSTR pszB ) throw()
- {
- return
- _mbscoll(
- reinterpret_cast < const unsigned char * > ( pszA ),
- reinterpret_cast < const unsigned char * > ( pszB )
- );
- }
- static int StringCollateIgnore( LPCSTR pszA, LPCSTR pszB ) throw()
- {
- return
- _mbsicoll(
- reinterpret_cast < const unsigned char * > ( pszA ),
- reinterpret_cast < const unsigned char * > ( pszB )
- );
- }
- static LPCSTR StringFindString( LPCSTR pszBlock, LPCSTR pszMatch ) throw()
- {
- return reinterpret_cast < LPCSTR > (
- _mbsstr(
- reinterpret_cast < const unsigned char * > ( pszBlock ),
- reinterpret_cast < const unsigned char * > ( pszMatch )
- )
- );
- }
- static LPSTR StringFindString( LPSTR pszBlock, LPCSTR pszMatch ) throw()
- {
- return ( const_cast < LPSTR >
- (
- StringFindString(
- const_cast < LPCSTR > ( pszBlock ),
- pszMatch
- )
- )
- );
- }
- static LPCSTR StringFindChar( LPCSTR pszBlock, char chMatch ) throw()
- {
- return reinterpret_cast < LPCSTR > (
- _mbschr(
- reinterpret_cast < const unsigned char* > ( pszBlock ),
- chMatch
- )
- );
- }
- static LPCSTR StringFindCharRev( LPCSTR psz, char ch ) throw()
- {
- return reinterpret_cast < LPSTR > (
- _mbsrchr(
- reinterpret_cast < const unsigned char* > ( psz ),
- ch
- )
- );
- }
- static LPCSTR StringScanSet( LPCSTR pszBlock, LPCSTR pszMatch ) throw()
- {
- return reinterpret_cast < LPCSTR > (
- _mbspbrk(
- reinterpret_cast < const unsigned char * > ( pszBlock ),
- reinterpret_cast < const unsigned char * > ( pszMatch )
- )
- );
- }
- static int StringSpanIncluding( LPCSTR pszBlock, LPCSTR pszSet ) throw()
- {
- return
- (int) _mbsspn(
- reinterpret_cast < const unsigned char * > ( pszBlock ),
- reinterpret_cast < const unsigned char * > ( pszSet )
- );
- }
- static int StringSpanExcluding( LPCSTR pszBlock, LPCSTR pszSet ) throw()
- {
- return
- (int) _mbscspn(
- reinterpret_cast < const unsigned char * > ( pszBlock ),
- reinterpret_cast < const unsigned char * > ( pszSet )
- );
- }
- static LPSTR StringUppercase( LPSTR psz ) throw()
- {
- return reinterpret_cast < LPSTR > (
- _mbsupr(
- reinterpret_cast < unsigned char * > ( psz )
- )
- );
- }
- static LPSTR StringLowercase( LPSTR psz ) throw()
- {
- return reinterpret_cast < LPSTR > (
- _mbslwr(
- reinterpret_cast < unsigned char * > ( psz )
- )
- );
- }
- static LPSTR StringReverse( LPSTR psz ) throw()
- {
- return reinterpret_cast < LPSTR > (
- _mbsrev(
- reinterpret_cast < unsigned char * > ( psz )
- )
- );
- }
- static int GetFormattedLength( LPCSTR pszFormat, va_list args ) throw()
- {
- return _vscprintf( pszFormat, args );
- }
- static int Format( LPSTR pszBuffer, LPCSTR pszFormat, va_list args ) throw()
- {
- return vsprintf( pszBuffer, pszFormat, args );
- }
- static int GetBaseTypeLength( LPCSTR pszSrc ) throw()
- {
- return int( strlen( pszSrc ) );
- }
- static int GetBaseTypeLength( LPCSTR pszSrc, int nLength ) throw()
- {
- (void)pszSrc;
- return nLength;
- }
- static int GetBaseTypeLength( LPCWSTR pszSource ) throw()
- {
- return ::WideCharToMultiByte( _AtlGetConversionACP(), 0, pszSource, -1, NULL, 0, NULL, NULL )-1;
- }
- static int GetBaseTypeLength( LPCWSTR pszSource, int nLength ) throw()
- {
- return
- ::WideCharToMultiByte(
- _AtlGetConversionACP(),
- 0,
- pszSource,
- nLength,
- NULL,
- 0,
- NULL,
- NULL
- );
- }
- static void
- ConvertToBaseType(
- LPSTR pszDest,
- int nDestLength,
- LPCSTR pszSrc,
- int nSrcLength = -1
- ) throw()
- {
- (void)nSrcLength;
- __EXT_MFC_MEMCPY(
- pszDest,
- nDestLength * sizeof(char),
- pszSrc,
- nDestLength * sizeof(char)
- );
- }
- static void ConvertToBaseType(
- LPSTR pszDest,
- int nDestLength,
- LPCWSTR pszSrc,
- int nSrcLength = -1
- ) throw()
- {
- ::WideCharToMultiByte(
- _AtlGetConversionACP(),
- 0,
- pszSrc,
- nSrcLength,
- pszDest,
- nDestLength,
- NULL,
- NULL
- );
- }
- static void ConvertToOem( LPSTR psz ) throw()
- {
- ::AnsiToOem( psz, psz );
- }
- static void ConvertToAnsi( LPSTR psz ) throw()
- {
- ::OemToAnsi( psz, psz );
- }
- static void FloodCharacters( char ch, int nLength, char* pch ) throw()
- {
- memset( pch, ch, nLength );
- }
- static BSTR AllocSysString( const char* pchData, int nDataLength ) throw()
- {
- int nLen =
- ::MultiByteToWideChar(
- _AtlGetConversionACP(),
- 0,
- pchData,
- nDataLength,
- NULL,
- NULL
- );
- BSTR bstr = ::SysAllocStringLen( NULL, nLen );
- if( bstr != NULL )
- {
- ::MultiByteToWideChar(
- _AtlGetConversionACP(),
- 0,
- pchData,
- nDataLength,
- bstr,
- nLen
- );
- }
- return bstr;
- }
- static BOOL ReAllocSysString( const char* pchData, BSTR* pbstr, int nDataLength ) throw()
- {
- int nLen =
- ::MultiByteToWideChar(
- _AtlGetConversionACP(),
- 0,
- pchData,
- nDataLength,
- NULL,
- NULL
- );
- BOOL bSuccess = ::SysReAllocStringLen( pbstr, NULL, nLen );
- if( bSuccess )
- {
- ::MultiByteToWideChar(
- _AtlGetConversionACP(),
- 0,
- pchData,
- nDataLength,
- *pbstr,
- nLen
- );
- }
- return bSuccess;
- }
- static DWORD FormatMessage(
- DWORD dwFlags,
- LPCVOID pSource,
- DWORD dwMessageID,
- DWORD dwLanguageID,
- LPSTR pszBuffer,
- DWORD nSize,
- va_list * pArguments
- ) throw()
- {
- return
- ::FormatMessageA(
- dwFlags,
- pSource,
- dwMessageID,
- dwLanguageID,
- pszBuffer,
- nSize,
- pArguments
- );
- }
- static int SafeStringLen( LPCSTR psz ) throw()
- {
- return (psz != NULL) ? int( strlen( psz ) ) : 0;
- }
- static int SafeStringLen( LPCWSTR psz ) throw()
- {
- return (psz != NULL) ? int( wcslen( psz ) ) : 0;
- }
- static int GetCharLen( const __wchar_t* pch ) throw()
- {
- (void)pch;
- return 1;
- }
- static int GetCharLen( const char* pch ) throw()
- {
- return int(
- _mbclen(
- reinterpret_cast < const unsigned char * > ( pch )
- )
- );
- }
- static DWORD GetEnvironmentVariable(
- LPCSTR pszVar,
- LPSTR pszBuffer,
- DWORD dwSize
- ) throw()
- {
- return ::GetEnvironmentVariableA( pszVar, pszBuffer, dwSize );
- }
- }; // class CExtSafeStrTraitMFC
- template<>
- class CExtSafeStrTraitMFC<
- __wchar_t,
- CExtSafeChTraitsCRT < __wchar_t >
- >
- {
- #if defined(_UNICODE) && !defined(_CSTRING_ALWAYS_THUNK)
- static DWORD _GetEnvironmentVariableW(
- const __wchar_t * pszName,
- __wchar_t * pszBuffer,
- DWORD nSize
- ) throw()
- {
- return
- ::GetEnvironmentVariableW(
- (LPCWSTR)pszName,
- (LPWSTR)pszBuffer,
- nSize
- );
- }
- #else
- static DWORD WINAPI _GetEnvironmentVariableW(
- const __wchar_t * pszName,
- __wchar_t * pszBuffer,
- DWORD nSize
- ) throw()
- {
- return
- _strthunks.pfnGetEnvironmentVariableW(
- (LPCWSTR)pszName,
- (LPWSTR)pszBuffer,
- nSize
- );
- }
- #endif
- public:
- static HINSTANCE FindStringResourceInstance( UINT nID ) throw()
- {
- return ( AfxFindStringResourceHandle( nID ) );
- }
- static ATL::IAtlStringMgr * GetDefaultManager() throw()
- {
- return ( AfxGetStringManager() );
- }
- static __wchar_t * CharNext( const __wchar_t * psz ) throw()
- {
- return const_cast < __wchar_t * > ( psz+1 );
- }
- static int IsDigit( __wchar_t ch ) throw()
- {
- return iswdigit( ch );
- }
- static int IsSpace( __wchar_t ch ) throw()
- {
- return iswspace( ch );
- }
- static int StringCompare( const __wchar_t * pszA, const __wchar_t * pszB ) throw()
- {
- return wcscmp( (wchar_t *)pszA, (wchar_t *)pszB );
- }
- static int StringCompareIgnore( const __wchar_t * pszA, const __wchar_t * pszB ) throw()
- {
- return _wcsicmp( (wchar_t *)pszA, (wchar_t *)pszB );
- }
- static int StringCollate( const __wchar_t * pszA, const __wchar_t * pszB ) throw()
- {
- return wcscoll( (wchar_t *)pszA, (wchar_t *)pszB );
- }
- static int StringCollateIgnore( const __wchar_t * pszA, const __wchar_t * pszB ) throw()
- {
- return _wcsicoll( (wchar_t *)pszA, (wchar_t *)pszB );
- }
- static const __wchar_t * StringFindString( const __wchar_t * pszBlock, const __wchar_t * pszMatch ) throw()
- {
- return (__wchar_t *)wcsstr( (wchar_t *)pszBlock, (wchar_t *)pszMatch );
- }
- static __wchar_t * StringFindString( __wchar_t * pszBlock, const __wchar_t * pszMatch ) throw()
- {
- return ( const_cast < __wchar_t * > ( StringFindString( const_cast < const __wchar_t * > ( pszBlock ), pszMatch ) ) );
- }
- static const __wchar_t * StringFindChar( const __wchar_t * pszBlock, __wchar_t chMatch ) throw()
- {
- return (__wchar_t *)wcschr( (wchar_t *)pszBlock, (wchar_t)chMatch );
- }
- static const __wchar_t * StringFindCharRev( const __wchar_t * psz, __wchar_t ch ) throw()
- {
- return (__wchar_t *)wcsrchr( (wchar_t *)psz, (wchar_t)ch );
- }
- static const __wchar_t * StringScanSet( const __wchar_t * pszBlock, const __wchar_t * pszMatch ) throw()
- {
- return (__wchar_t *)wcspbrk( (wchar_t *)pszBlock, (wchar_t *)pszMatch );
- }
- static int StringSpanIncluding( const __wchar_t * pszBlock, const __wchar_t * pszSet ) throw()
- {
- return (int)wcsspn( (wchar_t *)pszBlock, (wchar_t *)pszSet );
- }
- static int StringSpanExcluding( const __wchar_t * pszBlock, const __wchar_t * pszSet ) throw()
- {
- return (int)wcscspn( (wchar_t *)pszBlock, (wchar_t *)pszSet );
- }
- static __wchar_t * StringUppercase( __wchar_t * psz ) throw()
- {
- return (__wchar_t *)_wcsupr( (wchar_t *)psz );
- }
- static __wchar_t * StringLowercase( __wchar_t * psz ) throw()
- {
- return (__wchar_t *)_wcslwr( (wchar_t *)psz );
- }
- static __wchar_t * StringReverse( __wchar_t * psz ) throw()
- {
- return (__wchar_t *)_wcsrev( (wchar_t *)psz );
- }
- static int GetFormattedLength( const __wchar_t * pszFormat, va_list args) throw()
- {
- return _vscwprintf( (wchar_t *)pszFormat, args );
- }
- static int Format( __wchar_t * pszBuffer, const __wchar_t * pszFormat, va_list args) throw()
- {
- return vswprintf( (wchar_t *)pszBuffer, (wchar_t *)pszFormat, args );
- }
- static int GetBaseTypeLength( LPCSTR pszSrc ) throw()
- {
- return
- ::MultiByteToWideChar(
- _AtlGetConversionACP(),
- 0,
- pszSrc,
- -1,
- NULL,
- 0
- ) - 1;
- }
- static int GetBaseTypeLength( LPCSTR pszSrc, int nLength ) throw()
- {
- return
- ::MultiByteToWideChar(
- _AtlGetConversionACP(),
- 0,
- pszSrc,
- nLength,
- NULL,
- 0
- );
- }
- static int GetBaseTypeLength( const __wchar_t * pszSrc ) throw()
- {
- return (int)wcslen( (wchar_t *)pszSrc );
- }
- static int GetBaseTypeLength( const __wchar_t * pszSrc, int nLength ) throw()
- {
- (void)pszSrc;
- return nLength;
- }
- static void ConvertToBaseType(
- __wchar_t * pszDest,
- int nDestLength,
- LPCSTR pszSrc,
- int nSrcLength = -1
- ) throw()
- {
- ::MultiByteToWideChar(
- _AtlGetConversionACP(),
- 0,
- pszSrc,
- nSrcLength,
- (LPWSTR)pszDest,
- nDestLength
- );
- }
- static void ConvertToBaseType(
- __wchar_t * pszDest,
- int nDestLength,
- const __wchar_t * pszSrc,
- int nSrcLength = -1
- ) throw()
- {
- (void)nSrcLength;
- __EXT_MFC_MEMCPY(
- pszDest,
- nDestLength * sizeof(__wchar_t),
- pszSrc,
- nDestLength * sizeof(__wchar_t)
- );
- }
- static void FloodCharacters( __wchar_t ch, int nLength, __wchar_t * psz ) throw()
- {
- for( int i = 0; i < nLength; i++ )
- {
- psz[i] = ch;
- }
- }
- static BSTR AllocSysString( const __wchar_t * pchData, int nDataLength ) throw()
- {
- return ::SysAllocStringLen( (const OLECHAR *)pchData, nDataLength );
- }
- static BOOL ReAllocSysString( const __wchar_t * pchData, BSTR* pbstr, int nDataLength ) throw()
- {
- return ::SysReAllocStringLen( pbstr, (const OLECHAR *)pchData, nDataLength );
- }
- #ifdef _UNICODE
- static DWORD FormatMessage(
- DWORD dwFlags,
- LPCVOID pSource,
- DWORD dwMessageID,
- DWORD dwLanguageID,
- __wchar_t * pszBuffer,
- DWORD nSize,
- va_list * pArguments
- ) throw()
- {
- return
- ::FormatMessageW(
- dwFlags,
- pSource,
- dwMessageID,
- dwLanguageID,
- (LPWSTR)pszBuffer,
- nSize,
- pArguments
- );
- }
- #endif
- static int SafeStringLen( LPCSTR psz ) throw()
- {
- return (psz != NULL) ? (int)strlen( psz ) : 0;
- }
- static int SafeStringLen( const __wchar_t * psz ) throw()
- {
- return (psz != NULL) ? (int)wcslen((wchar_t *) psz ) : 0;
- }
- static int GetCharLen( const __wchar_t* pch ) throw()
- {
- (void)pch;
- return 1;
- }
- static int GetCharLen( const char* pch ) throw()
- {
- return (int)(
- _mbclen(
- reinterpret_cast < const unsigned char * > ( pch )
- )
- );
- }
- static DWORD GetEnvironmentVariable(
- const __wchar_t * pszVar,
- __wchar_t * pszBuffer,
- DWORD dwSize
- ) throw()
- {
- return _GetEnvironmentVariableW( pszVar, pszBuffer, dwSize );
- }
- }; // class CExtSafeStrTraitMFC
- template<>
- class CExtSafeStrTraitMFC<
- unsigned short,
- CExtSafeChTraitsCRT < unsigned short >
- >
- {
- #if defined(_UNICODE) && !defined(_CSTRING_ALWAYS_THUNK)
- static DWORD _GetEnvironmentVariableW(
- const unsigned short * pszName,
- unsigned short * pszBuffer,
- DWORD nSize
- ) throw()
- {
- return
- ::GetEnvironmentVariableW(
- (LPCWSTR)pszName,
- (LPWSTR)pszBuffer,
- nSize
- );
- }
- #else
- static DWORD WINAPI _GetEnvironmentVariableW(
- const unsigned short * pszName,
- unsigned short * pszBuffer,
- DWORD nSize
- ) throw()
- {
- return
- _strthunks.pfnGetEnvironmentVariableW(
- (LPCWSTR)pszName,
- (LPWSTR)pszBuffer,
- nSize
- );
- }
- #endif
- public:
- static HINSTANCE FindStringResourceInstance( UINT nID ) throw()
- {
- return ( AfxFindStringResourceHandle( nID ) );
- }
- static ATL::IAtlStringMgr * GetDefaultManager() throw()
- {
- return ( AfxGetStringManager() );
- }
- static unsigned short * CharNext( const unsigned short * psz ) throw()
- {
- return const_cast < unsigned short * > ( psz+1 );
- }
- static int IsDigit( unsigned short ch ) throw()
- {
- return iswdigit( ch );
- }
- static int IsSpace( unsigned short ch ) throw()
- {
- return iswspace( ch );
- }
- static int StringCompare( const unsigned short * pszA, const unsigned short * pszB ) throw()
- {
- return wcscmp( (wchar_t *)pszA, (wchar_t *)pszB );
- }
- static int StringCompareIgnore( const unsigned short * pszA, const unsigned short * pszB ) throw()
- {
- return _wcsicmp( (wchar_t *)pszA, (wchar_t *)pszB );
- }
- static int StringCollate( const unsigned short * pszA, const unsigned short * pszB ) throw()
- {
- return wcscoll( (wchar_t *)pszA, (wchar_t *)pszB );
- }
- static int StringCollateIgnore( const unsigned short * pszA, const unsigned short * pszB ) throw()
- {
- return _wcsicoll( (wchar_t *)pszA, (wchar_t *)pszB );
- }
- static const unsigned short * StringFindString( const unsigned short * pszBlock, const unsigned short * pszMatch ) throw()
- {
- return (unsigned short *)wcsstr( (wchar_t *)pszBlock, (wchar_t *)pszMatch );
- }
- static unsigned short * StringFindString( unsigned short * pszBlock, const unsigned short * pszMatch ) throw()
- {
- return ( const_cast < unsigned short * > ( StringFindString( const_cast < const unsigned short * > ( pszBlock ), pszMatch ) ) );
- }
- static const unsigned short * StringFindChar( const unsigned short * pszBlock, unsigned short chMatch ) throw()
- {
- return (unsigned short *)wcschr( (wchar_t *)pszBlock, (wchar_t)chMatch );
- }
- static const unsigned short * StringFindCharRev( const unsigned short * psz, unsigned short ch ) throw()
- {
- return (unsigned short *)wcsrchr( (wchar_t *)psz, (wchar_t)ch );
- }
- static const unsigned short * StringScanSet( const unsigned short * pszBlock, const unsigned short * pszMatch ) throw()
- {
- return (unsigned short *)wcspbrk( (wchar_t *)pszBlock, (wchar_t *)pszMatch );
- }
- static int StringSpanIncluding( const unsigned short * pszBlock, const unsigned short * pszSet ) throw()
- {
- return (int)wcsspn( (wchar_t *)pszBlock, (wchar_t *)pszSet );
- }
- static int StringSpanExcluding( const unsigned short * pszBlock, const unsigned short * pszSet ) throw()
- {
- return (int)wcscspn( (wchar_t *)pszBlock, (wchar_t *)pszSet );
- }
- static unsigned short * StringUppercase( unsigned short * psz ) throw()
- {
- return (unsigned short *)_wcsupr( (wchar_t *)psz );
- }
- static unsigned short * StringLowercase( unsigned short * psz ) throw()
- {
- return (unsigned short *)_wcslwr( (wchar_t *)psz );
- }
- static unsigned short * StringReverse( unsigned short * psz ) throw()
- {
- return (unsigned short *)_wcsrev( (wchar_t *)psz );
- }
- static int GetFormattedLength( const unsigned short * pszFormat, va_list args) throw()
- {
- return _vscwprintf( (wchar_t *)pszFormat, args );
- }
- static int Format( unsigned short * pszBuffer, const unsigned short * pszFormat, va_list args) throw()
- {
- return vswprintf( (wchar_t *)pszBuffer, (wchar_t *)pszFormat, args );
- }
- static int GetBaseTypeLength( LPCSTR pszSrc ) throw()
- {
- return
- ::MultiByteToWideChar(
- _AtlGetConversionACP(),
- 0,
- pszSrc,
- -1,
- NULL,
- 0
- ) - 1;
- }
- static int GetBaseTypeLength( LPCSTR pszSrc, int nLength ) throw()
- {
- return
- ::MultiByteToWideChar(
- _AtlGetConversionACP(),
- 0,
- pszSrc,
- nLength,
- NULL,
- 0
- );
- }
- static int GetBaseTypeLength( const unsigned short * pszSrc ) throw()
- {
- return (int)wcslen( (wchar_t *)pszSrc );
- }
- static int GetBaseTypeLength( const unsigned short * pszSrc, int nLength ) throw()
- {
- (void)pszSrc;
- return nLength;
- }
- static void ConvertToBaseType(
- unsigned short * pszDest,
- int nDestLength,
- LPCSTR pszSrc,
- int nSrcLength = -1
- ) throw()
- {
- ::MultiByteToWideChar(
- _AtlGetConversionACP(),
- 0,
- pszSrc,
- nSrcLength,
- (LPWSTR)pszDest,
- nDestLength
- );
- }
- static void ConvertToBaseType(
- unsigned short * pszDest,
- int nDestLength,
- const unsigned short * pszSrc,
- int nSrcLength = -1
- ) throw()
- {
- (void)nSrcLength;
- __EXT_MFC_MEMCPY(
- pszDest,
- nDestLength * sizeof(unsigned short),
- pszSrc,
- nDestLength * sizeof(unsigned short)
- );
- }
- static void FloodCharacters( unsigned short ch, int nLength, unsigned short * psz ) throw()
- {
- for( int i = 0; i < nLength; i++ )
- {
- psz[i] = ch;
- }
- }
- static BSTR AllocSysString( const unsigned short * pchData, int nDataLength ) throw()
- {
- return ::SysAllocStringLen( (const OLECHAR *)pchData, nDataLength );
- }
- static BOOL ReAllocSysString( const unsigned short * pchData, BSTR * pbstr, int nDataLength ) throw()
- {
- return ::SysReAllocStringLen( pbstr, (const OLECHAR *)pchData, nDataLength );
- }
- #ifdef _UNICODE
- static DWORD FormatMessage(
- DWORD dwFlags,
- LPCVOID pSource,
- DWORD dwMessageID,
- DWORD dwLanguageID,
- unsigned short * pszBuffer,
- DWORD nSize,
- va_list * pArguments
- ) throw()
- {
- return
- ::FormatMessageW(
- dwFlags,
- pSource,
- dwMessageID,
- dwLanguageID,
- (LPWSTR)pszBuffer,
- nSize,
- pArguments
- );
- }
- #endif
- static int SafeStringLen( LPCSTR psz ) throw()
- {
- return (psz != NULL) ? (int)strlen( psz ) : 0;
- }
- static int SafeStringLen( const unsigned short * psz ) throw()
- {
- return (psz != NULL) ? (int)wcslen( (wchar_t *)psz ) : 0;
- }
- static int GetCharLen( const unsigned short* pch ) throw()
- {
- (void)pch;
- return 1;
- }
- static int GetCharLen( const char* pch ) throw()
- {
- return (int)(
- _mbclen(
- reinterpret_cast < const unsigned char * > ( pch )
- )
- );
- }
- static DWORD GetEnvironmentVariable(
- const unsigned short * pszVar,
- unsigned short * pszBuffer,
- DWORD dwSize
- ) throw()
- {
- return _GetEnvironmentVariableW( pszVar, pszBuffer, dwSize );
- }
- }; // class CExtSafeStrTraitMFC
- template < typename BaseType >
- class CExtSafeSimpleStringT
- {
- public:
- typedef typename CExtSafeChTraitsBase< BaseType >::XCHAR XCHAR;
- typedef typename CExtSafeChTraitsBase< BaseType >::PXSTR PXSTR;
- typedef typename CExtSafeChTraitsBase< BaseType >::PCXSTR PCXSTR;
- typedef typename CExtSafeChTraitsBase< BaseType >::YCHAR YCHAR;
- typedef typename CExtSafeChTraitsBase< BaseType >::PYSTR PYSTR;
- typedef typename CExtSafeChTraitsBase< BaseType >::PCYSTR PCYSTR;
- public:
- explicit CExtSafeSimpleStringT( IAtlStringMgr * pStringMgr ) throw()
- {
- ATLASSERT( pStringMgr != NULL );
- CStringData * pData = pStringMgr->GetNilString();
- Attach( pData );
- }
- CExtSafeSimpleStringT( const CExtSafeSimpleStringT & strSrc )
- {
- CStringData * pSrcData = strSrc.GetData();
- CStringData * pNewData = CloneData( pSrcData );
- Attach( pNewData );
- }
- CExtSafeSimpleStringT( PCXSTR pszSrc, IAtlStringMgr * pStringMgr )
- {
- ATLASSERT( pStringMgr != NULL );
- int nLength = StringLength( pszSrc );
- CStringData * pData = pStringMgr->Allocate( nLength, sizeof(XCHAR) );
- if( pData == NULL )
- {
- ThrowMemoryException();
- }
- Attach( pData );
- SetLength( nLength );
- CopyChars( m_pszData, pszSrc, nLength );
- }
- CExtSafeSimpleStringT( const XCHAR * pchSrc, int nLength, IAtlStringMgr * pStringMgr )
- {
- ATLASSERT( pStringMgr != NULL );
- CStringData * pData = pStringMgr->Allocate( nLength, sizeof(XCHAR) );
- if( pData == NULL )
- {
- ThrowMemoryException();
- }
- Attach( pData );
- SetLength( nLength );
- CopyChars( m_pszData, pchSrc, nLength );
- }
- ~CExtSafeSimpleStringT() throw()
- {
- CStringData * pData = GetData();
- pData->Release();
- }
- CExtSafeSimpleStringT & operator = ( const CExtSafeSimpleStringT & strSrc )
- {
- CStringData * pSrcData = strSrc.GetData();
- CStringData * pOldData = GetData();
- if( pSrcData != pOldData )
- {
- if( pOldData->IsLocked() )
- {
- SetString( strSrc.GetString(), strSrc.GetLength() );
- }
- else
- {
- CStringData * pNewData = CloneData( pSrcData );
- pOldData->Release();
- Attach( pNewData );
- }
- }
- return ( *this );
- }
- CExtSafeSimpleStringT & operator = ( PCXSTR pszSrc )
- {
- SetString( pszSrc );
- return ( *this );
- }
- CExtSafeSimpleStringT & operator += ( const CExtSafeSimpleStringT & strSrc )
- {
- Append( strSrc );
- return ( *this );
- }
- CExtSafeSimpleStringT & operator += ( PCXSTR pszSrc )
- {
- Append( pszSrc );
- return ( *this );
- }
- template < int t_nSize >
- CExtSafeSimpleStringT & operator += ( const CStaticString < XCHAR, t_nSize > & strSrc )
- {
- Append( strSrc.m_psz, strSrc.GetLength() );
- return ( *this );
- }
- CExtSafeSimpleStringT & operator += ( char ch )
- {
- AppendChar( XCHAR( ch ) );
- return ( *this );
- }
- CExtSafeSimpleStringT & operator += ( unsigned char ch )
- {
- AppendChar( XCHAR( ch ) );
- return ( *this );
- }
- CExtSafeSimpleStringT & operator += ( wchar_t ch )
- {
- AppendChar( XCHAR( ch ) );
- return ( *this );
- }
- XCHAR operator [] ( int iChar ) const throw()
- {
- ATLASSERT( (iChar >= 0) && (iChar <= GetLength()) );
- return ( m_pszData[iChar] );
- }
- operator PCXSTR() const throw()
- {
- return ( m_pszData );
- }
- void Append( PCXSTR pszSrc )
- {
- Append( pszSrc, StringLength( pszSrc ) );
- }
- void Append( PCXSTR pszSrc, int nLength )
- {
- UINT_PTR nOffset = pszSrc-GetString();
- UINT nOldLength = GetLength();
- int nNewLength = nOldLength+nLength;
- PXSTR pszBuffer = GetBuffer( nNewLength );
- if( nOffset <= nOldLength )
- {
- pszSrc = pszBuffer+nOffset;
- }
- CopyChars( pszBuffer+nOldLength, pszSrc, nLength );
- ReleaseBufferSetLength( nNewLength );
- }
- void AppendChar( XCHAR ch )
- {
- UINT nOldLength = GetLength();
- int nNewLength = nOldLength+1;
- PXSTR pszBuffer = GetBuffer( nNewLength );
- pszBuffer[nOldLength] = ch;
- ReleaseBufferSetLength( nNewLength );
- }
- void Append( const CExtSafeSimpleStringT & strSrc )
- {
- Append( strSrc.GetString(), strSrc.GetLength() );
- }
- void Empty() throw()
- {
- CStringData * pOldData = GetData();
- IAtlStringMgr * pStringMgr = pOldData->pStringMgr;
- if( pOldData->nDataLength == 0 )
- {
- return;
- }
- if( pOldData->IsLocked() )
- {
- SetLength( 0 );
- }
- else
- {
- pOldData->Release();
- CStringData * pNewData = pStringMgr->GetNilString();
- Attach( pNewData );
- }
- }
- void FreeExtra() throw()
- {
- CStringData * pOldData = GetData();
- int nLength = pOldData->nDataLength;
- IAtlStringMgr * pStringMgr = pOldData->pStringMgr;
- if( pOldData->nAllocLength == nLength )
- {
- return;
- }
- if( !pOldData->IsLocked() )
- {
- CStringData * pNewData = pStringMgr->Allocate( nLength, sizeof(XCHAR) );
- if( pNewData == NULL )
- {
- SetLength( nLength );
- return;
- }
- CopyChars( PXSTR( pNewData->data() ), PCXSTR( pOldData->data() ), nLength );
- pOldData->Release();
- Attach( pNewData );
- SetLength( nLength );
- }
- }
- int GetAllocLength() const throw()
- {
- return ( GetData()->nAllocLength );
- }
- XCHAR GetAt( int iChar ) const throw()
- {
- ATLASSERT( (iChar >= 0) && (iChar <= GetLength()) );
- return ( m_pszData[iChar] );
- }
- PXSTR GetBuffer()
- {
- CStringData * pData = GetData();
- if( pData->IsShared() )
- {
- Fork( pData->nDataLength );
- }
- return ( m_pszData );
- }
- PXSTR GetBuffer( int nMinBufferLength )
- {
- return ( PrepareWrite( nMinBufferLength ) );
- }
- PXSTR GetBufferSetLength( int nLength )
- {
- PXSTR pszBuffer = GetBuffer( nLength );
- SetLength( nLength );
- return ( pszBuffer );
- }
- int GetLength() const throw()
- {
- return ( GetData()->nDataLength );
- }
- IAtlStringMgr * GetManager() const throw()
- {
- return ( GetData()->pStringMgr->Clone() );
- }
- PCXSTR GetString() const throw()
- {
- return ( m_pszData );
- }
- bool IsEmpty() const throw()
- {
- return ( GetLength() == 0 );
- }
- PXSTR LockBuffer()
- {
- CStringData * pData = GetData();
- if( pData->IsShared() )
- {
- Fork( pData->nDataLength );
- pData = GetData();
- }
- pData->Lock();
- return ( m_pszData );
- }
- void UnlockBuffer() throw()
- {
- CStringData * pData = GetData();
- pData->Unlock();
- }
- void Preallocate( int nLength )
- {
- PrepareWrite( nLength );
- }
- void ReleaseBuffer( int nNewLength = -1 ) throw()
- {
- if( nNewLength == -1 )
- {
- nNewLength = StringLength( m_pszData );
- }
- SetLength( nNewLength );
- }
- void ReleaseBufferSetLength( int nNewLength ) throw()
- {
- ATLASSERT( nNewLength >= 0 );
- SetLength( nNewLength );
- }
- void Truncate( int nNewLength )
- {
- ATLASSERT( nNewLength <= GetLength() );
- GetBuffer( nNewLength );
- ReleaseBufferSetLength( nNewLength );
- }
- void SetAt( int iChar, XCHAR ch )
- {
- ATLASSERT( (iChar >= 0) && (iChar < GetLength()) );
- int nLength = GetLength();
- PXSTR pszBuffer = GetBuffer();
- pszBuffer[iChar] = ch;
- ReleaseBufferSetLength( nLength );
- }
- void SetManager( IAtlStringMgr * pStringMgr )
- {
- ATLASSERT( IsEmpty() );
- CStringData * pData = GetData();
- pData->Release();
- pData = pStringMgr->GetNilString();
- Attach( pData );
- }
- void SetString( PCXSTR pszSrc )
- {
- SetString( pszSrc, StringLength( pszSrc ) );
- }
- void SetString( PCXSTR pszSrc, int nLength )
- {
- if( nLength == 0 )
- {
- Empty();
- }
- else
- {
- UINT nOldLength = GetLength();
- UINT_PTR nOffset = pszSrc-GetString();
- PXSTR pszBuffer = GetBuffer( nLength );
- if( nOffset <= nOldLength )
- {
- CopyCharsOverlapped( pszBuffer, pszBuffer+nOffset, nLength );
- }
- else
- {
- CopyChars( pszBuffer, pszSrc, nLength );
- }
- ReleaseBufferSetLength( nLength );
- }
- }
- public:
- friend CExtSafeSimpleStringT operator + (
- const CExtSafeSimpleStringT & str1,
- const CExtSafeSimpleStringT & str2
- )
- {
- CExtSafeSimpleStringT s( str1.GetManager() );
- Concatenate( s, str1, str1.GetLength(), str2, str2.GetLength() );
- return ( s );
- }
- friend CExtSafeSimpleStringT operator + (
- const CExtSafeSimpleStringT & str1,
- PCXSTR psz2
- )
- {
- CExtSafeSimpleStringT s( str1.GetManager() );
- Concatenate( s, str1, str1.GetLength(), psz2, StringLength( psz2 ) );
- return ( s );
- }
- friend CExtSafeSimpleStringT operator + (
- PCXSTR psz1,
- const CExtSafeSimpleStringT & str2
- )
- {
- CExtSafeSimpleStringT s( str2.GetManager() );
- Concatenate( s, psz1, StringLength( psz1 ), str2, str2.GetLength() );
- return ( s );
- }
- static void CopyChars( XCHAR * pchDest, const XCHAR * pchSrc, int nChars ) throw()
- {
- __EXT_MFC_MEMCPY(
- pchDest,
- nChars * sizeof(XCHAR),
- pchSrc,
- nChars * sizeof(XCHAR)
- );
- }
- static void CopyCharsOverlapped( XCHAR * pchDest, const XCHAR * pchSrc, int nChars ) throw()
- {
- UINT nSizeInBytes =
- nChars * sizeof(XCHAR);
- __EXT_MFC_MEMMOVE(
- pchDest,
- nSizeInBytes,
- pchSrc,
- nSizeInBytes
- );
- }
- #ifdef _ATL_MIN_CRT
- ATL_NOINLINE static int StringLength( PCXSTR psz ) throw()
- {
- int nLength = 0;
- if( psz != NULL )
- {
- const XCHAR * pch = psz;
- while( *pch != 0 )
- {
- nLength++;
- pch++;
- }
- }
- return ( nLength );
- }
- #else
- static int StringLength( const char* psz ) throw()
- {
- if( psz == NULL )
- {
- return ( 0 );
- }
- return ( int( strlen( psz ) ) );
- }
- static int StringLength( const __wchar_t * psz ) throw()
- {
- if( psz == NULL )
- {
- return ( 0 );
- }
- return ( int( wcslen( (wchar_t *)psz ) ) );
- }
- static int StringLength( const unsigned short * psz ) throw()
- {
- if( psz == NULL )
- {
- return ( 0 );
- }
- return ( int( wcslen( (wchar_t *)psz ) ) );
- }
- #endif
- protected:
- static void Concatenate( CExtSafeSimpleStringT & strResult, PCXSTR psz1, int nLength1, PCXSTR psz2, int nLength2 )
- {
- int nNewLength = nLength1+nLength2;
- PXSTR pszBuffer = strResult.GetBuffer( nNewLength );
- CopyChars( pszBuffer, psz1, nLength1 );
- CopyChars( pszBuffer+nLength1, psz2, nLength2 );
- strResult.ReleaseBufferSetLength( nNewLength );
- }
- ATL_NOINLINE __declspec( noreturn ) static void ThrowMemoryException()
- {
- AtlThrow( E_OUTOFMEMORY );
- }
- private:
- void Attach( CStringData * pData ) throw()
- {
- m_pszData = static_cast < PXSTR > ( pData->data() );
- }
- ATL_NOINLINE void Fork( int nLength )
- {
- CStringData * pOldData = GetData();
- int nOldLength = pOldData->nDataLength;
- CStringData * pNewData = pOldData->pStringMgr->Clone()->Allocate( nLength, sizeof(XCHAR) );
- if( pNewData == NULL )
- {
- ThrowMemoryException();
- }
- int nCharsToCopy = ((nOldLength < nLength) ? nOldLength : nLength)+1;
- CopyChars( PXSTR( pNewData->data() ), PCXSTR( pOldData->data() ), nCharsToCopy );
- pNewData->nDataLength = nOldLength;
- pOldData->Release();
- Attach( pNewData );
- }
- CStringData * GetData() const throw()
- {
- return ( reinterpret_cast < CStringData * > ( m_pszData ) - 1 );
- }
- PXSTR PrepareWrite( int nLength )
- {
- CStringData * pOldData = GetData();
- int nShared = 1-pOldData->nRefs;
- int nTooShort = pOldData->nAllocLength-nLength;
- if( (nShared|nTooShort) < 0 )
- {
- PrepareWrite2( nLength );
- }
- return ( m_pszData );
- }
- ATL_NOINLINE void PrepareWrite2( int nLength )
- {
- CStringData * pOldData = GetData();
- if( pOldData->nDataLength > nLength )
- {
- nLength = pOldData->nDataLength;
- }
- if( pOldData->IsShared() )
- {
- Fork( nLength );
- }
- else if( pOldData->nAllocLength < nLength )
- {
- int nNewLength = pOldData->nAllocLength;
- if( nNewLength > 1024 )
- {
- nNewLength += 1024;
- }
- else
- {
- nNewLength *= 2;
- }
- if( nNewLength < nLength )
- {
- nNewLength = nLength;
- }
- Reallocate( nNewLength );
- }
- }
- ATL_NOINLINE void Reallocate( int nLength )
- {
- CStringData * pOldData = GetData();
- ATLASSERT( pOldData->nAllocLength < nLength );
- IAtlStringMgr * pStringMgr = pOldData->pStringMgr;
- CStringData * pNewData = pStringMgr->Reallocate( pOldData, nLength, sizeof(XCHAR) );
- if( pNewData == NULL )
- {
- ThrowMemoryException();
- }
- Attach( pNewData );
- }
- void SetLength( int nLength ) throw()
- {
- ATLASSERT( nLength >= 0 );
- ATLASSERT( nLength <= GetData()->nAllocLength );
- GetData()->nDataLength = nLength;
- m_pszData[nLength] = 0;
- }
- static CStringData * CloneData( CStringData * pData )
- {
- CStringData * pNewData = NULL;
- IAtlStringMgr * pNewStringMgr = pData->pStringMgr->Clone();
- if( !pData->IsLocked() && (pNewStringMgr == pData->pStringMgr) )
- {
- pNewData = pData;
- pNewData->AddRef();
- }
- else
- {
- pNewData = pNewStringMgr->Allocate( pData->nDataLength, sizeof(XCHAR) );
- if( pNewData == NULL )
- {
- ThrowMemoryException();
- }
- pNewData->nDataLength = pData->nDataLength;
- CopyChars( PXSTR( pNewData->data() ), PCXSTR( pData->data() ), pData->nDataLength+1 );
- }
- return ( pNewData );
- }
- private:
- PXSTR m_pszData;
- }; // class CExtSafeSimpleStringT
- template < typename BaseType, class StringTraits >
- class CExtSafeStringT : public CExtSafeSimpleStringT < BaseType >
- {
- public:
- typedef CExtSafeSimpleStringT< BaseType > CThisSimpleString;
- typedef StringTraits StrTraits;
- typedef BaseType WCHAR;
- typedef BaseType * LPWSTR;
- typedef const BaseType * LCPWSTR;
- public:
- CExtSafeStringT() throw()
- : CThisSimpleString( StringTraits::GetDefaultManager() )
- {
- }
- explicit CExtSafeStringT( IAtlStringMgr * pStringMgr ) throw()
- : CThisSimpleString( pStringMgr )
- {
- }
- // CExtSafeStringT( const VARIANT & varSrc );
- // CExtSafeStringT( const VARIANT & varSrc, IAtlStringMgr * pStringMgr );
- static void Construct( CExtSafeStringT * pString )
- {
- new( pString ) CExtSafeStringT;
- }
- CExtSafeStringT( const CExtSafeStringT & strSrc )
- : CThisSimpleString( strSrc )
- {
- }
- CExtSafeStringT( const CThisSimpleString & strSrc )
- : CThisSimpleString( strSrc )
- {
- }
- CExtSafeStringT( const XCHAR * pszSrc )
- : CThisSimpleString( StringTraits::GetDefaultManager() )
- {
- if( !CheckImplicitLoad( pszSrc ) )
- {
- *this = pszSrc;
- }
- }
- CExtSafeStringT( LPCSTR pszSrc, IAtlStringMgr * pStringMgr )
- : CThisSimpleString( pStringMgr )
- {
- if( !CheckImplicitLoad( pszSrc ) )
- {
- *this = pszSrc;
- }
- }
- CSTRING_EXPLICIT CExtSafeStringT( const YCHAR * pszSrc )
- : CThisSimpleString( StringTraits::GetDefaultManager() )
- {
- if( !CheckImplicitLoad( pszSrc ) )
- {
- *this = pszSrc;
- }
- }
- CExtSafeStringT( LPCWSTR pszSrc, IAtlStringMgr * pStringMgr )
- : CThisSimpleString( pStringMgr )
- {
- if( !CheckImplicitLoad( pszSrc ) )
- {
- *this = (PCXSTR)pszSrc;
- }
- }
- //#ifdef _MANAGED
- // CExtSafeStringT( System::String* pString )
- // : CThisSimpleString( StringTraits::GetDefaultManager() )
- // {
- // const wchar_t __pin* psz = PtrToStringChars( pString );
- // *this = psz;
- // }
- //#endif
- CSTRING_EXPLICIT CExtSafeStringT( const unsigned char * pszSrc )
- : CThisSimpleString( StringTraits::GetDefaultManager() )
- {
- *this = reinterpret_cast < const char* > ( pszSrc );
- }
- CExtSafeStringT(
- const unsigned char * pszSrc,
- IAtlStringMgr * pStringMgr
- )
- : CThisSimpleString( pStringMgr )
- {
- *this = reinterpret_cast < const char * > ( pszSrc );
- }
- CSTRING_EXPLICIT CExtSafeStringT( char ch, int nLength = 1 )
- : CThisSimpleString( StringTraits::GetDefaultManager() )
- {
- ATLASSERT( nLength >= 0 );
- if( nLength > 0 )
- {
- PXSTR pszBuffer = GetBuffer( nLength );
- StringTraits::FloodCharacters( XCHAR( ch ), nLength, pszBuffer );
- ReleaseBufferSetLength( nLength );
- }
- }
- CSTRING_EXPLICIT CExtSafeStringT( wchar_t ch, int nLength = 1 )
- : CThisSimpleString( StringTraits::GetDefaultManager() )
- {
- ATLASSERT( nLength >= 0 );
- if( nLength > 0 )
- {
- PXSTR pszBuffer = GetBuffer( nLength );
- StringTraits::FloodCharacters( XCHAR( ch ), nLength, pszBuffer );
- ReleaseBufferSetLength( nLength );
- }
- }
- CExtSafeStringT( const XCHAR * pch, int nLength )
- : CThisSimpleString( pch, nLength, StringTraits::GetDefaultManager() )
- {
- }
- CExtSafeStringT( const XCHAR * pch, int nLength, IAtlStringMgr * pStringMgr )
- : CThisSimpleString( pch, nLength, pStringMgr )
- {
- }
- CExtSafeStringT( const YCHAR * pch, int nLength )
- : CThisSimpleString( StringTraits::GetDefaultManager() )
- {
- ATLASSERT( nLength >= 0 );
- if( nLength > 0 )
- {
- ATLASSERT( AtlIsValidAddress( pch, nLength * sizeof(YCHAR), FALSE ) );
- int nDestLength = StringTraits::GetBaseTypeLength( pch, nLength );
- PXSTR pszBuffer = GetBuffer( nDestLength );
- StringTraits::ConvertToBaseType( pszBuffer, nDestLength, pch, nLength );
- ReleaseBufferSetLength( nDestLength );
- }
- }
- CExtSafeStringT( const YCHAR * pch, int nLength, IAtlStringMgr * pStringMgr )
- : CThisSimpleString( pStringMgr )
- {
- ATLASSERT( nLength >= 0 );
- if( nLength > 0 )
- {
- ATLASSERT( AtlIsValidAddress( pch, nLength * sizeof(YCHAR), FALSE ) );
- int nDestLength = StringTraits::GetBaseTypeLength( pch, nLength );
- PXSTR pszBuffer = GetBuffer( nDestLength );
- StringTraits::ConvertToBaseType( pszBuffer, nDestLength, pch, nLength );
- ReleaseBufferSetLength( nDestLength );
- }
- }
- ~CExtSafeStringT() throw()
- {