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

📄 searchthread.cpp

📁 本程序是VC为平台开发的股票资讯系统
💻 CPP
字号:
// SearchThread.cpp : implementation file
//

#include "stdafx.h"
#include "SearchThread.h"
#include "HttpDown.h"
#include "StockRefer.h"

#include "ParseNewsLink.h"
#include "MainFrm.h"
#include "TMainServer.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

extern HANDLE hConnection;
extern long lThreadCount;
extern long lNewsCount;

CString m_NoNextPage = _T("No Next Page");

// simple worker thread Proc function
UINT CSearchThread::ThreadFunc(LPVOID pParam)
{
	ThreadParams *lpThreadParams = (ThreadParams *)pParam;
	CSearchThread *lpThread = (CSearchThread *)lpThreadParams->m_pThread;
	
	lpThread->ThreadRun(lpThreadParams);

	// Use  SendMessage instead of PostMessage here to keep the current thread count
	// Synchronizied. If the number of threads is greater than MAX_WAIT_THREAD
	// the program will be come	 unresponsive to user input

	::SendMessage(AfxGetMainWnd()->m_hWnd,
		WM_USER_THREAD_DONE, 0, (LPARAM)lpThreadParams);  // deletes lpThreadParams and decrements the thread count

	return 0;
}

/////////////////////////////////////////////////////////////////////////////
// CSearchThread

CSearchThread::CSearchThread(AFX_THREADPROC pfnThreadProc,ThreadParams *pThreadParams)
	:CWinThread(pfnThreadProc,pThreadParams)
{
	pThreadParams->m_pThread = this;
}

CSearchThread::~CSearchThread()
{
}

BEGIN_MESSAGE_MAP(CSearchThread, CWinThread)
	//{{AFX_MSG_MAP(CSearchThread)
		// NOTE - the ClassWizard will add and remove mapping macros here.
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CSearchThread message handlers

BOOL CSearchThread::ThreadRun(ThreadParams *pThreadParams)
{
	CString str,cont;

	str = pThreadParams->m_Proxy;
	if(!str.IsEmpty())
	{
		m_Proxy = str;
		m_Port = pThreadParams->m_Port;
	}
	m_TimeOut = pThreadParams->m_TimeOut;

	if(lThreadCount >= pThreadParams->m_MaxThreadCount) 
		WaitForSingleObject(hConnection,INFINITE);

	GetNewsContent(pThreadParams);

	return TRUE;
}

BOOL CSearchThread::GetNewsContent(ThreadParams *pThreadParams,BOOL ToCut)
{
	CString site,link,title,body,cut,stime;
	int pos;
	CHttpDown down;
	
	site = GetLink(pThreadParams);
	if(site.IsEmpty()) return FALSE;

	pos = site.Find('\n');
	while(pos != -1)
	{
		link = site.Left(pos);
		if(link.IsEmpty()) return FALSE;
		site = site.Mid(pos + 1);
		pos = site.Find('\n');
		if(pos == -1) return FALSE;
		title = site.Left(pos);
		site = site.Mid(pos + 1);
		if(title.IsEmpty()) return FALSE;
		if(lThreadCount >= pThreadParams->m_MaxThreadCount) 
			WaitForSingleObject(hConnection,INFINITE);
		body = down.GetHttpFileGet(link,lThreadCount,
			m_TimeOut,m_Proxy,m_Port,pThreadParams->m_IngoreLegth);
		if(!body.IsEmpty())
		{
			if(ToCut)
				cut = pThreadParams->m_CutMore;
			body = GetContent(body,title,pThreadParams->m_Key,cut,
				pThreadParams->m_SearchTime);
			if(!body.IsEmpty())
			{
				stime = pThreadParams->m_SearchTime.Format(_T("%Y-%m-%d"));
				body = stime + _T("\n") + body;
				LPCTSTR lbody = body;
				::SendMessage(AfxGetMainWnd()->m_hWnd,
					WM_USER_SEND_BODY,(WPARAM)lThreadCount,(LPARAM)lbody);
			}
		}
		pos = site.Find('\n');
	}
	
	return TRUE;
}

CString CSearchThread::GetLink(ThreadParams *pThreadParams)
{
	CString site,stoday,sprev,snext;
	CHttpDown down;
	CParseNewsLink news;
	LinkCharacter *linkchar;
	int pos,end;

	pos = pThreadParams->m_BaseUrl.Find(_T("@Date:"));
	if(pos != -1){
		end = pThreadParams->m_BaseUrl.Find(_T("@End;"));
		if(end == -1) return _T("");
		sprev = pThreadParams->m_BaseUrl.Left(pos);
		snext = pThreadParams->m_BaseUrl.Mid(end + 5);
		stoday = pThreadParams->m_BaseUrl.Mid(pos + 6,end - pos - 6);
		stoday = pThreadParams->m_SearchTime.Format(stoday);
		pThreadParams->m_BaseUrl = sprev + stoday + snext;
	}
	site = down.GetHttpFileGet(pThreadParams->m_BaseUrl,lThreadCount,
			m_TimeOut,m_Proxy,m_Port,pThreadParams->m_IngoreLegth);
	if(site.IsEmpty())
		return _T("");
	
	linkchar = new LinkCharacter;
	linkchar->SearchTime = pThreadParams->m_SearchTime;
	linkchar->BaseUrl = pThreadParams->m_BaseUrl;
	linkchar->BaseUrl.Replace(_T("http://"),NULL);
	linkchar->StartPos = pThreadParams->m_StartPos;
	linkchar->FinishPos = pThreadParams->m_FinishPos;
	linkchar->LinkFilter = pThreadParams->m_LinkFilter;
	linkchar->TitleFilter = pThreadParams->m_TitleFilter;
	linkchar->ClearString = pThreadParams->m_ClearString;
	linkchar->VerifyDate = pThreadParams->m_VerifyDate;

	news.Copy(linkchar);
	site = news.ExtractLinkAndTitle(site);
	delete linkchar;

	return site;
}

CString CSearchThread::GetContent(CString body,CString title,CString key,CString cut,CTime NewsDate)
{
	CString content;
	CParseNewsLink news;
	content = news.GetNewsBody(body,title,key,cut);
	
	return content;
}

⌨️ 快捷键说明

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