// ConvertFileDlg.cpp : implementation file
//

#include "stdafx.h"
#include "palmtool.h"
#include "tcpsocket.h"
#include "ConvertFileDlg.h"
#include "DirectoryDlg.h"
#include "Mergebin.h"
#include "version.h"
#include "CovertStr.h"

#include "HexFile.h"
#include "AudioFile.h"
#include "WaveFile.h"
#include "PhoneSettingsDlg.h"

#include "sdram.h"

static TCHAR szHexFilter[] = _T("MCS51 hex files (*.hex)|*.hex|All files (*.*)|*.*||");
static TCHAR szHex2Filter[] = _T("Bank-switch hex files (*.he?)|*.he?|All files (*.*)|*.*||");
static TCHAR szBinFilter[] = _T("Binary files (*.bin)|*.bin|All files (*.*)|*.*||");
static TCHAR szHFilter[] = _T("C Header files (*.h)|*.h|All files (*.*)|*.*||");
static TCHAR szDatFilter[] = _T("Data files (*.dat)|*.dat|All data files|*.dat;*.pcm;*.raw|All files (*.*)|*.*||");
static TCHAR szTxtFilter[] = _T("Text files (*.txt)|*.txt|All files (*.*)|*.*||");
static TCHAR szPcmFilter[] = _T("PCM files (*.pcm)|*.pcm|All data files|*.dat;*.pcm;*.raw|All files (*.*)|*.*||");
static TCHAR szWaveFilter[] = _T("Wave files (*.wav)|*.wav|All files (*.*)|*.*||");
static TCHAR szHtmlFilter[] = _T("Html files (*.htm)|*.htm|All files (*.*)|*.*||");

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

CString Palm1DebugConvertString(CString strOrig);
/////////////////////////////////////////////////////////////////////////////
// CConvertFileDlg dialog


CConvertFileDlg::CConvertFileDlg(CWnd* pParent /*=NULL*/)
	: CDialog(CConvertFileDlg::IDD, pParent)
{
	//{{AFX_DATA_INIT(CConvertFileDlg)
	m_strOutputDir = _T("");
	m_strBinaryFile = _T("");
	//}}AFX_DATA_INIT
}


void CConvertFileDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CConvertFileDlg)
	DDX_Control(pDX, IDC_NEWLOG_NAME, m_ctlNewLogName);
	DDX_Text(pDX, IDC_OUTPUT_DIR, m_strOutputDir);
	DDX_Text(pDX, IDC_BINARY_FILE, m_strBinaryFile);
	//}}AFX_DATA_MAP
}

BOOL CConvertFileDlg::OnInitDialog() 
{
	CDialog::OnInitDialog();
	
	m_iAudioType = AfxGetApp()->GetProfileInt("Audio", "CodecType", 0);

	m_bAudioNoAcm = (BOOL)AfxGetApp()->GetProfileInt(_T("UdpAudio"), _T("NoAcm"), 1);
	m_bUseVAD = (BOOL)AfxGetApp()->GetProfileInt(_T("UdpAudio"), _T("UseVAD"), 1);
	m_bUseHighRate = (BOOL)AfxGetApp()->GetProfileInt(_T("UdpAudio"), _T("UseHighRate"), 1);

#if defined VER_WG2000
	m_strSourceDir.Format("D:\\IPHONE\\Software\\v%1d_%1d%1d_%1d%1d%1d\\PalmAPI\\",
	                      SOFT_VER_HIGH,
	                      SOFT_VER_LOW,
	                      SOFT_VER_BUILD,
	                      ENG_BUILD_HIGH,
	                      ENG_BUILD_MID,
	                      ENG_BUILD_LOW);
	                      
	m_strOutputDir.Format("D:\\IPHONE\\Software\\v%1d_%1d%1d_%1d%1d%1d\\Build\\FlashMemory\\",
	                      SOFT_VER_HIGH,
	                      SOFT_VER_LOW,
	                      SOFT_VER_BUILD,
	                      ENG_BUILD_HIGH,
	                      ENG_BUILD_MID,
	                      ENG_BUILD_LOW);
#else	                      
	m_strOutputDir = AfxGetApp()->GetProfileString(_T("Build"), _T("OutputDir"), _T("C:\\PalmH323\\"));
	m_strSourceDir = m_strOutputDir;
	m_strBinaryFile = AfxGetApp()->GetProfileString(_T("Browse"), _T("BinaryFile"), _T("pa1688.bin"));
#endif
	UpdateData(FALSE);
	
	return TRUE;  // return TRUE unless you set the focus to a control
	              // EXCEPTION: OCX Property Pages should return FALSE
}

BEGIN_MESSAGE_MAP(CConvertFileDlg, CDialog)
	//{{AFX_MSG_MAP(CConvertFileDlg)
	ON_BN_CLICKED(IDC_ADSP_CONVERTER, OnAdspConverter)
	ON_BN_CLICKED(IDC_BINARY_CONVERTER, OnBinaryConverter)
	ON_BN_CLICKED(IDC_HEX_CONVERTER, OnHexConverter)
	ON_BN_CLICKED(IDC_ENCODE_FILE, OnEncodeFile)
	ON_BN_CLICKED(IDC_DECODE_FILE, OnDecodeFile)
	ON_BN_CLICKED(IDC_BATCH_WORK, OnBatchWork)
	ON_BN_CLICKED(IDC_HEX_TO_BIN, OnHexToBin)
	ON_BN_CLICKED(IDC_WAVE_CONVERTER, OnWaveConverter)
	ON_BN_CLICKED(IDC_CONVERT_DEBUG, OnConvertDebug)
	ON_BN_CLICKED(IDC_CONVERT_MAP, OnConvertMap)
	ON_BN_CLICKED(IDC_BUILD_004T, OnBuild004t)
	ON_BN_CLICKED(IDC_BUILD_F040, OnBuildF040)
	ON_BN_CLICKED(IDC_BUILD_008T, OnBuildStandard)
	ON_BN_CLICKED(IDC_BIN_TO_TXT, OnOptionsBinToTxt)
	ON_BN_CLICKED(IDC_TXT_TO_BIN, OnOptionsTxtToBin)
	ON_BN_CLICKED(IDC_BUILD_2MFlASH, OnBuildPA168T)
	ON_BN_CLICKED(IDC_BUILD_DIR, OnBuildDir)
	ON_BN_CLICKED(IDC_BROWSE, OnBrowse)
	ON_BN_CLICKED(IDC_MODIFY_LOG, OnModifyLog)
	ON_BN_CLICKED(IDC_ADSP_TO_BIN, OnAdspToBin)
	ON_BN_CLICKED(IDC_BIN_TO_ADSP, OnBinToAdsp)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CConvertFileDlg helper functions

