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

📄 searchserver.history.cpp

📁 c++系统开发实例精粹内附的80例源代码 环境:windows2000,c++6.0
💻 CPP
字号:
//////////////////////////////////////////////////////////////////////
// FileFury
// Copyright (c) 2000 Tenebril Incorporated
// All rights reserved.
//
// This source code is governed by the Tenebril open source
// license (http://www.tenebril.com/developers/opensource/license.html)
//
// For more information on this and other open source applications,
// visit the Tenebril OpenSource page:
//       http://www.tenebril.com/developers/opensource
//
//////////////////////////////////////////////////////////////////////

// SearchServer.History.cpp: implementation of the CSearchServer class.
//
//////////////////////////////////////////////////////////////////////

#include "stdafx.h"
#include "Oscar.h"
#include "SearchServer.h"

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

BOOL CSearchServer::RegisterSearch(char cIP [ 4 ], int nID)
{
	// This function will not allow the server to duplicate searches.
	// Return false if we've already done this one.

	// First, find the machine in the buffer.
	SearchRecord *pRecord = FindRecord(cIP);

	if(pRecord)
	{
		// We found it.  Now update its ID number.
		if(pRecord->nID < nID)
		{
			pRecord->nID = nID;

			// (Move-to-front heuristic)

			// Move it to the front.
			if(!CutOutRecord(pRecord))
			{
				// It's already at the front.
			}
			else
			{
				pRecord->pNext = (SearchRecord *)m_pRecords;
				m_pRecords = pRecord;
			}
		}
		else
			return FALSE;           // We already have it.
	}
	else
	{
		// We couldn't find it.  Delete the last one, and then
		// add the new one to the front.

		DeleteLastItem();

		pRecord = new SearchRecord;
		memcpy((void *)pRecord->tczIP, (void *)cIP, 4);
		pRecord->nID = nID;
		pRecord->pNext = (SearchRecord *)m_pRecords;

		m_pRecords = pRecord;
	}

	return TRUE;
}

BOOL CSearchServer::AllocateRecords(int nRecords)
{
	if(m_pRecords) return FALSE;      // Already have them.
	if(nRecords < 1) return FALSE;    // Bounds.

	// The root.
	m_pRecords = new SearchRecord;
	memset((void *)m_pRecords, 0, sizeof(SearchRecord));

	// The children.
	SearchRecord *pRecord = m_pRecords;
	for(int i = 1; i < nRecords; i++)
	{
		ASSERT(pRecord);

		SearchRecord *pNewRecord = new SearchRecord;
		memset((void *)pNewRecord, 0, sizeof(SearchRecord));

		pRecord->pNext = (void *)pNewRecord;
		pRecord = pNewRecord;
	}

	return TRUE;
}

int CSearchServer::FreeRecords()
{
	if(!m_pRecords) return 0;

	int i = 0;
	SearchRecord *pRecord = m_pRecords;
	SearchRecord *pNextRecord;

	while(pRecord)
	{
		pNextRecord = (SearchRecord *)pRecord->pNext;
		delete pRecord; i++;
		pRecord = pNextRecord;
	}

	m_pRecords = NULL;

	return i;
}

SearchRecord *CSearchServer::FindRecord(char cIP[4])
{
	if(!m_pRecords) return NULL;
	// We don't store blanks as valid.
	if(cIP[0] == '\0' && cIP[1] == '\0' && cIP[2] == '\0' && cIP[3] == '\0')
		return NULL;

	SearchRecord *pRecord = m_pRecords;

	while(pRecord)
	{
		if(cIP[0] == pRecord->tczIP[0] &&
		   cIP[1] == pRecord->tczIP[1] &&
		   cIP[2] == pRecord->tczIP[2] &&
		   cIP[3] == pRecord->tczIP[3])
		   return pRecord;

		pRecord = (SearchRecord *)pRecord->pNext;
	}

	return NULL;
}

BOOL CSearchServer::CutOutRecord(SearchRecord *pRecord)
{
	if(!pRecord) return FALSE;
	if(!m_pRecords) return FALSE;

	SearchRecord *pSearchRecord = m_pRecords;

	while(pSearchRecord)
	{
		if(pSearchRecord->pNext == (void *)pRecord)
		{
			pSearchRecord->pNext = pRecord->pNext;
			pRecord->pNext = NULL;

			return TRUE;
		}

		pSearchRecord = (SearchRecord *)pSearchRecord->pNext;
	}

	return FALSE;   // It's already at the front.
}

BOOL CSearchServer::DeleteLastItem()
{
	if(!m_pRecords) return FALSE;

	SearchRecord *pRecord = m_pRecords;
	SearchRecord *pBefore = NULL;

	while(pRecord->pNext)
	{
		pBefore = pRecord;
		pRecord = (SearchRecord *)pRecord->pNext;
	}

	if(pBefore)      // There exist more than one entry.
	{
		pBefore->pNext = NULL;
		delete pRecord;
	}
	else             // Only one entry.
	{
		ASSERT(m_pRecords == pRecord);
		m_pRecords = NULL;
		delete pRecord;
	}

	return TRUE;
}

⌨️ 快捷键说明

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