IPLocater.cpp
资源名称:GGBT.rar [点击查看]
上传用户:lds876
上传日期:2013-05-25
资源大小:567k
文件大小:4k
源码类别:
P2P编程
开发平台:
Visual C++
- // IPLocater.cpp: implementation of the CIPLocater class.
- //
- //////////////////////////////////////////////////////////////////////
- #include "stdafx.h"
- #include "IPLocater.h"
- #ifdef _DEBUG
- #undef THIS_FILE
- static char THIS_FILE[]=__FILE__;
- #define new DEBUG_NEW
- #endif
- //////////////////////////////////////////////////////////////////////
- // Construction/Destruction
- //////////////////////////////////////////////////////////////////////
- CIPLocater::CIPLocater()
- {
- m_pBuf = 0;
- m_bCreate = false;
- m_lBufCount = 0;
- }
- CIPLocater::~CIPLocater()
- {
- if (m_pBuf)
- {
- delete m_pBuf;
- m_pBuf = 0;
- }
- }
- BOOL CIPLocater::Create(CString strIPdbpath)
- {
- m_vCountry.clear();
- m_vIndex.clear();
- if (m_pBuf)
- {
- delete m_pBuf;
- m_pBuf = 0;
- }
- if (IsCreate())
- {
- // ASSERT(false);
- return false;
- }
- FILE* pfile = fopen(strIPdbpath, "rb");
- if (!pfile)
- {
- // ASSERT(false);
- return false;
- }
- ////////////////////////////////////////////////////////////////////////
- // read size
- long lCountryCount = 0, lIndexCount =0;
- fread(&lCountryCount, sizeof(char), sizeof(long), pfile);
- fread(&lIndexCount, sizeof(char), sizeof(long), pfile);
- fread(&m_lBufCount, sizeof(char), sizeof(long), pfile);
- ////////////////////////////////////////////////////////////////////////
- // read primary key.
- char* pBuf = new char[lCountryCount];
- auto_ptr<char> abuf(pBuf);
- memset(pBuf, 0, sizeof(lCountryCount));
- int iRet = fread(pBuf, sizeof(char), lCountryCount, pfile);
- if (iRet != lCountryCount)
- {
- // ASSERT(false);
- fclose(pfile);
- return false;
- }
- long lCount = 0;
- while (lCount < lCountryCount)
- {
- CString strTemp = pBuf + lCount;
- // ASSERT(!strTemp.IsEmpty());
- lCount += strTemp.GetLength() + 1;
- m_vCountry.push_back(strTemp);
- TRACE("%s, rn", strTemp.GetBuffer(0));
- }
- // ASSERT(lCount == lCountryCount);
- ////////////////////////////////////////////////////////////////////////
- // read index
- pBuf = new char[lIndexCount];
- auto_ptr<char> aindexbuf(pBuf);
- memset(pBuf, 0, sizeof(lIndexCount));
- iRet = fread(pBuf, sizeof(char), lIndexCount, pfile);
- if (iRet != lIndexCount)
- {
- // ASSERT(false);
- fclose(pfile);
- return false;
- }
- lCount = 0;
- while (lCount < lIndexCount)
- {
- long lbeg = *(ULONG*)(pBuf + lCount);
- lCount += sizeof(long);
- long linx= *(ULONG*)(pBuf + lCount);
- lCount += sizeof(long);
- m_vIndex.push_back(CSize(lbeg, linx));
- }
- // ASSERT(lCount == lIndexCount);
- m_pBuf = new char[m_lBufCount];
- memset(m_pBuf, 0, sizeof(m_lBufCount));
- iRet = fread(m_pBuf, sizeof(char), m_lBufCount, pfile);
- if (iRet != m_lBufCount)
- {
- // ASSERT(false);
- fclose(pfile);
- return false;
- }
- fclose(pfile);
- m_bCreate = true;
- return TRUE;
- }
- BOOL CIPLocater::IsCreate() const
- {
- return m_bCreate;
- }
- bool CIPLocater::FindIPName(CString strIP, CString &strName)const
- {
- ULONG lbeg = inet_addr(strIP.GetBuffer(0));
- if (lbeg == INADDR_NONE)
- {
- return false;
- }
- lbeg = ntohl(lbeg);
- return FindIPName(lbeg, strName);
- }
- bool CIPLocater::FindIPName(ULONG lIP, CString &strName)const
- {
- if (!IsCreate())
- {
- // ASSERT(false);
- return false;
- }
- strName.Empty();
- for (int i=0; i<m_vIndex.size(); i++)
- {
- if (lIP < m_vIndex[i].cx)
- {
- if (i <= 0)
- {
- // ASSERT(FALSE);
- return false;
- }
- if (m_vIndex[i - 1].cy < 0)
- return false;
- if (m_vIndex[i - 1].cy >= m_lBufCount )
- {
- // ASSERT(FALSE);
- return false;
- }
- long lnext = i; //m_vIndex[i].cy;
- while (m_vIndex[lnext].cy < 0)
- {
- if (lnext >= m_vIndex.size())
- return false;
- lnext ++;
- }
- ULONG lPrimaryIndex = *((ULONG *)(m_pBuf + m_vIndex[i - 1].cy));
- if (lPrimaryIndex >= m_vCountry.size() || lPrimaryIndex < 0)
- {
- // ASSERT(FALSE);
- return false;
- }
- strName += m_vCountry[lPrimaryIndex];
- char szText[1024] = {0};
- memcpy(szText, m_pBuf+ m_vIndex[i - 1].cy + sizeof(long), m_vIndex[lnext].cy - m_vIndex[i-1].cy - sizeof(long));
- strName += szText;
- return true;
- break;
- }
- }
- return false;
- }