MCPcopy Create free account
hub / github.com/WinMerge/winmerge / SaveToFile

Method SaveToFile

Src/DiffTextBuffer.cpp:383–562  ·  view source on GitHub ↗

* @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) */

Source from the content-addressed store, hash-verified

381 * @return SAVE_DONE or an error code (list in MergeDoc.h)
382 */
383int 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));

Callers 3

SaveBuffForDiffFunction · 0.45
TrySaveAsMethod · 0.45
DoSaveMethod · 0.45

Calls 15

GetCRLFModeFunction · 0.85
GetTemporaryFileNameFunction · 0.85
ErrorFunction · 0.85
GetLineLengthFunction · 0.85
TFileClass · 0.85
SetBomMethod · 0.80
OpenCreateMethod · 0.80
HasErrorMethod · 0.80
SetVBufMethod · 0.80
WriteBomMethod · 0.80
HasEolMethod · 0.80
PackingMethod · 0.80

Tested by

no test coverage detected