OSDN Git Service

* Remove unused functions(TransformUcs2ToUtf8, copyToUTF8)
authorsdottaka <none@none>
Sat, 12 Jan 2013 09:54:52 +0000 (18:54 +0900)
committersdottaka <none@none>
Sat, 12 Jan 2013 09:54:52 +0000 (18:54 +0900)
* Use AnyCodepageToUTF8() instead of AnsiToUTF8(), UCS2LEToUTF8() and UCS2BEToUTF8()
* Use FileTransform_AnyCodepageToUTF8() instead of FileTransform_ToUTF8() and FileTransform_UCS2ToUTF8()

Src/Common/multiformatText.cpp
Src/Common/multiformatText.h
Src/DiffFileData.cpp
Src/DiffWrapper.cpp
Src/FileTransform.cpp
Src/FileTransform.h

index 4adedbe..e9c5766 100644 (file)
@@ -552,54 +552,6 @@ VARIANT * storageForPlugins::GetDataBufferAnsi()
        }
 }
 
-
-
-
-
-
-//////////////////////////////////////////////////////////////////////////////////
-// other conversion functions
-// TODO : put in unicoder.cpp or merge in class storageForPlugins
-
-/**
- * @brief Copy UCS-2LE string to UTF-8 string
- *
- * @param nUcs is the size in wide characters of the source string
- * @param nUtf is the size in bytes of the resulting string
- *
- * @return if nUtf = 0, return the size required for the translation buffer
- */
-static size_t TransformUcs2ToUtf8(const wchar_t *psUcs, size_t nUcs, char *pcsUtf, size_t nUtf)
-{
-       if (nUtf == 0)
-               // just tell required length
-               return ucr::Utf8len_of_string(psUcs, nUcs);
-
-       // the buffer is allocated, output in it directly
-       unsigned char * pc = (unsigned char *) pcsUtf;
-       size_t nremains = nUtf;
-
-       // quick way 
-       size_t i=0;
-       for (i = 0 ; i < nUcs && nremains > 10; ++i)
-               nremains -= ucr::to_utf8_advance(psUcs[i], pc);
-
-       // be careful for the end of the buffer, risk of overflow because
-       // of the variable length of the UTF-8 character
-       unsigned char smallTempBuffer[20];
-       size_t nremainsend = nremains;
-       unsigned char * pcTemp = (unsigned char *) smallTempBuffer;
-       for ( ; i < nUcs && nremainsend > 0; ++i)
-               nremainsend -= ucr::to_utf8_advance(psUcs[i], pcTemp);
-
-       size_t ncomplement = std::min((size_t)nremains, (size_t)(pcTemp-smallTempBuffer));
-       std::memcpy(pc, smallTempBuffer, ncomplement);
-       nremains -= ncomplement;
-
-       // return number of written bytes
-       return (nUtf - nremains);
-}
-
 /**
  * @brief Copy UTF-8 string to UCS-2LE string
  *
@@ -718,232 +670,6 @@ bool UnicodeFileToOlechar(const String& filepath, const String& filepathDst, int
        }
 }
 
-/**
- * @brief  If file is Ansi, then convert it to UTF8
- *
- * If file has Ansi (is ascii), then convert it to UTF8.
- * (No other file conversions are done here; 
- * (UCS-2LE is the Windows standard Unicode encoding)
- *
- * Returns false if file is Unicode but opening it fails.
- * Returns false if file has Unicode BOM but is not Ansi.
- * Returns true if file is not Unicode, or if converted file successfully.
- */
-bool AnsiToUTF8(const String& filepath, const String& filepathDst, int & nFileChanged, bool bWriteBOM)
-{
-       UniMemFile ufile;
-       if (!ufile.OpenReadOnly(filepath))
-               return false; // error
-       if (ufile.IsUnicode())
-       {
-               // Finished with examing file contents
-               ufile.Close();
-               return false; // no ansi file
-       }
-
-       ucr::UNICODESET unicoding = ufile.GetUnicoding(); //ucr::UCS2LE
-       // Finished with examing file contents
-       ufile.Close();
-
-       // AnsiToUTF16 only converts Ascii files to UTF-16
-       if (unicoding != ucr::NONE)
-               return false;
-
-       try
-       {
-               // Init filedataIn struct and open file as memory mapped (input)
-               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 nSizeOldBOM = 0;
-               size_t nchars = (nBufSize - nSizeOldBOM)/sizeof(char);
-
-               size_t nSizeBOM = (bWriteBOM) ? 3 : 0;
-               DWORD flags =0;
-               int nDstSize;
-               //First convert to unicode UCS16
-               nDstSize = MultiByteToWideChar(ufile.GetCodepage(), flags, (const char*)(pszBuf + nSizeOldBOM),nchars,0,0);
-               std::vector<wchar_t> szTemp(nDstSize);
-               nDstSize = MultiByteToWideChar(ufile.GetCodepage(), flags, (const char*)(pszBuf + nSizeOldBOM),nchars,&szTemp[0],nDstSize);
-
-               //now convert to unicode UTF8
-               nDstSize = WideCharToMultiByte(CP_UTF8, flags, &szTemp[0], nDstSize, NULL, 0, NULL, NULL);
-
-               // create the destination file
-               TFile fileOut(filepathDst);
-               fileOut.setSize(nDstSize + nSizeBOM);
-               SharedMemory shmOut(fileOut, SharedMemory::AM_WRITE);
-
-               // write BOM
-               if (bWriteBOM)
-                       ucr::writeBom(shmOut.begin(), ucr::UTF8);
-
-               // write data
-               WideCharToMultiByte(CP_UTF8, flags, &szTemp[0], nDstSize,
-                       (shmOut.begin()+nSizeBOM), nDstSize, NULL, NULL);
-
-               nFileChanged ++;
-               return true;
-       }
-       catch (...)
-       {
-               return false;
-       }
-}
-/**
- * @brief From UCS-2LE to 8-bit (or UTF-8)
- *
- * (No other file conversions are done here; 
- * (UCS-2LE is the Windows standard Unicode encoding)
- *
- * Returns false if file is Unicode but opening it fails.
- * Returns false if file has Unicode BOM but is not Ansi.
- * Returns true if file is not Unicode, or if converted file successfully.
- */
-bool UCS2LEToUTF8(const String& filepath, const String& filepathDst, int & nFileChanged, bool bWriteBOM)
-{
-       UniMemFile ufile;
-       if (!ufile.OpenReadOnly(filepath))
-               return false; // error
-       if (!ufile.IsUnicode())
-       {
-               // Finished with examing file contents
-               ufile.Close();
-               return false; // no ansi file
-       }
-
-       ucr::UNICODESET unicoding = ufile.GetUnicoding(); //ucr::UCS2LE
-       // Finished with examing file contents
-       ufile.Close();
-
-       // UCS-2LE to 8-bit (or UTF-8)
-       if (unicoding != ucr::UCS2LE)
-               return false;
-
-       try
-       {
-               // Init filedataIn struct and open file as memory mapped (input)
-               TFile fileIn(filepath);
-               SharedMemory shmIn(fileIn, SharedMemory::AM_READ);
-
-               wchar_t * pszBuf = (wchar_t *)shmIn.begin();
-               size_t nBufSize = shmIn.end() - shmIn.begin();
-
-               // first pass : get the size of the destination file
-               size_t nSizeOldBOM = 1;
-               size_t nchars = (nBufSize - nSizeOldBOM) / sizeof(wchar_t);
-
-               size_t nSizeBOM = (bWriteBOM) ? 3 : 0;
-               DWORD flags =0;
-               size_t nDstSize;
-
-               //now convert to unicode UTF8
-               nDstSize = WideCharToMultiByte(CP_UTF8, flags, (pszBuf + nSizeOldBOM), nchars, NULL, 0, NULL, NULL);
-
-               // create the destination file
-               TFile fileOut(filepathDst);
-               fileOut.setSize(nDstSize + nSizeBOM);
-               SharedMemory shmOut(fileOut, SharedMemory::AM_WRITE);
-
-               // write BOM
-               if (bWriteBOM)
-                       ucr::writeBom(shmOut.begin(), ucr::UTF8);
-
-               // write data 
-               WideCharToMultiByte(CP_UTF8, flags, (pszBuf + nSizeOldBOM), nchars,
-                       (shmOut.begin()+nSizeBOM), nDstSize, NULL, NULL);
-
-               nFileChanged ++;
-               return true;
-       }
-       catch (...)
-       {
-               return false;
-       }
-}
-/**
- * @brief  From UCS-2BE to 8-bit (or UTF-8)
- *
- * (No other file conversions are done here; 
- * (UCS-2LE is the Windows standard Unicode encoding)
- *
- * Returns false if file is Unicode but opening it fails.
- * Returns false if file has Unicode BOM but is not Ansi.
- * Returns true if file is not Unicode, or if converted file successfully.
- */
-bool UCS2BEToUTF8(const String& filepath, const String& filepathDst, int & nFileChanged, bool bWriteBOM)
-{
-       UniMemFile ufile;
-       if (!ufile.OpenReadOnly(filepath))
-               return false; // error
-       if (!ufile.IsUnicode())
-       {
-               // Finished with examing file contents
-               ufile.Close();
-               return false; // no ansi file
-       }
-
-       ucr::UNICODESET unicoding = ufile.GetUnicoding(); //ucr::UCS2LE
-       // Finished with examing file contents
-       ufile.Close();
-
-       // UCS-2BE to 8-bit (or UTF-8)
-       if (unicoding != ucr::UCS2BE)
-               return false;
-
-       try
-       {
-               // Init filedataIn struct and open file as memory mapped (input)
-               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 nSizeOldBOM = 2;
-               size_t nchars = (nBufSize - nSizeOldBOM) / sizeof(wchar_t);
-
-               size_t nSizeBOM = (bWriteBOM) ? 3 : 0;
-               DWORD flags =0;
-               std::vector<char> szTemp(nBufSize);
-
-               // simple byte swap BE->LE
-               for (size_t i = 0; i < nBufSize; i += 2)
-               {
-                       // Byte-swap into destination
-                       szTemp[i] = pszBuf[i + 1];
-                       szTemp[i + 1] = pszBuf[i];
-               }
-
-               //now convert to unicode UTF8
-               size_t nDstSize = WideCharToMultiByte(CP_UTF8, flags,(wchar_t *)(&szTemp[0] + nSizeOldBOM), nchars, NULL, 0, NULL, NULL);
-
-               // create the destination file
-               TFile fileOut(filepathDst);
-               fileOut.setSize(nDstSize + nSizeBOM);
-               SharedMemory shmOut(fileOut, SharedMemory::AM_WRITE);
-
-               // write BOM
-               if (bWriteBOM)
-                       ucr::writeBom(shmOut.begin(), ucr::UTF8);
-
-               // write data 
-               WideCharToMultiByte(CP_UTF8, flags,(wchar_t *)(&szTemp[0] + nSizeOldBOM), nchars,
-                       (shmOut.begin()+nSizeBOM), nDstSize, NULL, NULL);
-
-               nFileChanged++;
-               return true;
-       }
-       catch (...)
-       {
-               return false;
-       }
-}
 
 bool AnyCodepageToUTF8(int codepage, const String& filepath, const String& filepathDst, int & nFileChanged, bool bWriteBOM)
 {
index a4d1628..ae3dab3 100644 (file)
@@ -137,13 +137,6 @@ private:
 /// 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)
-bool UCS2LEToUTF8(const String& filepath, const String& filepathDst, int & nFileChanged, bool bWriteBOM);
 bool AnyCodepageToUTF8(int codepage, const String& filepath, const String& filepathDst, int & nFileChanged, bool bWriteBOM);
 
-/// Convert UCS-2BE file to UTF-8 (for diffutils)
-bool UCS2BEToUTF8(const String& filepath, const String& filepathDst, int & nFileChanged, bool bWriteBOM);
-
-/// Convert Ansi file to UTF-8 (for diffutils)
-bool AnsiToUTF8(const String& filepath, const String& filepathDst, int & nFileChanged, bool bWriteBOM);
-
 #endif //__MULTIFORMATTEXT_H__
\ No newline at end of file
index 3b96ad2..230dc4c 100644 (file)
@@ -221,21 +221,13 @@ bool DiffFileData::Filepath_Transform(bool bForceUTF8,
                        return false;
        }
 
-       if (encoding.m_unicoding)
+       if (encoding.m_unicoding || bForceUTF8)
        {
                // fourth step : prepare for diffing
                // may overwrite if we've already copied to temp file
                bool bMayOverwrite = 0 != string_compare_nocase(filepathTransformed, filepath);
-               if (!FileTransform_ToUTF8(filepathTransformed, bMayOverwrite))
-                       return false;
-       }
-       else if (bForceUTF8)
-       {
-               // fourth step : prepare for diffing
-               // may overwrite if we've already copied to temp file
-               bool bMayOverwrite = (0 != string_compare_nocase(filepathTransformed, filepath));
                if (!FileTransform_AnyCodepageToUTF8(encoding.m_codepage, filepathTransformed, bMayOverwrite))
-                       return false;           
+                       return false;
        }
        return true;
 }
index dddbd52..e15dbf3 100644 (file)
@@ -749,7 +749,7 @@ bool CDiffWrapper::RunFileDiff()
                        }
                }
 
-               FileTransform_UCS2ToUTF8(strFileTemp[file], m_bPathsAreTemp);
+               FileTransform_AnyCodepageToUTF8(CP_UCS2LE, strFileTemp[file], m_bPathsAreTemp);
                // We use the same plugin for both files, so it must be defined before
                // second file
                assert(m_infoPrediffer->bToBeScanned == false);
index 92a8dd9..87e9884 100644 (file)
@@ -465,54 +465,6 @@ bool FileTransform_NormalizeUnicode(String & filepath, bool bMayOverwrite, ucr::
 
 ////////////////////////////////////////////////////////////////////////////////
 
-// for OLECHAR files, transform to UTF8 for diffutils
-// TODO : convert Ansi to UTF8 if other file is unicode or uses a different codepage
-bool FileTransform_UCS2ToUTF8(String & filepath, bool bMayOverwrite)
-{
-       String tempDir = env_GetTempPath();
-       if (tempDir.empty())
-               return false;
-       String tempFilepath = env_GetTempFileName(tempDir, _T("_W2"));
-       if (tempFilepath.empty())
-               return false;
-
-       // TODO : is it better with the BOM or without (just change the last argument)
-       int nFileChanged = 0;
-       bool bSuccess = UCS2LEToUTF8(filepath, tempFilepath, nFileChanged, false); 
-       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();
@@ -617,167 +569,5 @@ bool TextTransform_Interactive(String & text, const wchar_t *TransformationEvent
 
        return (nChanged != 0);
 }
-////////////////////////////////////////////////////////////////////////////////
-
-// for OLECHAR files, transform to UTF8 for diffutils
-// convert all Ansi or unicode-files to UTF8 
-// if other file is unicode or uses a different codepage
-bool FileTransform_ToUTF8(String & filepath, bool bMayOverwrite)
-{
-       bool bSuccess = false;
-       String tempDir = env_GetTempPath();
-       if (tempDir.empty())
-               return false;
-       String tempFilepath = env_GetTempFileName(tempDir, _T("_WM"));
-       if (tempFilepath.empty())
-               return false;
-
-       int nFileChanged = 0;
-
-       UniMemFile ufile;
-       if (!ufile.OpenReadOnly(filepath))
-               return false; // error
-       ufile.IsUnicode();
-       ucr::UNICODESET unicoding = ufile.GetUnicoding();
-       // Finished with examing file contents
-       ufile.Close();
-
-       // TODO : is it better with the BOM or without (just change the last argument)
-
-       switch (unicoding)
-       {
-       // AnsiToUTF16 only converts Ascii files to UTF-16
-       case ( ucr::NONE):
-               bSuccess = AnsiToUTF8(filepath, tempFilepath, nFileChanged, false); 
-               break;
-       case (ucr::UCS2LE):
-               bSuccess = UCS2LEToUTF8(filepath, tempFilepath, nFileChanged, false);
-               break;
-       case (ucr::UCS2BE):
-               bSuccess = UCS2BEToUTF8(filepath, tempFilepath, nFileChanged, false);
-               break;
-       case (ucr::UCS4LE):
-               bSuccess = false;
-               break;
-       case (ucr::UCS4BE):
-               bSuccess = false;
-               break;
-               
-       }
-
-       if (!bSuccess)
-               return false;
-
-       if (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 true;
-}
-
-/**
- * @brief check for both are same (Ansi or UTF8)
- * otherwise convert both to UTF8
- * @param [in,out] filepath Most plugins change this filename
- */
-bool Transform2FilesToUTF8(String &strFile1Temp, String &strFile2Temp,bool m_bPathsAreTemp)
-{
-       //check first file for unicodeing
-       UniMemFile ufile;
-       if (!ufile.OpenReadOnly(strFile1Temp))
-               return false; // error
-       ufile.IsUnicode();
-       ucr::UNICODESET unicoding1 = ufile.GetUnicoding();
-       // Finished with examing file contents
-       ufile.Close();
-
-       //check second file for unicodeing
-       if (!ufile.OpenReadOnly(strFile2Temp))
-               return false; // error
-       ufile.IsUnicode();
-
-       ucr::UNICODESET unicoding2 = ufile.GetUnicoding();
-       ufile.Close();
-
-       if ((unicoding1==ucr::NONE)&&(unicoding2==ucr::NONE))
-               return true; //OK do nothing both are ansi
-       if ((unicoding1==ucr::UTF8)&&(unicoding2==ucr::UTF8))
-               return true; //OK do nothing both are UTF16
-       // now both are different, convert to UTF8
-       if (unicoding1!=ucr::UTF8)
-               FileTransform_ToUTF8(strFile1Temp, m_bPathsAreTemp);
-       if (unicoding2!=ucr::UTF8)
-               FileTransform_ToUTF8(strFile2Temp, m_bPathsAreTemp);
-       return true;
-       }
 
 ////////////////////////////////////////////////////////////////////////////////
-
-// for OLECHAR files, transform to UTF8 for diffutils
-// TODO : convert Ansi to UTF8 if other file is unicode or uses a different codepage
-bool copyToUTF8(String & filepath, String &tempFilepath ,bool bMayOverwrite)
-{
-       bool bSuccess = false;
-
-       UniMemFile ufile;
-       if (!ufile.OpenReadOnly(filepath))
-               return false; // error
-       ufile.IsUnicode();
-       ucr::UNICODESET unicoding = ufile.GetUnicoding();
-       // Finished with examing file contents
-       ufile.Close();
-
-       int nFileChanged = 0;
-
-       // TODO : is it better with the BOM or without (just change the last argument)
-
-       switch (unicoding)
-       {
-       // AnsiToUTF16 only converts Ascii files to UTF-16
-       case ( ucr::NONE):
-               bSuccess = AnsiToUTF8(filepath, tempFilepath, nFileChanged, false); 
-               break;
-       case (ucr::UCS2LE):
-               bSuccess = UCS2LEToUTF8(filepath, tempFilepath, nFileChanged, false);
-               break;
-       case (ucr::UCS2BE):
-               bSuccess = UCS2BEToUTF8(filepath, tempFilepath, nFileChanged, false);
-               break;
-       case (ucr::UCS4LE):
-               bSuccess = false;
-               break;
-       case (ucr::UCS4BE):
-               bSuccess = false;
-               break;
-               
-       }
-
-       if (!bSuccess)
-               return false;
-
-       return true;
-}
index c11ffed..36fd9df 100644 (file)
@@ -209,21 +209,12 @@ bool FileTransform_Prediffing(String & filepath, const String& filteredText, Pre
  * @param filepath : [in, out] Most plugins change this filename
  */
 bool FileTransform_Prediffing(String & filepath, PrediffingInfo handler, bool bMayOverwrite);
-/**
- * @brief check for both are same (Ansi or UTF8)
- * otherwise convert both to UTF8
- * @param [in,out] filepath Most plugins change this filename
- */
-bool Transform2FilesToUTF8(String &strFile1Temp, String &strFile2Temp,bool m_bPathsAreTemp);
-/**
- * @brief Copy a Ansi or UCS2LE to UTF8)
- */
-bool copyToUTF8(String & filepath, String &tempFilepath ,bool bMayOverwrite);
 
 
 /**
  * @brief Transform all files to UTF8 aslong possible
  *
+ * @param codepage : [in] codepage of source file
  * @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
  *
@@ -231,10 +222,6 @@ bool copyToUTF8(String & filepath, String &tempFilepath ,bool bMayOverwrite);
  * convert all Ansi or unicode-files to UTF8 
  * if other file is unicode or uses a different codepage
  */
-bool FileTransform_ToUTF8(String & filepath, bool bMayOverwrite);
-bool FileTransform_UCS2ToUTF8(String & filepath, bool bMayOverwrite);
-
-
 bool FileTransform_AnyCodepageToUTF8(int codepage, String & filepath, bool bMayOverwrite);