📄 lancodeconverterdlg.cpp
字号:
// LancodeConverterDlg.cpp : implementation file
//
#include "stdafx.h"
#include "LancodeConverter.h"
#include "LancodeConverterDlg.h"
#include <sys/stat.h>
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/////////////////////////////////////////////////////////////////////////////
// CAboutDlg dialog used for App About
class CAboutDlg : public CDialog
{
public:
CAboutDlg();
// Dialog Data
//{{AFX_DATA(CAboutDlg)
enum { IDD = IDD_ABOUTBOX };
//}}AFX_DATA
// ClassWizard generated virtual function overrides
//{{AFX_VIRTUAL(CAboutDlg)
protected:
virtual void DoDataExchange(CDataExchange* pDX); // DDX/DDV support
//}}AFX_VIRTUAL
// Implementation
protected:
//{{AFX_MSG(CAboutDlg)
//}}AFX_MSG
DECLARE_MESSAGE_MAP()
};
CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD)
{
//{{AFX_DATA_INIT(CAboutDlg)
//}}AFX_DATA_INIT
}
void CAboutDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CAboutDlg)
//}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(CAboutDlg, CDialog)
//{{AFX_MSG_MAP(CAboutDlg)
// No message handlers
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CLanCodeConverterDlg dialog
CLanCodeConverterDlg::CLanCodeConverterDlg(CWnd* pParent /*=NULL*/)
: CDialog(CLanCodeConverterDlg::IDD, pParent)
{
//{{AFX_DATA_INIT(CLanCodeConverterDlg)
// NOTE: the ClassWizard will add member initialization here
//}}AFX_DATA_INIT
// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}
void CLanCodeConverterDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CLanCodeConverterDlg)
// NOTE: the ClassWizard will add DDX and DDV calls here
//}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(CLanCodeConverterDlg, CDialog)
//{{AFX_MSG_MAP(CLanCodeConverterDlg)
ON_WM_SYSCOMMAND()
ON_WM_PAINT()
ON_WM_QUERYDRAGICON()
ON_BN_CLICKED(IDC_BUTTON_DO, OnButtonDo)
ON_EN_SETFOCUS(IDC_SOURCE, OnSetfocusSource)
ON_EN_SETFOCUS(IDC_SOURCE_HEX, OnSetfocusSourceHex)
ON_BN_CLICKED(IDC_BUTTON_CLEAR, OnButtonClear)
ON_BN_CLICKED(IDC_BUTTON_DO_FILE, OnButtonDoFile)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CLanCodeConverterDlg message handlers
BOOL CLanCodeConverterDlg::OnInitDialog()
{
CDialog::OnInitDialog();
// Add "About..." menu item to system menu.
// IDM_ABOUTBOX must be in the system command range.
ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX);
ASSERT(IDM_ABOUTBOX < 0xF000);
CMenu* pSysMenu = GetSystemMenu(FALSE);
if (pSysMenu != NULL)
{
CString strAboutMenu;
strAboutMenu.LoadString(IDS_ABOUTBOX);
if (!strAboutMenu.IsEmpty())
{
pSysMenu->AppendMenu(MF_SEPARATOR);
pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);
}
}
// Set the icon for this dialog. The framework does this automatically
// when the application's main window is not a dialog
SetIcon(m_hIcon, TRUE); // Set big icon
SetIcon(m_hIcon, FALSE); // Set small icon
// TODO: Add extra initialization here
CString dlg_title_str;
dlg_title_str.LoadString(IDS_STRING_VERSION);
dlg_title_str="GB2312,BIG5,Unicode,UTF8 Mutual Converter"+dlg_title_str;
SetWindowText(dlg_title_str);
CheckRadioButton(IDC_RADIO_UNICODE,IDC_RADIO_UTF8,IDC_RADIO_UTF8);
CheckRadioButton(IDC_RADIO_TEXT,IDC_RADIO_HEX,IDC_RADIO_TEXT);
CheckRadioButton(IDC_RADIO_G_TO_U,IDC_RADIO_UTF8_TO_UNICODE,IDC_RADIO_G_TO_U);
// BOOL IsEnableSrcTxt =(GetCheckedRadioButton(IDC_RADIO_TEXT,IDC_RADIO_HEX)==IDC_RADIO_TEXT);
// (CEdit*)GetDlgItem(IDC_SOURCE)->EnableWindow(IsEnableSrcTxt);
// (CEdit*)GetDlgItem(IDC_SOURCE_HEX)->EnableWindow(!IsEnableSrcTxt);
return TRUE; // return TRUE unless you set the focus to a control
}
void CLanCodeConverterDlg::OnSysCommand(UINT nID, LPARAM lParam)
{
if ((nID & 0xFFF0) == IDM_ABOUTBOX)
{
CAboutDlg dlgAbout;
dlgAbout.DoModal();
}
else
{
CDialog::OnSysCommand(nID, lParam);
}
}
// If you add a minimize button to your dialog, you will need the code below
// to draw the icon. For MFC applications using the document/view model,
// this is automatically done for you by the framework.
void CLanCodeConverterDlg::OnPaint()
{
if (IsIconic())
{
CPaintDC dc(this); // device context for painting
SendMessage(WM_ICONERASEBKGND, (WPARAM) dc.GetSafeHdc(), 0);
// Center icon in client rectangle
int cxIcon = GetSystemMetrics(SM_CXICON);
int cyIcon = GetSystemMetrics(SM_CYICON);
CRect rect;
GetClientRect(&rect);
int x = (rect.Width() - cxIcon + 1) / 2;
int y = (rect.Height() - cyIcon + 1) / 2;
// Draw the icon
dc.DrawIcon(x, y, m_hIcon);
}
else
{
CDialog::OnPaint();
}
}
// The system calls this to obtain the cursor to display while the user drags
// the minimized window.
HCURSOR CLanCodeConverterDlg::OnQueryDragIcon()
{
return (HCURSOR) m_hIcon;
}
typedef enum
{
MY_Unicode =0,
MY_GB2312=936,
MY_BIG5 = 950,
MY_UTF8=CP_UTF8,
}ENCODE_Type_t;
/**Convert Unicode to GB, BIG5 or UTF8*/
DWORD Unicode_To_xxx(ENCODE_Type_t encoding,LPCWSTR unicode_buf,long unicode_len, char* xxx_buf,long* xxx_len )
{
if(encoding==MY_Unicode)
{
long copy_len = min(*xxx_len,unicode_len*2);
memcpy(xxx_buf,unicode_buf,copy_len);
*(LPWSTR)(xxx_buf+copy_len) = 0;
*xxx_len = copy_len;
return 0;
}
*xxx_len = WideCharToMultiByte(encoding,0,unicode_buf,unicode_len,xxx_buf,*xxx_len,NULL,NULL);
if(*xxx_len==0){
return GetLastError();
}
xxx_buf[*xxx_len] = 0;
return 0;
}
DWORD xxx_To_Unicode(ENCODE_Type_t encoding,const char* xxx_buf,long xxx_len,LPWSTR unicode_buf,long* unicode_len)
{
if(encoding==MY_Unicode)
{
long copy_len=min((*unicode_len)*2,xxx_len);
memcpy(unicode_buf,xxx_buf,copy_len);
unicode_buf[copy_len/2]=0;
*unicode_len = copy_len/2;
return 0;
}
*unicode_len=MultiByteToWideChar(encoding, 0, xxx_buf, xxx_len, unicode_buf, *unicode_len);
if(*unicode_len==0)
{
return GetLastError();
}
unicode_buf[*unicode_len]=0;
return 0;
}
void ConvertTextToHex(const char* src,long src_len,CString& des)
{
int i=0;
CString temp_str;
des="";
for(i=0;i<src_len;i++)
{
temp_str.Format("0x%.2X,",(unsigned char)src[i]);
des+=temp_str;
}
}
/* Assume the des buf is enough */
long ConverHexToText(const char* src,char* des)
{
int i=0;int j=0;
long len=strlen(src);
unsigned char temp=0;
for(i=0;i<len;i+=5,temp=0)
{
sscanf(src+i,"0x%2X",&temp);
des[j++]=temp;
}
des[j]=0;
return j;
}
void CLanCodeConverterDlg::OnButtonDo()
{
// TODO: Add your control notification handler code here
CEdit* pSrc=(CEdit*)GetDlgItem(IDC_SOURCE);
CEdit* pSrcHex=(CEdit*)GetDlgItem(IDC_SOURCE_HEX);
CEdit* pDest=(CEdit*)GetDlgItem(IDC_DESTINATION);
CEdit* pDestHex=(CEdit*)GetDlgItem(IDC_DESTINATION_HEX);
CEdit* pWhichSrc=NULL;
LPWSTR Unicode_buf=NULL;
long unicode_len;
char* des_str=NULL;
long des_length=0;
char* src_str =NULL;
long src_length =0;
BOOL IsTextSource = (GetCheckedRadioButton(IDC_RADIO_TEXT,IDC_RADIO_HEX)==IDC_RADIO_TEXT);
if(IsTextSource)
{
pWhichSrc = pSrc;
}
else
{
pWhichSrc = pSrcHex;
}
{
CString src_string;
pWhichSrc->GetWindowText(src_string);
src_length = src_string.GetLength();
src_str =(char*)malloc(src_length+1);
memset(src_str,0x00,src_length+1);
if(IsTextSource)
{
strncpy(src_str,src_string,src_length);
src_str[src_length] = 0;
}
else
{
src_length = ConverHexToText(src_string.GetBuffer(0),src_str);
}
}
unicode_len = src_length+1;
Unicode_buf = (LPWSTR)malloc(unicode_len* sizeof(WCHAR));
memset(Unicode_buf,0x00,unicode_len* sizeof(WCHAR));
des_length = src_length*3+1;
des_str=(char*)malloc(des_length);
memset(des_str,0x00,des_length);
int ConvertManner=GetCheckedRadioButton(IDC_RADIO_G_TO_U,IDC_RADIO_UTF8_TO_UNICODE);
ENCODE_Type_t encode_array[2] = {MY_Unicode,MY_Unicode};
ENCODE_Type_t UnicodeOrUTF8=(GetCheckedRadioButton(IDC_RADIO_UNICODE,IDC_RADIO_UTF8)==IDC_RADIO_UNICODE)?MY_Unicode:MY_UTF8;
switch(ConvertManner)
{
case IDC_RADIO_G_TO_U:
encode_array[0]=MY_GB2312;
encode_array[1]=UnicodeOrUTF8;
break;
case IDC_RADIO_U_TO_G:
encode_array[0]=UnicodeOrUTF8;
encode_array[1]=MY_GB2312;
break;
case IDC_RADIO_B_TO_U:
encode_array[0]=MY_BIG5;
encode_array[1]=UnicodeOrUTF8;
break;
case IDC_RADIO_U_TO_B:
encode_array[0]=UnicodeOrUTF8;
encode_array[1]=MY_BIG5;
break;
case IDC_RADIO_UNICODE_TO_UTF8:
encode_array[0]=MY_Unicode;
encode_array[1]=MY_UTF8;
break;
case IDC_RADIO_UTF8_TO_UNICODE:
encode_array[0]=MY_UTF8;
encode_array[1]=MY_Unicode;
break;
default:
CString info;
info.Format("Application Error:%s(LINE:%d)",__FILE__,__LINE__);
MessageBox(info,"ERROR",MB_OK);
break;
}
xxx_To_Unicode(encode_array[0],src_str,src_length,Unicode_buf,&unicode_len);
Unicode_To_xxx(encode_array[1],Unicode_buf,unicode_len,des_str,&des_length);
pDest->SetWindowText(des_str);
CString hex_string;
if(IsTextSource==TRUE)
{
ConvertTextToHex(src_str,src_length,hex_string);
pSrcHex->SetWindowText(hex_string);
}
else
{
pSrc->SetWindowText(src_str);
}
ConvertTextToHex(des_str,des_length,hex_string);
pDestHex->SetWindowText(hex_string);
if(Unicode_buf)
{
free(Unicode_buf);
}
if(src_str)
{
free(src_str);
}
if(des_str)
{
free(des_str);
}
}
void CLanCodeConverterDlg::OnSetfocusSource()
{
// TODO: Add your control notification handler code here
CheckRadioButton(IDC_RADIO_TEXT, IDC_RADIO_HEX,IDC_RADIO_TEXT);
}
void CLanCodeConverterDlg::OnSetfocusSourceHex()
{
// TODO: Add your control notification handler code here
CheckRadioButton(IDC_RADIO_TEXT, IDC_RADIO_HEX,IDC_RADIO_HEX);
}
void CLanCodeConverterDlg::OnButtonClear()
{
// TODO: Add your control notification handler code here
((CEdit*)GetDlgItem(IDC_SOURCE))->SetWindowText("");
((CEdit*)GetDlgItem(IDC_SOURCE_HEX))->SetWindowText("");
((CEdit*)GetDlgItem(IDC_DESTINATION))->SetWindowText("");
((CEdit*)GetDlgItem(IDC_DESTINATION_HEX))->SetWindowText("");
}
void CLanCodeConverterDlg::OnButtonDoFile()
{
// TODO: Add your control notification handler code here
CString src_file_name;
CString dst_file_name;
struct stat src_stat;
FILE* src_fp = NULL;
FILE* dst_fp = NULL;
char* src_buf = NULL;
char* dst_buf = NULL;
long des_buf_len = 0;
LPWSTR Unicode_buf = NULL;
long unicode_len = 0;
int ConvertManner;
ENCODE_Type_t encode_array[2] = {MY_Unicode,MY_Unicode};
ENCODE_Type_t UnicodeOrUTF8;
GetDlgItem(IDC_EDIT_SOURCE_FILE)->GetWindowText(src_file_name);
GetDlgItem(IDC_EDIT_DEST_FILE)->GetWindowText(dst_file_name);
src_fp = fopen(src_file_name,"rb");
if(!src_fp)
{
MessageBox("Error happend when opening "+src_file_name);
goto MYERROR;
}
dst_fp = fopen(dst_file_name,"wb");
if(!dst_fp)
{
MessageBox("Error happend when creating "+dst_file_name);
goto MYERROR;
}
if(stat(src_file_name,&src_stat)!=0)
{
MessageBox("Error happend when state "+src_file_name);
goto MYERROR;
}
src_buf = (char*)malloc(src_stat.st_size);
if(!src_buf)
{
MessageBox("Error happend when malloc buffer for file "+src_file_name+" perhaps the file size is too large");
goto MYERROR;
}
if(fread(src_buf,src_stat.st_size,1,src_fp)<0)
{
MessageBox("Error happend when read " + src_file_name);
goto MYERROR;
}
unicode_len = src_stat.st_size+1;
Unicode_buf = (LPWSTR)malloc(unicode_len * sizeof(WCHAR));
des_buf_len = src_stat.st_size*3+1;
dst_buf = (char*)malloc(des_buf_len);
if(!dst_buf)
{
CString info;
info.Format("Malloc failed for %dKBytes",des_buf_len);
goto MYERROR;
}
ConvertManner=GetCheckedRadioButton(IDC_RADIO_G_TO_U,IDC_RADIO_UTF8_TO_UNICODE);
UnicodeOrUTF8=(GetCheckedRadioButton(IDC_RADIO_UNICODE,IDC_RADIO_UTF8)==IDC_RADIO_UNICODE)?MY_Unicode:MY_UTF8;
switch(ConvertManner)
{
case IDC_RADIO_G_TO_U:
encode_array[0]=MY_GB2312;
encode_array[1]=UnicodeOrUTF8;
break;
case IDC_RADIO_U_TO_G:
encode_array[0]=UnicodeOrUTF8;
encode_array[1]=MY_GB2312;
break;
case IDC_RADIO_B_TO_U:
encode_array[0]=MY_BIG5;
encode_array[1]=UnicodeOrUTF8;
break;
case IDC_RADIO_U_TO_B:
encode_array[0]=UnicodeOrUTF8;
encode_array[1]=MY_BIG5;
break;
case IDC_RADIO_UNICODE_TO_UTF8:
encode_array[0]=MY_Unicode;
encode_array[1]=MY_UTF8;
break;
case IDC_RADIO_UTF8_TO_UNICODE:
encode_array[0]=MY_UTF8;
encode_array[1]=MY_Unicode;
break;
default:
CString info;
info.Format("Application Error:%s(LINE:%d)",__FILE__,__LINE__);
MessageBox(info,"ERROR",MB_OK);
break;
}
xxx_To_Unicode(encode_array[0],src_buf,src_stat.st_size,Unicode_buf,&unicode_len);
Unicode_To_xxx(encode_array[1],Unicode_buf,unicode_len,dst_buf,&des_buf_len);
fwrite(dst_buf,des_buf_len,1,dst_fp);
MessageBox("Convert Completed!");
MYERROR:
if(src_fp)
{
fclose(src_fp);
}
if(dst_fp)
{
fclose(dst_fp);
}
if(src_buf)
{
free(src_buf);
}
if(dst_buf)
{
free(dst_buf);
}
if(Unicode_buf)
{
free(Unicode_buf);
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -