📄 stdstring.h
字号:
// When this macro is defined, the code attempts only to use ANSI/ISO
// standard library functions to do it's work. It will NOT attempt to use
// any Win32 of Visual C++ specific functions -- even if they are
// available. You may define this flag yourself to prevent any Win32
// of VC++ specific functions from being called.
// If we're not on Win32, we MUST use an ANSI build
#ifndef SS_WIN32
#if !defined(SS_NO_ANSI)
#define SS_ANSI
#endif
#endif
// Avoid legacy code screw up: if _UNICODE is defined, UNICODE must be as well
#if defined (_UNICODE) && !defined (UNICODE)
#define UNICODE
#endif
#if defined (UNICODE) && !defined (_UNICODE)
#define _UNICODE
#endif
// -----------------------------------------------------------------------------
// MIN and MAX. The Standard C++ template versions go by so many names (at
// at least in the MS implementation) that you never know what's available
// -----------------------------------------------------------------------------
template<class Type>
inline const Type& SSMIN(const Type& arg1, const Type& arg2)
{
return arg2 < arg1 ? arg2 : arg1;
}
template<class Type>
inline const Type& SSMAX(const Type& arg1, const Type& arg2)
{
return arg2 > arg1 ? arg2 : arg1;
}
// If they have not #included W32Base.h (part of my W32 utility library) then
// we need to define some stuff. Otherwise, this is all defined there.
#if !defined(W32BASE_H)
// If they want us to use only standard C++ stuff (no Win32 stuff)
#ifdef SS_ANSI
// On Win32 we have TCHAR.H so just include it. This is NOT violating
// the spirit of SS_ANSI as we are not calling any Win32 functions here.
#ifdef SS_WIN32
#include <TCHAR.H>
#include <WTYPES.H>
#ifndef STRICT
#define STRICT
#endif
// ... but on non-Win32 platforms, we must #define the types we need.
#else
typedef const char* PCSTR;
typedef char* PSTR;
typedef const wchar_t* PCWSTR;
typedef wchar_t* PWSTR;
#ifdef UNICODE
typedef wchar_t TCHAR;
#else
typedef char TCHAR;
#endif
typedef wchar_t OLECHAR;
#endif // #ifndef _WIN32
// Make sure ASSERT and verify are defined using only ANSI stuff
#ifndef ASSERT
#include <assert.h>
#define ASSERT(f) assert((f))
#endif
#ifndef VERIFY
#ifdef _DEBUG
#define VERIFY(x) ASSERT((x))
#else
#define VERIFY(x) x
#endif
#endif
#else // ...else SS_ANSI is NOT defined
#include <TCHAR.H>
#include <WTYPES.H>
#ifndef STRICT
#define STRICT
#endif
// Make sure ASSERT and verify are defined
#ifndef ASSERT
#include <crtdbg.h>
#define ASSERT(f) _ASSERTE((f))
#endif
#ifndef VERIFY
#ifdef _DEBUG
#define VERIFY(x) ASSERT((x))
#else
#define VERIFY(x) x
#endif
#endif
#endif // #ifdef SS_ANSI
#ifndef UNUSED
#define UNUSED(x) x
#endif
#endif // #ifndef W32BASE_H
// Standard headers needed
#include <string> // basic_string
#include <algorithm> // for_each, etc.
#include <functional> // for StdStringLessNoCase, et al
#ifdef SS_NOLOCALE
#define SS_NO_LOCALE
#endif
#ifndef SS_NO_LOCALE
#include <locale> // for various facets
#endif
// If this is a recent enough version of VC include comdef.h, so we can write
// member functions to deal with COM types & compiler support classes e.g. _bstr_t
#if defined (_MSC_VER) && (_MSC_VER >= 1100)
#include <comdef.h>
#define SS_INC_COMDEF // signal that we #included MS comdef.h file
#define STDSTRING_INC_COMDEF
#define SS_NOTHROW __declspec(nothrow)
#else
#define SS_NOTHROW
#endif
#ifndef TRACE
#define TRACE_DEFINED_HERE
#define TRACE
#endif
// Microsoft defines PCSTR, PCWSTR, etc, but no PCTSTR. I hate to use the
// versions with the "L" in front of them because that's a leftover from Win 16
// days, even though it evaluates to the same thing. Therefore, Define a PCSTR
// as an LPCTSTR.
#if !defined(PCTSTR) && !defined(PCTSTR_DEFINED)
typedef const TCHAR* PCTSTR;
#define PCTSTR_DEFINED
#endif
#if !defined(PCOLESTR) && !defined(PCOLESTR_DEFINED)
typedef const OLECHAR* PCOLESTR;
#define PCOLESTR_DEFINED
#endif
#if !defined(POLESTR) && !defined(POLESTR_DEFINED)
typedef OLECHAR* POLESTR;
#define POLESTR_DEFINED
#endif
#if !defined(PCUSTR) && !defined(PCUSTR_DEFINED)
typedef const unsigned char* PCUSTR;
typedef unsigned char* PUSTR;
#define PCUSTR_DEFINED
#endif
// SS_USE_FACET macro and why we need it:
//
// Since I'm a good little Standard C++ programmer, I use locales. Thus, I
// need to make use of the use_facet<> template function here. Unfortunately,
// this need is complicated by the fact the MS' implementation of the Standard
// C++ Library has a non-standard version of use_facet that takes more
// arguments than the standard dictates. Since I'm trying to write CStdString
// to work with any version of the Standard library, this presents a problem.
//
// The upshot of this is that I can't do 'use_facet' directly. The MS' docs
// tell me that I have to use a macro, _USE() instead. Since _USE obviously
// won't be available in other implementations, this means that I have to write
// my OWN macro -- SS_USE_FACET -- that evaluates either to _USE or to the
// standard, use_facet.
//
// If you are having trouble with the SS_USE_FACET macro, in your implementation
// of the Standard C++ Library, you can define your own version of SS_USE_FACET.
#ifndef schMSG
#define schSTR(x) #x
#define schSTR2(x) schSTR(x)
#define schMSG(desc) message(__FILE__ "(" schSTR2(__LINE__) "):" #desc)
#endif
#ifndef SS_USE_FACET
// STLPort #defines a macro (__STL_NO_EXPLICIT_FUNCTION_TMPL_ARGS) for
// all MSVC builds, erroneously in my opinion. It causes problems for
// my SS_ANSI builds. In my code, I always comment out that line. You'll
// find it in \stlport\config\stl_msvc.h
#if defined(__SGI_STL_PORT) && (__SGI_STL_PORT >= 0x400 )
#if defined(__STL_NO_EXPLICIT_FUNCTION_TMPL_ARGS) && defined(_MSC_VER)
#ifdef SS_ANSI
#pragma schMSG(__STL_NO_EXPLICIT_FUNCTION_TMPL_ARGS defined!!)
#endif
#endif
#define SS_USE_FACET(loc, fac) std::use_facet<fac >(loc)
#elif defined(_MSC_VER )
#define SS_USE_FACET(loc, fac) std::_USE(loc, fac)
// ...and
#elif defined(_RWSTD_NO_TEMPLATE_ON_RETURN_TYPE)
#define SS_USE_FACET(loc, fac) std::use_facet(loc, (fac*)0)
#else
#define SS_USE_FACET(loc, fac) std::use_facet<fac >(loc)
#endif
#endif
// =============================================================================
// UNICODE/MBCS conversion macros. Made to work just like the MFC/ATL ones.
// =============================================================================
#include <wchar.h> // Added to Std Library with Amendment #1.
// First define the conversion helper functions. We define these regardless of
// any preprocessor macro settings since their names won't collide.
#ifdef SS_ANSI // Are we doing things the standard, non-Win32 way?...
// Not sure if we need all these headers. I believe ANSI says we do.
#include <stdio.h>
#include <stdarg.h>
#include <wctype.h>
#include <ctype.h>
#include <stdlib.h>
#ifndef va_start
#include <varargs.h>
#endif
// Standard C++ Library implementations that do not provide the required
// locale header (and thus the codecvt facet) can use good old mbstowcs and
// wcstombs instead by #defining SS_NO_LOCALE
#ifdef SS_NO_LOCALE
inline PWSTR StdCodeCvt(PWSTR pW, PCSTR pA, int nChars)
{
ASSERT(0 != pA);
ASSERT(0 != pW);
pW[0] = '\0';
int nCvt = mbstowcs(pW, pA, nChars);
ASSERT(nCvt >=0);
return pW;
}
inline PWSTR StdCodeCvt(PWSTR pW, PCUSTR pA, int nChars)
{
return StdCodeCvt(pW, (PCSTR)pA, nChars);
}
inline PSTR StdCodeCvt(PSTR pA, PCWSTR pW, int nChars)
{
ASSERT(0 != pA);
ASSERT(0 != pW);
pA[0] = '\0';
int nCvt = wcstombs(pA, pW, nChars);
ASSERT(nCvt >=0);
return pA;
}
inline PUSTR StdCodeCvt(PUSTR pA, PCWSTR pW, int nChars)
{
return (PUSTR)StdCodeCvt((PSTR)pA, pW, nChars);
}
#else
typedef std::codecvt<wchar_t, char, mbstate_t> SSCodeCvt;
// StdCodeCvt - made to look like Win32 functions WideCharToMultiByte
// and MultiByteToWideChar but uses locales in SS_ANSI
// builds
inline PWSTR StdCodeCvt(PWSTR pW, PCSTR pA, int nChars,
const std::locale& loc=std::locale())
{
ASSERT(0 != pA);
ASSERT(0 != pW);
pW[0] = '\0';
PCSTR pBadA = 0;
PWSTR pBadW = 0;
SSCodeCvt::result res = SSCodeCvt::ok;
const SSCodeCvt& conv = SS_USE_FACET(loc, SSCodeCvt);
SSCodeCvt::state_type st= { 0 };
res = conv.in(st,
pA, pA + nChars, pBadA,
pW, pW + nChars, pBadW);
ASSERT(SSCodeCvt::ok == res);
return pW;
}
inline PWSTR StdCodeCvt(PWSTR pW, PCUSTR pA, int nChars,
const std::locale& loc=std::locale())
{
return StdCodeCvt(pW, (PCSTR)pA, nChars, loc);
}
inline PSTR StdCodeCvt(PSTR pA, PCWSTR pW, int nChars,
const std::locale& loc=std::locale())
{
ASSERT(0 != pA);
ASSERT(0 != pW);
pA[0] = '\0';
PSTR pBadA = 0;
PCWSTR pBadW = 0;
SSCodeCvt::result res = SSCodeCvt::ok;
const SSCodeCvt& conv = SS_USE_FACET(loc, SSCodeCvt);
SSCodeCvt::state_type st= { 0 };
res = conv.out(st,
pW, pW + nChars, pBadW,
pA, pA + nChars, pBadA);
ASSERT(SSCodeCvt::ok == res);
return pA;
}
inline PUSTR StdCodeCvt(PUSTR pA, PCWSTR pW, int nChars,
const std::locale& loc=std::locale())
{
return (PUSTR)StdCodeCvt((PSTR)pA, pW, nChars, loc);
}
#endif
#else // ...or are we doing things assuming win32 and Visual C++?
#include <malloc.h> // needed for _alloca
inline PWSTR StdCodeCvt(PWSTR pW, PCSTR pA, int nChars, UINT acp=CP_ACP)
{
ASSERT(0 != pA);
ASSERT(0 != pW);
pW[0] = '\0';
MultiByteToWideChar(acp, 0, pA, -1, pW, nChars);
return pW;
}
inline PWSTR StdCodeCvt(PWSTR pW, PCUSTR pA, int nChars, UINT acp=CP_ACP)
{
return StdCodeCvt(pW, (PCSTR)pA, nChars, acp);
}
inline PSTR StdCodeCvt(PSTR pA, PCWSTR pW, int nChars, UINT acp=CP_ACP)
{
ASSERT(0 != pA);
ASSERT(0 != pW);
pA[0] = '\0';
WideCharToMultiByte(acp, 0, pW, -1, pA, nChars, 0, 0);
return pA;
}
inline PUSTR StdCodeCvt(PUSTR pA, PCWSTR pW, int nChars, UINT acp=CP_ACP)
{
return (PUSTR)StdCodeCvt((PSTR)pA, pW, nChars, acp);
}
// 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
#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
#endif
#ifdef _CONVERSION_USES_THREAD_LOCALE
#define SSA2W(pa) (\
((_pa = pa) == 0) ? 0 : (\
_cvt = (strlen(_pa)+1),\
StdCodeCvt((PWSTR) _alloca(_cvt*2), _pa, _cvt, _acp)))
#define SSW2A(pw) (\
((_pw = pw) == 0) ? 0 : (\
_cvt = (wcslen(_pw)+1)*2,\
StdW2AHelper((LPSTR) _alloca(_cvt), _pw, _cvt, _acp)))
#else
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -