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

📄 xstring.cpp

📁 读取XML信息
💻 CPP
📖 第 1 页 / 共 2 页
字号:
//
// Purpose:     Find a substring in a string (case insensitive)
//
// Parameters:  str     - pointer to string; upon return, str will be updated 
//                        with the character removals
//              substr  - substring to find
//
// Returns:     TCHAR * - Pointer to the first occurrence of substr in str, 
//                        or NULL if substr does not appear in string.  If 
//                        substr points to a string of zero length, the 
//                        function returns str.
//
TCHAR * _tcsistr(const TCHAR * str, const TCHAR * substr)
{
	if (!str || !substr || (substr[0] == _T('\0')))
		return (TCHAR *) str;

	size_t nLen = _tcslen(substr);
	while (*str)
	{
		if (_tcsnicmp(str, substr, nLen) == 0)
			break;
		str++;
	}

	if (*str == _T('\0'))
		str = NULL;

	return (TCHAR *) str;
}

///////////////////////////////////////////////////////////////////////////////
//
// _tcsistrrep()
//
// Purpose:     Replace one substring in a string with another 
//              substring (case insensitive)
//
// Parameters:  lpszStr    - Pointer to string; upon return, lpszStr will be 
//                           updated with the character removals
//              lpszOld    - Pointer to substring that is to be replaced
//              lpszNew    - Pointer to substring that will replace lpszOld
//              lpszResult - Pointer to buffer that receives result string.  
//                           This may be NULL, in which case the required size
//                           of the result buffer will be returned. (Call
//                           _tcsistrrep once to get size, then allocate 
//                           buffer, and call _tcsistrrep again.)
//
// Returns:     int        - Size of result string.  If lpszResult is NULL,
//                           the size of the buffer (in TCHARs) required to 
//                           hold the result string is returned.  Does not 
//                           include terminating nul character.  Returns 0
//                           if no replacements.
//
int _tcsistrrep(const TCHAR * lpszStr, 
				const TCHAR * lpszOld, 
				const TCHAR * lpszNew, 
				TCHAR * lpszResult)
{
	if (!lpszStr || !lpszOld || !lpszNew)
		return 0;

	size_t nStrLen = _tcslen(lpszStr);
	if (nStrLen == 0)
		return 0;

	size_t nOldLen = _tcslen(lpszOld);
	if (nOldLen == 0)
		return 0;

	size_t nNewLen = _tcslen(lpszNew);

	// loop once to figure out the size of the result string
	int nCount = 0;
	TCHAR *pszStart = (TCHAR *) lpszStr;
	TCHAR *pszEnd = (TCHAR *) lpszStr + nStrLen;
	TCHAR *pszTarget = NULL;
	TCHAR * pszResultStr = NULL;

	while (pszStart < pszEnd)
	{
		while ((pszTarget = _tcsistr(pszStart, lpszOld)) != NULL)
		{
			nCount++;
			pszStart = pszTarget + nOldLen;
		}
		pszStart += _tcslen(pszStart);
	}

	// if any changes, make them now
	if (nCount > 0)
	{
		// allocate buffer for result string
		size_t nResultStrSize = nStrLen + (nNewLen - nOldLen) * nCount + 2;
		pszResultStr = new TCHAR [nResultStrSize];
		ZeroMemory(pszResultStr, nResultStrSize*sizeof(TCHAR));

		pszStart = (TCHAR *) lpszStr;
		pszEnd = (TCHAR *) lpszStr + nStrLen;
		TCHAR *cp = pszResultStr;

		// loop again to actually do the work
		while (pszStart < pszEnd)
		{
			while ((pszTarget = _tcsistr(pszStart, lpszOld)) != NULL)
			{
				int nCopyLen = (int)(pszTarget - pszStart);
				_tcsncpy(cp, &lpszStr[pszStart-lpszStr], nCopyLen);

				cp += nCopyLen;

				pszStart = pszTarget + nOldLen;

				_tcscpy(cp, lpszNew);

				cp += nNewLen;
			}
			_tcscpy(cp, pszStart);
			pszStart += _tcslen(pszStart);
		}

		//TRACE("pszResultStr=<%s>\n", pszResultStr);

		_ASSERTE(pszResultStr[nResultStrSize-2] == _T('\0'));

		if (lpszResult && pszResultStr)
			_tcscpy(lpszResult, pszResultStr);
	}

	int nSize = 0;
	if (pszResultStr)
	{
		nSize = (int)_tcslen(pszResultStr);
		delete [] pszResultStr;
	}

	//TRACE("_tcsistrrep returning %d\n", nSize);

	return nSize;
}

