Click here to Skip to main content
15,899,754 members
Home / Discussions / C / C++ / MFC
   

C / C++ / MFC

 
GeneralHelp with searching for things in text files. Pin
Snyp15-Nov-03 12:00
Snyp15-Nov-03 12:00 
GeneralRe: Help with searching for things in text files. Pin
Peter Molnar15-Nov-03 14:42
Peter Molnar15-Nov-03 14:42 
GeneralRe: Help with searching for things in text files. Pin
Snyp15-Nov-03 14:44
Snyp15-Nov-03 14:44 
GeneralRe: Help with searching for things in text files. Pin
Antti Keskinen15-Nov-03 14:56
Antti Keskinen15-Nov-03 14:56 
GeneralRe: Help with searching for things in text files. Pin
Snyp15-Nov-03 14:59
Snyp15-Nov-03 14:59 
GeneralRe: Help with searching for things in text files. Pin
Peter Molnar15-Nov-03 14:57
Peter Molnar15-Nov-03 14:57 
GeneralRe: Help with searching for things in text files. Pin
Joe Woodbury15-Nov-03 18:57
professionalJoe Woodbury15-Nov-03 18:57 
GeneralRe: Help with searching for things in text files. Pin
Joe Woodbury15-Nov-03 18:50
professionalJoe Woodbury15-Nov-03 18:50 
Using memory mapped file functionality, map the destination file. Then search it using one of the following implementations of a Boyer-Moore text search:

// Boyer-Moore text search
const char* TextSearch(const char* pSearchStr, const char* pStr, int textLen)
{
	if (!pSearchStr || !*pSearchStr)
		return pStr;

	if (!pStr || !*pStr)
		return NULL;

	if (textLen < 0)
		textLen = (int)strlen(pStr);

	int searchStrLen = (int)strlen(pSearchStr);

	// Setup m_skip table
	int pSkipTable[256];

	int i;
	for (i = 0; i < 256; i++)
		pSkipTable[i] = searchStrLen;

	for (i = 0; pSearchStr[i]; i++)
		pSkipTable[(BYTE)pSearchStr[i]] = searchStrLen - 1 - i;

	register int right = searchStrLen - 1;
	while (right < textLen)
	{
		int j;
		for (j = searchStrLen - 1, i = right; j >= 0 && pSearchStr[j] == pStr[i]; j--)
			i--;

		if (j == -1)
			return &pStr[i + 1];

		right = max(i + pSkipTable[(BYTE)pStr[i]], right + 1);
	}
	return NULL;
}

const char* TextSearchNoCase(const char* pSearchStr, const char* pStr, int textLen)
{
	if (!pSearchStr || !*pSearchStr)
		return pStr;

	if (!pStr || !*pStr)
		return NULL;

	if (textLen < 0)
		textLen = (int)strlen(pStr);

	int searchStrLen = (int)strlen(pSearchStr);

	// Setup m_skip table
	int pSkipTable[256];

	int i;
	for (i = 0; i < 256; i++)
		pSkipTable[i] = searchStrLen;

	char* pSearchStrUpper = (char*) _alloca(searchStrLen);
	memcpy(pSearchStrUpper, pSearchStr, searchStrLen);
	CharUpperBuffA(pSearchStrUpper, searchStrLen);

	char* pSearchStrLower = (char*) _alloca(searchStrLen);
	memcpy(pSearchStrLower, pSearchStr, searchStrLen);
	CharLowerBuffA(pSearchStrLower, searchStrLen);

	for (i = 0; i < searchStrLen; i++)
	{
		pSkipTable[(BYTE)pSearchStrUpper[i]] =
		pSkipTable[(BYTE)pSearchStrLower[i]] = searchStrLen - 1 - i;
	}

	register int right = searchStrLen - 1;
	while (right < textLen)
	{
		int j;
		for (j = searchStrLen - 1, i = right; j >= 0 && 
			(pSearchStrLower[j] == pStr[i] || pSearchStrUpper[j] == pStr[i]); j--)
			i--;

		if (j == -1)
			return &pStr[i + 1];

		for (j = searchStrLen - 1; j >= 0; j--)
		{
			if (pSearchStrLower[j] == pStr[i] || pSearchStrUpper[j] == pStr[i])
			{
				right = max(i + pSkipTable[(BYTE)pStr[i]], right + 1);
				break;
			}
		}
		if (j < 0)
			right = max(i + searchStrLen, right + 1);
	}
	return NULL;
}


Anyone who thinks he has a better idea of what's good for people than people do is a swine.
- P.J. O'Rourke

General_CrtIsValidHeapPointer Pin
DougW4815-Nov-03 11:26
DougW4815-Nov-03 11:26 
GeneralWindows Explorer Pin
harsh91115-Nov-03 9:54
harsh91115-Nov-03 9:54 
GeneralRe: Windows Explorer Pin
Peter Molnar15-Nov-03 14:51
Peter Molnar15-Nov-03 14:51 
GeneralRe: Windows Explorer Pin
harsh91115-Nov-03 19:24
harsh91115-Nov-03 19:24 
GeneralRe: Windows Explorer Pin
Prakash Nadar15-Nov-03 19:51
Prakash Nadar15-Nov-03 19:51 
GeneralHTML default document icon Pin
alex.barylski15-Nov-03 8:40
alex.barylski15-Nov-03 8:40 
GeneralRe: HTML default document icon Pin
Peter Molnar15-Nov-03 14:28
Peter Molnar15-Nov-03 14:28 
GeneralRe: HTML default document icon Pin
alex.barylski15-Nov-03 14:38
alex.barylski15-Nov-03 14:38 
QuestionHow to duplicate an object? Pin
Atlence15-Nov-03 7:46
Atlence15-Nov-03 7:46 
AnswerRe: How to duplicate an object? Pin
Jeryth15-Nov-03 8:15
Jeryth15-Nov-03 8:15 
AnswerRe: How to duplicate an object? Pin
Tim Smith15-Nov-03 8:18
Tim Smith15-Nov-03 8:18 
GeneralRe: How to duplicate an object? Pin
Jeryth15-Nov-03 8:27
Jeryth15-Nov-03 8:27 
GeneralRe: How to duplicate an object? Pin
Anonymous15-Nov-03 9:17
Anonymous15-Nov-03 9:17 
GeneralMFC serialization fails from COM Pin
DimkaSPB15-Nov-03 7:44
DimkaSPB15-Nov-03 7:44 
GeneralChat Server&amp;Client Protocol + how2tx/rx question?! Pin
[ Jûroehn ]15-Nov-03 7:35
[ Jûroehn ]15-Nov-03 7:35 
GeneralRe: Chat Server&amp;Client Protocol + how2tx/rx question?! Pin
Peter Molnar15-Nov-03 14:07
Peter Molnar15-Nov-03 14:07 
GeneralDirectShow (DirectX) on CD Pin
TFB15-Nov-03 7:32
TFB15-Nov-03 7:32 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.