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

📄 strcore.cpp

📁 基于Nuleus操作系统和s3c4510的编写的EFC。已经包含了该EFC的设计说明。这是个实际产品的代码
💻 CPP
字号:
///////////////////////////////////////////////////////////////////////////////
//
// Copyright (c) 2003, Ulink Telecom Equipment Co., Ltd. All rights reserved.
//
// File:
//
//    StrCore.cpp
//
// Abstract:
//
//    implementation of the CString class. This class is a helper for string.
//
// History:
//
//    V1.0	2003-01-21	Alex Duan	Original version.
//    V1.1	2003-03-17	Alex Duan	Add searching & concatenation support
//    V1.2	2003-04-14	Alex Duan	Add numerical value converting support
//    V1.3	2003-04-15	Alex Duan	Add trimming support
//    V1.4	2003-04-17	Alex Duan	Modify = operator
//	  V1.6  2006-11-15	Bozhong Xu  Add FormatW() function
///////////////////////////////////////////////////////////////////////////////

#include "uktype.h"
#include "efcapi.h"
#include "STRCORE.H"
#include <stdio.h>
#include <ctype.h>

//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////

// Constructs empty CString
CString::CString()
{
	m_pchData = NULL;
}

CString::~CString()
{
	Empty();
}

// Copy constructor
CString::CString(const CString &strSrc)
{
	if (strSrc.m_pchData)
	{
		m_pchData = new char[strlen(strSrc.m_pchData) + 1];
		
		ASSERT(m_pchData);
		
		strcpy(m_pchData, strSrc.m_pchData);
		
		ASSERT(strcmp(m_pchData, strSrc.m_pchData) == 0);
	}
	else
	{
		m_pchData = NULL;
	}
}

// Constructs from a const ANSI string
CString::CString(LPCSTR lpsz)
{
	if (lpsz)
	{
		m_pchData = new char[strlen(lpsz) + 1];
		ASSERT(m_pchData);
		strcpy(m_pchData, lpsz);
		ASSERT(strcmp(m_pchData, lpsz) == 0);
	}
	else
	{
		m_pchData = NULL;
	}
}

// from unsigned chars
CString::CString(const unsigned char* psz)
{
	if (psz)
	{
		m_pchData = new char[strlen((LPCSTR)psz) + 1];
		ASSERT(m_pchData);
		strcpy(m_pchData, (LPCSTR)psz);
		ASSERT(strcmp(m_pchData, (LPCSTR)psz) == 0);
	}
	else
	{
		m_pchData = NULL;
	}
}

// Copies an ANSI string.
const CString& CString::operator=(LPCSTR lpsz)
{
	char *pchData = NULL;
	if (lpsz) 
	{// create a new string same as lpsz
		pchData = new char[strlen(lpsz) + 1];
		ASSERT(pchData);
		strcpy(pchData, lpsz);
		ASSERT(strcmp(pchData, lpsz) == 0);
	}
	Empty(); // free old string data
	m_pchData = pchData;
	return *this;
}

// Returns a pointer to the characters in the CString.
LPSTR CString::GetBuffer(int nMinBufLength)
{
	int nLength = GetLength();
	if (nMinBufLength > nLength)
	{
		LPSTR lpsz = new char[nMinBufLength + 1];
		ASSERT(lpsz);
		if (nLength)
		{
			strcpy(lpsz, m_pchData);
			ASSERT(strcmp(lpsz, m_pchData) == 0);
			delete[] m_pchData;
		}
		m_pchData = lpsz;
		m_pchData[nMinBufLength] = '\0';
	}
	return m_pchData;
}

// Releases control of the buffer return by GetBuffer.
void CString::ReleaseBuffer(int nNewLength)
{
	if (m_pchData == NULL)
		return;

	int nLength = GetLength();
	if (nNewLength == -1 || nNewLength > nLength) 
		nNewLength = nLength;
	m_pchData[nNewLength] = '\0';
}

// Compares two strings (case sensitive).
int CString::Compare(LPCSTR lpsz) const
{
	int nRetVal = 0;
	if (m_pchData != lpsz)
	{
		if (m_pchData != NULL && lpsz != NULL) 
		{
			nRetVal = strcmp(m_pchData, lpsz);
		}
		else
		{
			nRetVal = (m_pchData == NULL) ? -1 : 1;
		}
	}
	return nRetVal;
}

///////////////////////////////////////////////////////////////////////////////
// CString friend functions.

// Concatenates two CString
CString operator+(const CString& string1, const CString& string2)
{
	CString strRetVal;
	int nLen1 = string1.GetLength();
	int nLen2 = string2.GetLength();
	strRetVal.m_pchData = new char[nLen1 + nLen2 + 1];

	ASSERT(strRetVal.m_pchData);

	memcpy(strRetVal.m_pchData, (LPCSTR)string1, nLen1);

	ASSERT(memcmp(strRetVal.m_pchData, (LPCSTR)string1, nLen1) == 0);

	memcpy(strRetVal.m_pchData + nLen1, (LPCSTR)string2, nLen2);

	ASSERT(memcmp(strRetVal.m_pchData + nLen1, (LPCSTR)string2, nLen2) == 0);
	
	strRetVal.m_pchData[nLen1 + nLen2] = '\0';
	return strRetVal;
}

///////////////////////////////////////////////////////////////////////////////
// Parameters:
//		lpszSub	A substring to search for
//		nStart	The zero-based index of the character in the string to begin
//				the search with.
// Return Value:
//		The zero-based index of the first character in this CString object that 
//		matches the requested substring or characters; -1 if the substring or 
//		character is not found.
// Remarks:
//		Searches this string for the first match of a substring. 
int CString::Find(LPCSTR lpszSub, int nStart) const
{
	ASSERT(lpszSub);
	
	if (nStart >= GetLength())
		return -1;
	
	// find first matching substring
	LPCSTR lpsz = strstr(m_pchData + nStart, lpszSub);
	
	// return -1 for not found, distance from beginning otherwise
	return (lpsz == NULL) ? -1 : (int)(lpsz - m_pchData);	
}

///////////////////////////////////////////////////////////////////////////////
// Parameters:
//		ch		The character to search for.
//		nStart	The zero-based index of the character in the string to begin
//				the searching with.
// Return Value:
//		The zero-based index of the first character  in  this CString  object 
//		that matches the requested characters; -1 if the substring or character
//		is not found.
int CString::Find(char ch, int nStart) const
{
	if (nStart >= GetLength())
		return -1;
	
	// find first single character
	LPCSTR lpsz = strchr(m_pchData + nStart, ch);
	
	// return -1 if not found and index otherwise
	return (lpsz == NULL) ? -1 : (int)(lpsz - m_pchData);	
}

///////////////////////////////////////////////////////////////////////////////
// Parameters:
//		ch	The character to search for.
// Return Value:
//		The zero-based index of the last character in this CString object
//		that matches the requested characters. -1 if the character is not
//		found.
// Remarks:
//		Searches this CString object for the last match of a character.
int CString::ReverseFind(char ch) const
{
	if (GetLength() == 0)
		return -1;

	// find last single character
	LPCSTR lpsz = strrchr(m_pchData, ch);
	
	// return -1 if not found, distance from beginning otherwise
	return (lpsz == NULL) ? -1 : (int)(lpsz - m_pchData);	
}

///////////////////////////////////////////////////////////////////////////////
// Parameters:
//		lpszCharSet	String containing characters for matching.
// Return Value:
//		The zero-based index of the first character in this string that is 
//		also in lpszCharSet; –1 if there is no match.
// Remarks:
//		Searches this string  for  the  first character  that  matches any 
//		character contained in lpszCharSet.
int CString::FindOneOf(LPCSTR lpszCharSet) const
{
	ASSERT(lpszCharSet);

	if (GetLength() == 0)
		return -1;

	LPCSTR lpsz = strpbrk(m_pchData, lpszCharSet);
	return (lpsz == NULL) ? -1 : (int)(lpsz - m_pchData);
}

///////////////////////////////////////////////////////////////////////////////
// CString formatting

#ifndef MAX_FORMAT_BUFFER
#define MAX_FORMAT_BUFFER	255
#endif
///////////////////////////////////////////////////////////////////////////////
// Parameters:
//		lpszFormat	A format-control string.
//		argList		A list of arguments to be passed.
// Remarks:
//		Formats the string as vsprintf does. Be careful to insure that the 
//		length of the string is less than MAX_FORMAT_BUFFER
void CString::FormatV(LPCSTR lpszFormat, va_list argList)
{
	ASSERT(lpszFormat);

	GetBuffer(MAX_FORMAT_BUFFER);
	VERIFY(vsprintf(m_pchData, lpszFormat, argList) <= MAX_FORMAT_BUFFER);
	ReleaseBuffer();
}

