#include "PatchInfoListV1.h"

const char* CPatchInfoListV1::ms_cszHeaderString = "PAT1";



//////////////////////////////////////////////////////////////////////////
//
bool CPatchInfoListV1::Load( const char* szFileName )
{
	FILE* fp = fopen(szFileName, "rb");
	
	if( NULL == fp )
	{
		return false;
	}
	
	// check header
	char acTmp[4] = { 0, };
	fread( acTmp, 4, sizeof(char), fp );
	if( acTmp[0] != ms_cszHeaderString[0] ||
		acTmp[1] != ms_cszHeaderString[1] ||
		acTmp[2] != ms_cszHeaderString[2] ||
		acTmp[3] != ms_cszHeaderString[3]
		)
	{
		fclose(fp);
		return false;
	}
	
	// read version
	fread( &m_dwVersion, sizeof(DWORD), 1, fp );
	
	// read count
	long lCount;
	fread( &lCount, sizeof(long), 1, fp );
	
	// read data
	m_vecPatchInfo.resize( lCount );
	
	if( lCount !=
		(long)fread( &(m_vecPatchInfo[0]), sizeof(FilePatchInfoRecordV1),
		lCount, fp ) )
	{
		fclose(fp);
		return false;
	}
	
	
	fclose(fp);
	return true;
}


bool CPatchInfoListV1::Save( const char* szFileName )
{
	FILE* fp = fopen(szFileName, "wb");
	
	if( NULL == fp )
	{
		return false;
	}
	
	// write header
	fwrite( ms_cszHeaderString, 4, sizeof(char), fp );
	
	// write version
	fwrite( &m_dwVersion, sizeof(DWORD), 1, fp );
	
	// write count
	long lCount = m_vecPatchInfo.size();
	fwrite( &lCount, sizeof(long), 1, fp );
	
	// write data
	fwrite( &(m_vecPatchInfo[0]), sizeof(FilePatchInfoRecordV1),
		lCount, fp );
	
	
	fclose(fp);
	return true;
}


void CPatchInfoListV1::Reset()
{
	m_dwVersion = 0;
	m_vecPatchInfo.clear();
}

