// --------
// Strings
// --------
void KStrings::SplitString(	const KString&	String,
							LPCTSTR			pSplitter,
							bool			bAddEmpty,
							bool			bClearFirst)
{
	if(bClearFirst)
		Clear();

	const size_t szSplitterLength = _tcslen(pSplitter);

	size_t szPos = 0;
	for(;;)
	{
		size_t szOldPos = szPos;
		szPos = String.Find(pSplitter, szPos);
		if(szPos == UINT_MAX)
		{
			if(bAddEmpty || szOldPos < String.GetLength())
				*AddLast() = String.Mid(szOldPos);

			break;
		}

		if(bAddEmpty || szPos > szOldPos)
			*AddLast() = String.Mid(szOldPos, szPos - szOldPos), szPos += szSplitterLength;
	}
}
void KRegistryKey::WriteString(const KString& ValueName, const KString& Value)
{
	DEBUG_VERIFY_ALLOCATION;

	DEBUG_VERIFY(IsOpen());

	size_t szLength = Value.GetLength();
	TValueArray<TCHAR, true> Buf;
	Buf.Allocate(szLength + 2), Buf.Add(szLength + 2);
	memcpy(Buf.GetDataPtr(), (LPCTSTR)Value, (szLength + 1)*sizeof(TCHAR)), Buf[szLength + 1] = 0;

	if(RegSetValueEx(m_hKey, ValueName, NULL, REG_SZ, (BYTE*)Buf.GetDataPtr(), (szLength + 1) * sizeof(TCHAR)))
		INITIATE_FAILURE;
}
示例#3
0
// -------
// Tokens
// -------
KString TTokens::Process(const KString& String) const
{
	KString DstString;

	for(size_t szStart = 0 ; szStart < String.GetLength() ; )
	{
		// Scanning for the closest token starting at 'szStart'
		const TToken*	pClosestToken = NULL;
		size_t			szClosestTokenPos;

		for(size_t i = 0 ; i < GetN() ; i++)
		{
			const TToken& CurToken = GetDataRef(i);

			size_t szPos = String.Find(CurToken.m_SrcString, szStart);
			if(szPos == UINT_MAX)
				continue;

			if(	pClosestToken == NULL		||
				szPos < szClosestTokenPos	||
				szPos == szClosestTokenPos &&
					CurToken.m_SrcString.GetLength() >
						pClosestToken->m_SrcString.GetLength())
			{
				pClosestToken = &CurToken, szClosestTokenPos = szPos;
			}
		}
		
		if(pClosestToken == NULL) // no more tokens found
		{
			DstString += String.Mid(szStart); // adding leftovers
			break;
		}

		DstString += String.Mid(szStart, szClosestTokenPos - szStart); // adding pre-token part
		DstString += pClosestToken->m_DstString; // adding token replacement
		
		// Forwarding 'szStart' to the end of just substed token
		szStart = szClosestTokenPos + pClosestToken->m_SrcString.GetLength();
	}

	return DstString;
}
KString KString::operator + (const KString& SString) const
{
	return KString(GetDataPtr(), GetLength(), SString, SString.GetLength());
}