📄 stdstring.h
字号:
#include <wctype.h>
#include <ctype.h>
#include <stdlib.h>
#ifndef va_start
#include <varargs.h>
#endif
// StdCodeCvt - made to look like Win32 functions WideCharToMultiByte
// and MultiByteToWideChar but uses locales in SS_ANSI
// builds. There are a number of overloads.
// First argument is the destination buffer.
// Second argument is the source buffer
//#if defined (SS_ANSI) || !defined (SS_WIN32)
// 'SSCodeCvt' - shorthand name for the codecvt facet we use
typedef std::codecvt<wchar_t, char, mbstate_t> SSCodeCvt;
inline PWSTR StdCodeCvt(PWSTR pDstW, int nDst, PCSTR pSrcA, int nSrc,
const std::locale& loc=std::locale())
{
ASSERT(0 != pSrcA);
ASSERT(0 != pDstW);
pDstW[0] = '\0';
if ( nSrc > 0 )
{
PCSTR pNextSrcA = pSrcA;
PWSTR pNextDstW = pDstW;
SSCodeCvt::result res = SSCodeCvt::ok;
const SSCodeCvt& conv = SS_USE_FACET(loc, SSCodeCvt);
SSCodeCvt::state_type st= { 0 };
res = conv.in(st,
pSrcA, pSrcA + nSrc, pNextSrcA,
pDstW, pDstW + nDst, pNextDstW);
ASSERT(SSCodeCvt::ok == res);
ASSERT(SSCodeCvt::error != res);
ASSERT(pNextDstW >= pDstW);
ASSERT(pNextSrcA >= pSrcA);
// Null terminate the converted string
if ( pNextDstW - pDstW > nDst )
*(pDstW + nDst) = '\0';
else
*pNextDstW = '\0';
}
return pDstW;
}
inline PWSTR StdCodeCvt(PWSTR pDstW, int nDst, PCUSTR pSrcA, int nSrc,
const std::locale& loc=std::locale())
{
return StdCodeCvt(pDstW, nDst, (PCSTR)pSrcA, nSrc, loc);
}
inline PSTR StdCodeCvt(PSTR pDstA, int nDst, PCWSTR pSrcW, int nSrc,
const std::locale& loc=std::locale())
{
ASSERT(0 != pDstA);
ASSERT(0 != pSrcW);
pDstA[0] = '\0';
if ( nSrc > 0 )
{
PSTR pNextDstA = pDstA;
PCWSTR pNextSrcW = pSrcW;
SSCodeCvt::result res = SSCodeCvt::ok;
const SSCodeCvt& conv = SS_USE_FACET(loc, SSCodeCvt);
SSCodeCvt::state_type st= { 0 };
res = conv.out(st,
pSrcW, pSrcW + nSrc, pNextSrcW,
pDstA, pDstA + nDst, pNextDstA);
ASSERT(SSCodeCvt::error != res);
ASSERT(SSCodeCvt::ok == res); // strict, comment out for sanity
ASSERT(pNextDstA >= pDstA);
ASSERT(pNextSrcW >= pSrcW);
// Null terminate the converted string
if ( pNextDstA - pDstA > nDst )
*(pDstA + nDst) = '\0';
else
*pNextDstA = '\0';
}
return pDstA;
}
inline PUSTR StdCodeCvt(PUSTR pDstA, int nDst, PCWSTR pSrcW, int nSrc,
const std::locale& loc=std::locale())
{
return (PUSTR)StdCodeCvt((PSTR)pDstA, nDst, pSrcW, nSrc, loc);
}
/*
#else // ...or are we doing things assuming win32 and Visual C++?
inline PWSTR StdCodeCvt(PWSTR pDstW, int nDst, PCSTR pSrcA, int nSrc, UINT acp=CP_ACP)
{
ASSERT(0 != pSrcA);
ASSERT(0 != pDstW);
pW[0] = '\0';
MultiByteToWideChar(acp, 0, pSrcA, nSrc, pDstW, nDst);
return pW;
}
inline PWSTR StdCodeCvt(PWSTR pDstW, nDst, PCUSTR pSrcA, int nSrc, UINT acp=CP_ACP)
{
return StdCodeCvt(pDstW, nDst, (PCSTR)pSrcA, nSrc, acp);
}
inline PSTR StdCodeCvt(PSTR pDstA, nDst PCWSTR pSrcW, int nSrc, UINT acp=CP_ACP)
{
ASSERT(0 != pDstA);
ASSERT(0 != pSrcW);
pA[0] = '\0';
WideCharToMultiByte(acp, 0, pSrcW, nSrc, pDstA, nDst, 0, 0);
return pA;
}
inline PUSTR StdCodeCvt(PUSTR pDstA, nDst, PCWSTR pSrcW, int nSrc, UINT acp=CP_ACP)
{
return (PUSTR)StdCodeCvt((PSTR)pDstA, nDst, pSrcW, nSrc, acp);
}
#endif
*/
// Unicode/MBCS conversion macros are only available on implementations of
// the "C" library that have the non-standard _alloca function. As far as I
// know that's only Microsoft's though I've heard that the function exists
// elsewhere.
#if defined(SS_ALLOCA) && !defined SS_NO_CONVERSION
#include <malloc.h> // needed for _alloca
// Define our conversion macros to look exactly like Microsoft's to
// facilitate using this stuff both with and without MFC/ATL
#ifdef _CONVERSION_USES_THREAD_LOCALE
#ifndef _DEBUG
#define SSCVT int _cvt; _cvt; UINT _acp=GetACP(); \
_acp; PCWSTR _pw; _pw; PCSTR _pa; _pa
#else
#define SSCVT int _cvt = 0; _cvt; UINT _acp=GetACP();\
_acp; PCWSTR _pw=0; _pw; PCSTR _pa=0; _pa
#endif
#define SSA2W(pa) (\
((_pa = pa) == 0) ? 0 : (\
_cvt = (sslen(_pa)),\
StdCodeCvt((PWSTR) _alloca((_cvt+1)*2), (_cvt+1)*2, \
_pa, _cvt, _acp)))
#define SSW2A(pw) (\
((_pw = pw) == 0) ? 0 : (\
_cvt = sslen(_pw), \
StdCodeCvt((LPSTR) _alloca((_cvt+1)*2), (_cvt+1)*2, \
_pw, _cvt, _acp)))
#else
#ifndef _DEBUG
#define SSCVT int _cvt; _cvt; UINT _acp=CP_ACP; _acp;\
PCWSTR _pw; _pw; PCSTR _pa; _pa
#else
#define SSCVT int _cvt = 0; _cvt; UINT _acp=CP_ACP; \
_acp; PCWSTR _pw=0; _pw; PCSTR _pa=0; _pa
#endif
#define SSA2W(pa) (\
((_pa = pa) == 0) ? 0 : (\
_cvt = (sslen(_pa)),\
StdCodeCvt((PWSTR) _alloca((_cvt+1)*2), (_cvt+1)*2, \
_pa, _cvt)))
#define SSW2A(pw) (\
((_pw = pw) == 0) ? 0 : (\
_cvt = (sslen(_pw)),\
StdCodeCvt((LPSTR) _alloca((_cvt+1)*2), (_cvt+1)*2, \
_pw, _cvt)))
#endif
#define SSA2CW(pa) ((PCWSTR)SSA2W((pa)))
#define SSW2CA(pw) ((PCSTR)SSW2A((pw)))
#ifdef UNICODE
#define SST2A SSW2A
#define SSA2T SSA2W
#define SST2CA SSW2CA
#define SSA2CT SSA2CW
// (Did you get a compiler error here about not being able to convert
// PTSTR into PWSTR? Then your _UNICODE and UNICODE flags are messed
// up. Best bet: #define BOTH macros before including any MS headers.)
inline PWSTR SST2W(PTSTR p) { return p; }
inline PTSTR SSW2T(PWSTR p) { return p; }
inline PCWSTR SST2CW(PCTSTR p) { return p; }
inline PCTSTR SSW2CT(PCWSTR p) { return p; }
#else
#define SST2W SSA2W
#define SSW2T SSW2A
#define SST2CW SSA2CW
#define SSW2CT SSW2CA
inline PSTR SST2A(PTSTR p) { return p; }
inline PTSTR SSA2T(PSTR p) { return p; }
inline PCSTR SST2CA(PCTSTR p) { return p; }
inline PCTSTR SSA2CT(PCSTR p) { return p; }
#endif // #ifdef UNICODE
#if defined(UNICODE)
// in these cases the default (TCHAR) is the same as OLECHAR
inline PCOLESTR SST2COLE(PCTSTR p) { return p; }
inline PCTSTR SSOLE2CT(PCOLESTR p) { return p; }
inline POLESTR SST2OLE(PTSTR p) { return p; }
inline PTSTR SSOLE2T(POLESTR p) { return p; }
#elif defined(OLE2ANSI)
// in these cases the default (TCHAR) is the same as OLECHAR
inline PCOLESTR SST2COLE(PCTSTR p) { return p; }
inline PCTSTR SSOLE2CT(PCOLESTR p) { return p; }
inline POLESTR SST2OLE(PTSTR p) { return p; }
inline PTSTR SSOLE2T(POLESTR p) { return p; }
#else
//CharNextW doesn't work on Win95 so we use this
#define SST2COLE(pa) SSA2CW((pa))
#define SST2OLE(pa) SSA2W((pa))
#define SSOLE2CT(po) SSW2CA((po))
#define SSOLE2T(po) SSW2A((po))
#endif
#ifdef OLE2ANSI
#define SSW2OLE SSW2A
#define SSOLE2W SSA2W
#define SSW2COLE SSW2CA
#define SSOLE2CW SSA2CW
inline POLESTR SSA2OLE(PSTR p) { return p; }
inline PSTR SSOLE2A(POLESTR p) { return p; }
inline PCOLESTR SSA2COLE(PCSTR p) { return p; }
inline PCSTR SSOLE2CA(PCOLESTR p){ return p; }
#else
#define SSA2OLE SSA2W
#define SSOLE2A SSW2A
#define SSA2COLE SSA2CW
#define SSOLE2CA SSW2CA
inline POLESTR SSW2OLE(PWSTR p) { return p; }
inline PWSTR SSOLE2W(POLESTR p) { return p; }
inline PCOLESTR SSW2COLE(PCWSTR p) { return p; }
inline PCWSTR SSOLE2CW(PCOLESTR p){ return p; }
#endif
// Above we've defined macros that look like MS' but all have
// an 'SS' prefix. Now we need the real macros. We'll either
// get them from the macros above or from MFC/ATL.
#if defined (USES_CONVERSION)
#define _NO_STDCONVERSION // just to be consistent
#else
#ifdef _MFC_VER
#include <afxconv.h>
#define _NO_STDCONVERSION // just to be consistent
#else
#define USES_CONVERSION SSCVT
#define A2CW SSA2CW
#define W2CA SSW2CA
#define T2A SST2A
#define A2T SSA2T
#define T2W SST2W
#define W2T SSW2T
#define T2CA SST2CA
#define A2CT SSA2CT
#define T2CW SST2CW
#define W2CT SSW2CT
#define ocslen sslen
#define ocscpy sscpy
#define T2COLE SST2COLE
#define OLE2CT SSOLE2CT
#define T2OLE SST2COLE
#define OLE2T SSOLE2CT
#define A2OLE SSA2OLE
#define OLE2A SSOLE2A
#define W2OLE SSW2OLE
#define OLE2W SSOLE2W
#define A2COLE SSA2COLE
#define OLE2CA SSOLE2CA
#define W2COLE SSW2COLE
#define OLE2CW SSOLE2CW
#endif // #ifdef _MFC_VER
#endif // #ifndef USES_CONVERSION
#endif // #ifndef SS_NO_CONVERSION
// Define ostring - generic name for std::basic_string<OLECHAR>
#if !defined(ostring) && !defined(OSTRING_DEFINED)
typedef std::basic_string<OLECHAR> ostring;
#define OSTRING_DEFINED
#endif
// StdCodeCvt when there's no conversion to be done
inline PSTR StdCodeCvt(PSTR pDst, int nDst, PCSTR pSrc, int nSrc)
{
int nChars = SSMIN(nSrc, nDst);
if ( nChars > 0 )
{
pDst[0] = '\0';
std::basic_string<char>::traits_type::copy(pDst, pSrc, nChars);
// std::char_traits<char>::copy(pDst, pSrc, nChars);
pDst[nChars] = '\0';
}
return pDst;
}
inline PSTR StdCodeCvt(PSTR pDst, int nDst, PCUSTR pSrc, int nSrc)
{
return StdCodeCvt(pDst, nDst, (PCSTR)pSrc, nSrc);
}
inline PUSTR StdCodeCvt(PUSTR pDst, int nDst, PCSTR pSrc, int nSrc)
{
return (PUSTR)StdCodeCvt((PSTR)pDst, nDst, pSrc, nSrc);
}
inline PWSTR StdCodeCvt(PWSTR pDst, int nDst, PCWSTR pSrc, int nSrc)
{
int nChars = SSMIN(nSrc, nDst);
if ( nChars > 0 )
{
pDst[0] = '\0';
std::basic_string<wchar_t>::traits_type::copy(pDst, pSrc, nChars);
// std::char_traits<wchar_t>::copy(pDst, pSrc, nChars);
pDst[nChars] = '\0';
}
return pDst;
}
// Define tstring -- generic name for std::basic_string<TCHAR>
#if !defined(tstring) && !defined(TSTRING_DEFINED)
typedef std::basic_string<TCHAR> tstring;
#define TSTRING_DEFINED
#endif
// a very shorthand way of applying the fix for KB problem Q172398
// (basic_string assignment bug)
#if defined ( _MSC_VER ) && ( _MSC_VER < 1200 )
#define Q172398(x) (x).erase()
#else
#define Q172398(x)
#endif
// =============================================================================
// INLINE FUNCTIONS ON WHICH CSTDSTRING RELIES
//
// Usually for generic text mapping, we rely on preprocessor macro definitions
// to map to string functions. However the CStdStr<> template cannot use
// macro-based generic text mappings because its character types do not get
// resolved until template processing which comes AFTER macro processing. In
// other words, the preprocessor macro UNICODE is of little help to us in the
// CStdStr template
//
// Therefore, to keep the CStdStr declaration simple, we have these inline
// functions. The template calls them often. Since they are inline (and NOT
// exported when this is built as a DLL), they will probably be resolved away
// to nothing.
//
// Without these functions, the CStdStr<> template would probably have to broken
// out into two, almost identical classes. Either that or it would be a huge,
// convoluted mess, with tons of "if" statements all over the place checking the
// size of template parameter CT.
//
// In several cases, you will see two versions of each function. One version is
// the more portable, standard way of doing things, while the other is the
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -