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

📄 hostsfile.cpp

📁 Boosts Browsing Speeds Up To 3,000 Times Faster.zip
💻 CPP
字号:
// HostsFile.cpp: implementation of the CHostsFile class.
//
//////////////////////////////////////////////////////////////////////
/**************************************************************
**        __                                          __
**     __/_/__________________________________________\_\__
**  __|_                                                  |__
** (___O)     William SerGio & Co., Inc.                 (O___)
**(_____O)	  www.software-rus.com         			    (O_____)
**(_____O)	  Author: Bill SerGio, Infomercial King(tm) (O_____)
** (__O)		                    					 (O__)
**    |___________________________________________________|
**
****************************************************************/

#include "stdafx.h"
#include "HostsFile.h"
#include "SuperCharger.h"
#include <algorithm>

// NEEDED FOR REMOVE TABS
//////////////////////////////////////////////
//#include <fstream.h>  //includes <iostream.h>
#include <stdlib.h>
#include <string.h>
#include <direct.h>
//////////////////////////////////////////////

#ifdef _MEMORY_DEBUG 
	#define new	   DEBUG_NEW  
	#define malloc DEBUG_MALLOC  
    static char THIS_FILE[] = __FILE__;  
#endif

#define CHostsFile_Class "CHostsFile"

CHostsFile::CHostsFile()
{

}

CHostsFile::~CHostsFile()
{
}

BOOL CHostsFile::LoadFile(const char* file, BOOL bAppend)
{
    #define MAX_LINE_LENGTH 1023
	BOOL m_bSort = FALSE;
	m_sFile	= file;

	if (!FileExists((char*)m_sFile.c_str()))
		return FALSE;

	if (!bAppend)
	    m_aHosts.clear();

	try
	{
		//Try to open file
		FILE* pFile;
		if (!(pFile=fopen(m_sFile.c_str(),"rt")))
			return FALSE;

		//Our map for tmps
		HostSet aSet;

		//Start to read the data
		while (!feof(pFile))
		{
			//Read it
			char aTmp[16384];
			if (fscanf(pFile,"%[^\n]",aTmp)==EOF)
				break;

			//  Remove final line feed
			//char *ptr;
            //ptr = strchr(aTmp,LINEFEED);
            //if ( ptr != NULL )
			//	*ptr = 0; 
			
			//And read NULL
			char aDummy[10];
			fscanf(pFile,"%[\n]",aDummy); 

			//Convert to lower case
			//strlwr(aTmp);

			////////////////////////////////////////////////
			// BILL SERGIO - Replaces all occurrences of tab
			// characters in aTmp[] with appropriate number 
			// of spaces if there is sufficient space in aTmp[]
			int i			= 0;
			int num_spaces	= 0;
			int tab_size	= 4;
			int num_tabs	= 0;
			char *ptr1, *ptr2;

			while ( ( ptr1 = strchr(aTmp,TAB) ) != NULL )
			{
				num_tabs++;
				num_spaces = tab_size - (ptr1-aTmp)%tab_size;
				if ( strlen(aTmp) + num_spaces > sizeof(aTmp) - 1 )
					 break;
				ptr2 = strchr(aTmp,0) + 1;
				while ( --ptr2 > ptr1 )
					*(ptr2+num_spaces-1) = *ptr2;
				for ( i=0; i<num_spaces; i++ )
					*(ptr1+i) = SPACE;
			}
			//////////////////////////////////////////////////

			CString sline;
			CString domain;
			CString temp;
			char aDomain[16384];

			sline = aTmp;
			sline.TrimLeft();

			i = sline.Find((TCHAR)'#');
			if (i==0)
			{
				//Do we have this comment item?
				if (aSet.find(aTmp)==aSet.end())
				{
					//Add to our string
					m_aHosts.push_back(aTmp);

					//AfxMessageBox((char*)aTmp);
					//Add to map
					aSet.insert(aTmp);
				}
			}
			else
			{
				i = sline.Find((TCHAR)' ');
				if (i>0)
				{
					temp = sline.Mid(i,sline.GetLength());
					temp.TrimLeft();
					temp.TrimRight();
					AfxExtractSubString(domain,temp,0,(TCHAR)' ');
					domain.TrimRight();
					wsprintf(aDomain,"%s",domain);
						
					//Do we have this item?
					if (aSet.find(aDomain)==aSet.end())
					{
						//Add to our string
						m_aHosts.push_back(aTmp);

						//AfxMessageBox((char*)aTmp);
						//Do we need it

						//Add to map
						aSet.insert(aDomain);
					}

				}
			}
		}

		//Close file
		fclose(pFile);

		//Do we need to sort?
		m_bSort = FALSE;
		if (m_bSort)
			//Sort data
			SortData();
	
		//Done
		return TRUE;
	}
	catch ( ... ) { return FALSE; }

	return FALSE;

}

void CHostsFile::SortData()
{
	try
	{
		//Sort vector
		std::sort(m_aHosts.begin(),
				  m_aHosts.end(),
				  CompareHosts);
	}
	catch ( ... ) { }
}

bool CHostsFile::CompareHosts(const std::string& rAddress1,
							  const std::string& rAddress2)
{

	return false;
}

const std::string& CHostsFile::GetFileName()const
{
	return m_sFile;
}



int CHostsFile::GetCount()const
{
	return m_aHosts.size();
}

const std::string& CHostsFile::GetDataAt(int iIndex)const
{
	return m_aHosts[iIndex];
}

BOOL CHostsFile::IsEmpty()const
{
	return m_sFile.empty();
}

void CHostsFile::Clear()
{
	m_aHosts.clear();
}

void CHostsFile::Insert(const std::string& rData)
{
	m_aHosts.push_back(rData);
}

⌨️ 快捷键说明

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