BOOL CConvertFileDlg::Adsp2H(CString strDstName, CString strSrcName)
{
	CStdioFile in, out;
	CString strIn, strOut;

	if (!in.Open(strSrcName, CFile::modeRead|CFile::typeText))
	{
		AfxMessageBox("Can not open source file");
		return FALSE;
	}

	if (!out.Open(strDstName, CFile::modeCreate|CFile::modeWrite|CFile::typeText))
	{
		AfxMessageBox("Can not create dest file");
		return FALSE;
	}

	AfxGetApp()->BeginWaitCursor();
	while (in.ReadString(strIn))
	{
		strOut.Format("0x%s,\n", strIn);
		out.WriteString(strOut);
	}
	AfxGetApp()->EndWaitCursor();

	in.Close();
	out.Close();

	return TRUE;
}

BOOL CConvertFileDlg::Adsp2BIN(CString strDstName, CString strSrcName)
{
	CFile out;
	CStdioFile in;
	CString strIn, strOut, strRead;
	int iPos, iFileLen;
	char * p, *pCur;
	char pTemp[3];
	int i, iAddressOffset;

	if (!in.Open(strSrcName, CFile::modeRead))
	{
		AfxMessageBox("Can not open source file");
		return FALSE;
	}
	if (!out.Open(strDstName, CFile::modeCreate|CFile::modeWrite))
	{
		AfxMessageBox("Can not create dest file");
		in.Close();
		return FALSE;
	}
		
	AfxGetApp()->BeginWaitCursor();
	strRead = "";
	while (in.ReadString(strIn))
	{
		iPos = strIn.Find("0x");
		strIn.Delete(iPos, 2);
		strRead += strIn;
	}
	iFileLen = strRead.GetLength();
	p = (char *)malloc(iFileLen / 2);
	memset(p, 0, (iFileLen/2));
	iAddressOffset = 0;

	for (i = 0; i < iFileLen / 4; i++)
	{
		pCur = (char *)malloc(iFileLen+1);
		memset(pCur, 0, iFileLen+1);
		memcpy((char *)pCur, strRead, iFileLen);

		pTemp[2] = 0;
		pTemp[0] = *(pCur+2+(i*4));
		pTemp[1] = *(pCur+3+(i*4));
		p[iAddressOffset] = (UCHAR)_atoi_16(pTemp, 16);
		pTemp[0] = *(pCur+(i*4));
		pTemp[1] = *(pCur+1+(i*4));
		p[iAddressOffset+1] = (UCHAR)_atoi_16(pTemp, 16);

		iAddressOffset += 2;
	}

	AfxGetApp()->EndWaitCursor();
	out.Write(p, iFileLen/2);

	free(p);
	in.Close();
	out.Close();

	return TRUE;
}

BOOL CConvertFileDlg::BIN2Adsp(CString strDstName, CString strSrcName)
{
	CFile in;
	CStdioFile out;
	int i, iLength;
	BYTE * p;
	CString str, strOut;

	if (!in.Open(strSrcName, CFile::modeRead))
	{
		AfxMessageBox("Can not open source file");
		return FALSE;
	}
	if (!out.Open(strDstName, CFile::modeCreate|CFile::modeWrite|CFile::typeText))
	{
		AfxMessageBox("Can not create dest file");
		in.Close();
		return FALSE;
	}
		
	AfxGetApp()->BeginWaitCursor();
	iLength = in.GetLength();
	p = (BYTE *)malloc(iLength);
	in.Read(p, iLength);

	for (i = 0; i < iLength; i += 2)
	{
		strOut = "";
		str.Format("0x%02x%02x", p[i+1], p[i]);
		strOut += str;
		strOut += "\n";
		out.WriteString(strOut);
	}

	AfxGetApp()->EndWaitCursor();
	
	free(p);
	in.Close();
	out.Close();

	return TRUE;
}

BOOL CConvertFileDlg::Binary2H(CString strDstName, CString strSrcName)
{
	#define FRAME_LEN	24
	CFile in;
	CStdioFile out;
	CString str, strOut;
	int i, j, iLength;
	BYTE * p;
	char drive[_MAX_DRIVE], dir[_MAX_DIR], fname[_MAX_FNAME], ext[_MAX_EXT];

	strDstName.MakeUpper();
	_splitpath(strDstName, drive, dir, fname, ext);

	if (!in.Open(strSrcName, CFile::modeRead))
	{
		AfxMessageBox("Can not open source file");
		return FALSE;
	}

	if (!out.Open(strDstName, CFile::modeCreate|CFile::modeWrite|CFile::typeText))
	{
		AfxMessageBox("Can not create dest file");
		return FALSE;
	}

	AfxGetApp()->BeginWaitCursor();
	iLength = in.GetLength();
	p = (BYTE *)malloc(iLength);
	in.Read(p, iLength);
	strOut.Format("#define DATA_%s_SIZE\t%d\n#ifdef __LOAD_SDRAM\n", fname, iLength);
	out.WriteString(strOut);
	strOut.Format("static CCHAR _cData_%s[DATA_%s_SIZE] = {\n", fname, fname);
	out.WriteString(strOut);

	for (i = 0; i < iLength; i += FRAME_LEN)
	{
		strOut = "";
		for (j = 0; j < FRAME_LEN; j ++)
		{
			if (i+j < iLength)
			{
				str.Format("0x%02x, ", p[i+j]);
				strOut += str;
			}
		}
		strOut += "\n";
		out.WriteString(strOut);
	}

	strOut.Format("};\n#endif\n");
	out.WriteString(strOut);
	AfxGetApp()->EndWaitCursor();

	free(p);
	in.Close();
	out.Close();

	return TRUE;
}

BOOL CConvertFileDlg::Hex2H(CString strDstName, CString strSrcName)
{
	unsigned char * p;
	int iLength;

	char drive[_MAX_DRIVE], dir[_MAX_DIR], fname[_MAX_FNAME], ext[_MAX_EXT];
	strSrcName.MakeUpper();
	_splitpath(strSrcName, drive, dir, fname, ext);

	p = (unsigned char *)malloc(HEXFILE_SIZE);
	memset(p, 0, HEXFILE_SIZE);
	iLength = ReadHexFile(strSrcName, (char *)p);

	if (iLength != 0)
	{
		int i;
		CFile file;
		CString str, strChar;

		file.Open(strDstName, CFile::modeCreate|CFile::modeWrite);

		str.Format("#define %s_CODE_SIZE\t%d\r\nstatic CCHAR %s_Code[%s_CODE_SIZE] = {", fname, iLength, fname, fname);
		for (i = 0; i < iLength; i ++)
		{
			if ((i % 16) == 0)
			{
				str += _T("\r\n");
				file.Write(LPCTSTR(str), str.GetLength());
				str = _T("");
			}
			strChar.Format("0x%02x, ", p[i]);
			str += strChar;
		}
		str += _T("\r\n};");
		file.Write(LPCTSTR(str), str.GetLength());

		file.Close();
	}

	free(p);

	return TRUE;
}

BOOL CConvertFileDlg::Wave2Pcm(CString strDstName, CString strSrcName)
{
	CWaveFile fileSrc;
	CFile fileDst;
	int iLen;
	char * p;

	if (!fileSrc.Open(strSrcName, MMIO_READ))
	{
		AfxMessageBox("Can not open source file");
		return FALSE;
	}

	iLen = fileSrc.GetDataLength();
	p = (char *)malloc(iLen);
	if (p == NULL)
	{
		AfxMessageBox("Not enough memory");
		fileSrc.Close();
		return FALSE;
	}

	fileSrc.Read((unsigned char *)p, iLen);
	fileSrc.Close();

	fileDst.Open(strDstName, CFile::modeCreate|CFile::modeWrite);
	fileDst.Write(p, iLen);
	fileDst.Close();
	free(p);

	return TRUE;
}

void CConvertFileDlg::OnHexToBin() 
{
	CString strDstName = AfxGetApp()->GetProfileString(_T("HEX2BIN"), _T("DstName"), _T("demo.bin"));
	CFileDialog dlgSave(FALSE, _T("*.bin"), strDstName, OFN_PATHMUSTEXIST, szBinFilter);

	if (dlgSave.DoModal() != IDOK)
		return;

	unsigned char * p;
	CFile fileWrite;
	strDstName = dlgSave.GetPathName();
	AfxGetApp()->WriteProfileString(_T("HEX2BIN"), _T("DstName"), strDstName);

	p = (unsigned char *)malloc(HEXFILE_SIZE);
	fileWrite.Open(strDstName, CFile::modeCreate|CFile::modeWrite);
	do
	{
		CString strSrcName = AfxGetApp()->GetProfileString(_T("HEX2BIN"), _T("SrcName"), _T("demo.hex"));
		CFileDialog dlgOpen(TRUE, _T("*.hex"), strSrcName, OFN_FILEMUSTEXIST|OFN_PATHMUSTEXIST, szHexFilter);

		if (dlgOpen.DoModal() != IDOK)
			break;

		strSrcName = dlgOpen.GetPathName();
		AfxGetApp()->WriteProfileString(_T("HEX2BIN"), _T("SrcName"), strSrcName);
		memset(p, 0xff, HEXFILE_SIZE);
		ReadHexFile(strSrcName, (char *)p);
		fileWrite.Write(p, HEXFILE_SIZE);
	} while (1);
	free(p);
	fileWrite.Close();
}

/*void CConvertFileDlg::OnMergeHex() 
{
	CString strDstName = AfxGetApp()->GetProfileString(_T("MergeHex"), _T("DstName"), _T("page4.hex"));
	CFileDialog dlgSave(FALSE, _T("*.hex"), strDstName, OFN_PATHMUSTEXIST, szHexFilter);

	if (dlgSave.DoModal() != IDOK)
		return;

	int i;
	CStdioFile fileWrite;
	CString strEnd = ":00000001FF";

	strDstName = dlgSave.GetPathName();
	AfxGetApp()->WriteProfileString(_T("MergeHex"), _T("DstName"), strDstName);

	fileWrite.Open(strDstName, CFile::modeCreate|CFile::modeWrite|CFile::typeText);

	for (i = 0; i < 2; i ++)
	{
		CString strSrcName = AfxGetApp()->GetProfileString(_T("MergeHex"), _T("SrcName"), _T("page1.hec"));
		CFileDialog dlgOpen(TRUE, _T("*.he?"), strSrcName, OFN_FILEMUSTEXIST|OFN_PATHMUSTEXIST, szHex2Filter);

		if (dlgOpen.DoModal() != IDOK)
			break;

		strSrcName = dlgOpen.GetPathName();
		AfxGetApp()->WriteProfileString(_T("MergeHex"), _T("SrcName"), strSrcName);

		CStdioFile fileRead;
		CString strRead;

		if (!fileRead.Open(strSrcName, CFile::modeRead|CFile::typeText))
		{
			AfxMessageBox("Can not open source file");
			break;
		}

		AfxGetApp()->BeginWaitCursor();
		while (fileRead.ReadString(strRead))
		{
			if (strRead != strEnd)
			{
				fileWrite.WriteString(strRead);
				fileWrite.WriteString("\n");
			}
		}
		AfxGetApp()->EndWaitCursor();
		fileRead.Close();
	}

	fileWrite.WriteString(strEnd);
	fileWrite.WriteString("\n");
	fileWrite.Close();
}
*/
void CConvertFileDlg::OnConvertDebug() 
{
	CString strSrcName = AfxGetApp()->GetProfileString(_T("ConvertDebug"), _T("SrcName"), _T("sdram.dat"));
	CFileDialog dlgOpen(TRUE, _T("*.dat"), strSrcName, OFN_FILEMUSTEXIST|OFN_PATHMUSTEXIST, szDatFilter);

	if (dlgOpen.DoModal() != IDOK)
		return;

	CStdioFile fileRead;
	CStdioFile fileWrite;
	strSrcName = dlgOpen.GetPathName();
	AfxGetApp()->WriteProfileString(_T("ConvertDebug"), _T("SrcName"), strSrcName);

	if (!fileRead.Open(strSrcName, CFile::modeRead|CFile::typeText))
	{
		AfxMessageBox("Can not open source file");
		return;
	}

	CString strDstName = AfxGetApp()->GetProfileString(_T("ConvertDebug"), _T("DstName"), _T("convert.dat"));
	CFileDialog dlgSave(FALSE, _T("*.dat"), strDstName, OFN_PATHMUSTEXIST, szDatFilter);

	if (dlgSave.DoModal() != IDOK)
		return;

	strDstName = dlgSave.GetPathName();
	AfxGetApp()->WriteProfileString(_T("ConvertDebug"), _T("DstName"), strDstName);
	fileWrite.Open(strDstName, CFile::modeCreate|CFile::modeWrite|CFile::typeText);

	CString strRead;
	AfxGetApp()->BeginWaitCursor();

	CString	strDisplay;

	while (fileRead.ReadString(strRead))
	{
//		if (strRead[0]==0x00 || strRead[0] == 0x0d || strRead[0] == 0x0a || (strRead[0]>=0x20 && strRead[0]<=0x7e))
//		{
			strDisplay = Palm1DebugConvertString(strRead);
			fileWrite.WriteString(strDisplay);
			fileWrite.WriteString("\n");
//		}
//		else
//		{
//			break;
//		}
	}
	AfxGetApp()->EndWaitCursor();
	fileRead.Close();
	fileWrite.Close();
}

