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<size_t>(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;
// 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
{
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)
////////////////////////////////////////////////////////////////////////////////
-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();
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