// formatting (big data block)
// Parameters:
//		lpszFormat	A format-control string.
//		nMaxLen		Maximal data length
void CString::FormatW(UINT nMaxLen, LPCSTR lpszFormat, ...)
{
	ASSERT(lpszFormat);
	
	va_list argList;
	va_start(argList, lpszFormat);
	
	GetBuffer(nMaxLen);
	VERIFY(vsprintf(m_pchData, lpszFormat, argList) > 0);
	ReleaseBuffer();	
	
	va_end(argList);
}

// formatting (using sprintf style formatting)
void CString::Format(LPCSTR lpszFormat, ...)
{
	ASSERT(lpszFormat);
	
	va_list argList;
	va_start(argList, lpszFormat);
	FormatV(lpszFormat, argList);
	va_end(argList);
}

///////////////////////////////////////////////////////////////////////////////
// Very simple sub-string extraction

///////////////////////////////////////////////////////////////////////////////
// Parameters:
//		nFirst	The zero-based index of the first character in this CString
//				object that is to be included in the extracted substring.
//		nCount	The number of characters to extract from this CString object.
// Return Value:
//		A CString object containing a copy of the specified range of characters.
// Remarks:
//		Extracts a substring of length nCount characters from this CString 
//		object, starting at position nFirst (zero-based). 
CString CString::Mid(int nFirst, int nCount) const
{
	// out-of-bounds requests return sensible things
	if (nFirst < 0)
		nFirst = 0;
	if (nCount < 0)
		nCount = 0;
	
	int nLength = GetLength();
	if (nFirst > nLength)
	{
		nFirst = nCount = 0;
	}
	else if (nFirst + nCount > nLength)
	{
		nCount = nLength - nFirst;
	}
	ASSERT(nFirst >= 0);
	ASSERT(nFirst + nCount <= nLength);
	
	// optimize case of returning entire string
	if (nFirst == 0 && nFirst + nCount == nLength)
		return *this;
	
	CString strRetVal;
	if (nCount)
	{
		memcpy(strRetVal.GetBuffer(nCount), m_pchData + nFirst, nCount);
		strRetVal.ReleaseBuffer();
	}
	return strRetVal;
}

///////////////////////////////////////////////////////////////////////////////
// Parameters:
//		lpszLeft	the left substring of the extracted substring.
//		lpszRight	the right substring of the extracted substring.
// Return Value:
//		A CString object containing a copy of the specified range of characters.
// Remarks:
//		Extracts a substring from this CString object, starting at lpszLeft, 
//		stopping at lpszRight (excluded lpszLeft & lpszRight; case sensitive)
CString CString::Mid(LPCSTR lpszLeft, LPCSTR lpszRight) const
{
	int nLeftPos, nRightPos, nLeftLen, nRightLen, nCount = 0;

	nLeftLen  = (lpszLeft == NULL)  ? 0 : strlen(lpszLeft);
	nRightLen = (lpszRight == NULL) ? 0 : strlen(lpszRight);
	nLeftPos  = (nLeftLen  == 0)    ? 0 : Find(lpszLeft);
	if (nLeftPos != -1)
	{
		nLeftPos += nLeftLen;
		nRightPos = (nRightLen == 0) ? GetLength() : Find(lpszRight, nLeftPos);
		if (nRightPos != -1)
		{
			nCount = nRightPos - nLeftPos;
		}
	}
	return Mid(nLeftPos, nCount);
}

///////////////////////////////////////////////////////////////////////////////
// Parameters:
//		chLeft	the left character of the extracted substring.
//		chRight	the right character of the extracted substring.
// Return Value:
//		A CString object containing a copy of the specified range of characters.
// Remarks:
//		Extracts a substring from this CString object, starting at chLeft, 
//		stopping at chRight (excluded chLeft & chRight; case sensitive)
CString CString::Mid(char chLeft, char chRight) const
{
	int nLeftPos, nRightPos, nCount = 0;

	nLeftPos = Find(chLeft);
	if (nLeftPos != -1) 
	{
		nLeftPos += 1;
		nRightPos = Find(chRight, nLeftPos);
		if (nRightPos != -1)
		{
			nCount = nRightPos - nLeftPos;
		}
	}
	return Mid(nLeftPos, nCount);
}