///////////////////////////////////////////////////////////////////////////////
//
// _tcstrim()
//
// Purpose:     Removes (trims) leading and trailing whitespace characters 
//              from a string
//
// Parameters:  str     - Pointer to the null-terminated string to be trimmed. 
//                        On return, str will hold the trimmed string.
//              targets - Pointer to string containing whitespace characters.
//                        If this is NULL, a default set of whitespace
//                        characters is used.
//
// Returns:     TCHAR * - Pointer to trimmed string
//
TCHAR *_tcstrim(TCHAR *str, const TCHAR *targets)
{
	return _tcsltrim(_tcsrtrim(str, targets), targets);
}

///////////////////////////////////////////////////////////////////////////////
//
// _tcsrtrim()
//
// Purpose:     Removes (trims) trailing whitespace characters from a string
//
// Parameters:  str     - Pointer to the null-terminated string to be trimmed. 
//                        On return, str will hold the trimmed string.
//              targets - Pointer to string containing whitespace characters.
//                        If this is NULL, a default set of whitespace
//                        characters is used.
//
// Returns:     TCHAR * - Pointer to trimmed string
//
TCHAR *_tcsrtrim(TCHAR *str, const TCHAR *targets)
{
	TCHAR *end;
	
	if (!str)
		return NULL;

	if (!targets)
		targets = _T(" \t\n\r");
		
	end = str + _tcslen(str);

	while (end-- > str)
	{
		if (!_tcschr(targets, *end))
			return str;
		*end = 0;
	}
	return str;
}

///////////////////////////////////////////////////////////////////////////////
//
// _tcsltrim()
//
// Purpose:     Removes (trims) leading whitespace characters from a string
//
// Parameters:  str     - Pointer to the null-terminated string to be trimmed. 
//                        On return, str will hold the trimmed string.
//              targets - Pointer to string containing whitespace characters.
//                        If this is NULL, a default set of whitespace
//                        characters is used.
//
// Returns:     TCHAR * - Pointer to trimmed string
//
TCHAR *_tcsltrim(TCHAR *str, const TCHAR *targets)
{
	if (!str)
		return NULL;
	
	if (!targets)
		targets = _T(" \t\r\n");
	
	while (*str)
	{
		if (!_tcschr(targets, *str))
			return str;
		++str;
	}
	return str;
}

///////////////////////////////////////////////////////////////////////////////
//
// _tcszdup()
//
// Purpose:     Allocates buffer with new, fills it with zeros, copies string
//              to buffer
//
// Parameters:  str     - Pointer to the null-terminated string to be copied. 
//
// Returns:     TCHAR * - Pointer to duplicated string (allocated with new)
//
TCHAR *_tcszdup(const TCHAR *str)
{
	if (!str)
		return NULL;

	size_t len = _tcslen(str);
	TCHAR *cp = new TCHAR [len + 16];
	memset(cp, 0, (len+16)*sizeof(TCHAR));
	_tcsncpy(cp, str, len);

	return cp;
}

///////////////////////////////////////////////////////////////////////////////
//
// _tcsnzdup()
//
// Purpose:     Allocates buffer with new, fills it with zeros, copies count
//              characters from string to buffer
//
// Parameters:  str     - Pointer to the null-terminated string to be copied
//              count   - Number of characters to copy
//
// Returns:     TCHAR * - Pointer to duplicated string (allocated with new)
//
TCHAR *_tcsnzdup(const TCHAR *str, size_t count)
{
	if (!str)
		return NULL;

	TCHAR *cp = new TCHAR [count + 16];
	memset(cp, 0, (count+16)*sizeof(TCHAR));
	_tcsncpy(cp, str, count);

	return cp;
}

///////////////////////////////////////////////////////////////////////////////
//
// _tcsccnt()
//
// Purpose:     Count number of occurrences of a character in a string
//
// Parameters:  str - pointer to string
//              ch  - character to look for
//
// Returns:     int - Number of times ch is found in str
//
int _tcsccnt(const TCHAR *str, TCHAR ch)
{
	if (!str)
		return 0;

	int count = 0;

	while (*str)
	{
		if (*str++ == ch)
			count++;
	}

	return count;
}

⌨️ 快捷键说明

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