Click here to Skip to main content
15,891,423 members
Articles / Desktop Programming / MFC

CGoogle: A Google Search class

Rate me:
Please Sign up or sign in to vote.
4.84/5 (27 votes)
8 Dec 20053 min read 151.3K   2.3K   81  
A simple class to perform Google searches without the official Google API.
/* ===================================================================

CGoogle Class (Google.h and Google.cpp)

Author:  Stuart Konen
Contact: skonen@gmail.com

Description: An extremely simple class for searching Google
and retrieving the query results.

====================================================================*/


#if !defined(GOOGLE_H_INCLUDED_)
#define GOOGLE_H_INCLUDED_


#include "afxtempl.h"


struct GoogleResult
{
	CString cstrTitle;
	CString cstrURL;
	CString cstrDesc;

	size_t  nPage;	
};


class CGoogle  
{
public:
	CGoogle();
	virtual ~CGoogle();


	GoogleResult& operator[]( size_t nIndex )
	{
		return *GetAt(nIndex);
	}

	GoogleResult& operator[]( int nIndex )
	{
		return *GetAt(nIndex);
	}
	
	GoogleResult* GetAt( size_t nIndex );
	GoogleResult* GetAt( int nIndex )
	{
		return GetAt(static_cast<size_t>(nIndex));
	}
		


	bool PerformSearch( LPCTSTR lpszSearchTerms );
	void Reset();	


	__inline void SetSafeSearch ( bool bSafe )
	{
		(Reset(), m_bSafeSearch = bSafe); 
	}
	
	__inline size_t GetResultCount()
	{
		return m_nResultCount; 
	}


private:

	bool     internal_GetInnerText   ( CString& cstr, LPCTSTR lpszStart,
	                                   LPCTSTR lpszEnd, int &nFrom,
	                                   CString& cstrDest);

	bool     internal_DownloadPage   ( CString& cstrURL, CString& cstrDest );
	void     internal_PushResult     ( GoogleResult &grResult );	
	void     internal_ExtractResults ( CString& cstrHTML );		
	bool     internal_ExtractSpecial ( CString& cstrHTML, int &nStart );
	CString& internal_FormatQuery    ( LPCTSTR lpszSearchTerms );
	CString& internal_GetSearchURL   ( CString& cstrDest );

	size_t   internal_RetrieveResultCount();


	CArray<GoogleResult, GoogleResult> m_aResults;
		
	CString    m_cstrQuery;
	CString    m_cstrOrigQuery;
	size_t     m_nPage;
	size_t     m_nResultCount;

	bool       m_bSafeSearch;
};

#endif // !defined(GOOGLE_H_INCLUDED_)

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 has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
United States United States
Well first of all, it is fairly obvious my name is Stuart Konen (I'm sure 50% of you just took notice), all of my life I've lived on a farm located in Northern Idaho. What shatters the stereotype of rural residence however, is the fact that I'm very active in the technology and programming worlds. I took up the hobby of programming at age 9, at that point it was little language known as Quick Basic *sigh*. Fast forward another 9 years... (Woah... I just realized that's half of my existence. But that's something I'll have to contemplate later, as I have an autobiography to tell).

Now my experience in programming has improved vastly, I've released various technologies and programs and I'm continuing to pump out concepts and systems that are getting glances from all over the world. My weapon of choice is C++, the core language of the majority of freshly released software, it's sleak, mean, and incrediably powerful. On the side I venture into web application development and site design, where my interest lies in PHP. Over the years my project have included everything from Artificial Intelligence to Web Statistic Tracking systems, but that's the past. What matters is the future... Remember that question we were always asked in grade school? Where did we see ourselves in 10 years. Well that question was asked about 8 years ago in my life, so it looks as though I only have two more for planning stages. In two years I see myself plunging into the world of research, creating my own Research and Development firm, aiming to meet the never-ending need for new and superior software and technology. Soon after becoming a multi-billionare I'll pursue my dream of world domination. Nobody is perfect...

Actually when it comes down to things, the money has no meaning. But there you have it, a 5 minute slice of my thoughts and time... If you have any job opportunities or have the slight urge to initiate a conversation with me, it can be done via email: skonen [at] gmail [dot] com

Comments and Discussions