2 * @file DiffTextBuffer.cpp
4 * @brief Implementation file for CDiffTextBuffer
7 // ID line follows -- this is updated by SVN
15 #include "coretools.h"
17 #include "OptionsDef.h"
18 #include "Environment.h"
19 #include "MergeLineFlags.h"
21 #include "FileTransform.h"
22 #include "FileTextEncoding.h"
23 #include "DiffTextBuffer.h"
28 static char THIS_FILE[] = __FILE__;
31 static bool IsTextFileStylePure(const UniMemFile::txtstats & stats);
32 static CString GetLineByteTimeReport(UINT lines, __int64 bytes,
33 const COleDateTime & start);
34 static void EscapeControlChars(CString &s);
35 static LPCTSTR GetEol(const CString &str);
36 static CRLFSTYLE GetTextFileStyle(const UniMemFile::txtstats & stats);
39 * @brief Check if file has only one EOL type.
40 * @param [in] stats File's text stats.
41 * @return true if only one EOL type is found, false otherwise.
43 static bool IsTextFileStylePure(const UniMemFile::txtstats & stats)
46 nType += (stats.ncrlfs > 0);
47 nType += (stats.ncrs > 0);
48 nType += (stats.nlfs > 0);
53 * @brief Return a string giving #lines and #bytes and how much time elapsed.
54 * @param [in] lines Count of lines.
55 * @param [in] bytes Count of bytes.
56 * @param [in] start Time used.
57 * @return Formatted string.
59 static CString GetLineByteTimeReport(UINT lines, __int64 bytes,
60 const COleDateTime & start)
62 String sLines = locality::NumToLocaleStr((int)lines);
63 String sBytes = locality::NumToLocaleStr(bytes);
64 COleDateTimeSpan duration = COleDateTime::GetCurrentTime() - start;
65 String sMinutes = locality::NumToLocaleStr((int)duration.GetTotalMinutes());
67 str.Format(_T("%s lines (%s byte) saved in %sm%02ds")
68 , sLines.c_str(), sBytes.c_str(), sMinutes.c_str()
69 , duration.GetSeconds()
75 * @brief Escape control characters.
76 * @param [in,out] s Line of text excluding eol chars.
78 * @note Escape sequences follow the pattern
79 * (leadin character, high nibble, low nibble, leadout character).
80 * The leadin character is '\x0F'. The leadout character is a backslash.
82 static void EscapeControlChars(CString &s)
84 // Compute buffer length required for escaping
85 int n = s.GetLength();
91 // Is it a control character in the range 0..31 except TAB?
92 if (!(c & ~_T('\x1F')) && c != _T('\t'))
94 n += 3; // Need 3 extra characters to escape
97 // Reallocate accordingly
99 LPTSTR p = s.GetBufferSetLength(n);
100 // Copy/translate characters starting at end of string
104 // Is it a control character in the range 0..31 except TAB?
105 if (!(c & ~_T('\x1F')) && c != _T('\t'))
107 // Bitwise OR with 0x100 so _itot() will output 3 hex digits
108 _itot(0x100 | c, p + n - 4, 16);
109 // Replace terminating zero with leadout character
111 // Prepare to replace 1st hex digit with leadin character
120 * @brief Get EOL of the string.
121 * This function returns a pointer to the EOL chars in the given string.
122 * Behavior is similar to CCrystalTextBuffer::GetLineEol().
123 * @param [in] str String whose EOL chars are returned.
124 * @return Pointer to string's EOL chars, or empty string if no EOL found.
126 static LPCTSTR GetEol(const CString &str)
128 if (str.GetLength()>1 && str[str.GetLength()-2]=='\r' && str[str.GetLength()-1]=='\n')
129 return (LPCTSTR)str + str.GetLength()-2;
130 if (str.GetLength()>0 && (str[str.GetLength()-1]=='\r' || str[str.GetLength()-1]=='\n'))
131 return (LPCTSTR)str + str.GetLength()-1;
136 * @brief Get file's EOL type.
137 * @param [in] stats File's text stats.
140 static CRLFSTYLE GetTextFileStyle(const UniMemFile::txtstats & stats)
142 if (stats.ncrlfs >= stats.nlfs)
144 if (stats.ncrlfs >= stats.ncrs)
145 return CRLF_STYLE_DOS;
147 return CRLF_STYLE_MAC;
151 if (stats.nlfs >= stats.ncrs)
152 return CRLF_STYLE_UNIX;
154 return CRLF_STYLE_MAC;
159 * @brief Constructor.
160 * @param [in] pDoc Owning CMergeDoc.
161 * @param [in] pane Pane number this buffer is associated with.
163 CDiffTextBuffer::CDiffTextBuffer(CMergeDoc * pDoc, int pane)
166 , m_unpackerSubcode(0)
171 * @brief Get a line from the buffer.
172 * @param [in] nLineIndex Index of the line to get.
173 * @param [out] strLine Returns line text in the index.
175 BOOL CDiffTextBuffer::GetLine(int nLineIndex, CString &strLine)
177 int nLineLength = CCrystalTextBuffer::GetLineLength(nLineIndex);
180 else if (nLineLength == 0)
184 _tcsncpy(strLine.GetBuffer(nLineLength + 1),
185 CCrystalTextBuffer::GetLineChars(nLineIndex), nLineLength);
186 strLine.ReleaseBuffer(nLineLength);
192 * @brief Set the buffer modified status.
193 * @param [in] bModified New modified status, TRUE if buffer has been
194 * modified since last saving.
196 void CDiffTextBuffer::SetModified(BOOL bModified /*= TRUE*/)
198 CCrystalTextBuffer::SetModified (bModified);
199 m_pOwnerDoc->SetModifiedFlag (bModified);
203 * @brief Get a line (with EOL bytes) from the buffer.
204 * This function is like GetLine() but it also includes line's EOL to the
206 * @param [in] nLineIndex Index of the line to get.
207 * @param [out] strLine Returns line text in the index. Existing content
208 * of this string is overwritten.
210 BOOL CDiffTextBuffer::GetFullLine(int nLineIndex, CString &strLine)
212 int cchText = GetFullLineLength(nLineIndex);
218 LPTSTR pchText = strLine.GetBufferSetLength(cchText);
219 memcpy(pchText, GetLineChars(nLineIndex), cchText * sizeof(TCHAR));
223 void CDiffTextBuffer::AddUndoRecord(BOOL bInsert, const CPoint & ptStartPos,
224 const CPoint & ptEndPos, LPCTSTR pszText, int cchText,
225 int nLinesToValidate, int nActionType /*= CE_ACTION_UNKNOWN*/,
226 CDWordArray *paSavedRevisonNumbers)
228 CGhostTextBuffer::AddUndoRecord(bInsert, ptStartPos, ptEndPos, pszText,
229 cchText, nLinesToValidate, nActionType, paSavedRevisonNumbers);
230 if (m_aUndoBuf[m_nUndoPosition - 1].m_dwFlags & UNDO_BEGINGROUP)
232 m_pOwnerDoc->undoTgt.erase(m_pOwnerDoc->curUndo, m_pOwnerDoc->undoTgt.end());
233 m_pOwnerDoc->undoTgt.push_back(m_pOwnerDoc->GetView(m_nThisPane));
234 m_pOwnerDoc->curUndo = m_pOwnerDoc->undoTgt.end();
238 * @brief Checks if a flag is set for line.
239 * @param [in] line Index (0-based) for line.
240 * @param [in] flag Flag to check.
241 * @return TRUE if flag is set, FALSE otherwise.
243 BOOL CDiffTextBuffer::FlagIsSet(UINT line, DWORD flag)
245 return ((m_aLines[line].m_dwFlags & flag) == flag);
249 Remove blank lines and clear winmerge flags
250 (2003-06-21, Perry: I don't understand why this is necessary, but if this isn't
251 done, more and more gray lines appear in the file)
252 (2003-07-31, Laoran I don't understand either why it is necessary, but it works
253 fine, so let's go on with it)
255 void CDiffTextBuffer::prepareForRescan()
257 RemoveAllGhostLines();
258 for (int ct = GetLineCount() - 1; ct >= 0; --ct)
260 SetLineFlag(ct, LF_DIFF, FALSE, FALSE, FALSE);
261 SetLineFlag(ct, LF_TRIVIAL, FALSE, FALSE, FALSE);
262 SetLineFlag(ct, LF_MOVED, FALSE, FALSE, FALSE);
267 * @brief Called when line has been edited.
268 * After editing a line, we don't know if there is a diff or not.
269 * So we clear the LF_DIFF flag (and it is more easy to read during edition).
270 * Rescan will set the proper color.
271 * @param [in] nLine Line that has been edited.
274 void CDiffTextBuffer::OnNotifyLineHasBeenEdited(int nLine)
276 SetLineFlag(nLine, LF_DIFF, FALSE, FALSE, FALSE);
277 SetLineFlag(nLine, LF_TRIVIAL, FALSE, FALSE, FALSE);
278 SetLineFlag(nLine, LF_MOVED, FALSE, FALSE, FALSE);
279 CGhostTextBuffer::OnNotifyLineHasBeenEdited(nLine);
283 * @brief Set the folder for temp files.
284 * @param [in] path Temp files folder.
286 void CDiffTextBuffer::SetTempPath(const String &path)
288 m_strTempPath = path;
292 * @brief Is the buffer initialized?
293 * @return TRUE if the buffer is initialized, FALSE otherwise.
295 bool CDiffTextBuffer::IsInitialized() const
301 * @brief Load file from disk into buffer
303 * @param [in] pszFileNameInit File to load
304 * @param [in] infoUnpacker Unpacker plugin
305 * @param [in] sToFindUnpacker String for finding unpacker plugin
306 * @param [out] readOnly Loading was lossy so file should be read-only
307 * @param [in] nCrlfStyle EOL style used
308 * @param [in] encoding Encoding used
309 * @param [out] sError Error message returned
310 * @return FRESULT_OK when loading succeed or (list in files.h):
311 * - FRESULT_OK_IMPURE : load OK, but the EOL are of different types
312 * - FRESULT_ERROR_UNPACK : plugin failed to unpack
313 * - FRESULT_ERROR : loading failed, sError contains error message
314 * - FRESULT_BINARY : file is binary file
315 * @note If this method fails, it calls InitNew so the CDiffTextBuffer is in a valid state
317 int CDiffTextBuffer::LoadFromFile(LPCTSTR pszFileNameInit,
318 PackingInfo * infoUnpacker, LPCTSTR sToFindUnpacker, BOOL & readOnly,
319 CRLFSTYLE nCrlfStyle, const FileTextEncoding & encoding, CString &sError)
322 ASSERT(m_aLines.GetSize() == 0);
324 // Unpacking the file here, save the result in a temporary file
325 String sFileName(pszFileNameInit);
326 if (infoUnpacker->bToBeScanned)
328 if (!FileTransform_Unpacking(sFileName, sToFindUnpacker, infoUnpacker,
331 InitNew(); // leave crystal editor in valid, empty state
332 return FileLoadResult::FRESULT_ERROR_UNPACK;
337 if (!FileTransform_Unpacking(sFileName, infoUnpacker, &m_unpackerSubcode))
339 InitNew(); // leave crystal editor in valid, empty state
340 return FileLoadResult::FRESULT_ERROR_UNPACK;
343 // we use the same unpacker for both files, so it must be defined after first file
344 ASSERT(infoUnpacker->bToBeScanned != PLUGIN_AUTO);
345 // we will load the transformed file
346 LPCTSTR pszFileName = sFileName.c_str();
349 DWORD nRetVal = FileLoadResult::FRESULT_OK;
351 // Set encoding based on extension, if we know one
352 SplitFilename(pszFileName, NULL, NULL, &sExt);
353 CCrystalTextView::TextDefinition *def =
354 CCrystalTextView::GetTextType(sExt.c_str());
355 if (def && def->encoding != -1)
356 m_nSourceEncoding = def->encoding;
358 UniFile *pufile = infoUnpacker->pufile;
360 pufile = new UniMemFile;
362 // Now we only use the UniFile interface
363 // which is something we could implement for HTTP and/or FTP files
365 if (!pufile->OpenReadOnly(pszFileName))
367 nRetVal = FileLoadResult::FRESULT_ERROR;
368 UniFile::UniError uniErr = pufile->GetLastUniError();
369 if (uniErr.HasError())
371 sError = uniErr.GetError().c_str();
373 InitNew(); // leave crystal editor in valid, empty state
374 goto LoadFromFileExit;
378 // If the file is not unicode file, use the codepage we were given to
379 // interpret the 8-bit characters. If the file is unicode file,
380 // determine its type (IsUnicode() does that).
381 if (encoding.m_unicoding == ucr::NONE || !pufile->IsUnicode())
382 pufile->SetCodepage(encoding.m_codepage);
387 UINT next_line_report = 100; // for trace messages
388 UINT next_line_multiple = 5; // for trace messages
389 COleDateTime start = COleDateTime::GetCurrentTime(); // for trace messages
391 // Manually grow line array exponentially
392 UINT arraysize = 500;
393 m_aLines.SetSize(arraysize);
395 // preveol must be initialized for empty files
400 done = !pufile->ReadString(sline, eol, &lossy);
402 // if last line had no eol, we can quit
403 if (done && preveol.empty())
405 // but if last line had eol, we add an extra (empty) line to buffer
407 // Manually grow line array exponentially
408 if (lineno == arraysize)
411 m_aLines.SetSize(arraysize);
414 sline += eol; // TODO: opportunity for optimization, as CString append is terrible
417 // TODO: Should record lossy status of line
419 AppendLine(lineno, sline.c_str(), sline.length());
424 // send occasional line counts to trace
425 // (at 100, 500, 1000, 5000, etc)
426 if (lineno == next_line_report)
428 __int64 dwBytesRead = pufile->GetPosition();
429 COleDateTimeSpan duration = COleDateTime::GetCurrentTime() - start;
430 if (duration.GetTotalMinutes() > 0)
432 CString strace = GetLineByteTimeReport(lineno, dwBytesRead, start);
433 TRACE(_T("%s\n"), (LPCTSTR)strace);
435 next_line_report = next_line_multiple * next_line_report;
436 next_line_multiple = (next_line_multiple == 5) ? 2 : 5;
442 // Send report of duration to trace (if it took a while)
443 COleDateTime end = COleDateTime::GetCurrentTime();
444 COleDateTimeSpan duration = end - start;
445 if (duration.GetTotalMinutes() > 0)
447 __int64 dwBytesRead = pufile->GetPosition();
448 CString strace = GetLineByteTimeReport(lineno, dwBytesRead, start);
449 TRACE(_T("%s\n"), (LPCTSTR)strace);
453 // fix array size (due to our manual exponential growth
454 m_aLines.SetSize(lineno);
457 //Try to determine current CRLF mode (most frequent)
458 if (nCrlfStyle == CRLF_STYLE_AUTOMATIC)
460 nCrlfStyle = GetTextFileStyle(pufile->GetTxtStats());
462 ASSERT(nCrlfStyle >= 0 && nCrlfStyle <= 2);
463 SetCRLFMode(nCrlfStyle);
465 // At least one empty line must present
466 // (view does not work for empty buffers)
467 ASSERT(m_aLines.GetSize() > 0);
471 m_bUndoGroup = m_bUndoBeginGroup = FALSE;
472 m_nSyncPosition = m_nUndoPosition = 0;
473 ASSERT(m_aUndoBuf.size() == 0);
474 m_ptLastChange.x = m_ptLastChange.y = -1;
477 // flags don't need initialization because 0 is the default value
479 // Set the return value : OK + info if the file is impure
480 // A pure file is a file where EOL are consistent (all DOS, or all UNIX, or all MAC)
481 // An impure file is a file with several EOL types
482 // WinMerge may display impure files, but the default option is to unify the EOL
483 // We return this info to the caller, so it may display a confirmation box
484 if (IsTextFileStylePure(pufile->GetTxtStats()))
485 nRetVal = FileLoadResult::FRESULT_OK;
487 nRetVal = FileLoadResult::FRESULT_OK_IMPURE;
489 // stash original encoding away
490 m_encoding.m_unicoding = pufile->GetUnicoding();
491 m_encoding.m_bom = pufile->HasBom();
492 m_encoding.m_codepage = pufile->GetCodepage();
494 if (pufile->GetTxtStats().nlosses)
496 FileLoadResult::AddModifier(nRetVal, FileLoadResult::FRESULT_LOSSY);
502 // close the file now to free the handle
506 // delete the file that unpacking may have created
507 if (_tcscmp(pszFileNameInit, pszFileName) != 0)
508 if (!::DeleteFile(pszFileName))
510 LogErrorString(Fmt(_T("DeleteFile(%s) failed: %s"),
511 pszFileName, GetSysError(GetLastError())));
518 * @brief Saves file from buffer to disk
520 * @param bTempFile : FALSE if we are saving user files and
521 * TRUE if we are saving workin-temp-files for diff-engine
523 * @return SAVE_DONE or an error code (list in MergeDoc.h)
525 int CDiffTextBuffer::SaveToFile (LPCTSTR pszFileName,
526 BOOL bTempFile, String & sError, PackingInfo * infoUnpacker /*= NULL*/,
527 CRLFSTYLE nCrlfStyle /*= CRLF_STYLE_AUTOMATIC*/,
528 BOOL bClearModifiedFlag /*= TRUE*/ )
532 if (!pszFileName || _tcslen(pszFileName) == 0)
533 return SAVE_FAILED; // No filename, cannot save...
535 if (nCrlfStyle == CRLF_STYLE_AUTOMATIC &&
536 !GetOptionsMgr()->GetBool(OPT_ALLOW_MIXED_EOL) ||
537 infoUnpacker && infoUnpacker->disallowMixedEOL)
539 // get the default nCrlfStyle of the CDiffTextBuffer
540 nCrlfStyle = GetCRLFMode();
541 ASSERT(nCrlfStyle >= 0 && nCrlfStyle <= 2);
544 BOOL bOpenSuccess = TRUE;
545 BOOL bSaveSuccess = FALSE;
548 file.SetUnicoding(m_encoding.m_unicoding);
549 file.SetBom(m_encoding.m_bom);
550 file.SetCodepage(m_encoding.m_codepage);
552 String sIntermediateFilename; // used when !bTempFile
556 bOpenSuccess = !!file.OpenCreate(pszFileName);
560 sIntermediateFilename = env_GetTempFileName(m_strTempPath.c_str(),
562 if (sIntermediateFilename.empty())
563 return SAVE_FAILED; //Nothing to do if even tempfile name fails
564 bOpenSuccess = !!file.OpenCreate(sIntermediateFilename.c_str());
569 UniFile::UniError uniErr = file.GetLastUniError();
570 if (uniErr.HasError())
572 sError = uniErr.GetError().c_str();
574 LogErrorString(Fmt(_T("Opening file %s failed: %s"),
575 pszFileName, sError));
577 LogErrorString(Fmt(_T("Opening file %s failed: %s"),
578 sIntermediateFilename, sError));
585 // line loop : get each real line and write it in the file
587 CString sEol = GetStringEol(nCrlfStyle);
588 int nLineCount = m_aLines.GetSize();
589 for (int line = 0; line < nLineCount; ++line)
591 if (GetLineFlags(line) & LF_GHOST)
594 // get the characters of the line (excluding EOL)
595 if (GetLineLength(line) > 0)
596 GetText(line, 0, line, GetLineLength(line), sLine, 0);
601 EscapeControlChars(sLine);
603 if (line == ApparentLastRealLine())
605 // last real line is never EOL terminated
606 ASSERT (_tcslen(GetLineEol(line)) == 0);
607 // write the line and exit loop
608 String tmpLine(sLine);
609 file.WriteString(tmpLine);
613 // normal real line : append an EOL
614 if (nCrlfStyle == CRLF_STYLE_AUTOMATIC)
616 // either the EOL of the line (when preserve original EOL chars is on)
617 sLine += GetLineEol(line);
621 // or the default EOL for this file
625 // write this line to the file (codeset or unicode conversions are done there)
626 String tmpLine(sLine);
627 file.WriteString(tmpLine);
633 // If we are saving user files
634 // we need an unpacker/packer, at least a "do nothing" one
635 ASSERT(infoUnpacker != NULL);
636 // repack the file here, overwrite the temporary file we did save in
637 String csTempFileName = sIntermediateFilename;
638 infoUnpacker->subcode = m_unpackerSubcode;
639 if (!FileTransform_Packing(csTempFileName, *infoUnpacker))
641 if (!::DeleteFile(sIntermediateFilename.c_str()))
643 LogErrorString(Fmt(_T("DeleteFile(%s) failed: %s"),
644 sIntermediateFilename.c_str(), GetSysError(GetLastError())));
646 // returns now, don't overwrite the original file
647 return SAVE_PACK_FAILED;
649 // the temp filename may have changed during packing
650 if (csTempFileName != sIntermediateFilename)
652 if (!::DeleteFile(sIntermediateFilename.c_str()))
654 LogErrorString(Fmt(_T("DeleteFile(%s) failed: %s"),
655 sIntermediateFilename.c_str(), GetSysError(GetLastError())));
657 sIntermediateFilename = csTempFileName;
660 // Write tempfile over original file
661 if (::CopyFile(sIntermediateFilename.c_str(), pszFileName, FALSE))
663 if (!::DeleteFile(sIntermediateFilename.c_str()))
665 LogErrorString(Fmt(_T("DeleteFile(%s) failed: %s"),
666 sIntermediateFilename.c_str(), GetSysError(GetLastError())));
668 if (bClearModifiedFlag)
671 m_nSyncPosition = m_nUndoPosition;
675 // remember revision number on save
676 m_dwRevisionNumberOnSave = m_dwCurrentRevisionNumber;
678 // redraw line revision marks
679 UpdateViews (NULL, NULL, UPDATE_FLAGSONLY);
683 sError = GetSysError(GetLastError());
684 LogErrorString(Fmt(_T("CopyFile(%s, %s) failed: %s"),
685 sIntermediateFilename.c_str(), pszFileName, sError));
690 if (bClearModifiedFlag)
693 m_nSyncPosition = m_nUndoPosition;
705 * @brief Replace a line with new text.
706 * This function replaces line's text without changing the EOL style/bytes
708 * @param [in] pSource Editor view where text is changed.
709 * @param [in] nLine Index of the line to change.
710 * @param [in] pchText New text of the line.
711 * @param [in] cchText New length of the line (not inc. EOL bytes).
712 * @param [in] nAction Edit action to use.
714 void CDiffTextBuffer::ReplaceLine(CCrystalTextView * pSource, int nLine,
715 LPCTSTR pchText, int cchText, int nAction /*=CE_ACTION_UNKNOWN*/)
717 if (GetLineLength(nLine)>0)
718 DeleteText(pSource, nLine, 0, nLine, GetLineLength(nLine), nAction);
721 InsertText(pSource, nLine, 0, pchText, cchText, endl, endc, nAction);
724 /// Replace line (removing any eol, and only including one if in strText)
726 * @brief Replace a line with new text.
727 * This function replaces line's text including EOL bytes. If the @p strText
728 * does not include EOL bytes, the "line" does not get EOL bytes.
729 * @param [in] pSource Editor view where text is changed.
730 * @param [in] nLine Index of the line to change.
731 * @param [in] pchText New text of the line.
732 * @param [in] cchText New length of the line (not inc. EOL bytes).
733 * @param [in] nAction Edit action to use.
735 void CDiffTextBuffer::ReplaceFullLine(CCrystalTextView * pSource, int nLine,
736 const CString &strText, int nAction /*=CE_ACTION_UNKNOWN*/)
738 LPCTSTR eol = GetEol(strText);
739 if (_tcscmp(GetLineEol(nLine), eol) == 0)
741 // (optimization) eols are the same, so just replace text inside line
742 // we must clean strText from its eol...
743 int eolLength = _tcslen(eol);
744 ReplaceLine(pSource, nLine, strText, strText.GetLength() - eolLength, nAction);
748 // we may need a last line as the DeleteText end is (x=0,y=line+1)
749 if (nLine + 1 == GetLineCount())
750 InsertGhostLine (pSource, GetLineCount());
752 if (GetFullLineLength(nLine))
753 DeleteText(pSource, nLine, 0, nLine + 1, 0, nAction);
755 const int cchText = strText.GetLength();
757 InsertText(pSource, nLine, 0, strText, cchText, endl, endc, nAction);
760 bool CDiffTextBuffer::curUndoGroup()
762 return (m_aUndoBuf.size() != 0 && m_aUndoBuf[0].m_dwFlags&UNDO_BEGINGROUP);