void CConvertFileDlg::OnConvertMap() 
{
	CString strSrcName = AfxGetApp()->GetProfileString(_T("ConvertMap"), _T("SrcName"), _T("sdram.dat"));
	CFileDialog dlgOpen(TRUE, _T("*.txt"), strSrcName, OFN_FILEMUSTEXIST|OFN_PATHMUSTEXIST, szTxtFilter);

	if (dlgOpen.DoModal() != IDOK)
		return;

	CFile fileRead;
	CStdioFile fileWrite;
	strSrcName = dlgOpen.GetPathName();
	AfxGetApp()->WriteProfileString(_T("ConvertMap"), _T("SrcName"), strSrcName);

	if (!fileRead.Open(strSrcName, CFile::modeRead|CFile::typeText))
	{
		AfxMessageBox("Can not open source file");
		return;
	}

	CString strDstName = AfxGetApp()->GetProfileString(_T("ConvertMap"), _T("DstName"), _T("demo.h"));
	CFileDialog dlgSave(FALSE, _T("*.h"), strDstName, OFN_PATHMUSTEXIST, szHFilter);

	if (dlgSave.DoModal() != IDOK)
		return;

	strDstName = dlgSave.GetPathName();
	AfxGetApp()->WriteProfileString(_T("ConvertMap"), _T("DstName"), strDstName);
	fileWrite.Open(strDstName, CFile::modeCreate|CFile::modeWrite|CFile::typeText);

	int i, iLength;
	char * p;
	CString strWrite, strTemp;

	iLength = fileRead.GetLength();
	if (iLength > OPTIONS_DIGITMAP_LEN)		iLength = OPTIONS_DIGITMAP_LEN;
	p = (char *)malloc(OPTIONS_DIGITMAP_LEN);

	AfxGetApp()->BeginWaitCursor();
	fileRead.Read(p, iLength);
	for (i = 0; i < OPTIONS_DIGITMAP_LEN; i ++)
	{
		if (i != 0 && (i % 16) == 0) 
		{
			strWrite += "\n";
		}
		if (i < iLength && i < (OPTIONS_DIGITMAP_LEN - 1))
		{
			if (p[i] == 0x0d)
			{
				strWrite += "0x0d, ";
			}
			else if (p[i] == 0x0a)
			{
				strWrite += "0x0a, ";
			}
			else
			{
				strTemp.Format("'%c', ", p[i]);
				strWrite += strTemp;
			}
		}
		else
		{
			strWrite += "0, ";
		}
	}
	fileWrite.WriteString(strWrite);
	AfxGetApp()->EndWaitCursor();
	free(p);
	fileRead.Close();
	fileWrite.Close();
}


/*void CConvertFileDlg::OnMergeG729file() 
{
	CString strSrcName1 = AfxGetApp()->GetProfileString(_T("MergeFile"), _T("SrcName1"), _T("ring1.dat"));
	CFileDialog dlgOpen1(TRUE, _T("*.dat"), strSrcName1, OFN_FILEMUSTEXIST|OFN_PATHMUSTEXIST, szDatFilter);
	CFile fileRead1;
	CAudioFile file;

	if (dlgOpen1.DoModal() != IDOK)
		return;
	strSrcName1 = dlgOpen1.GetPathName();
	AfxGetApp()->WriteProfileString(_T("MergeFile"), _T("SrcName1"), strSrcName1);
	if (!fileRead1.Open(strSrcName1, CFile::modeRead))
	{
		AfxMessageBox("Can not open source file");
		return;
	}

	CString strSrcName2 = AfxGetApp()->GetProfileString(_T("MergeFile"), _T("SrcName2"), _T("ring2.dat"));
	CFileDialog dlgOpen2(TRUE, _T("*.dat"), strSrcName1, OFN_FILEMUSTEXIST|OFN_PATHMUSTEXIST, szDatFilter);
	CFile fileRead2;
	
	if (dlgOpen2.DoModal() != IDOK)
		return;
	strSrcName2 = dlgOpen2.GetPathName();
	AfxGetApp()->WriteProfileString(_T("MergeFile"), _T("SrcName2"), strSrcName2);
	if (!fileRead2.Open(strSrcName2, CFile::modeRead))
	{
		AfxMessageBox("Can not open source file");
		fileRead1.Close();
		return;
	}
	
	CString strDstName = AfxGetApp()->GetProfileString(_T("MergeFile"), _T("DstName"), _T("demo.dat"));
	CFileDialog dlgSave(FALSE, _T("*.dat"), strDstName, OFN_PATHMUSTEXIST, szDatFilter);
	CFile fileWrite;

	if (dlgSave.DoModal() != IDOK)
		return;

	strDstName = dlgSave.GetPathName();
	AfxGetApp()->WriteProfileString(_T("MergeFile"), _T("DstName"), strDstName);
	fileWrite.Open(strDstName, CFile::modeCreate|CFile::modeWrite);

	int i, iLen1, iLen2;
	char * p1, * p2, * p;
	char * pSrc1, * pSrc2, * pDst;

	iLen1 = fileRead1.GetLength();
	iLen2 = fileRead2.GetLength();

	if (file.Open(strDstName, CFile::modeCreate|CFile::modeWrite))
	{
		file.SetOptions(m_iAudioType, (!m_bAudioNoAcm), m_bUseVAD, m_bUseHighRate);
		file.Close();
	}
	
	if( m_iAudioType == 6 )
	{
		if (iLen1 != iLen2 || (iLen1 % 24) != 0 || (iLen2 % 24) != 0)
		{
			AfxMessageBox("G723 file format error");
			fileRead1.Close();
			fileRead2.Close();
			return;
		}
	}
	else
	{
		if (iLen1 != iLen2 || (iLen1 % 12) != 0 || (iLen2 % 12) != 0)
		{
			AfxMessageBox("G729 file format error");
			fileRead1.Close();
			fileRead2.Close();
			return;
		}
	}

	AfxGetApp()->BeginWaitCursor();

	p1 = (char *)malloc(iLen1);
	p2 = (char *)malloc(iLen2);
	p = (char *)malloc(iLen1 + iLen2);
	fileRead1.Read(p1, iLen1);
	fileRead2.Read(p2, iLen2);
	pSrc1 = p1;
	pSrc2 = p2;
	pDst = p;

	if( m_iAudioType == 6 )
	{
		for (i = 0; i < iLen1; i += 24)
		{
			memcpy(pDst, pSrc1, 24);
			pDst += 24;
			pSrc1 += 24;
			memcpy(pDst, pSrc2, 24);
			pDst += 24;
			pSrc2 += 24;
		}
	}
	else
	{
		for (i = 0; i < iLen1; i += 12)
		{
			memcpy(pDst, pSrc1, 12);
			pDst += 12;
			pSrc1 += 12;
			memcpy(pDst, pSrc2, 12);
			pDst += 12;
			pSrc2 += 12;
		}
	}

	fileWrite.Write(p, iLen1+iLen2);
	AfxGetApp()->EndWaitCursor();
	free(p1);
	free(p2);
	free(p);
	fileRead1.Close();
	fileRead2.Close();
	fileWrite.Close();
}
*/

BOOL CConvertFileDlg::AudioEncode(CString strDstName, CString strSrcName)
{
	CAudioFile file;
	CFile fileRead;
	int iSize;
	char * p;

	BeginWaitCursor();
	fileRead.Open(strSrcName, CFile::modeRead);
	iSize = fileRead.GetLength();
	p = (char *)malloc(iSize);
	fileRead.Read(p, iSize);
	fileRead.Close();
	
	if (file.Open(strDstName, CFile::modeCreate|CFile::modeWrite))
	{
		file.SetOptions(m_iAudioType, (!m_bAudioNoAcm), m_bUseVAD, m_bUseHighRate);
		file.Write(p, iSize);
		file.Close();
	}

	free(p);
	EndWaitCursor();

	return TRUE;
}

BOOL CConvertFileDlg::AudioDecode(CString strDstName, CString strSrcName)
{
	CAudioFile file;
	CFile fileWrite;
	int iSize;
	char * p;

	BeginWaitCursor();
	file.Open(strSrcName, CFile::modeRead);
	iSize = file.SetOptions(m_iAudioType, (!m_bAudioNoAcm), m_bUseVAD, m_bUseHighRate);
	p = (char *)malloc(iSize);
	iSize = file.Read(p, iSize);
	file.Close();

	fileWrite.Open(strDstName, CFile::modeCreate|CFile::modeWrite);
	fileWrite.Write(p, iSize);
	fileWrite.Close();

	free(p);
	EndWaitCursor();

	return TRUE;
}

/////////////////////////////////////////////////////////////////////////////
// CConvertFileDlg message handlers

void CConvertFileDlg::OnAdspConverter() 
{
	// Test to convert ADSP data file to PalmAdsp data file
	CString strSrcName = AfxGetApp()->GetProfileString(_T("ADSP2DATA"), _T("SrcName"), _T("demo.dat"));
	CString strDstName = AfxGetApp()->GetProfileString(_T("ADSP2DATA"), _T("DstName"), _T("demo.h"));
	CFileDialog dlgOpen(TRUE, _T("*.dat"), strSrcName, OFN_FILEMUSTEXIST|OFN_PATHMUSTEXIST, szDatFilter);
	CFileDialog dlgSave(FALSE, _T("*.h"), strDstName, OFN_PATHMUSTEXIST, szHFilter);

	if (dlgOpen.DoModal() != IDOK)
		return;

	if (dlgSave.DoModal() != IDOK)
		return;

	strSrcName = dlgOpen.GetPathName();
	AfxGetApp()->WriteProfileString(_T("ADSP2DATA"), _T("SrcName"), strSrcName);
	strDstName = dlgSave.GetPathName();
	AfxGetApp()->WriteProfileString(_T("ADSP2DATA"), _T("DstName"), strDstName);

	Adsp2H(strDstName, strSrcName);
}

void CConvertFileDlg::OnBinaryConverter() 
{
	// Test to convert binary data file to .h file
	CString strSrcName = AfxGetApp()->GetProfileString(_T("Binary2H"), _T("SrcName"), _T("demo.dat"));
	CString strDstName = AfxGetApp()->GetProfileString(_T("Binary2H"), _T("DstName"), _T("demo.h"));
	CFileDialog dlgOpen(TRUE, _T("*.dat"), strSrcName, OFN_FILEMUSTEXIST|OFN_PATHMUSTEXIST, szDatFilter);
	CFileDialog dlgSave(FALSE, _T("*.h"), strDstName, OFN_PATHMUSTEXIST, szHFilter);

	if (dlgOpen.DoModal() != IDOK)
		return;

	if (dlgSave.DoModal() != IDOK)
		return;

	strSrcName = dlgOpen.GetPathName();
	AfxGetApp()->WriteProfileString(_T("Binary2H"), _T("SrcName"), strSrcName);
	strDstName = dlgSave.GetPathName();
	AfxGetApp()->WriteProfileString(_T("Binary2H"), _T("DstName"), strDstName);

	Binary2H(strDstName, strSrcName);
}

void CConvertFileDlg::OnHexConverter() 
{
	CString strSrcName = AfxGetApp()->GetProfileString(_T("HEX2DATA"), _T("SrcName"), _T("demo.hex"));
	CString strDstName = AfxGetApp()->GetProfileString(_T("HEX2DATA"), _T("DstName"), _T("demo.h"));
	CFileDialog dlgOpen(TRUE, _T("*.hex"), strSrcName, OFN_FILEMUSTEXIST|OFN_PATHMUSTEXIST, szHexFilter);
	CFileDialog dlgSave(FALSE, _T("*.h"), strDstName, OFN_PATHMUSTEXIST, szHFilter);

	if (dlgOpen.DoModal() != IDOK)
		return;

	strSrcName = dlgOpen.GetPathName();
	AfxGetApp()->WriteProfileString(_T("HEX2DATA"), _T("SrcName"), strSrcName);

	if (dlgSave.DoModal() != IDOK)
		return;

	strDstName = dlgSave.GetPathName();
	AfxGetApp()->WriteProfileString(_T("HEX2DATA"), _T("DstName"), strDstName);

	Hex2H(strDstName, strSrcName);
}

void CConvertFileDlg::OnWaveConverter() 
{
	CString strSrcName = AfxGetApp()->GetProfileString(_T("WAVE2PCM"), _T("SrcName"), _T("demo.wav"));
	CString strDstName = AfxGetApp()->GetProfileString(_T("WAVE2PCM"), _T("DstName"), _T("demo.pcm"));
	CFileDialog dlgOpen(TRUE, _T("*.wav"), strSrcName, OFN_FILEMUSTEXIST|OFN_PATHMUSTEXIST, szWaveFilter);
	CFileDialog dlgSave(FALSE, _T("*.pcm"), strDstName, OFN_PATHMUSTEXIST, szPcmFilter);

	if (dlgOpen.DoModal() != IDOK)
		return;

	strSrcName = dlgOpen.GetPathName();
	AfxGetApp()->WriteProfileString(_T("WAVE2PCM"), _T("SrcName"), strSrcName);

	if (dlgSave.DoModal() != IDOK)
		return;

	strDstName = dlgSave.GetPathName();
	AfxGetApp()->WriteProfileString(_T("WAVE2PCM"), _T("DstName"), strDstName);

	Wave2Pcm(strDstName, strSrcName);
}

void CConvertFileDlg::OnEncodeFile() 
{
	CString strSrcName = AfxGetApp()->GetProfileString(_T("AudioEncode"), _T("SrcName"), _T("udpaudio.pcm"));
	CFileDialog dlgOpen(TRUE, _T("*.pcm"), strSrcName, OFN_FILEMUSTEXIST|OFN_PATHMUSTEXIST, szPcmFilter);

	if (dlgOpen.DoModal() != IDOK)
		return;

	strSrcName = dlgOpen.GetPathName();
	AfxGetApp()->WriteProfileString(_T("AudioEncode"), _T("SrcName"), strSrcName);

	CString strDstName = AfxGetApp()->GetProfileString(_T("AudioEncode"), _T("DstName"), _T("udpaudio.dat"));
	CFileDialog dlgSave(FALSE, _T("*.dat"), strDstName, OFN_PATHMUSTEXIST, szDatFilter);

	if (dlgSave.DoModal() != IDOK)
		return;

	strDstName = dlgSave.GetPathName();
	AfxGetApp()->WriteProfileString(_T("AudioEncode"), _T("DstName"), strDstName);

	AudioEncode(strDstName, strSrcName);
}

void CConvertFileDlg::OnDecodeFile() 
{
	CString strSrcName = AfxGetApp()->GetProfileString(_T("AudioDecode"), _T("SrcName"), _T("udpaudio.dat"));
	CFileDialog dlgOpen(TRUE, _T("*.dat"), strSrcName, OFN_FILEMUSTEXIST|OFN_PATHMUSTEXIST, szDatFilter);

	if (dlgOpen.DoModal() != IDOK)
		return;

	strSrcName = dlgOpen.GetPathName();
	AfxGetApp()->WriteProfileString(_T("AudioDecode"), _T("SrcName"), strSrcName);

	CString strDstName = AfxGetApp()->GetProfileString(_T("AudioDecode"), _T("DstName"), _T("udpaudio.pcm"));
	CFileDialog dlgSave(FALSE, _T("*.pcm"), strDstName, OFN_PATHMUSTEXIST, szPcmFilter);

	if (dlgSave.DoModal() != IDOK)
		return;

	strDstName = dlgSave.GetPathName();
	AfxGetApp()->WriteProfileString(_T("AudioDecode"), _T("DstName"), strDstName);

	AudioDecode(strDstName, strSrcName);
}

BOOL CConvertFileDlg::Pcm2H(CString strDstName, CString strSrcName)
{
	CString strTmp = m_strSourceDir + _T("encode.tmp");

	if (AudioEncode(strTmp, strSrcName))
	{
		return Binary2H(strDstName, strTmp);
	}
	return FALSE;
}

BOOL CConvertFileDlg::Pcm2H_2(CString strDstPath, CString strSrcPath, CString strName)
{
	return Pcm2H(strDstPath + strName + _T(".h"), strSrcPath + strName + _T(".pcm"));
}

void CConvertFileDlg::Pcm2H_All(CString strDstPath, CString strSrcPath)
{
	Pcm2H_2(strDstPath, strSrcPath, _T("0"));
	Pcm2H_2(strDstPath, strSrcPath, _T("1"));
	Pcm2H_2(strDstPath, strSrcPath, _T("2"));
	Pcm2H_2(strDstPath, strSrcPath, _T("3"));
	Pcm2H_2(strDstPath, strSrcPath, _T("4"));
	Pcm2H_2(strDstPath, strSrcPath, _T("5"));
	Pcm2H_2(strDstPath, strSrcPath, _T("6"));
	Pcm2H_2(strDstPath, strSrcPath, _T("7"));
	Pcm2H_2(strDstPath, strSrcPath, _T("8"));
	Pcm2H_2(strDstPath, strSrcPath, _T("9"));
	Pcm2H_2(strDstPath, strSrcPath, _T("10"));
	Pcm2H_2(strDstPath, strSrcPath, _T("hundred"));
	Pcm2H_2(strDstPath, strSrcPath, _T("local"));
	Pcm2H_2(strDstPath, strSrcPath, _T("blank"));
	Pcm2H_2(strDstPath, strSrcPath, _T("dot"));
	Pcm2H_2(strDstPath, strSrcPath, _T("number"));
	Pcm2H_2(strDstPath, strSrcPath, _T("ip"));
	Pcm2H_2(strDstPath, strSrcPath, _T("signal"));
	Pcm2H_2(strDstPath, strSrcPath, _T("gateway"));
	Pcm2H_2(strDstPath, strSrcPath, _T("mask"));
	Pcm2H_2(strDstPath, strSrcPath, _T("service"));
	Pcm2H_2(strDstPath, strSrcPath, _T("subnet"));
	Pcm2H_2(strDstPath, strSrcPath, _T("one"));
	Pcm2H_2(strDstPath, strSrcPath, _T("two"));
//	Pcm2H_2(strDstPath, strSrcPath, _T("error"));
//	Pcm2H_2(strDstPath, strSrcPath, _T("message"));
//	Pcm2H_2(strDstPath, strSrcPath, _T("you"));
//	Pcm2H_2(strDstPath, strSrcPath, _T("before"));
//	Pcm2H_2(strDstPath, strSrcPath, _T("input"));
//	Pcm2H_2(strDstPath, strSrcPath, _T("tiao"));
//	Pcm2H_2(strDstPath, strSrcPath, _T("wan"));
//	Pcm2H_2(strDstPath, strSrcPath, _T("new"));
//	Pcm2H_2(strDstPath, strSrcPath, _T("have"));
}

void CConvertFileDlg::OnBatchWork() 
{
	CString strSrcPath, strDstPath;

	m_iAudioType = 6;	// g.723.1
	m_bAudioNoAcm = TRUE;
	m_bUseVAD = FALSE;
	m_bUseHighRate = TRUE;

//	m_iAudioType = 7;	// ilbc
	// Australian
//	Pcm2H_All(m_strSourceDir + _T("inc\\ivr\\au\\"), m_strSourceDir + _T("data\\ivr\\au_m\\"));
	// Chinese Male
//	Pcm2H_All(m_strSourceDir + _T("inc\\ivr\\cn\\"), m_strSourceDir + _T("data\\ivr\\cn_m\\"));
	// Chinese Female
//	Pcm2H_All(m_strSourceDir + _T("inc\\ivr\\cn\\"), m_strSourceDir + _T("data\\ivr\\cn_f\\"));
	// French
//	Pcm2H_All(m_strSourceDir + _T("inc\\ivr\\fr\\"), m_strSourceDir + _T("data\\ivr\\fr_f\\"));
	// German
//	Pcm2H_All(m_strSourceDir + _T("inc\\ivr\\de\\"), m_strSourceDir + _T("data\\ivr\\de_f\\"));
	// Japanese
//	Pcm2H_All(m_strSourceDir + _T("inc\\ivr\\jp\\"), m_strSourceDir + _T("data\\ivr\\jp_m\\"));
	// Italian
//	Pcm2H_All(m_strSourceDir + _T("inc\\ivr\\it\\"), m_strSourceDir + _T("data\\ivr\\it_m\\"));
	// American
//	Pcm2H_All(m_strSourceDir + _T("inc\\ivr\\us\\"), m_strSourceDir + _T("data\\ivr\\us_m\\"));
	// Spanish
//	Pcm2H_All(m_strSourceDir + _T("inc\\ivr\\es\\"), m_strSourceDir + _T("data\\ivr\\es_m\\"));
	// Catalonian (Spain)
//	Pcm2H_All(m_strSourceDir + _T("inc\\ivr\\es_c\\"), m_strSourceDir + _T("data\\ivr\\es_c_f\\"));
	// Poland
//	Pcm2H_All(m_strSourceDir + _T("inc\\ivr\\pl\\"), m_strSourceDir + _T("data\\ivr\\pl_m\\"));
	// Slovakia
//	Pcm2H_All(m_strSourceDir + _T("inc\\ivr\\sk\\"), m_strSourceDir + _T("data\\ivr\\sk_f\\"));
	// Portugal
	Pcm2H_All(m_strSourceDir + _T("inc\\ivr\\pt\\"), m_strSourceDir + _T("data\\ivr\\pt_m\\"));
	// Brazil (Portuguese)
//	Pcm2H_All(m_strSourceDir + _T("inc\\ivr\\br\\"), m_strSourceDir + _T("data\\ivr\\br_m\\"));
}

//void CConvertFileDlg::MergeHex(CString strDstName, CString strSrcName1, CString strSrcName2)
void CConvertFileDlg::MergeHex(CString strDstName, ...)
{
	CStdioFile fileWrite;
	CStdioFile fileRead;
	CString strRead;
	CString strEnd = ":00000001FF";
	CString strMessage;
	char * strSrcName;

	va_list v1;

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

		if (!fileRead.Open(strSrcName, CFile::modeRead|CFile::typeText))
		{
			strMessage = "Can not open ";
			strMessage += strSrcName;
			AfxMessageBox(strMessage);
			return;
		}
		while (fileRead.ReadString(strRead))
		{
			if (strRead != strEnd)
			{
				fileWrite.WriteString(strRead);
				fileWrite.WriteString("\n");
			}
		}
		fileRead.Close();
	} while (1);
	va_end(v1);

	fileWrite.WriteString(strEnd);
	fileWrite.WriteString("\n");
	fileWrite.Close();

	AfxGetApp()->EndWaitCursor();
}

void CConvertFileDlg::OnBuild004t() 
{
	MergePa168f("004T", m_strOutputDir, m_strSourceDir);
}

void CConvertFileDlg::OnBuildF040() 
{
	MergeYuxin("F040", m_strOutputDir, m_strSourceDir);
}

void CConvertFileDlg::OnBuildPA168T() 
{
	MergePa168t("2MFlash", m_strOutputDir, m_strSourceDir);
}

void CConvertFileDlg::OnBuildStandard() 
{
	MergePa168s("1MFlash", m_strOutputDir, m_strSourceDir);
}

void CConvertFileDlg::OnOptionsBinToTxt() 
{
	CString strSrcName = AfxGetApp()->GetProfileString(_T("BIN2TXT"), _T("SrcName"), _T("options.dat"));
	CFileDialog dlgOpen(TRUE, _T("*.dat"), strSrcName, OFN_FILEMUSTEXIST|OFN_PATHMUSTEXIST, szDatFilter);
	char * p;
	CStdioFile fileRead;
	CStdioFile fileWrite;
//	int iLength;
	CString	cStr;

	if (dlgOpen.DoModal() != IDOK)
		return;

	strSrcName = dlgOpen.GetPathName();
	AfxGetApp()->WriteProfileString(_T("BIN2TXT"), _T("SrcName"), strSrcName);

	if (!fileRead.Open(strSrcName, CFile::modeRead))
	{
		AfxMessageBox("Can not open source file");
		return;
	}

	CString strDstName = AfxGetApp()->GetProfileString(_T("BIN2TXT"), _T("DstName"), _T("options.txt"));
	CFileDialog dlgSave(FALSE, _T("*.txt"), strDstName, OFN_PATHMUSTEXIST, szTxtFilter);

	if (dlgSave.DoModal() != IDOK)
		return;

	strDstName = dlgSave.GetPathName();
	AfxGetApp()->WriteProfileString(_T("BIN2TXT"), _T("DstName"), strDstName);
	fileWrite.Open(strDstName, CFile::modeCreate|CFile::modeWrite|CFile::typeText);

	AfxGetApp()->BeginWaitCursor();
//	iLength = fileRead.GetLength();
	p = (char *)malloc(PHONE_SETTINGS_SIZE);
	fileRead.Read(p, PHONE_SETTINGS_SIZE);

	ConvertSettings((char *)p, cStr, PHONE_SETTINGS_SIZE);

	fileWrite.WriteString(cStr);

	AfxGetApp()->EndWaitCursor();

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

}

