* @brief Saves file from buffer to disk * * @param bTempFile : false if we are saving user files and * true if we are saving working-temp-files for diff-engine * * @return SAVE_DONE or an error code (list in MergeDoc.h) */
| 381 | * @return SAVE_DONE or an error code (list in MergeDoc.h) |
| 382 | */ |
| 383 | int CDiffTextBuffer::SaveToFile (const String& pszFileName, |
| 384 | bool bTempFile, String & sError, PackingInfo& infoUnpacker /*= nullptr*/, |
| 385 | CRLFSTYLE nCrlfStyle /*= CRLFSTYLE::AUTOMATIC*/, |
| 386 | bool bClearModifiedFlag /*= true*/, |
| 387 | int nStartLine /*= 0*/, int nLines /*= -1*/) |
| 388 | { |
| 389 | ASSERT (nCrlfStyle == CRLFSTYLE::AUTOMATIC || nCrlfStyle == CRLFSTYLE::DOS || |
| 390 | nCrlfStyle == CRLFSTYLE::UNIX || nCrlfStyle == CRLFSTYLE::MAC); |
| 391 | ASSERT (m_bInit); |
| 392 | |
| 393 | if (nLines == -1) |
| 394 | nLines = static_cast<int>(m_aLines.size() - nStartLine); |
| 395 | |
| 396 | if (pszFileName.empty()) |
| 397 | return SAVE_FAILED; // No filename, cannot save... |
| 398 | |
| 399 | if (nCrlfStyle == CRLFSTYLE::AUTOMATIC && |
| 400 | !GetOptionsMgr()->GetBool(OPT_ALLOW_MIXED_EOL)) |
| 401 | { |
| 402 | // get the default nCrlfStyle of the CDiffTextBuffer |
| 403 | nCrlfStyle = GetCRLFMode(); |
| 404 | ASSERT(nCrlfStyle != CRLFSTYLE::AUTOMATIC); |
| 405 | } |
| 406 | |
| 407 | bool bOpenSuccess = true; |
| 408 | bool bSaveSuccess = false; |
| 409 | |
| 410 | UniStdioFile file; |
| 411 | |
| 412 | String sIntermediateFilename; // used when !bTempFile |
| 413 | |
| 414 | if (bTempFile) |
| 415 | { |
| 416 | file.SetUnicoding(ucr::UTF8); |
| 417 | file.SetBom(true); |
| 418 | bOpenSuccess = !!file.OpenCreate(pszFileName); |
| 419 | } |
| 420 | else |
| 421 | { |
| 422 | file.SetUnicoding(m_encoding.m_unicoding); |
| 423 | file.SetBom(m_encoding.m_bom); |
| 424 | file.SetCodepage(m_encoding.m_codepage); |
| 425 | sIntermediateFilename = env::GetTemporaryFileName(m_strTempPath, |
| 426 | _T("MRG_"), nullptr); |
| 427 | if (sIntermediateFilename.empty()) |
| 428 | return SAVE_FAILED; //Nothing to do if even tempfile name fails |
| 429 | bOpenSuccess = !!file.OpenCreate(sIntermediateFilename); |
| 430 | } |
| 431 | |
| 432 | if (!bOpenSuccess) |
| 433 | { |
| 434 | UniFile::UniError uniErr = file.GetLastUniError(); |
| 435 | if (uniErr.HasError()) |
| 436 | { |
| 437 | sError = uniErr.GetError(); |
| 438 | if (bTempFile) |
| 439 | RootLogger::Error(strutils::format(_T("Opening file %s failed: %s"), |
| 440 | pszFileName, sError)); |
no test coverage detected