/*******************************************************************

Copyright (c) 2004 Open Mobile Alliance, Ltd.  All Rights Reserved.

This software is made available under the usage and distribution terms 
covered by the Common Public License 1.0 and by using this software you
are bound by the terms and conditions of the same.

EXCEPT AS EXPRESSLY SET FORTH IN THE COMMON PUBLIC LICENSE,
THIS PROGRAM IS PROVIDED ON AN "AS IS" BASIS, 
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, 
EITHER EXPRESS OR IMPLIED INCLUDING, WITHOUT LIMITATION, 
ANY WARRANTIES OR CONDITIONS OF TITLE, NON-INFRINGEMENT, 
MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE.
   
Each recipient is solely responsible for determining the 
appropriateness of using and distributing this program and assumes 
all risks associated with its exercise of rights under the Common 
Public License, including but not limited to the risks and 
costs of program errors, compliance with applicable laws, 
damage to or loss of data, programs or equipment, and 
unavailability or interruption of operations.

********************************************************************/

// DataPane.cpp : implementation file
//

#include "stdafx.h"
#include "scts3devman.h"
#include "DataPane.h"

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

/////////////////////////////////////////////////////////////////////////////
// CDataPane dialog


CDataPane::CDataPane(char *&_nodeData,SctsDMNodeFormat &_nodeFormat,unsigned int &_nodeSize,CWnd* pParent /*=NULL*/)
	: CDialog(CDataPane::IDD, pParent),nodeData(_nodeData),nodeFormat(_nodeFormat),nodeSize(_nodeSize),isBinary(false)
{
	//{{AFX_DATA_INIT(CDataPane)
	m_data = _T("");
	m_bin = -1;
	//}}AFX_DATA_INIT
}


void CDataPane::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CDataPane)
	DDX_Control(pDX, IDC_DATA, m_dataCtrl);
	DDX_Text(pDX, IDC_DATA, m_data);
	DDX_Radio(pDX, IDC_DATABIN, m_bin);
	//}}AFX_DATA_MAP
}


BEGIN_MESSAGE_MAP(CDataPane, CDialog)
	//{{AFX_MSG_MAP(CDataPane)
	ON_BN_CLICKED(IDC_DATABIN, OnDatabin)
	ON_BN_CLICKED(IDC_DATACHR, OnDatachr)
	ON_BN_CLICKED(IDC_DATAAPPLY, OnDataapply)
	ON_BN_CLICKED(IDC_DATADISCARD, OnDatadiscard)
	ON_BN_CLICKED(IDC_DATALOAD, OnDataload)
	ON_BN_CLICKED(IDC_DATASAVE, OnDatasave)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CDataPane message handlers

BOOL CDataPane::OnInitDialog() 
{
	CDialog::OnInitDialog();
	int pos=nodeSize;
	unsigned char *data=(unsigned char *)nodeData;
	while (pos-- && ((data[pos]>=32 && data[pos]<=126) || data[pos]==10 || data[pos]==13 || data[pos]==8));
	isBinary=(pos!=-1);
	m_bin=isBinary?0:1;
	if (pos!=-1) 
	{
		char temp[10];
		m_data="";
		for (pos=0;pos<nodeSize;pos++)
		{
			sprintf(temp,"%02X",(unsigned char)data[pos]);
			m_data+=temp;
		}
	}
	else
		m_data=nodeData;
	nodeData=0;
	UpdateData(FALSE);
	return TRUE;  // return TRUE unless you set the focus to a control
	              // EXCEPTION: OCX Property Pages should return FALSE
}

BOOL CDataPane::PreTranslateMessage(MSG* pMsg) 
{
	/* Disable ESC  and Enter */
	if (pMsg->message==WM_KEYDOWN && (pMsg->wParam==VK_ESCAPE || pMsg->wParam==VK_RETURN)) return TRUE;

	if (isBinary && pMsg->message==WM_CHAR && pMsg->hwnd==m_dataCtrl)
	{
		char c=toupper((char)pMsg->wParam);
		if ((c<'0' || c>'9') && (c<'A' || c>'F')) return TRUE;
		pMsg->wParam=c;
	}

	return CDialog::PreTranslateMessage(pMsg);
}

void CDataPane::OnDatabin() 
{
	UpdateData(TRUE);
	if (!isBinary)
	{
		char temp[10];
		int pos;
		CString _data;
		unsigned char *data;
		_data=m_data;
		data=(unsigned char *)(LPCTSTR)_data;
		pos=_data.GetLength();
		m_data="";
		while (pos--)
		{
			sprintf(temp,"%02X",*data);
			m_data+=temp;
			++data;
		}
		UpdateData(FALSE);
	}
	isBinary=true;
}

void CDataPane::OnDatachr() 
{
	UpdateData(TRUE);
	if (isBinary)
	{
		m_data="";
		UpdateData(FALSE);
	}
	isBinary=false;
}

void CDataPane::OnDataapply() 
{
	nodeData=0;
	nodeSize=0;
	UpdateData(TRUE);
	if (!m_data.GetLength())
		nodeFormat=DMFORMAT_NULL;
	else
	{
		if (isBinary)
		{
			unsigned char *data;
			unsigned char *dataReadPos;
			unsigned char *dataWritePos;
			unsigned char c;
			int pos;
			if (m_data.GetLength()&1) m_data+="0";
			data=(unsigned char *)sctsMalloc((m_data.GetLength()>>1)+1);
			dataReadPos=(unsigned char *)(LPCTSTR)m_data;
			dataWritePos=data;
			pos=m_data.GetLength();
			while (pos--)
			{
				c=*dataReadPos++;
				if (pos&1)
					*dataWritePos=(c>='A'?c-'A'+10:c-'0')<<4;
				else
				{
					*dataWritePos=*dataWritePos|(c>='A'?c-'A'+10:c-'0');
					++dataWritePos;
				}
			}
			*dataWritePos=0;
			nodeFormat=DMFORMAT_BIN;
			nodeData=(char *)data;
			nodeSize=dataWritePos-data;
		}
		else
		{
			nodeFormat=DMFORMAT_CHR;
			nodeData=(char *)sctsMalloc(m_data.GetLength()+1);
			nodeSize=m_data.GetLength();
			strcpy(nodeData,(LPCTSTR)m_data);
		}
	}
	EndDialog(TRUE);
}

void CDataPane::OnDatadiscard() 
{
	EndDialog(FALSE);	
}

void CDataPane::OnDataload() 
{
	FILE *fp;
	CFileDialog fDlg(TRUE,NULL,NULL,OFN_HIDEREADONLY,"All Files (*.*)|*.*||");
 	fDlg.m_ofn.lpstrTitle="Load Node data from file";
	fDlg.m_ofn.lpstrInitialDir=logPath;
	if (fDlg.DoModal()==IDCANCEL) 
	{
		SetCurrentDirectory(logPath);
		return;
	}
	SetCurrentDirectory(logPath);
	if ((fp=fopen(fDlg.GetPathName(),"rb")))
	{
		fseek(fp,0,SEEK_END);
		int size=ftell(fp);
		int pos=size;
		fseek(fp,0,SEEK_SET);
		unsigned char *data=(unsigned char *)sctsMalloc(pos+1);
		fread(data,pos,1,fp);
		fclose(fp);
		while (pos-- && ((data[pos]>=32 && data[pos]<=126) || data[pos]==10 || data[pos]==13 || data[pos]==8));
		isBinary=(pos!=-1);
		m_bin=isBinary?0:1;
		if (pos!=-1) 
		{
			char temp[10];
			m_data="";
			for (pos=0;pos<size;pos++)
			{
				sprintf(temp,"%02X",(unsigned char)data[pos]);
				m_data+=temp;
			}
		}
		else
		{
			data[size]=0;
			m_data=data;
		}
		sctsFree(data);
		UpdateData(FALSE);
	}
}

void CDataPane::OnDatasave() 
{
	FILE *fp;
	CFileDialog fDlg(FALSE,NULL,NULL,OFN_HIDEREADONLY,"All Files (*.*)|*.*||");
 	fDlg.m_ofn.lpstrTitle="Save Node data to file";
	fDlg.m_ofn.lpstrInitialDir=logPath;
	if (fDlg.DoModal()==IDCANCEL) 
	{
		SetCurrentDirectory(logPath);
		return;
	}
	SetCurrentDirectory(logPath);
	if ((fp=fopen(fDlg.GetPathName(),"wb")))
	{
		UpdateData(TRUE);
		if (isBinary)
		{
			unsigned char *dataReadPos;
			unsigned char c,d;
			int pos;
			if (m_data.GetLength()&1) m_data+="0";
			dataReadPos=(unsigned char *)(LPCTSTR)m_data;
			pos=m_data.GetLength();
			while (pos--)
			{
				c=*dataReadPos++;
				if (pos&1)
					d=(c>='A'?c-'A'+10:c-'0')<<4;
				else
				{
					d=d|(c>='A'?c-'A'+10:c-'0');
					fputc(d,fp);
				}
			}
		}
		else
			fwrite((LPCTSTR)m_data,m_data.GetLength(),1,fp);
		fclose(fp);
	}
}