void CConvertFileDlg::OnOptionsTxtToBin() 
{
	CString strSrcName = AfxGetApp()->GetProfileString(_T("TXT2BIN"), _T("SrcName"), _T("options.txt"));
	CFileDialog dlgOpen(TRUE, _T("*.txt"), strSrcName, OFN_PATHMUSTEXIST, szTxtFilter);
	char * p;
	CStdioFile fileRead;
	CFile fileWrite;
	int iCur;
	CString	cReadStr;

	if (dlgOpen.DoModal() != IDOK)
		return;

	strSrcName = dlgOpen.GetPathName();
	AfxGetApp()->WriteProfileString(_T("TXT2BIN"), _T("SrcName"), strSrcName);

	if (!fileRead.Open(strSrcName, CFile::modeRead))
	{
		AfxMessageBox("Can not open source file");
		return;
	}

	CString strDstName = AfxGetApp()->GetProfileString(_T("TXT2BIN"), _T("DstName"), _T("options.dat"));
	CFileDialog dlgSave(FALSE, _T("*.dat"), strDstName, OFN_FILEMUSTEXIST|OFN_PATHMUSTEXIST, szDatFilter);

	if (dlgSave.DoModal() != IDOK)
		return;

	strDstName = dlgSave.GetPathName();
	AfxGetApp()->WriteProfileString(_T("TXT2BIN"), _T("DstName"), strDstName);
	fileWrite.Open(strDstName, CFile::modeCreate|CFile::modeWrite);
//	fileWrite.Open(strDstName, CFile::modeCreate|CFile::modeWrite|CFile::typeText);

	AfxGetApp()->BeginWaitCursor();
	p = (char *)malloc(PHONE_SETTINGS_SIZE);
	memset(p, 0, PHONE_SETTINGS_SIZE);

	iCur = 0;
	while (fileRead.ReadString(cReadStr))
	{
		if (cReadStr.GetAt(0) == '[')
		{
			cReadStr.MakeUpper();
			if (cReadStr.Left(8) == "[SETTING")
			{
				iCur = cReadStr.GetAt(8) - ']';
			}
		}
		else
		{
			if( !(CompareSettings((char *)(p+512*iCur), cReadStr)) )
				return;
		}
	}
	AfxGetApp()->EndWaitCursor();

	fileWrite.Write(p, PHONE_SETTINGS_SIZE);

	AfxGetApp()->EndWaitCursor();

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


void CConvertFileDlg::OnBuildDir() 
{
	CDirectoryDlg dlg;
	if (dlg.DoModal() != IDOK)	return;

	CString strPathName = dlg.GetPathName();
	CString strName = dlg.GetFileName();
	m_strOutputDir = strPathName.Left(strPathName.GetLength() - strName.GetLength());
	m_strSourceDir = m_strOutputDir;
	UpdateData(FALSE);

	AfxGetApp()->WriteProfileString(_T("Build"), _T("OutputDir"), m_strOutputDir);
}

void CConvertFileDlg::OnBrowse() 
{
	CString strSrcName = AfxGetApp()->GetProfileString(_T("Browse"), _T("SrcName"), _T("pa1688.bin"));
	CFileDialog dlgOpen(TRUE, _T("*.bin"), strSrcName, OFN_FILEMUSTEXIST|OFN_PATHMUSTEXIST, szBinFilter);

	CDirectoryDlg dlg;
	if (dlgOpen.DoModal() != IDOK)
		return;

	strSrcName = dlgOpen.GetPathName();
	m_strSourcePath = strSrcName;
	AfxGetApp()->WriteProfileString(_T("Browse"), _T("SrcName"), strSrcName);
	m_strBinaryFile = dlgOpen.GetFileName();

	UpdateData(FALSE);
}

void CConvertFileDlg::OnModifyLog() 
{
	CFile srcFile, dstFile;
	int iLength;
	char * p;
	CString strLogName;
	char buf[15];
	LONG lOffset = 0x00bf10;

	m_ctlNewLogName.GetWindowText(strLogName);
	if (!strlen(strLogName))
	{
		AfxMessageBox("Please input log name");
		return;
	}

	if (strlen(strLogName) > 15)
	{
		AfxMessageBox("Log name error");
		return;
	}

	if (!srcFile.Open(m_strSourcePath, CFile::modeReadWrite))
	{
		AfxMessageBox("Can not open source file");
		return;
	}

	iLength = srcFile.GetLength();
	AfxGetApp()->BeginWaitCursor();
	p = (char *)malloc(iLength);
	srcFile.Read(p, iLength);
	memset(buf, 0, 15);
	strcpy(buf, strLogName);

	if( (iLength % 524288) == 0 )
	{
		memcpy(p+lOffset, buf, 15);
		lOffset = 0x01bf10;
		memcpy(p+lOffset, buf, 15);
		lOffset = 0x03bf10;
		memcpy(p+lOffset, buf, 15);
	}
	else
	{
		memcpy(p+lOffset, buf, 15);
		lOffset = 0x02bf10;
		memcpy(p+lOffset, buf, 15);
	}


	srcFile.SeekToBegin();
	srcFile.Write(p, iLength);
	AfxGetApp()->EndWaitCursor();
	free(p);
	srcFile.Close();
}

void CConvertFileDlg::OnAdspToBin() 
{
	CString strSrcName = AfxGetApp()->GetProfileString(_T("ADSP2BIN"), _T("SrcName"), _T("ADSP.txt"));
	CFileDialog dlgOpen(TRUE, _T("*.txt"), strSrcName, OFN_PATHMUSTEXIST, szTxtFilter);
	CString strDstName = AfxGetApp()->GetProfileString(_T("ADSP2BIN"), _T("DstName"), _T("demo.dat"));
	CFileDialog dlgSave(FALSE, _T("*.dat"), strDstName, OFN_FILEMUSTEXIST|OFN_PATHMUSTEXIST, szDatFilter);

	if (dlgOpen.DoModal() != IDOK)
		return;

	if (dlgSave.DoModal() != IDOK)
		return;

	strSrcName = dlgOpen.GetPathName();
	AfxGetApp()->WriteProfileString(_T("TXT2BIN"), _T("SrcName"), strSrcName);
	strDstName = dlgSave.GetPathName();
	AfxGetApp()->WriteProfileString(_T("ADSP2BIN"), _T("DstName"), strDstName);

	Adsp2BIN(strDstName, strSrcName);

}

void CConvertFileDlg::OnBinToAdsp() 
{
	CString strSrcName = AfxGetApp()->GetProfileString(_T("BIN2ADSP"), _T("SrcName"), _T("adsp.dat"));
	CString strDstName = AfxGetApp()->GetProfileString(_T("BIN2ADSP"), _T("DstName"), _T("adsp.txt"));
	CFileDialog dlgOpen(TRUE, _T("*.dat"), strSrcName, OFN_FILEMUSTEXIST|OFN_PATHMUSTEXIST, szDatFilter);
	CFileDialog dlgSave(FALSE, _T("*.txt"), strDstName, OFN_PATHMUSTEXIST, szHFilter);

	if (dlgOpen.DoModal() != IDOK)
		return;

	if (dlgSave.DoModal() != IDOK)
		return;

	strSrcName = dlgOpen.GetPathName();
	AfxGetApp()->WriteProfileString(_T("BIN2ADSP"), _T("SrcName"), strSrcName);
	strDstName = dlgSave.GetPathName();
	AfxGetApp()->WriteProfileString(_T("BIN2ADSP"), _T("DstName"), strDstName);

	BIN2Adsp(strDstName, strSrcName);
	
}
