⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 stdstring.cpp

📁 字符串处理源代码:StdString
💻 CPP
📖 第 1 页 / 共 2 页
字号:
	}
	else
	{
		erase();
	}
	return *this;
}


// ---------------------------------------------------------------------------------------
// FUNCTION:  CStdString::TrimLeft
//		CStdString& TrimLeft();
//
// DESCRIPTION:
//		This function removes any whitespace characters from the left end of the string.
//
// PARAMETERS: none
// RETURN VALUE:
//		a reference to this object (*this) -- allows chaining together of
//		these calls, (eg. strTest.TrimRight().TrimLeft().ToUpper();)
// ---------------------------------------------------------------------------------------
CStdString& CStdString::TrimLeft()
{
	CStdString::iterator iter = std::find_if(begin(), end(), NotSpace());
	CStdString strNew(iter, end());
	STRBASE::assign(strNew);
	return *this;
}


// ---------------------------------------------------------------------------------------
// FUNCTION:  CStdString::ToUpper
//		CStdString& ToUpper()
//           
// DESCRIPTION:
//		This function converts the CStdString to all uppercase characters using ctype
//
// PARAMETERS: 
// RETURN VALUE: 
//		a reference to this object (*this) -- allows chaining together of
//		these calls, (eg. strTest.TrimRight().TrimLeft().ToUpper();)
// ---------------------------------------------------------------------------------------
CStdString& CStdString::ToUpper()
{
//	std::transform(begin(), end(), begin(), toupper);	// portable, slow way of doing it
	_tcsupr(const_cast<PTSTR>(data()));					// unportable, fast way
	return *this;
}


// ---------------------------------------------------------------------------------------
// FUNCTION:  CStdString::ToLower
//		CStdString& ToLower()
//           
// DESCRIPTION:
//		This function converts the CStdString to all lowercase characters using ctype
//
// PARAMETERS: 
// RETURN VALUE: 
//		a reference to this object (*this) -- allows chaining together of
//		these calls, (eg. strTest.ToLower().TrimLeft().ToUpper();)
// ---------------------------------------------------------------------------------------
CStdString& CStdString::ToLower()
{
	//std::transform(begin(), end(), begin(), tolower);	// portable, slow way of doing it
	_tcslwr(const_cast<PTSTR>(data()));					// unportable, fast way
	return *this;
}


// ---------------------------------------------------------------------------------------
// FUNCTION:  CStdString::CopyString
//		static void CopyString(PCTSTR p_szSource, PTSTR p_szDest, int p_nMaxChars=0);
//		static void CopyString(PCOSTR p_szSource, POSTR p_szDest, int p_nMaxChars=0);
//		static void CopyString(PCSTR p_szSource, PWSTR p_szDest, int p_nMaxChars=0);
//		static void CopyString(PCWSTR p_szSource, PSTR p_szDest, int p_nMaxChars=0);
//
// DESCRIPTION:
//		These 3 overloads simplify copying one C-style string into another.
//
// PARAMETERS: 
//		p_szSource - the string to be copied FROM.  May be either an MBCS string (char) or
//					 a wide string (wchar_t)
//		p_szDest - the string to be copied TO.  Also may be either MBCS or wide
//		p_nMaxChars - the maximum number of characters to be copied into p_szDest.  Note
//					  that this is expressed in whatever a "character" means to p_szDest.
//					  If p_szDest is a wchar_t type string than this will be the maximum
//					  number of wchar_ts that my be copied.  The p_szDest string must be
//					  large enough to hold least p_nMaxChars+1 characters.
//
// RETURN VALUE: none
// ---------------------------------------------------------------------------------------
void CStdString::CopyString(PCTSTR p_szSource, PTSTR p_szDest, int p_nMaxChars)
{
	int nSrcLen = ( p_szSource == NULL ? 0 : _tcslen(p_szSource) );
	int nChars = ( p_nMaxChars > 0 ? min(p_nMaxChars,nSrcLen) : nSrcLen );
	memcpy(p_szDest, p_szSource, nChars * sizeof(TCHAR));
	p_szDest[nChars] = '\0';
}
void CStdString::CopyString(PCOSTR p_szSource, POSTR p_szDest, int p_nMaxChars)
{
#ifdef _UNICODE
	int nSrcLen = ( p_szSource == NULL ? 0 : strlen(p_szSource) );
#else
	int nSrcLen = ( p_szSource == NULL ? 0 : wcslen(p_szSource) );
#endif
	int nChars = ( p_nMaxChars > 0 ? min(p_nMaxChars,nSrcLen) : nSrcLen );
	memcpy(p_szDest, p_szSource, nChars * sizeof(TOTHER));
	p_szDest[nChars] = '\0';
}
void CStdString::CopyString(PCSTR p_szSource, PWSTR p_szDest, int p_nMaxChars)
{
	USES_CONVERSION;
	PCWSTR szConverted = (A2W(p_szSource));
	int nSrcLen = ( szConverted == NULL ? 0 : wcslen(szConverted) );
	int nChars = ( p_nMaxChars > 0 ? min(p_nMaxChars,nSrcLen) : nSrcLen );
	memcpy(p_szDest, szConverted, nChars * sizeof(wchar_t));
	p_szDest[nChars] = '\0';
}
void CStdString::CopyString(PCWSTR p_szSource, PSTR p_szDest, int p_nMaxChars)
{
	USES_CONVERSION;
	PCSTR szConverted = (W2A(p_szSource));
	int nSrcLen = ( szConverted == NULL ? 0 : strlen(szConverted) );
	int nChars = ( p_nMaxChars > 0 ? min(p_nMaxChars,nSrcLen) : nSrcLen );
	memcpy(p_szDest, szConverted, nChars);
	p_szDest[nChars] = '\0';
}


