From 6d10243040025078a766c9eccbb48cd6ca0a2486 Mon Sep 17 00:00:00 2001 From: sdottaka Date: Sat, 12 Jan 2013 22:53:09 +0900 Subject: [PATCH] Remove FileTransform_NormalizeUnicode(), UnicodeFileToOlechar() --- Src/Common/multiformatText.cpp | 87 ------------------------------------------ Src/Common/multiformatText.h | 4 +- Src/DiffFileData.cpp | 12 ------ Src/FileTransform.cpp | 45 ---------------------- Src/FileTransform.h | 12 ------ 5 files changed, 1 insertion(+), 159 deletions(-) diff --git a/Src/Common/multiformatText.cpp b/Src/Common/multiformatText.cpp index e9c576604..059d72f1f 100644 --- a/Src/Common/multiformatText.cpp +++ b/Src/Common/multiformatText.cpp @@ -584,93 +584,6 @@ static size_t TransformUtf8ToUcs2(const char * pcsUtf, size_t nUtf, wchar_t * ps return (nUcs - nremains); } - -bool UnicodeFileToOlechar(const String& filepath, const String& filepathDst, int & nFileChanged, ucr::UNICODESET unicoding) -{ - UniMemFile ufile; - if (!ufile.OpenReadOnly(filepath) || !ufile.IsUnicode()) - { - if (unicoding == 0) - return true; // not unicode file, nothing to do - } - - if (unicoding) - ufile.SetUnicoding(unicoding); - ucr::UNICODESET codeOldBOM = ufile.GetUnicoding(); - size_t nSizeOldBOM = static_cast(ufile.GetPosition()); - if (nSizeOldBOM > 0 && codeOldBOM == ucr::UCS2LE) - return true; // unicode UCS-2LE, nothing to do - bool bBom = ufile.HasBom(); - // Finished with examing file contents - ufile.Close(); - - // Init filedataIn struct and open file as memory mapped (input) - try - { - TFile fileIn(filepath); - SharedMemory shmIn(fileIn, SharedMemory::AM_READ); - - char * pszBuf = shmIn.begin(); - size_t nBufSize = shmIn.end() - shmIn.begin(); - - // first pass : get the size of the destination file - size_t nchars = 0; - switch (codeOldBOM) - { - case ucr::UTF8: - nSizeOldBOM = bBom ? 3 : 0; - nchars = TransformUtf8ToUcs2(pszBuf + nSizeOldBOM, nBufSize - nSizeOldBOM, NULL, 0); - break; - case ucr::UCS2BE: - case ucr::UCS2LE: - // same number of characters - nchars = (nBufSize - nSizeOldBOM)/sizeof(wchar_t); - } - - size_t nSizeBOM = 2; - size_t nDstSize = nchars * sizeof(wchar_t); // data size in bytes - - // create the destination file - TFile fileOut(filepathDst); - fileOut.setSize(nDstSize + nSizeBOM); - SharedMemory shmOut(fileOut, SharedMemory::AM_WRITE); - - // write BOM - ucr::writeBom(shmOut.begin(), ucr::UCS2LE); - - // write data - wchar_t * pszWideDst = (wchar_t *) ((char *)shmOut.begin()+nSizeBOM); - switch (codeOldBOM) - { - case ucr::UTF8: - TransformUtf8ToUcs2( pszBuf + nSizeOldBOM, nBufSize - nSizeOldBOM, pszWideDst, nchars); - break; - case ucr::UCS2LE: - memcpy(pszWideDst, pszBuf + nSizeOldBOM, nchars * sizeof(wchar_t)); - break; - case ucr::UCS2BE: - wchar_t * pszWideBuf = (wchar_t *) (pszBuf + nSizeOldBOM); - // swap all characters - size_t i; - for (i = 0 ; i < nchars ; i++) - { - wchar_t wc = pszWideBuf[i]; - wc = ((wc & 0xFF) << 8) + (wc >> 8); - pszWideDst[i] = wc; - } - break; - } - - nFileChanged ++; - return true; - } - catch (...) - { - return false; - } -} - - bool AnyCodepageToUTF8(int codepage, const String& filepath, const String& filepathDst, int & nFileChanged, bool bWriteBOM) { UniMemFile ufile; diff --git a/Src/Common/multiformatText.h b/Src/Common/multiformatText.h index ae3dab3b2..6566a612a 100644 --- a/Src/Common/multiformatText.h +++ b/Src/Common/multiformatText.h @@ -134,9 +134,7 @@ private: // other conversion functions -/// Convert any unicode file to UCS-2LE -bool UnicodeFileToOlechar(const String& filepath, const String& filepathDst, int & nFileChanged, ucr::UNICODESET unicoding = ucr::NONE); -/// Convert UCS-2LE file to UTF-8 (for diffutils) +/// Convert file to UTF-8 (for diffutils) bool AnyCodepageToUTF8(int codepage, const String& filepath, const String& filepathDst, int & nFileChanged, bool bWriteBOM); #endif //__MULTIFORMATTEXT_H__ \ No newline at end of file diff --git a/Src/DiffFileData.cpp b/Src/DiffFileData.cpp index 230dc4c6c..7ad820577 100644 --- a/Src/DiffFileData.cpp +++ b/Src/DiffFileData.cpp @@ -189,18 +189,6 @@ bool DiffFileData::Filepath_Transform(bool bForceUTF8, { bool bMayOverwrite = false; // temp variable set each time it is used - if (encoding.m_unicoding && (!encoding.m_bom || encoding.m_unicoding != ucr::UCS2LE)) - { - // second step : normalize Unicode to OLECHAR (most of time, do nothing) - // (OLECHAR = UCS-2LE in Windows) - bMayOverwrite = (filepathTransformed != filepath); // may overwrite if we've already copied to temp file - if (!FileTransform_NormalizeUnicode(filepathTransformed, bMayOverwrite, encoding.m_unicoding)) - return false; - } - - // Note: filepathTransformed may be in UCS-2 (if toUtf8), or it may be raw encoding (if !Utf8) - // prediff plugins must handle both - // third step : prediff (plugins) bMayOverwrite = (filepathTransformed != filepath); // may overwrite if we've already copied to temp file if (infoPrediffer->bToBeScanned) diff --git a/Src/FileTransform.cpp b/Src/FileTransform.cpp index 87e988479..e933c4af4 100644 --- a/Src/FileTransform.cpp +++ b/Src/FileTransform.cpp @@ -420,51 +420,6 @@ bool FileTransform_Prediffing(String & filepath, const String& filteredText, Pre //////////////////////////////////////////////////////////////////////////////// -bool FileTransform_NormalizeUnicode(String & filepath, bool bMayOverwrite, ucr::UNICODESET unicoding) -{ - String tempDir = env_GetTempPath(); - if (tempDir.empty()) - return false; - String tempFilepath = env_GetTempFileName(tempDir, _T("_W1")); - if (tempFilepath.empty()) - return false; - - int nFileChanged = 0; - bool bSuccess = UnicodeFileToOlechar(filepath, tempFilepath, nFileChanged, unicoding); - if (bSuccess && nFileChanged) - { - // we do not overwrite so we delete the old file - if (bMayOverwrite) - { - try - { - TFile(filepath).remove(); - } - catch (Exception& e) - { - LogErrorStringUTF8(e.displayText()); - } - } - // and change the filepath if everything works - filepath = tempFilepath; - } - else - { - try - { - TFile(tempFilepath).remove(); - } - catch (Exception& e) - { - LogErrorStringUTF8(e.displayText()); - } - } - - return bSuccess; -} - -//////////////////////////////////////////////////////////////////////////////// - bool FileTransform_AnyCodepageToUTF8(int codepage, String & filepath, bool bMayOverwrite) { String tempDir = env_GetTempPath(); diff --git a/Src/FileTransform.h b/Src/FileTransform.h index 36fd9dfa2..2b53e49de 100644 --- a/Src/FileTransform.h +++ b/Src/FileTransform.h @@ -180,18 +180,6 @@ bool FileTransform_Unpacking(String & filepath, const PackingInfo * handler, int bool FileTransform_Packing(String & filepath, PackingInfo handler); /** - * @brief Normalize Unicode files to OLECHAR - * - * @param filepath : [in,out] path of file to be prepared. This filename is updated if bMayOverwrite is false - * @param bMayOverwrite : [in] True only if the filepath points out a temp file - * - * @return Tells if we succeed - * - * @note Ansi files are not changed - */ -bool FileTransform_NormalizeUnicode(String & filepath, bool bMayOverwrite, ucr::UNICODESET unicoding = ucr::NONE); - -/** * @brief Prepare one file for diffing, scan all available plugins (events+filename filtering) * * @param filepath : [in, out] Most plugins change this filename -- 2.11.0