📄 basicstr.h
字号:
template <class CharType>
bool CBasicStringBase<CharType>::EnsureLength( size_t nMaxSize )
{
//
// If the string is already long enough, just return true
//
if (m_nMaxSize >= nMaxSize)
{
return true;
}
// Get the new size
//
size_t nNewMaxSize = nMaxSize + m_nGranularity;
//
// Allocate the new buffer
//
size_t nAllocated = 0;
CharType *pszTmp = CreateStorage(nNewMaxSize,nAllocated);
//
// Make sure the allocation succeeded
//
if (pszTmp)
{
//
// If we have an existing string, copy it and delete it
//
if (m_pstrData)
{
GenericCopyLength( pszTmp, m_pstrData, Length()+1 );
DeleteStorage();
}
//
// Save the new max size
//
m_nMaxSize = nAllocated;
//
// Save this new string
//
m_pstrData = pszTmp;
//
// Return success
//
return true;
}
//
// Couldn't allocate memory
//
return false;
}
template <class CharType>
CBasicStringBase<CharType>::CBasicStringBase()
: m_pstrData(m_pstrAutoData),
m_nMaxSize(ARRAYSIZE(m_pstrAutoData)),
m_nGranularity(c_nDefaultGranularity),
m_bError(false)
{
#if defined(SIMSTR_UNIT_TEST)
m_bForceError = false;
#endif
m_pstrAutoData[0] = 0;
}
template <class CharType>
CBasicStringBase<CharType>::CBasicStringBase( const CBasicStringBase &other )
: m_pstrData(m_pstrAutoData),
m_nMaxSize(ARRAYSIZE(m_pstrAutoData)),
m_nGranularity(c_nDefaultGranularity),
m_bError(false)
{
#if defined(SIMSTR_UNIT_TEST)
m_bForceError = false;
#endif
m_pstrAutoData[0] = 0;
Assign(other);
}
template <class CharType>
CBasicStringBase<CharType>::CBasicStringBase( const CharType *szStr )
: m_pstrData(m_pstrAutoData),
m_nMaxSize(ARRAYSIZE(m_pstrAutoData)),
m_nGranularity(c_nDefaultGranularity),
m_bError(false)
{
#if defined(SIMSTR_UNIT_TEST)
m_bForceError = false;
#endif
m_pstrAutoData[0] = 0;
Assign(szStr);
}
template <class CharType>
CBasicStringBase<CharType>::CBasicStringBase( CharType ch )
: m_pstrData(m_pstrAutoData),
m_nMaxSize(ARRAYSIZE(m_pstrAutoData)),
m_nGranularity(c_nDefaultGranularity),
m_bError(false)
{
#if defined(SIMSTR_UNIT_TEST)
m_bForceError = false;
#endif
m_pstrAutoData[0] = 0;
CharType szTmp[2];
szTmp[0] = ch;
szTmp[1] = 0;
Assign(szTmp);
}
template <class CharType>
CBasicStringBase<CharType>::CBasicStringBase( UINT nResId, HMODULE hModule )
: m_pstrData(m_pstrAutoData),
m_nMaxSize(ARRAYSIZE(m_pstrAutoData)),
m_nGranularity(c_nDefaultGranularity),
m_bError(false)
{
#if defined(SIMSTR_UNIT_TEST)
m_bForceError = false;
#endif
m_pstrAutoData[0] = 0;
LoadString( nResId, hModule );
}
template <>
inline CBasicStringBase<WCHAR> &CBasicStringBase<WCHAR>::Format( const WCHAR *strFmt, ... )
{
//
// Initialize the string
//
Assign(NULL);
//
// Prepare the argument list
//
va_list ArgList;
va_start( ArgList, strFmt );
//
// How many characters do we need?
//
int cchLength = _vscwprintf( strFmt, ArgList );
//
// Make sure we have a valid length
//
if (cchLength >= 0)
{
//
// Get a pointer to the buffer
//
LPWSTR pszBuffer = GetBuffer(cchLength + 1);
if (pszBuffer)
{
//
// Print the string
//
StringCchVPrintfW( pszBuffer, cchLength + 1, strFmt, ArgList );
}
}
//
// Done with the argument list
//
va_end( ArgList );
return *this;
}
template <>
inline CBasicStringBase<CHAR> &CBasicStringBase<CHAR>::Format( const CHAR *strFmt, ... )
{
//
// Initialize the string
//
Assign(NULL);
//
// Prepare the argument list
//
va_list ArgList;
va_start( ArgList, strFmt );
//
// How many characters do we need?
//
int cchLength = _vscprintf( strFmt, ArgList );
//
// Make sure we have a valid length
//
if (cchLength >= 0)
{
//
// Get a pointer to the buffer
//
LPSTR pszBuffer = GetBuffer(cchLength + 1);
if (pszBuffer)
{
//
// Print the string
//
StringCchVPrintfA( pszBuffer, cchLength + 1, strFmt, ArgList );
}
}
//
// Done with the argument list
//
va_end( ArgList );
return *this;
}
template <>
inline CBasicStringBase<WCHAR> &CBasicStringBase<WCHAR>::Format( int nResId, HINSTANCE hInst, ... )
{
//
// Initialize the string
//
Assign(NULL);
//
// Load the format string
//
CBasicStringBase<WCHAR> strFmt;
if (strFmt.LoadString( nResId, hInst ))
{
//
// Prepare the argument list
//
va_list ArgList;
va_start( ArgList, hInst );
//
// How many characters do we need?
//
int cchLength = _vscwprintf( strFmt, ArgList );
//
// Make sure we have a valid length
//
if (cchLength >= 0)
{
//
// Get a pointer to the buffer
//
LPWSTR pszBuffer = GetBuffer(cchLength + 1);
if (pszBuffer)
{
//
// Print the string
//
StringCchVPrintfW( pszBuffer, cchLength + 1, strFmt, ArgList );
}
}
//
// Done with the argument list
//
va_end( ArgList );
}
return *this;
}
template <>
inline CBasicStringBase<CHAR> &CBasicStringBase<CHAR>::Format( int nResId, HINSTANCE hInst, ... )
{
//
// Initialize the string
//
Assign(NULL);
//
// Load the format string
//
CBasicStringBase<CHAR> strFmt;
if (strFmt.LoadString(nResId,hInst))
{
//
// Prepare the argument list
//
va_list ArgList;
va_start( ArgList, hInst );
//
// How many characters do we need?
//
int cchLength = _vscprintf( strFmt, ArgList );
//
// Make sure we have a valid length
//
if (cchLength >= 0)
{
//
// Get a pointer to the buffer
//
LPSTR pszBuffer = GetBuffer(cchLength + 1);
if (pszBuffer)
{
//
// Print the string
//
StringCchVPrintfA( pszBuffer, cchLength + 1, strFmt, ArgList );
}
}
//
// Done with the argument list
//
va_end(ArgList);
}
return *this;
}
template <>
inline bool CBasicStringBase<CHAR>::LoadString( UINT nResId, HMODULE hModule )
{
//
// Assume failure
//
bool bResult = false;
//
// Initialize the current string
//
Assign(NULL);
//
// If no hmodule was provided, use the current EXE's
//
if (!hModule)
{
hModule = GetModuleHandle(NULL);
}
//
// Loop through, doubling the size of the string, until we get to 64K
//
for (int nSize = c_nInitialLoadStringBuffer;nSize < 0x0000FFFF; nSize <<= 1 )
{
//
// Get a buffer to hold the string
//
LPSTR pszBuffer = GetBuffer(nSize);
//
// If we can't get a buffer, exit the loop
//
if (!pszBuffer)
{
break;
}
//
// Make sure the string is NULL terminated.
//
*pszBuffer = '\0';
//
// Attempt to load the string
//
int nResult = ::LoadStringA( hModule, nResId, pszBuffer, nSize );
//
// If the buffer was long enough, exit the loop, and set the success flag
//
if (nResult < (nSize - 1))
{
bResult = true;
break;
}
//
// If it was unsuccessful, exit the loop
//
if (!nResult)
{
break;
}
}
return bResult;
}
template <>
inline bool CBasicStringBase<WCHAR>::LoadString( UINT nResId, HMODULE hModule )
{
//
// Assume failure
//
bool bResult = false;
//
// Initialize the current string
//
Assign(NULL);
//
// If no hmodule was provided, use the current EXE's
//
if (!hModule)
{
hModule = GetModuleHandle(NULL);
}
//
// Loop through, doubling the size of the string, until we get to 64K
//
for (int nSize = c_nInitialLoadStringBuffer;nSize < 0x0000FFFF; nSize <<= 1 )
{
//
// Get a buffer to hold the string
//
LPWSTR pszBuffer = GetBuffer(nSize);
//
// If we can't get a buffer, exit the loop
//
if (!pszBuffer)
{
break;
}
//
// Make sure the string is NULL terminated.
//
*pszBuffer = L'\0';
//
// Attempt to load the string
//
int nResult = ::LoadStringW( hModule, nResId, pszBuffer, nSize );
//
// If the buffer was long enough, exit the loop, and set the success flag
//
if (nResult < (nSize - 1))
{
bResult = true;
break;
}
//
// If it was unsuccessful, exit the loop
//
if (!nResult)
{
break;
}
}
return bResult;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -