#include "stdafx.h"

#include "MergeBin.h"
#include "HexFile.h"

int ReadHexFile(CString strFileName, char * pData)
{
	CHexFile hex;
	int iLength = 0, iCurAddress, iCurNumber;

	if (hex.Open(strFileName))
	{
		AfxGetApp()->BeginWaitCursor();
		while (hex.ReadLine())
		{
			iCurAddress = hex.GetAddress();
			iCurNumber = hex.GetNumber();
			memcpy(pData + iCurAddress, hex.GetData(), iCurNumber);		
			if (iCurAddress >= iLength)
			{	
				iLength = iCurAddress + iCurNumber; 
			}
		}
		hex.Close();
		AfxGetApp()->EndWaitCursor();
	}
	else
	{
		AfxMessageBox("can not open hex file");
	}
	return iLength;
}

void MergeBin(CString strDstName, ...)
{
	char * p;
	CFile fileWrite;
	va_list v1;
	char * strSrcName;

	p = (char *)malloc(HEXFILE_SIZE);
	AfxGetApp()->BeginWaitCursor();
	fileWrite.Open(strDstName, CFile::modeCreate|CFile::modeWrite);
	va_start(v1, strDstName);
	do
	{
		memset(p, 0xff, HEXFILE_SIZE);
		strSrcName = va_arg(v1, char *);
		if (strlen(strSrcName) == 0)	break;

		ReadHexFile(strSrcName, p);
		fileWrite.Write(p, HEXFILE_SIZE);
	} while (1);
	va_end(v1);

	free(p);
	fileWrite.Close();
}

void MergePa168f(CString strDstName, CString strDstDir, CString strSrcDir)
{
	MergeBin(strDstDir + strDstName + ".bin", 
				strSrcDir + "loaddata\\loaddata.hex",
				strSrcDir + "g723\\g723.hex",
				strSrcDir + "ilbc\\ilbc.hex",
				strSrcDir + "main\\page4.hex",
				strSrcDir + "main\\page5.hex",
				strSrcDir + "g729\\g729.hex",
				strSrcDir + "settings\\settings.hex",
				""
				);

	CFileStatus status;
	if (CFile::GetStatus(strSrcDir + "page0\\page0.hex", status))
	{
		MergeBin(strDstDir + strDstName + "_all.bin", 
				strSrcDir + "page0\\page0.hex", 
				strSrcDir + "loaddata\\loaddata.hex",
				strSrcDir + "g723\\g723.hex",
				strSrcDir + "ilbc\\ilbc.hex",
				strSrcDir + "main\\page4.hex",
				strSrcDir + "main\\page5.hex",
				strSrcDir + "g729\\g729.hex",
				strSrcDir + "settings\\settings.hex",
				""
				);
	}
}

void MergeYuxin(CString strDstName, CString strDstDir, CString strSrcDir)
{
	MergeBin(strDstDir + strDstName + ".bin", 
				strSrcDir + "loaddata\\loaddata.hex",
				strSrcDir + "g723\\g723.hex",
				strSrcDir + "ilbc\\ilbc.hex",
				strSrcDir + "main\\page4.hex",
				strSrcDir + "main\\page5.hex",
				strSrcDir + "g729\\g729.hex",
				""
				);

	CFileStatus status;
	if (CFile::GetStatus(strSrcDir + "page0\\page0.hex", status))
	{
		if (CFile::GetStatus(strSrcDir + "page7\\page7.hex", status))
		{
			MergeBin(strDstDir + strDstName + "_all.bin", 
				strSrcDir + "page0\\page0.hex", 
				strSrcDir + "loaddata\\loaddata.hex",
				strSrcDir + "g723\\g723.hex",
				strSrcDir + "ilbc\\ilbc.hex",
				strSrcDir + "main\\page4.hex",
				strSrcDir + "main\\page5.hex",
				strSrcDir + "g729\\g729.hex",
				strSrcDir + "page7\\page7.hex",
				""
				);
		}
	}
}