// Special, TEMPORARY operators that allow us to serialize CStdStrings to CArchives.
#ifdef _MFC_VER
CArchive& AFXAPI operator<<(CArchive& ar, const CStdString& string)
{
	USES_CONVERSION;

	// All CStdStrings are serialized as wide strings
	PCWSTR pWide = T2CW(string.data());
	int nChars = wcslen(pWide);
	ar << nChars;
	ar.Write(pWide, nChars*sizeof(wchar_t));
	return ar;
}

CArchive& AFXAPI operator>>(CArchive& ar, CStdString& string)
{
	// All CStdStrings are serialized as wide strings
	UINT nLen;
	ar >> nLen;
	if ( nLen > 0 )
	{
		UINT nByteLen = nLen * sizeof(wchar_t);
		PWSTR pWide = (PWSTR)_alloca(nByteLen+sizeof(wchar_t));
		VERIFY(ar.Read(pWide, nByteLen) == nByteLen);
		pWide[nLen] = '\0';
		string = CStdString(pWide);
	}
	else
	{
		string.erase();
	}
	return ar;
}
#endif
#ifdef STDSTRING_INC_COMDEF
// -----------------------------------------------------------------------------
// FUNCTION: CStdString::StreamSave
//		HRESULT StreamSave(IStream* pStream) const;
//
// DESCRIPTION:
//		This function write the length and contents of the CStdString object
//		out to an IStream as a char based string;
//
// PARAMETERS:
//		pStream - the stream to which the string must be written
//
// RETURN VALUE: 
//		HRESULT return valued of IStream Write function
// -----------------------------------------------------------------------------
HRESULT CStdString::StreamSave(IStream* pStream) const
{
	USES_CONVERSION;
	HRESULT hr = E_FAIL;
	ASSERT(pStream != NULL);

	// All CStdStrings are serialized as wide strings

//	PCWSTR pWide = T2CW(this->data());
//	ULONG nChars = wcslen(pWide);
	PCSTR pStr = T2CA(this->data());
	ULONG nChars = strlen(pStr);

	if ( FAILED(hr=pStream->Write(&nChars, sizeof(ULONG), NULL)) )
		TRACE(_T("CStdString::StreamSave -- Unable to write length to IStream due to error 0x%X\n"), hr);
	else if ( nChars < 0 )
		;	// (zero length means nothing to write)
	else if ( FAILED(hr=pStream->Write(pStr, nChars*sizeof(char), NULL)) )
		TRACE(_T("CStdString::StreamSave -- Unable to write string to IStream due to error 0x%X\n"), hr);

	return hr;
}

// -----------------------------------------------------------------------------
// FUNCTION: CStdString::StreamLoad
//		HRESULT StreamLoad(IStream* pStream);
//
// DESCRIPTION:
//		This function reads in a CStdString object from an IStream
//
// PARAMETERS:
//		pStream - the stream from which the string must be read
//
// RETURN VALUE: 
//		HRESULT return value of the IStream Read function
// -----------------------------------------------------------------------------
HRESULT CStdString::StreamLoad(IStream* pStream)
{
	// All CStdStrings are serialized as char strings
	ASSERT(pStream != NULL);

	ULONG nChars;
	HRESULT hr = E_FAIL;
	if ( FAILED(hr=pStream->Read(&nChars, sizeof(ULONG), NULL)) )
	{
		TRACE(_T("CStdString::StreamLoad -- Unable to read length from IStream due to error 0x%X\n"), hr);
	}
	else if ( nChars > 0 )
	{
	//	ULONG nByteLen = nChars * sizeof(wchar_t);
		ULONG nByteLen = nChars * sizeof(char);
//		PWSTR pWide = (PWSTR)_alloca(nByteLen+sizeof(wchar_t));		// add an extra wchar_t for terminating NULL
		PSTR pStr = (PSTR)_alloca(nByteLen+sizeof(char));		// add an extra char for terminating NULL
		if ( FAILED(hr=pStream->Read(pStr, nByteLen, NULL)) )
			TRACE(_T("CStdString::StreamLoad -- Unable to read string from IStream due to error 0x%X\n"), hr);
		pStr[nChars] = '\0';
		*this = CStdString(pStr);
	}
	else
	{
		this->erase();
	}
	return hr;
}

// -----------------------------------------------------------------------------
// FUNCTION: CStdString::StreamSize
//		ULONG StreamSize() const;	
//           
// DESCRIPTION:
//		This function tells the caller how many bytes will be required to write
//		this CStdString object to an IStream using the StreamSave() function.
//		This is the capability lacking in CComBSTR which would force an IPersistXXX
//		implementation to know the implementation details of CComBSTR::StreamSave
//		in order to use CComBSTR in an IPersistXXX implementation.  
//
// PARAMETERS: none
// RETURN VALUE: 
//		length in bytes required to write the CStdString
// -----------------------------------------------------------------------------
ULONG CStdString::StreamSize() const
{
	USES_CONVERSION;
	return ( strlen(T2CA(this->data())) * sizeof(char) ) + sizeof(ULONG);
///	return ( wcslen(T2CW(this->data())) * sizeof(wchar_t) ) + sizeof(ULONG);
}

#endif


// -----------------------------------------------------------------------------
// FUNCTION: WUSysMessage
//		TSTRING WUSysMessage(DWORD p_dwError, bool bUseDefault=false)
//           
// DESCRIPTION:
//		This function simplifies the process of obtaining a string equivalent
//		of a system error code returned from GetLastError().  You simply
//		supply the value returned by GetLastError() to this function and the
//		corresponding system string is returned in the form of a CStdString.
//
// PARAMETERS: 
//		p_dwError - a DWORD value representing the error code to be translated
//		p_dwLangId - the language id to use.  defaults to english.
//
// RETURN VALUE: 
//		a CStdString equivalent of the error code.  Currently, this function
//		only returns either English of the system default language strings.  
// -----------------------------------------------------------------------------
#define MAX_FMT_TRIES	5
#define FMT_BLOCK_SIZE	256
CStdString WUSysMessage(DWORD p_dwError, DWORD p_dwLangId)
{
	TCHAR szBuf[512];

	if ( ::FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM, NULL, p_dwError, p_dwLangId, szBuf, 511, NULL) != 0 )
		return WUFormat(_T("%s (0x%X)"), szBuf, p_dwError);
    else
 		return WUFormat(_T("Unknown error (0x%X)"), p_dwError);
}




// ---------------------------------------------------------------------------------------------------
// GLOBAL FUNCTION:  WUFormat
//		CStdString WUFormat(UINT nId, ...);
//		CStdString WUFormat(PCTSTR szFormat, ...);
//
// REMARKS:
//		This function allows the caller for format and return a CStdString object with a single line
//		of code.  Frequently you want to print out a formatted string but don't care about it once
//		you are done with it.  You end up having to create temporary CStdString objects and then
//		calling their Format() functions.   By using this function instead, you can cut down on the
//		clutter.
// ---------------------------------------------------------------------------------------------------
CStdString WUFormat(UINT nId, ...)
{
	va_list argList;
	va_start(argList, nId);

	CStdString strFmt;
	CStdString strOut;
	if ( strFmt.Load(nId) )
		strOut.FormatV(strFmt, argList);

	va_end(argList);
	return strOut;
}
CStdString WUFormat(PCTSTR szFormat, ...)
{
	va_list argList;
	va_start(argList, szFormat);
	CStdString strOut;
	strOut.FormatV(szFormat, argList);
	va_end(argList);
	return strOut;
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -