Index: src/ch/CustomCopyDlg.cpp =================================================================== diff -u -r68739164e349c34dcd0bcb36c6eb381f23cb8b77 -rc435ab507c8b8280264188b49e9ada56d46c0261 --- src/ch/CustomCopyDlg.cpp (.../CustomCopyDlg.cpp) (revision 68739164e349c34dcd0bcb36c6eb381f23cb8b77) +++ src/ch/CustomCopyDlg.cpp (.../CustomCopyDlg.cpp) (revision c435ab507c8b8280264188b49e9ada56d46c0261) @@ -18,13 +18,15 @@ ***************************************************************************/ #include "stdafx.h" #include "resource.h" +#include "FileInfo.h" #include "CustomCopyDlg.h" #include "structs.h" #include "dialogs.h" #include "BufferSizeDlg.h" #include "FilterDlg.h" #include "StringHelpers.h" #include "ch.h" +#include #ifdef _DEBUG #define new DEBUG_NEW @@ -166,8 +168,8 @@ SetWindowPos(&wndNoTopMost, 0, 0, 0, 0, SWP_NOMOVE | SWP_NOSIZE /*| SWP_SHOWWINDOW*/); // paths' listbox - init images - system image list - SHFILEINFO sfi; - HIMAGELIST hImageList = (HIMAGELIST)SHGetFileInfo(_T("C:\\"), FILE_ATTRIBUTE_NORMAL, &sfi, sizeof(SHFILEINFO), + SHFILEINFO sfi; + HIMAGELIST hImageList = (HIMAGELIST)SHGetFileInfo(_T("C:\\"), FILE_ATTRIBUTE_NORMAL, &sfi, sizeof(SHFILEINFO), SHGFI_SYSICONINDEX | SHGFI_SMALLICON); m_ilImages.Attach(hImageList); @@ -201,15 +203,15 @@ CString strText; cbi.mask=CBEIF_IMAGE | CBEIF_TEXT; - for (int i=0;i<(int)m_ccData.m_vRecent.size();i++) + for(size_t stIndex = 0; stIndex < m_ccData.m_vRecent.size(); ++stIndex) { - cbi.iItem=i; - strText=m_ccData.m_vRecent.at(i); - cbi.pszText=strText.GetBuffer(1); - sfi.iIcon=-1; + cbi.iItem = stIndex; + strText=m_ccData.m_vRecent.at(stIndex); + cbi.pszText = strText.GetBuffer(1); + sfi.iIcon = -1; SHGetFileInfo(strText, FILE_ATTRIBUTE_NORMAL, &sfi, sizeof(SHFILEINFO), SHGFI_SYSICONINDEX | SHGFI_SMALLICON); - cbi.iImage=sfi.iIcon; + cbi.iImage = sfi.iIcon; m_ctlDstPath.InsertItem(&cbi); } @@ -389,11 +391,11 @@ // refresh the entries in filters' list m_ctlFilters.DeleteAllItems(); - for (size_t i=0;i(stIndex)); } } @@ -408,29 +410,30 @@ { CFileDialog dlg(TRUE, NULL, NULL, OFN_ALLOWMULTISELECT | OFN_ENABLESIZING | OFN_EXPLORER | OFN_FILEMUSTEXIST | OFN_NODEREFERENCELINKS | OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT, GetResManager().LoadString(IDS_FILEDLGALLFILTER_STRING), this); - TCHAR *pszBuffer=new TCHAR[65535]; + TCHAR *pszBuffer = new TCHAR[65535]; memset(pszBuffer, 0, 65535*sizeof(TCHAR)); dlg.m_ofn.lpstrFile=pszBuffer; dlg.m_ofn.nMaxFile=65535; - if (dlg.DoModal() == IDOK) + if(dlg.DoModal() == IDOK) { + pszBuffer[65534] = _T('\0'); // first element is the path CString strPath=pszBuffer; - int iOffset = (int)_tcslen(pszBuffer) + 1; + size_t stOffset = _tcslen(pszBuffer) + 1; // get filenames - if (pszBuffer[iOffset] == _T('\0')) + if(pszBuffer[stOffset] == _T('\0')) AddPath(strPath); else { - if (strPath.Right(1) != _T("\\")) - strPath+=_T("\\"); - while (pszBuffer[iOffset] != _T('\0')) + if(strPath.Right(1) != _T("\\")) + strPath += _T("\\"); + while(pszBuffer[stOffset] != _T('\0')) { - AddPath(strPath+CString(pszBuffer+iOffset)); - iOffset += (int)_tcslen(pszBuffer+iOffset) + 1; + AddPath(strPath + CString(pszBuffer + stOffset)); + stOffset += _tcslen(pszBuffer + stOffset) + 1; } } } @@ -812,15 +815,15 @@ dlg.m_ffFilter = *pFilter; CString strData; - for (size_t i=0;im_bUseMask && i != iItem) + if(pFilter->m_bUseMask && boost::numeric_cast(stIndex) != iItem) dlg.m_astrAddMask.Add(pFilter->GetCombinedMask(strData)); - if (pFilter->m_bUseExcludeMask && i != iItem) + if (pFilter->m_bUseExcludeMask && boost::numeric_cast(stIndex) != iItem) dlg.m_astrAddExcludeMask.Add(pFilter->GetCombinedExcludeMask(strData)); } } @@ -934,32 +937,87 @@ void CCustomCopyDlg::OnImportButton() { + boost::shared_array spBuffer; + ulong_t ulSize = 0; + CFileDialog dlg(TRUE, NULL, NULL, OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT, GetResManager().LoadString(IDS_FLTALLFILTER_STRING)); - if (dlg.DoModal() == IDOK) + if(dlg.DoModal() == IDOK) { UINT uiCount=0; - CString strData; try { - CFile file(dlg.GetPathName(), CFile::modeRead); - CArchive ar(&file, CArchive::load); + icpf::file file; + file.open(dlg.GetPathName(), FA_READ); - while (ar.ReadString(strData)) + // load files max 1MB in size; + ll_t llSize = file.get_size(); + if(llSize > 1*1024*1024 || llSize < 2) { - strData.TrimLeft(_T("\" \t")); - strData.TrimRight(_T("\" \t")); - AddPath(strData); - uiCount++; + AfxMessageBox(GetResManager().LoadString(IDS_IMPORTERROR_STRING)); + return; } - ar.Close(); - file.Close(); + ulong_t ulSize = boost::numeric_cast(llSize); + spBuffer.reset(new BYTE[ulSize + 3]); // guarantee that we have null at the end of the string (3 bytes to compensate for possible odd number of bytes and for unicode) + memset(spBuffer.get(), 0, ulSize + 3); + + ulSize = file.read(spBuffer.get(), ulSize); + file.close(); } - catch(CException* e) + catch(...) { - e->Delete(); + AfxMessageBox(GetResManager().LoadString(IDS_IMPORTERROR_STRING)); + return; } + // parse text from buffer (there is no point processing files with size < 3 - stIndex.e. "c:") + if(!spBuffer || ulSize < 3) + { + AfxMessageBox(GetResManager().LoadString(IDS_IMPORTERROR_STRING)); + return; + } + + // which format? + CString strData; + if(spBuffer[0] == 0xff && spBuffer[1] == 0xfe) + { + // utf-16 (native) + strData = (wchar_t*)(spBuffer.get() + 2); + + } + else if(ulSize >= 3 && spBuffer[0] == 0xef && spBuffer[1] == 0xbb && spBuffer[2] == 0xbf) + { + boost::shared_array spWideBuffer(new wchar_t[ulSize + 1]); + memset(spWideBuffer.get(), 0, (ulSize + 1) * sizeof(wchar_t)); + + // utf-8 - needs conversion + int iRes = MultiByteToWideChar(CP_UTF8, MB_ERR_INVALID_CHARS, (char*)(spBuffer.get() + 3), ulSize - 3, spWideBuffer.get(), ulSize); + if(iRes == 0) + return; // failed to convert + + spWideBuffer[iRes] = L'\0'; + strData = spWideBuffer.get(); + } + else + { + // assuming ansi + strData = (char*)spBuffer.get(); + } + + CString strToken; + int iPos = 0; + strToken = strData.Tokenize(_T("\n"), iPos); + while(strToken != _T("")) + { + strToken.TrimLeft(_T("\" \t\r\n")); + strToken.TrimRight(_T("\" \t\r\n")); + + AddPath(strToken); + uiCount++; + + strToken = strData.Tokenize(_T("\n"), iPos); + } + // report ictranslate::CFormat fmt(GetResManager().LoadString(IDS_IMPORTREPORT_STRING)); fmt.SetParam(_t("%count"), uiCount);