Esempio n. 1
0
bool CIpInfo::LoadInfoFile(const char* lpszFilePathName)
{
	bool bReturn = false;

	ifstream hSrcFile(lpszFilePathName,ios_base::binary);
	if(hSrcFile.is_open()&& !hSrcFile.fail())
	{
        hSrcFile.seekg(0,ios::end);
		unsigned long dwNumberOfBytesToRead = hSrcFile.tellg();//Get file size
        hSrcFile.seekg(0,ios::beg);
		m_pDataBuffer = new unsigned char[dwNumberOfBytesToRead + 1];//would not be failed in win32?
        memset(m_pDataBuffer,0,dwNumberOfBytesToRead+1);
		if(m_pDataBuffer != NULL)
		{
			hSrcFile.read((char*)m_pDataBuffer,dwNumberOfBytesToRead);
            unsigned long readBytes = hSrcFile.gcount();
            if(readBytes > 0)
			{
				//calculate the ip information record count
				unsigned long *pIntData = (unsigned long *)m_pDataBuffer;
				unsigned long nBuffer_0 = *pIntData;//Start position of record infor in file
				unsigned long nBuffer_4	= *(pIntData + 1);//End position of record infor in file
                first_index_pos = *pIntData;
                last_index_pos= *(pIntData+1);

				nBuffer_4 -= nBuffer_0;
				
				m_dwRecordCount = nBuffer_4 / 7;//IP record count
				int nsuiv = nBuffer_4 % 7;
				if(nsuiv == 0)//must be integral info struct,7 bytes
				{
					m_bInit = true;
					bReturn = m_bInit;
				}
			}
			if(hSrcFile.fail())
			{
				DEBUG_PRINT1("Error Opening File\n");
			}
            if(hSrcFile.bad())
			{
				DEBUG_PRINT1("Error reading File\n");
			}
            while(readBytes < dwNumberOfBytesToRead && hSrcFile.eof())
            {
                hSrcFile.read((char*)(m_pDataBuffer+readBytes),dwNumberOfBytesToRead-readBytes);
                unsigned long cur_read = hSrcFile.gcount();
                readBytes += cur_read;
            }
		}
		hSrcFile.close();//close IP data file
	}
	if(!m_bInit)
			FreeBuffer();//Verify the null buffer if we got some error

	return bReturn;
}