Click here to Skip to main content
15,893,594 members
Articles / Desktop Programming / MFC

XSearch - a class that implements a search engine-style advanced search

Rate me:
Please Sign up or sign in to vote.
4.85/5 (31 votes)
16 May 2007CPOL5 min read 82.3K   1.3K   80  
XSearch implements a search engine-style advanced search, including ALL, EXACT PHRASE, AT LEAST ONE, and WITHOUT words. XSearch is based on a multiple-substring search algorithm.
// XStringSet.H: interface for the CIVStringSet class.
//
// Written 12 June 2002 by Scot T Brennecke
// Thanks to Moishe Halibard and Moshe Rubin for their article,
// "A Multiple Substring Search Algorithm" in the June 2002
// edition of C/C++ Users Journal.  This class is based on
// the algorthim therein described, but extended to return
// all strings and use MFC classes.
//
// Modified by Hans Dietrich  20 July 2005
//

#ifndef  XSTRINGSET_H 
#define  XSTRINGSET_H 

class CIVStringSet : public CStringArray
{
// Constructors / destructors
public:
	CIVStringSet(WORD wInitialWidth = 256);	// Initial width of FSM
	virtual ~CIVStringSet();

// Attributes
public:
	void	SetMatchCase(BOOL bFlag)  { m_bMatchCase = bFlag; }
	void	SetWholeWords(BOOL bFlag) { m_bWholeWords = bFlag; }
	void	SetFirstMatch(BOOL bFlag) { m_bFirstMatch = bFlag; }

// Operations
public:
	BOOL	Add(LPCTSTR pszWord);							// a single word
	UINT	FindFirstIn(CString strText, int & rnFirst);	// begin iteration
	UINT	FindNext(int & rnNext);							// continue interation

// Implementation
protected:
	DWORD	(* m_apnFSM)[128];	// Finite State Machine. Array of 128 char arrays
	size_t  m_nCurDim;			// Dimension of allocated width of FSM
	size_t  m_nUsedCols;		// Used portion of allocated width
	WORD	m_wMaxUsedState;	// largest state value used
	CString m_strSearch;		// Current search string
	UINT	m_nCurTextChar;		// Current position in search string
	BOOL	m_bMatchCase;		// TRUE = match case
	BOOL	m_bWholeWords;		// TRUE = match whole words only
	BOOL	m_bFirstMatch;		// TRUE = stop matching after first match
	BOOL	m_bMatchFound;		// TRUE = at least one word has been found

	BOOL	InsertWord(LPCTSTR pszWord, WORD wIndex);	// put the new word into 
														// the FSM for given index
	BOOL	SetColDim(size_t nNewDim);					// set the current width 
														// to at least nNewDim columns
	BOOL	IsWordChar(TCHAR c);						// returns TRUE if char is a 
														// word separator (a-z, 0-9, _)

private:
	// =====  Hide several base class members that shouldn't be used  =====
	//
	// Using these members could cause the index numbers of strings to change, 
	// throwing off the embedded index entries in the FSM
	void SetAt(int nIndex, LPCTSTR newElement);
	void SetAt(int nIndex, const CString & newElement);
	void SetAtGrow(int nIndex, LPCTSTR newElement);
	void SetAtGrow(int nIndex, const CString & newElement);
	int  Append(const CStringArray & src);
	void Copy(const CStringArray & src);
	void InsertAt(int nIndex, LPCTSTR newElement, int nCount = 1);
	void InsertAt(int nIndex, const CString & newElement, int nCount = 1);
	void RemoveAt(int nIndex, int nCount = 1);
	void InsertAt(int nStartIndex, CStringArray * pNewArray);
};

#endif // XSTRINGSET_H 

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer (Senior) Hans Dietrich Software
United States United States
I attended St. Michael's College of the University of Toronto, with the intention of becoming a priest. A friend in the University's Computer Science Department got me interested in programming, and I have been hooked ever since.

Recently, I have moved to Los Angeles where I am doing consulting and development work.

For consulting and custom software development, please see www.hdsoft.org.






Comments and Discussions