void _Merge15Pages(CString strDstName, CString strDstDir, CString strSrcDir)
{
	MergeBin(strDstDir + strDstName + ".bin", 
				strSrcDir + "loaddata\\loaddata.hex",
				strSrcDir + "g723\\g723.hex",
				strSrcDir + "ilbc\\ilbc.hex",
				strSrcDir + "main\\page4.hex",
				strSrcDir + "main\\page5.hex",
				strSrcDir + "g729\\g729.hex",
				strSrcDir + "main\\page7.hex",
				strSrcDir + "main\\page8.hex",
				strSrcDir + "autotest\\autotest.hex",
				strSrcDir + "settings\\settings.hex",
				strSrcDir + "userring\\userring.hex",
				strSrcDir + "page12\\page12.hex",
				strSrcDir + "autoprov\\autoprov.hex",
				strSrcDir + "page14\\page14.hex",
				strSrcDir + "settings\\settings.hex",
				""
				);
}

void MergePa168s(CString strDstName, CString strDstDir, CString strSrcDir)
{
	_Merge15Pages(strDstName, strDstDir, strSrcDir);

	CFileStatus status;
	if (CFile::GetStatus(strSrcDir + "page0\\page0.hex", status))
	{
		MergeBin(strDstDir + strDstName + "_all.bin", 
				strSrcDir + "page0\\page0.hex",
				strSrcDir + "loaddata\\loaddata.hex",
				strSrcDir + "g723\\g723.hex",
				strSrcDir + "ilbc\\ilbc.hex",
				strSrcDir + "main\\page4.hex",
				strSrcDir + "main\\page5.hex",
				strSrcDir + "g729\\g729.hex",
				strSrcDir + "main\\page7.hex",
				strSrcDir + "main\\page8.hex",
				strSrcDir + "autotest\\autotest.hex",
				strSrcDir + "settings\\settings.hex",
				strSrcDir + "userring\\userring.hex",
				strSrcDir + "page12\\page12.hex",
				strSrcDir + "autoprov\\autoprov.hex",
				strSrcDir + "page14\\page14.hex",
				strSrcDir + "settings\\settings.hex",
				""
				);
	}
}

void MergePa168t(CString strDstName, CString strDstDir, CString strSrcDir)
{
	_Merge15Pages(strDstName, strDstDir, strSrcDir);

	CFileStatus status;

	if (CFile::GetStatus(strSrcDir + "page0\\page0.hex", status))
	{
		MergeBin(strDstDir + strDstName + "_all.bin", 
				strSrcDir + "page0\\page0.hex", 
				strSrcDir + "loaddata\\loaddata.hex",
				strSrcDir + "g723\\g723.hex",
				strSrcDir + "ilbc\\ilbc.hex",
				strSrcDir + "main\\page4.hex",
				strSrcDir + "main\\page5.hex",
				strSrcDir + "g729\\g729.hex",
				strSrcDir + "main\\page7.hex",
				strSrcDir + "main\\page8.hex",
				strSrcDir + "autotest\\autotest.hex",
				strSrcDir + "settings\\settings.hex",
				strSrcDir + "userring\\userring.hex",
				strSrcDir + "page12\\page12.hex",
				strSrcDir + "autoprov\\autoprov.hex",
				strSrcDir + "page14\\page14.hex",
				strSrcDir + "settings\\settings.hex",

				strSrcDir + "settings\\settings.hex",
				strSrcDir + "settings\\settings.hex",
				strSrcDir + "settings\\settings.hex",
				strSrcDir + "settings\\settings.hex",
				strSrcDir + "settings\\settings.hex",
				strSrcDir + "settings\\settings.hex",
				strSrcDir + "settings\\settings.hex",
				strSrcDir + "settings\\settings.hex",
				strSrcDir + "settings\\settings.hex",
				strSrcDir + "settings\\settings.hex",
				strSrcDir + "settings\\settings.hex",
				strSrcDir + "settings\\settings.hex",
				strSrcDir + "settings\\settings.hex",
				strSrcDir + "settings\\settings.hex",
				strSrcDir + "userring\\userring.hex",
				strSrcDir + "page7\\page7.hex",
				""
				);
	}
}