///////////////////////////////////////////////////////////////////////////////
// Parameters:
//		lpszTarget	A pointer to a string containing the target characters to 
//					be trimmed.
// Remarks:
//		Removes a particular group of characters from the end of a string.
void CString::TrimRight(LPCSTR lpszTarget)
{
	if (IsEmpty())
		return;
	if (lpszTarget == NULL || *lpszTarget == '\0')
		return;
	// find beginning of trailing matches
	// by starting at beginning

	LPSTR lpsz = m_pchData;
	LPSTR lpszLast = NULL;

	while (*lpsz != '\0')
	{
		if (strchr(lpszTarget, *lpsz) != NULL)
		{
			if (lpszLast == NULL)
				lpszLast = lpsz;
		}
		else
			lpszLast = NULL;
		lpsz++;
	}

	if (lpszLast != NULL)
	{
		// truncate at left-most matching character
		*lpszLast = '\0';
	}
}

///////////////////////////////////////////////////////////////////////////////
// Parameters:
//		chTarget	The target characters to be trimmed.
// Remarks:
//		Removes a particular character from the end of a string.
void CString::TrimRight(char chTarget)
{
	if (IsEmpty())
		return;
	// find beginning of trailing matches
	// by starting at beginning (DBCS aware)

	LPSTR lpsz = m_pchData;
	LPSTR lpszLast = NULL;

	while (*lpsz != '\0')
	{
		if (*lpsz == chTarget)
		{
			if (lpszLast == NULL)
				lpszLast = lpsz;
		}
		else
			lpszLast = NULL;
		lpsz++;
	}

	if (lpszLast != NULL)
	{
		// truncate at left-most matching character
		*lpszLast = '\0';
	}
}

// Trims trailing whitespace characters from the end of a string.
void CString::TrimRight()
{
	if (IsEmpty())
		return;
	// find beginning of trailing spaces by starting at beginning

	LPSTR lpsz = m_pchData;
	LPSTR lpszLast = NULL;

	while (*lpsz != '\0')
	{
		if (isspace(*lpsz))
		{
			if (lpszLast == NULL)
				lpszLast = lpsz;
		}
		else
			lpszLast = NULL;
		lpsz++;
	}

	if (lpszLast != NULL)
	{
		// truncate at trailing space start
		*lpszLast = '\0';
	}
}

///////////////////////////////////////////////////////////////////////////////
// Parameters:
//		lpszTarget	A pointer to a string containing the target characters to 
//					be trimmed.
// Remarks:
//		Removes a particular group of characters from the beginning of a string.
void CString::TrimLeft(LPCSTR lpszTargets)
{
	if (IsEmpty())
		return;

	// if we're not trimming anything, we're not doing any work
	if (lpszTargets == NULL || *lpszTargets == '\0')
		return;

	LPCSTR lpsz = m_pchData;

	while (*lpsz != '\0')
	{
		if (strchr(lpszTargets, *lpsz) == NULL)
			break;
		lpsz++;
	}

	if (lpsz != m_pchData)
	{
		// fix up data and length
		int nDataLength = GetLength() - (lpsz - m_pchData);
		memmove(m_pchData, lpsz, nDataLength + 1);
	}
}

///////////////////////////////////////////////////////////////////////////////
// Parameters:
//		chTarget	The target characters to be trimmed.
// Remarks:
//		Removes a particular character from the beginning of a string.
void CString::TrimLeft(char chTarget)
{
	if (IsEmpty())
		return;
	// find first non-matching character

	LPCSTR lpsz = m_pchData;

	while (chTarget == *lpsz)
		lpsz++;

	if (lpsz != m_pchData)
	{
		// fix up data and length
		int nDataLength = GetLength() - (lpsz - m_pchData);
		memmove(m_pchData, lpsz, nDataLength + 1);
	}
}

// Trim leading whitespace characters from the string.
void CString::TrimLeft()
{
	if (IsEmpty())
		return;

	// find first non-space character
	
	LPCSTR lpsz = m_pchData;

	while (isspace(*lpsz))
		lpsz++;

	if (lpsz != m_pchData)
	{
		// fix up data and length
		int nDataLength = GetLength() - (lpsz - m_pchData);
		memmove(m_pchData, lpsz, nDataLength + 1);
	}
}

⌨️ 快捷键说明

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