2 * @file DiffTextBuffer.cpp
4 * @brief Implementation file for CDiffTextBuffer
9 #include "DiffTextBuffer.h"
10 #include <Poco/Exception.h>
15 #include "OptionsDef.h"
16 #include "OptionsMgr.h"
17 #include "Environment.h"
18 #include "MergeLineFlags.h"
20 #include "FileTransform.h"
21 #include "FileTextEncoding.h"
22 #include "codepage_detect.h"
25 using Poco::Exception;
31 static bool IsTextFileStylePure(const UniMemFile::txtstats & stats);
32 static CRLFSTYLE GetTextFileStyle(const UniMemFile::txtstats & stats);
35 * @brief Check if file has only one EOL type.
36 * @param [in] stats File's text stats.
37 * @return true if only one EOL type is found, false otherwise.
39 static bool IsTextFileStylePure(const UniMemFile::txtstats & stats)
52 * @brief Get file's EOL type.
53 * @param [in] stats File's text stats.
56 static CRLFSTYLE GetTextFileStyle(const UniMemFile::txtstats & stats)
58 // Check if file has more than one EOL type.
59 if (!IsTextFileStylePure(stats))
60 return CRLFSTYLE::MIXED;
61 else if (stats.ncrlfs >= stats.nlfs)
63 if (stats.ncrlfs >= stats.ncrs)
64 return CRLFSTYLE::DOS;
66 return CRLFSTYLE::MAC;
70 if (stats.nlfs >= stats.ncrs)
71 return CRLFSTYLE::UNIX;
73 return CRLFSTYLE::MAC;
79 * @param [in] pDoc Owning CMergeDoc.
80 * @param [in] pane Pane number this buffer is associated with.
82 CDiffTextBuffer::CDiffTextBuffer(CMergeDoc * pDoc, int pane)
90 * @brief Get a line from the buffer.
91 * @param [in] nLineIndex Index of the line to get.
92 * @param [out] strLine Returns line text in the index.
94 bool CDiffTextBuffer::GetLine(int nLineIndex, CString &strLine) const
96 int nLineLength = CCrystalTextBuffer::GetLineLength(nLineIndex);
99 else if (nLineLength == 0)
103 _tcsncpy_s(strLine.GetBuffer(nLineLength + 1), nLineLength + 1,
104 CCrystalTextBuffer::GetLineChars(nLineIndex), nLineLength);
105 strLine.ReleaseBuffer(nLineLength);
111 * @brief Set the buffer modified status.
112 * @param [in] bModified New modified status, true if buffer has been
113 * modified since last saving.
115 void CDiffTextBuffer:: /* virtual override */
116 SetModified(bool bModified /*= true*/)
118 CCrystalTextBuffer::SetModified (bModified);
119 m_pOwnerDoc->SetModifiedFlag (bModified);
123 * @brief Get a line (with EOL bytes) from the buffer.
124 * This function is like GetLine() but it also includes line's EOL to the
126 * @param [in] nLineIndex Index of the line to get.
127 * @param [out] strLine Returns line text in the index. Existing content
128 * of this string is overwritten.
130 bool CDiffTextBuffer::GetFullLine(int nLineIndex, CString &strLine) const
132 int cchText = GetFullLineLength(nLineIndex);
138 LPTSTR pchText = strLine.GetBufferSetLength(cchText);
139 memcpy(pchText, GetLineChars(nLineIndex), cchText * sizeof(TCHAR));
143 void CDiffTextBuffer:: /* virtual override */
144 AddUndoRecord(bool bInsert, const CPoint & ptStartPos,
145 const CPoint & ptEndPos, LPCTSTR pszText, size_t cchText,
146 int nActionType /*= CE_ACTION_UNKNOWN*/,
147 CDWordArray *paSavedRevisionNumbers /*= nullptr*/)
149 CGhostTextBuffer::AddUndoRecord(bInsert, ptStartPos, ptEndPos, pszText,
150 cchText, nActionType, paSavedRevisionNumbers);
151 if (m_aUndoBuf[m_nUndoPosition - 1].m_dwFlags & UNDO_BEGINGROUP)
153 m_pOwnerDoc->undoTgt.erase(m_pOwnerDoc->curUndo, m_pOwnerDoc->undoTgt.end());
154 m_pOwnerDoc->undoTgt.push_back(m_nThisPane);
155 m_pOwnerDoc->curUndo = m_pOwnerDoc->undoTgt.end();
160 * @brief Checks if a flag is set for line.
161 * @param [in] line Index (0-based) for line.
162 * @param [in] flag Flag to check.
163 * @return true if flag is set, false otherwise.
165 bool CDiffTextBuffer::FlagIsSet(UINT line, DWORD flag) const
167 return ((m_aLines[line].m_dwFlags & flag) == flag);
171 Remove blank lines and clear winmerge flags
172 (2003-06-21, Perry: I don't understand why this is necessary, but if this isn't
173 done, more and more gray lines appear in the file)
174 (2003-07-31, Laoran I don't understand either why it is necessary, but it works
175 fine, so let's go on with it)
177 void CDiffTextBuffer::prepareForRescan()
179 RemoveAllGhostLines();
180 for (int ct = GetLineCount() - 1; ct >= 0; --ct)
183 LF_INVISIBLE | LF_DIFF | LF_TRIVIAL | LF_MOVED | LF_SNP,
184 false, false, false);
189 * @brief Called when line has been edited.
190 * After editing a line, we don't know if there is a diff or not.
191 * So we clear the LF_DIFF flag (and it is more easy to read during editing).
192 * Rescan will set the proper color.
193 * @param [in] nLine Line that has been edited.
196 void CDiffTextBuffer:: /* virtual override */
197 OnNotifyLineHasBeenEdited(int nLine)
199 SetLineFlag(nLine, LF_DIFF, false, false, false);
200 SetLineFlag(nLine, LF_TRIVIAL, false, false, false);
201 SetLineFlag(nLine, LF_MOVED, false, false, false);
202 SetLineFlag(nLine, LF_SNP, false, false, false);
203 CGhostTextBuffer::OnNotifyLineHasBeenEdited(nLine);
207 * @brief Load file from disk into buffer
209 * @param [in] pszFileNameInit File to load
210 * @param [in] infoUnpacker Unpacker plugin
211 * @param [in] sToFindUnpacker String for finding unpacker plugin
212 * @param [out] readOnly Loading was lossy so file should be read-only
213 * @param [in] nCrlfStyle EOL style used
214 * @param [in] encoding Encoding used
215 * @param [out] sError Error message returned
216 * @return FRESULT_OK when loading succeed or (list in files.h):
217 * - FRESULT_OK_IMPURE : load OK, but the EOL are of different types
218 * - FRESULT_ERROR_UNPACK : plugin failed to unpack
219 * - FRESULT_ERROR : loading failed, sError contains error message
220 * - FRESULT_BINARY : file is binary file
221 * @note If this method fails, it calls InitNew so the CDiffTextBuffer is in a valid state
223 int CDiffTextBuffer::LoadFromFile(LPCTSTR pszFileNameInit,
224 PackingInfo& infoUnpacker, LPCTSTR sToFindUnpacker, bool & readOnly,
225 CRLFSTYLE nCrlfStyle, const FileTextEncoding & encoding, CString &sError)
228 ASSERT(m_aLines.size() == 0);
230 // Unpacking the file here, save the result in a temporary file
231 m_strTempFileName = pszFileNameInit;
232 if (!infoUnpacker.Unpacking(&m_unpackerSubcodes, m_strTempFileName, sToFindUnpacker, { m_strTempFileName }))
234 InitNew(); // leave crystal editor in valid, empty state
235 return FileLoadResult::FRESULT_ERROR_UNPACK;
238 // we will load the transformed file
239 LPCTSTR pszFileName = m_strTempFileName.c_str();
242 DWORD nRetVal = FileLoadResult::FRESULT_OK;
244 // Set encoding based on extension, if we know one
245 paths::SplitFilename(pszFileName, nullptr, nullptr, &sExt);
246 CrystalLineParser::TextDefinition *def =
247 CrystalLineParser::GetTextType(sExt.c_str());
248 if (def && def->encoding != -1)
249 m_nSourceEncoding = def->encoding;
251 UniFile *pufile = new UniMemFile;
253 // Now we only use the UniFile interface
254 // which is something we could implement for HTTP and/or FTP files
256 if (!pufile->OpenReadOnly(pszFileName))
258 nRetVal = FileLoadResult::FRESULT_ERROR;
259 UniFile::UniError uniErr = pufile->GetLastUniError();
260 if (uniErr.HasError())
262 sError = uniErr.GetError().c_str();
264 InitNew(); // leave crystal editor in valid, empty state
268 if (!infoUnpacker.GetPluginPipeline().empty())
270 // re-detect codepage
271 int iGuessEncodingType = GetOptionsMgr()->GetInt(OPT_CP_DETECT);
272 FileTextEncoding encoding2 = codepage_detect::Guess(pszFileName, iGuessEncodingType);
273 pufile->SetUnicoding(encoding2.m_unicoding);
274 pufile->SetCodepage(encoding2.m_codepage);
275 pufile->SetBom(encoding2.m_bom);
281 // If the file is not unicode file, use the codepage we were given to
282 // interpret the 8-bit characters. If the file is unicode file,
283 // determine its type (IsUnicode() does that).
284 if (encoding.m_unicoding == ucr::NONE || !pufile->IsUnicode())
285 pufile->SetCodepage(encoding.m_codepage);
291 COleDateTime start = COleDateTime::GetCurrentTime(); // for trace messages
293 // Manually grow line array exponentially
294 UINT arraysize = 500;
295 m_aLines.resize(arraysize);
297 // preveol must be initialized for empty files
302 done = !pufile->ReadString(sline, eol, &lossy);
304 // if last line had no eol, we can quit
305 if (done && preveol.empty())
307 // but if last line had eol, we add an extra (empty) line to buffer
310 if (lineno == arraysize)
312 // For smaller sizes use exponential growth, but for larger
313 // sizes grow by constant ratio. Unlimited exponential growth
314 // easily runs out of memory.
315 if (arraysize < 100 * 1024)
318 arraysize += 100 * 1024;
319 m_aLines.resize(arraysize);
322 sline += eol; // TODO: opportunity for optimization, as CString append is terrible
325 // TODO: Should record lossy status of line
327 AppendLine(lineno, sline.c_str(), static_cast<int>(sline.length()));
333 // fix array size (due to our manual exponential growth
334 m_aLines.resize(lineno);
337 //Try to determine current CRLF mode (most frequent)
338 if (nCrlfStyle == CRLFSTYLE::AUTOMATIC)
340 nCrlfStyle = GetTextFileStyle(pufile->GetTxtStats());
342 ASSERT (nCrlfStyle != CRLFSTYLE::AUTOMATIC);
343 SetCRLFMode(nCrlfStyle);
345 // At least one empty line must present
346 // (view does not work for empty buffers)
347 ASSERT(m_aLines.size() > 0);
351 m_bUndoGroup = m_bUndoBeginGroup = false;
352 m_nSyncPosition = m_nUndoPosition = 0;
353 ASSERT(m_aUndoBuf.size() == 0);
354 m_ptLastChange.x = m_ptLastChange.y = -1;
357 // flags don't need initialization because 0 is the default value
359 // Set the return value : OK + info if the file is impure
360 // A pure file is a file where EOL are consistent (all DOS, or all UNIX, or all MAC)
361 // An impure file is a file with several EOL types
362 // WinMerge may display impure files, but the default option is to unify the EOL
363 // We return this info to the caller, so it may display a confirmation box
364 if (IsTextFileStylePure(pufile->GetTxtStats()))
365 nRetVal = FileLoadResult::FRESULT_OK;
367 nRetVal = FileLoadResult::FRESULT_OK_IMPURE;
369 // stash original encoding away
370 m_encoding.m_unicoding = pufile->GetUnicoding();
371 m_encoding.m_bom = pufile->HasBom();
372 m_encoding.m_codepage = pufile->GetCodepage();
374 if (pufile->GetTxtStats().nlosses)
376 FileLoadResult::AddModifier(nRetVal, FileLoadResult::FRESULT_LOSSY);
381 // close the file now to free the handle
385 // delete the file that unpacking may have created
386 if (_tcscmp(pszFileNameInit, pszFileName) != 0)
390 TFile(pszFileName).remove();
394 LogErrorStringUTF8(e.displayText());
401 * @brief Saves file from buffer to disk
403 * @param bTempFile : false if we are saving user files and
404 * true if we are saving working-temp-files for diff-engine
406 * @return SAVE_DONE or an error code (list in MergeDoc.h)
408 int CDiffTextBuffer::SaveToFile (const String& pszFileName,
409 bool bTempFile, String & sError, PackingInfo& infoUnpacker /*= nullptr*/,
410 CRLFSTYLE nCrlfStyle /*= CRLFSTYLE::AUTOMATIC*/,
411 bool bClearModifiedFlag /*= true*/,
412 int nStartLine /*= 0*/, int nLines /*= -1*/)
414 ASSERT (nCrlfStyle == CRLFSTYLE::AUTOMATIC || nCrlfStyle == CRLFSTYLE::DOS ||
415 nCrlfStyle == CRLFSTYLE::UNIX || nCrlfStyle == CRLFSTYLE::MAC);
419 nLines = static_cast<int>(m_aLines.size() - nStartLine);
421 if (pszFileName.empty())
422 return SAVE_FAILED; // No filename, cannot save...
424 if (nCrlfStyle == CRLFSTYLE::AUTOMATIC &&
425 !GetOptionsMgr()->GetBool(OPT_ALLOW_MIXED_EOL))
427 // get the default nCrlfStyle of the CDiffTextBuffer
428 nCrlfStyle = GetCRLFMode();
429 ASSERT(nCrlfStyle != CRLFSTYLE::AUTOMATIC);
432 bool bOpenSuccess = true;
433 bool bSaveSuccess = false;
437 String sIntermediateFilename; // used when !bTempFile
441 file.SetUnicoding(ucr::UTF8);
442 file.SetBom(GetOptionsMgr()->GetInt(OPT_CMP_DIFF_ALGORITHM) == 0);
443 bOpenSuccess = !!file.OpenCreate(pszFileName);
447 file.SetUnicoding(m_encoding.m_unicoding);
448 file.SetBom(m_encoding.m_bom);
449 file.SetCodepage(m_encoding.m_codepage);
450 sIntermediateFilename = env::GetTemporaryFileName(m_strTempPath,
451 _T("MRG_"), nullptr);
452 if (sIntermediateFilename.empty())
453 return SAVE_FAILED; //Nothing to do if even tempfile name fails
454 bOpenSuccess = !!file.OpenCreate(sIntermediateFilename);
459 UniFile::UniError uniErr = file.GetLastUniError();
460 if (uniErr.HasError())
462 sError = uniErr.GetError();
464 LogErrorString(strutils::format(_T("Opening file %s failed: %s"),
465 pszFileName, sError));
467 LogErrorString(strutils::format(_T("Opening file %s failed: %s"),
468 sIntermediateFilename, sError));
475 // line loop : get each real line and write it in the file
477 String sEol = GetStringEol(nCrlfStyle);
478 int lastRealLine = ApparentLastRealLine();
479 for (int line = nStartLine; line < nStartLine + nLines; ++line)
481 if (GetLineFlags(line) & LF_GHOST)
484 // get the characters of the line (excluding EOL)
485 if (GetLineLength(line) > 0)
487 int nLineLength = GetLineLength(line);
489 sLine.reserve(nLineLength + 4);
490 sLine.append(GetLineChars(line), nLineLength);
495 if (bTempFile && m_bTableEditing && m_bAllowNewlinesInQuotes)
497 strutils::replace(sLine, _T("\x1b"), _T("\x1b\x1b"));
498 strutils::replace(sLine, _T("\r"), _T("\x1br"));
499 strutils::replace(sLine, _T("\n"), _T("\x1bn"));
503 if (line == lastRealLine || lastRealLine == -1 )
505 // If original last line had no EOL, then we are done
506 if( !m_aLines[line].HasEol() )
508 file.WriteString(sLine);
511 // Otherwise, add the appropriate EOL to the last line ...
514 // normal line : append an EOL
515 if (nCrlfStyle == CRLFSTYLE::AUTOMATIC || nCrlfStyle == CRLFSTYLE::MIXED)
517 // either the EOL of the line (when preserve original EOL chars is on)
518 sLine += GetLineEol(line);
522 // or the default EOL for this file
526 // write this line to the file (codeset or unicode conversions are done there
527 file.WriteString(sLine);
529 if (line == lastRealLine || lastRealLine == -1)
531 // Last line, so now done
539 // If we are saving user files
540 // we need an unpacker/packer, at least a "do nothing" one
541 // repack the file here, overwrite the temporary file we did save in
542 bSaveSuccess = infoUnpacker.Packing(sIntermediateFilename, pszFileName, m_unpackerSubcodes, { pszFileName });
545 TFile(sIntermediateFilename).remove();
549 LogErrorStringUTF8(e.displayText());
553 // returns now, don't overwrite the original file
554 return SAVE_PACK_FAILED;
557 if (bClearModifiedFlag)
560 m_nSyncPosition = m_nUndoPosition;
563 // remember revision number on save
564 m_dwRevisionNumberOnSave = m_dwCurrentRevisionNumber;
566 // redraw line revision marks
567 UpdateViews (nullptr, nullptr, UPDATE_FLAGSONLY);
571 if (bClearModifiedFlag)
574 m_nSyncPosition = m_nUndoPosition;
585 /// Replace line (removing any eol, and only including one if in strText)
586 void CDiffTextBuffer::ReplaceFullLines(CDiffTextBuffer& dbuf, CDiffTextBuffer& sbuf, CCrystalTextView * pSource, int nLineBegin, int nLineEnd, int nAction /*=CE_ACTION_UNKNOWN*/)
589 if (nLineBegin != nLineEnd || sbuf.GetLineLength(nLineEnd) > 0)
590 sbuf.GetTextWithoutEmptys(nLineBegin, 0, nLineEnd, sbuf.GetLineLength(nLineEnd), strText);
591 strText += sbuf.GetLineEol(nLineEnd);
593 if (nLineBegin != nLineEnd || dbuf.GetFullLineLength(nLineEnd) > 0)
595 int nLineEndSource = nLineEnd < dbuf.GetLineCount() ? nLineEnd : dbuf.GetLineCount();
596 if (nLineEnd+1 < GetLineCount())
597 dbuf.DeleteText(pSource, nLineBegin, 0, nLineEndSource + 1, 0, nAction);
599 dbuf.DeleteText(pSource, nLineBegin, 0, nLineEndSource, dbuf.GetLineLength(nLineEndSource), nAction);
602 if (int cchText = strText.GetLength())
605 dbuf.InsertText(pSource, nLineBegin, 0, strText, cchText, endl,endc, nAction);
609 bool CDiffTextBuffer::curUndoGroup()
611 return (m_aUndoBuf.size() != 0 && m_aUndoBuf[0].m_dwFlags&UNDO_BEGINGROUP);
614 bool CDiffTextBuffer:: /* virtual override */
615 DeleteText2(CCrystalTextView * pSource, int nStartLine, int nStartChar,
616 int nEndLine, int nEndChar, int nAction /*= CE_ACTION_UNKNOWN*/, bool bHistory /*= true*/)
618 for (auto syncpnt : m_pOwnerDoc->GetSyncPointList())
620 const int nLineSyncPoint = syncpnt[m_nThisPane];
621 if (((nStartChar == 0 && nStartLine == nLineSyncPoint) || nStartLine < nLineSyncPoint) &&
622 nLineSyncPoint < nEndLine)
623 m_pOwnerDoc->DeleteSyncPoint(m_nThisPane, nLineSyncPoint, false);
625 return CGhostTextBuffer::DeleteText2(pSource, nStartLine, nStartChar, nEndLine, nEndChar, nAction, bHistory);