#include "stdafx.h"
#include "Convert.h"
#include "CovertStr.h"

void OnBin2Cfg(CString strSrcFile, CString strDstFile, BOOL bForce, BOOL bInnomedia) 
{
	CFile in;
	CStdioFile out;
	CFileStatus status;
	CString	cStr, strAnswer;
	char * p;

	if (CFile::GetStatus(strDstFile, status))
	{
		if (!bForce)
		{
			while (1)
			{
				printf("The destination file exists. Overwrite the file?(Yes/No):");
				gets((char *)(LPCTSTR)strAnswer);
				if (!strAnswer.CompareNoCase("no") || !strAnswer.CompareNoCase("n"))
				{
					printf("Exit Convert without writing destination file");
					return;
				}
				if (!strAnswer.CompareNoCase("yes") || !strAnswer.CompareNoCase("ye") || !strAnswer.CompareNoCase("y"))
				{
					break;
				}
			}
		}
	}

	if (!in.Open(strSrcFile, CFile::modeRead))
	{
		printf("Can not open source file %s", strSrcFile);
		return;
	}

	if (!out.Open(strDstFile, CFile::modeCreate|CFile::modeWrite|CFile::typeText))
	{
		printf("Can not create destination file %s", strDstFile);
		in.Close();
		return;
	}

	AfxGetApp()->BeginWaitCursor();
	p = (char *)malloc(SETTINGS_FILE_SIZE);
	in.Read(p, SETTINGS_FILE_SIZE);

	if (bInnomedia)
	{
		AR168ConvertCfg_IM((char *)p, cStr);
	}
	else
	{
		AR168ConvertCfg((char *)p, cStr);
	}

	out.WriteString(cStr);

	AfxGetApp()->EndWaitCursor();

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

void OnCfg2Bin(CString strSrcFile, CString strDstFile, BOOL bForce) 
{
	CFile out;
	CStdioFile in;
	CString	strRead, strAnswer;
	CFileStatus status;
	char * p;
	BOOL bSettings = TRUE;
	int iItem;

	if (CFile::GetStatus(strDstFile, status))
	{
		if (!bForce)
		{
			while (1)
			{
				printf("The destination file exists. Overwrite the file?(Yes/No):");
				gets((char *)(LPCTSTR)strAnswer);
				if (!strAnswer.CompareNoCase("no") || !strAnswer.CompareNoCase("n"))
				{
					printf("Exit Convert without writing destination file");
					return;
				}
				if (!strAnswer.CompareNoCase("yes") || !strAnswer.CompareNoCase("ye") || !strAnswer.CompareNoCase("y"))
				{
					break;
				}
			}
		}
	}

	if (!in.Open(strSrcFile, CFile::modeRead))
	{
		printf("Can not open source file %s", strSrcFile);
		return;
	}

	if (!out.Open(strDstFile, CFile::modeCreate|CFile::modeWrite))
	{
		printf("Can not create destination file %s", strDstFile);
		in.Close();
		return;
	}

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

	while (in.ReadString(strRead))
	{
		if (!strRead.GetLength())
		{
			continue;
		}
		if (strRead.GetAt(0) == '[')
		{
			strRead.MakeLower();
			if (strRead.Left(10) == "[settings]")
			{
				bSettings = TRUE;
			}
			else if (strRead.Left(10) == "[digitmap]")
			{
				bSettings = FALSE;
			}
			continue;
		}
		else if (strRead.Left(12) == "ENDOFCONFIG=")
		{
			break;
		}
		else
		{
			if (bSettings)
			{
				AR168CompareCfg((char *)p, strRead);
			}
			else
			{
				if (iItem < DIGITMAP_ENTRY_NUM)
				{
					if (AR168CompareMap((char *)(p + OPT_DIGIT_MAP), strRead, iItem))
					{
						iItem++;					
					}
				}
			}
		}
	}

	out.Write(p, SETTINGS_FILE_SIZE);

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

void OnBin2H(CString strSrcFile, CString strDstFile, BOOL bForce, int iAddressOffset, int iConvertLength)
{
	CFile in;
	CStdioFile out;
	CString	strAnswer, strName;
	CFileStatus status;
	CString strOut, strItem;
	char drive[_MAX_DRIVE], dir[_MAX_DIR], fname[_MAX_FNAME], ext[_MAX_EXT];
	char * p;
	int iLength, i, j;

	if (CFile::GetStatus(strDstFile, status))
	{
		if (!bForce)
		{
			while (1)
			{
				printf("The destination file exists. Overwrite the file?(Yes/No):");
				gets((char *)(LPCTSTR)strAnswer);
				if (!strAnswer.CompareNoCase("no") || !strAnswer.CompareNoCase("n"))
				{
					printf("Exit Convert without writing destination file");
					return;
				}
				if (!strAnswer.CompareNoCase("yes") || !strAnswer.CompareNoCase("ye") || !strAnswer.CompareNoCase("y"))
				{
					break;
				}
			}
		}
	}

	if (!in.Open(strSrcFile, CFile::modeRead))
	{
		printf("Can not open source file %s", strSrcFile);
		return;
	}

	if (!out.Open(strDstFile, CFile::modeCreate|CFile::modeWrite))
	{
		printf("Can not create destination file %s", strDstFile);
		in.Close();
		return;
	}

	iLength = (int)in.GetLength();
	p = (char *)malloc(iLength);
	in.Read(p, iLength);
	if (iConvertLength == 0)
	{
		iConvertLength = iLength;
	}

	_splitpath(strDstFile, drive, dir, fname, ext);
	strName = fname;
	strName.MakeUpper();

	strOut.Format("#define DATA_%s_SIZE\t\t%d\n", strName, iConvertLength);
	out.WriteString(strOut);
	strOut.Format("const unsigned char _c_%s[DATA_%s_SIZE] = {\n", fname, strName);
	out.WriteString(strOut);

	for (i = iAddressOffset; i < iAddressOffset+iConvertLength; i += FRAME_LEN)
	{
		strOut = "";
		for (j = 0; j < FRAME_LEN; j ++)
		{
			if (i+j < iAddressOffset+iConvertLength)
			{
				strItem.Format("0x%02x", (unsigned char)p[i+j]);
				strOut += strItem;
			}
			if (i+j < ((iAddressOffset+iConvertLength) - 1))
			{
				strOut += ", ";
			}
		}
		strOut += "\n";
		out.WriteString(strOut);
	}

	strOut.Format("};\n");
	out.WriteString(strOut);
	
	free(p);
	in.Close();
	out.Close();
}

void OnRing2Bin(CString strSrcFile, CString strDstFile, BOOL bForce) 
{
	CFile in, out;
//	CStdioFile in;
	CString	strRead, strAnswer;
	CFileStatus status;
	char * pRead;
	char * pWrite;
	int i, iAddressOffset;

	if (!in.Open(strSrcFile, CFile::modeRead))
	{
		printf("Can not open source file %s", strSrcFile);
		return;
	}

	if (out.GetStatus(strDstFile, status))
	{
		if (!bForce)
		{
			while (1)
			{
				printf("The destination file exists. Overwrite the file?(Yes/No):");
				gets((char *)(LPCTSTR)strAnswer);
				if (!strAnswer.CompareNoCase("no") || !strAnswer.CompareNoCase("n"))
				{
					printf("Exit Convert without writing destination file");
					in.Close();
					return;
				}
				if (!strAnswer.CompareNoCase("yes") || !strAnswer.CompareNoCase("ye") || !strAnswer.CompareNoCase("y"))
				{
					break;
				}
			}
		}
	}

	if (!out.Open(strDstFile, CFile::modeCreate|CFile::modeWrite))
	{
		printf("Can not create destination file %s", strDstFile);
		in.Close();
		return;
	}

	pRead = (char *)malloc(DATA_RING_SIZE);
	in.Read(pRead, DATA_RING_SIZE);

	pWrite = (char *)malloc(DATA_RING_SIZE);
	memset(pWrite, 0, DATA_RING_SIZE);

	iAddressOffset = 0;
	for (i = 0; i < DATA_RING_SIZE / (DATA_BLOCK_LEN + 128); i ++)
	{
		memcpy(pWrite + iAddressOffset, pRead + i * DATA_BLOCK_LEN, DATA_BLOCK_LEN);
		iAddressOffset += FILE_HALF_PAGE_SIZE;
	}

	out.Write(pWrite, DATA_RING_SIZE);

	free(pRead);
	free(pWrite);
	in.Close();
	out.Close();
	
}

void OnIvr2Bin(CString strSrcFile, CString strDstFile, BOOL bForce) 
{
	CFile in, out;
	CString	strRead, strAnswer;
	CFileStatus status;
	char * pRead;
	char * pWrite;
	int iOffset, iInLen, iOutLen, iLeftLen;

	if (!in.Open(strSrcFile, CFile::modeRead))
	{
		printf("Can not open source file %s", strSrcFile);
		return;
	}

	if (out.GetStatus(strDstFile, status))
	{
		if (!bForce)
		{
			while (1)
			{
				printf("The destination file exists. Overwrite the file?(Yes/No):");
				gets((char *)(LPCTSTR)strAnswer);
				if (!strAnswer.CompareNoCase("no") || !strAnswer.CompareNoCase("n"))
				{
					printf("Exit Convert without writing destination file");
					in.Close();
					return;
				}
				if (!strAnswer.CompareNoCase("yes") || !strAnswer.CompareNoCase("ye") || !strAnswer.CompareNoCase("y"))
				{
					break;
				}
			}
		}
	}

	if (!out.Open(strDstFile, CFile::modeCreate|CFile::modeWrite|CFile::typeBinary))
	{
		printf("Can not create destination file %s", strDstFile);
		in.Close();
		return;
	}

	iInLen = (int)in.GetLength();
	pRead = (char *)malloc(iInLen);
	in.Read(pRead, iInLen);

	pWrite = (char *)malloc(IVR_FILE_SIZE);
	memset(pWrite, 0, IVR_FILE_SIZE);

	iOffset = 0;
	iOutLen = 0;
	while (iOffset < iInLen)
	{
		iLeftLen = iInLen - iOffset;
		if (!iLeftLen)	break;
		if (iLeftLen > IVR_PAGE_SIZE)
		{
			iLeftLen = IVR_PAGE_SIZE;
		}
		if ((iOutLen + iLeftLen) > IVR_FILE_SIZE)
		{
			break;
		}
		memcpy((char *)(pWrite + iOutLen), (char *)(pRead + iOffset), iLeftLen);
		iOutLen += FILE_HALF_PAGE_SIZE;
		iOffset += iLeftLen;
	}
	out.Write(pWrite, IVR_FILE_SIZE);

	free(pRead);
	free(pWrite);
	in.Close();
	out.Close();
}

void OnPB2Bin(CString strSrcFile, CString strDstFile, BOOL bForce) 
{
	CFile out;
	CStdioFile in;
	CString	strRead, strAnswer;
	CFileStatus status;
	char * p;

	if (CFile::GetStatus(strDstFile, status))
	{
		if (!bForce)
		{
			while (1)
			{
				printf("The destination file exists. Overwrite the file?(Yes/No):");
				gets((char *)(LPCTSTR)strAnswer);
				if (!strAnswer.CompareNoCase("no") || !strAnswer.CompareNoCase("n"))
				{
					printf("Exit Convert without writing destination file");
					return;
				}
				if (!strAnswer.CompareNoCase("yes") || !strAnswer.CompareNoCase("ye") || !strAnswer.CompareNoCase("y"))
				{
					break;
				}
			}
		}
	}

	if (!in.Open(strSrcFile, CFile::modeRead))
	{
		printf("Can not open source file %s", strSrcFile);
		return;
	}

	if (!out.Open(strDstFile, CFile::modeCreate|CFile::modeWrite))
	{
		printf("Can not create destination file %s", strDstFile);
		in.Close();
		return;
	}

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

	while (in.ReadString(strRead))
	{
		if (!strRead.GetLength())
		{
			continue;
		}
		if (strRead.Left(11) == "[phonebook]")
		{
			continue;
		}
		AR168ComparePB((char *)p, strRead);
	}
	out.Write(p, PHONEBOOK_FILE_SIZE);

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

void OnBin2PB(CString strSrcFile, CString strDstFile, BOOL bForce) 
{
	CStdioFile in, out;
	CFileStatus status;
	CString	cStr, strAnswer;
	char * p;

	if (CFile::GetStatus(strDstFile, status))
	{
		if (!bForce)
		{
			while (1)
			{
				printf("The destination file exists. Overwrite the file?(Yes/No):");
				gets((char *)(LPCTSTR)strAnswer);
				if (!strAnswer.CompareNoCase("no") || !strAnswer.CompareNoCase("n"))
				{
					printf("Exit Convert without writing destination file");
					return;
				}
				if (!strAnswer.CompareNoCase("yes") || !strAnswer.CompareNoCase("ye") || !strAnswer.CompareNoCase("y"))
				{
					break;
				}
			}
		}
	}

	if (!in.Open(strSrcFile, CFile::modeRead))
	{
		printf("Can not open source file %s", strSrcFile);
		return;
	}

	if (!out.Open(strDstFile, CFile::modeCreate|CFile::modeWrite|CFile::typeText))
	{
		printf("Can not create destination file %s", strDstFile);
		in.Close();
		return;
	}

	AfxGetApp()->BeginWaitCursor();
	p = (char *)malloc(PHONEBOOK_FILE_SIZE);
	in.Read(p, PHONEBOOK_FILE_SIZE);

	AR168ConvertPB((char *)p, cStr);

	out.WriteString(cStr);

	AfxGetApp()->EndWaitCursor();

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

void OnDsp2H(CStringList& strFileList, CString strSrcDir, CString strDstDir, BOOL bForce)
{
	CString strSrcFile, strDstFile, strTemp, strVal, strOut;;
	CStdioFile out;
	CFileStatus status;
	POSITION pos;
	int iDot, iOffset, iPage, iLength;

	strDstFile = strDstDir + strFileList.RemoveTail();
	if (CFile::GetStatus(strDstFile, status))
	{
		if (!bForce)
		{
			while (1)
			{
				printf("The destination file exists. Overwrite the file?(Yes/No):");
				gets((char *)(LPCTSTR)strTemp);
				if (!strTemp.CompareNoCase("no") || !strTemp.CompareNoCase("n"))
				{
					printf("Exit Convert without writing destination file");
					return;
				}
				if (!strTemp.CompareNoCase("yes") || !strTemp.CompareNoCase("ye") || !strTemp.CompareNoCase("y"))
				{
					break;
				}
			}
		}
	}

	iOffset = 0;
	iPage = 0;
	strOut = "";
	for (pos = strFileList.GetHeadPosition(); pos != NULL; )
	{
		strTemp = strFileList.GetNext(pos);
		strSrcFile = strSrcDir + strTemp;
		if (!CFile::GetStatus(strSrcFile, status))
		{
			printf("Source file %s doesn't exist", strSrcFile);
			return;
		}
		iLength = (int)status.m_size;
		iDot = strTemp.Find('.');
		strTemp = strTemp.Left(iDot);
		strTemp.MakeUpper();
		strVal.Format("#define SYSTEM_%s_PAGE\t\t(SYSTEM_DSP_PAGE + %d)\n", strTemp, iPage);
		strOut += strVal;
		strVal.Format("#define SYSTEM_%s_OFFSET\t\t0x%x\n", strTemp, iOffset);
		strOut += strVal;
		iOffset += iLength;
		while (iOffset >= FILE_FLAG_PAGE_SIZE)
		{
			iOffset -= FILE_FLAG_PAGE_SIZE;
			iPage ++;
			if (iPage >= SYSTEM_DSP_PAGE_NUM)
			{
				printf("DSP dat files too large!");
				return;
			}
		}
	}

	if (!out.Open(strDstFile, CFile::modeCreate|CFile::modeWrite|CFile::typeText))
	{
		printf("Can not create destination file %s", strDstFile);
		return;
	}
	out.WriteString(strOut);
	out.Close();
}
