www.pudn.com > FileConverter.zip > FileOprView.cpp
// FileOprView.cpp : インプリメンテーション ファイル
//
#include "stdafx.h"
#include "FileConverter.h"
#include "FileOprView.h"
#include "LogView.h"
#include "MainFrm.h"
#include "MessageInf.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/////////////////////////////////////////////////////////////////////////////
// CFileOprView
IMPLEMENT_DYNCREATE(CFileOprView, CFormView)
CFileOprView::CFileOprView()
: CFormView(CFileOprView::IDD)
{
//{{AFX_DATA_INIT(CFileOprView)
//}}AFX_DATA_INIT
}
CFileOprView::~CFileOprView()
{
}
void CFileOprView::DoDataExchange(CDataExchange* pDX)
{
CFormView::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CFileOprView)
DDX_Control(pDX, IDC_EDIT_TO, m_PathTo);
DDX_Control(pDX, IDC_EDIT_FROM, m_PathFrom);
DDX_Control(pDX, IDC_CMB_TO, m_TypeTo);
DDX_Control(pDX, IDC_CMB_FROM, m_TypeFrom);
//}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(CFileOprView, CFormView)
//{{AFX_MSG_MAP(CFileOprView)
ON_BN_CLICKED(IDC_START, OnStart)
ON_BN_CLICKED(IDC_BTN_TO, OnBtnTo)
ON_BN_CLICKED(IDC_BTN_FROM, OnBtnFrom)
ON_WM_CTLCOLOR()
ON_BN_CLICKED(IDC_STOP, OnClear)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CFileOprView 診断
#ifdef _DEBUG
void CFileOprView::AssertValid() const
{
CFormView::AssertValid();
}
void CFileOprView::Dump(CDumpContext& dc) const
{
CFormView::Dump(dc);
}
#endif //_DEBUG
/////////////////////////////////////////////////////////////////////////////
// CFileOprView メッセージ ハンドラ
void CFileOprView::OnStart()
{
GetDlgItem(IDC_START)->EnableWindow(FALSE);
// フレームを取得する
CMainFrame* pMF=(CMainFrame*)AfxGetApp()->GetMainWnd();
// ログビューを取得して、メッセージを送る
CLogView* activeVW=(CLogView*)pMF->wndSplitter1.GetPane(1,0);
activeVW->PostMessage(WM_MY_MESSAGE_CLEAR,(WPARAM)WM_MY_MESSAGE_CLEAR,0);
activeVW->PostMessage(WM_MY_MESSAGE_START,(WPARAM)WM_MY_MESSAGE_START,0);
if (!doCheck()) {
activeVW->PostMessage(WM_MY_MESSAGE_FAIL,(WPARAM)WM_MY_MESSAGE_FAIL,0);
return;
}
if (!ConvertFile()) {
activeVW->PostMessage(WM_MY_MESSAGE_FAIL,(WPARAM)WM_MY_MESSAGE_FAIL,0);
return;
}
activeVW->PostMessage(WM_MY_MESSAGE_SUCCESS,(WPARAM)WM_MY_MESSAGE_SUCCESS,0);
activeVW->PostMessage(WM_MY_MESSAGE_END,(WPARAM)WM_MY_MESSAGE_END,0);
GetDlgItem(IDC_START)->EnableWindow(TRUE);
}
BOOL CFileOprView::doCheck()
{
if (m_TypeFrom.GetCurSel()==-1) {
MessageBox("元ファイル文字コードを選択してください。","ファイル変換",MB_OK|MB_ICONEXCLAMATION );
return FALSE;
}
if (m_TypeTo.GetCurSel()==-1) {
MessageBox("目標ファイル文字コードを選択してください。","ファイル変換",MB_OK|MB_ICONEXCLAMATION );
return FALSE;
}
m_PathFrom.GetWindowText(strPathFrom);
if (strPathFrom=="") {
MessageBox("変換元を入力してください。","ファイル変換",MB_OK|MB_ICONEXCLAMATION );
return FALSE;
}
m_PathTo.GetWindowText(strPathTo);
if (strPathTo=="") {
MessageBox("変換先を入力を選択してください。","ファイル変換",MB_OK|MB_ICONEXCLAMATION );
return FALSE;
}
// コントロールの値を変数にセットする
m_TypeFrom.GetLBText(m_TypeFrom.GetCurSel(),strTypeFrom);
m_TypeTo.GetLBText(m_TypeTo.GetCurSel(),strTypeTo);
return TRUE;
}
void CFileOprView::OnInitialUpdate()
{
CFormView::OnInitialUpdate();
// TODO: この位置に固有の処理を追加するか、または基本クラスを呼び出してください
InitialControl(0,_T(""),_T(""),_T(""));
}
BOOL CFileOprView::InitialControl(int iFlag,CString kokyakuType,CString kokyakuID,CString kokyakuName)
{
// 画面初期化
if ((iFlag == 0)||(kokyakuID.Compare(_T(""))==0)) {
SetDlgItemText(IDC_KOKYAKU,"顧客:なし");
GetDlgItem(IDC_CMB_FROM)->EnableWindow(FALSE);
GetDlgItem(IDC_CMB_TO)->EnableWindow(FALSE);
GetDlgItem(IDC_EDIT_FROM)->EnableWindow(FALSE);
GetDlgItem(IDC_EDIT_TO)->EnableWindow(FALSE);
GetDlgItem(IDC_BTN_FROM)->EnableWindow(FALSE);
GetDlgItem(IDC_BTN_TO)->EnableWindow(FALSE);
GetDlgItem(IDC_START)->EnableWindow(FALSE);
GetDlgItem(IDC_STOP)->EnableWindow(FALSE);
} else {
// 左側のTreeViewからのメッセージ
SetDlgItemText(IDC_KOKYAKU,_T("顧客:" + kokyakuName + "(ID:" + kokyakuID +",レベル:"+ kokyakuType +")") );
GetDlgItem(IDC_CMB_FROM)->EnableWindow(TRUE);
GetDlgItem(IDC_CMB_TO)->EnableWindow(TRUE);
GetDlgItem(IDC_EDIT_FROM)->EnableWindow(TRUE);
GetDlgItem(IDC_EDIT_TO)->EnableWindow(TRUE);
GetDlgItem(IDC_BTN_FROM)->EnableWindow(TRUE);
GetDlgItem(IDC_BTN_TO)->EnableWindow(TRUE);
GetDlgItem(IDC_START)->EnableWindow(TRUE);
GetDlgItem(IDC_STOP)->EnableWindow(TRUE);
}
return TRUE;
}
void CFileOprView::OnBtnTo()
{
CString sFolderPath;
BROWSEINFO bi;
char Buffer[MAX_PATH];
// パラメータを初期化する
bi.hwndOwner = NULL;
bi.pidlRoot =NULL;
bi.pszDisplayName = Buffer;
bi.lpszTitle = "ファイル出力パスを設定する";
bi.ulFlags = BIF_EDITBOX;
bi.lpfn = NULL;
bi.iImage=IDR_MAINFRAME;
LPITEMIDLIST pIDList = SHBrowseForFolder(&bi);
if(pIDList)
{
SHGetPathFromIDList(pIDList, Buffer);
sFolderPath = Buffer;
}
LPMALLOC lpMalloc;
if(FAILED(SHGetMalloc(&lpMalloc))) return;
// メモリにプットする
lpMalloc->Free(pIDList);
lpMalloc->Release();
// 取得したパスをテキストボックスにセットする
SetDlgItemText(IDC_EDIT_TO,sFolderPath);
}
void CFileOprView::OnBtnFrom()
{
CString strAppName;// カレントパス
::GetModuleFileName(NULL, strAppName.GetBuffer(_MAX_PATH), _MAX_PATH);
strAppName.ReleaseBuffer();
int nPos = strAppName.ReverseFind('\\');
strAppName = strAppName.Left(nPos + 1);
// ファイルの拡張子を設定する
LPCTSTR szFilter = "CSVファイル (*.csv)|*.csv|テキストファイル (*.txt)|*.txt|";
// オープン:TRUE 保存:FALSE
CFileDialog dlg(TRUE,NULL ,NULL,OFN_ENABLESIZING ,szFilter,NULL);
if(dlg.DoModal() == IDOK)
{
CString strFile = dlg.GetPathName(); // パスを取得する
SetDlgItemText(IDC_EDIT_FROM,strFile);
}
}
HBRUSH CFileOprView::OnCtlColor(CDC* pDC, CWnd* pWnd, UINT nCtlColor)
{
HBRUSH hbr = CFormView::OnCtlColor(pDC, pWnd, nCtlColor);
CString xx;
xx.Format("%d",(int)nCtlColor);
TRACE("AAAAAAAA");
TRACE(xx);
// TODO: この位置で DC のアトリビュートを変更してください
switch (nCtlColor)
{
case CTLCOLOR_EDIT:
HBRUSH B = CreateSolidBrush(RGB(255,255,255));
return (HBRUSH) B;
}
// TODO: デフォルトのブラシが望みのものでない場合には、違うブラシを返してください
return hbr;
}
CString CFileOprView::ConvertStr(CString str, int sourceCodepage, int targetCodepage)
{
int len=str.GetLength();
int unicodeLen=MultiByteToWideChar(sourceCodepage,0,str,-1,NULL,0);
wchar_t* pUnicode;
pUnicode=new wchar_t[unicodeLen+1];
memset(pUnicode,0,(unicodeLen+1)*sizeof(wchar_t));
MultiByteToWideChar(sourceCodepage,0,str,-1,(LPWSTR)pUnicode,unicodeLen);
BYTE * pTargetData = NULL;
int targetLen=WideCharToMultiByte(targetCodepage,0,
(LPWSTR)pUnicode,-1,(char *)pTargetData,0,NULL,NULL);
pTargetData=new BYTE[targetLen+1];
memset(pTargetData,0,targetLen+1);
WideCharToMultiByte(targetCodepage,0,(LPWSTR)pUnicode,
-1,(char *)pTargetData,targetLen,NULL,NULL);
CString rt;
rt.Format("%s",pTargetData);
delete pUnicode;
delete pTargetData;
return rt;
}
BOOL CFileOprView::ConvertFile()
{
try
{
CStdioFile fileRead;
CStdioFile fileWrite;
fileRead.Open(strPathFrom,CFile::modeRead);
CString strTemp;
int i =0;
strTemp=fileRead.GetFileTitle();
CString filename;
filename = fileRead.GetFileName();
int ipos=0;
while (filename.Find(".",ipos+1)!=-1) {
ipos =filename.Find(".",0);
}
filename=filename.Mid(0,ipos)+GetSystemTime()+filename.Mid(ipos,filename.GetLength()-ipos);
fileWrite.Open(strPathTo + "\\" +filename,CFile::modeCreate|CFile::modeWrite);
fileWrite.SeekToBegin();
int wordCodeFrom;
int wordCodeTo;
wordCodeFrom = ((strTypeFrom.Compare("Shift-JIS")==0)?932:65001);
wordCodeTo = ((strTypeTo.Compare("Shift-JIS")==0)?932:65001);
while(fileRead.ReadString(strTemp)!=NULL)
{
fileWrite.WriteString(ConvertStr(strTemp, wordCodeFrom, wordCodeTo) + "\n");
i++;
}
fileWrite.Flush();
fileWrite.Close();
fileRead.Close();
}
catch(CFileException *e)
{
CString str;
str.Format("データエラー:%d",e->m_cause);
e->Delete();
return FALSE;
}
return TRUE;
}
CString CFileOprView::GetSystemTime()
{
CString year;
CString month;
CString day;
CString hour;
CString minute;
CString second;
CString milliseconds;
SYSTEMTIME sys;
GetLocalTime(&sys);
CString x;
year.Format("%.4d",sys.wYear);
month.Format("%.2d",sys.wMonth);
day.Format("%.2d",sys.wDay);
hour.Format("%.2d",sys.wHour);
minute.Format("%.2d",sys.wMinute);
second.Format("%.2d",sys.wSecond);
milliseconds.Format("%.3d",sys.wMilliseconds);
return year+month+day+hour+minute+second+milliseconds;
}
void CFileOprView::OnClear()
{
SetDlgItemText(IDC_KOKYAKU,"顧客:なし");
SetDlgItemText(IDC_CMB_FROM,"");
SetDlgItemText(IDC_CMB_TO,"");
SetDlgItemText(IDC_EDIT_FROM,"");
SetDlgItemText(IDC_EDIT_TO,"");
}