From 549342b3d9a35957f815c165ed6e91dd6f624463 Mon Sep 17 00:00:00 2001 From: Andy McFadden Date: Fri, 22 Sep 2017 11:25:08 -0700 Subject: [PATCH] Fix file viewer glitch with empty output The file viewer was seeing zero-length formatted output and assuming that it was the result of zero-length input. This is a problem because the code disables the format options when there's nothing to format. This resulted in the strange behavior noted in issue #14. Now the "is source empty" value is passed explicitly, and we display a different message when the formatter fails. --- app/ViewFilesDialog.cpp | 34 +++++++++++++++++++++++++++------- app/ViewFilesDialog.h | 12 ++++++++++-- 2 files changed, 37 insertions(+), 9 deletions(-) diff --git a/app/ViewFilesDialog.cpp b/app/ViewFilesDialog.cpp index 0d12bca..57482fc 100644 --- a/app/ViewFilesDialog.cpp +++ b/app/ViewFilesDialog.cpp @@ -266,7 +266,22 @@ static void DumpBitmapInfo(HBITMAP hBitmap) LOGD(" bmPits = 0x%p", info.bmBits); } -void ViewFilesDialog::DisplayText(const WCHAR* fileName) +bool ViewFilesDialog::IsSourceEmpty(const GenericEntry* pEntry, + ReformatHolder::ReformatPart part) +{ + switch (part) { + case ReformatHolder::ReformatPart::kPartData: + return pEntry->GetDataForkLen() == 0; + case ReformatHolder::ReformatPart::kPartRsrc: + return pEntry->GetRsrcForkLen() == 0; + case ReformatHolder::ReformatPart::kPartCmmt: + return !pEntry->GetHasNonEmptyComment(); + default: + return false; + } +} + +void ViewFilesDialog::DisplayText(const WCHAR* fileName, bool zeroSourceLen) { CWaitCursor wait; // streaming of big files can take a little while bool errFlg; @@ -404,10 +419,14 @@ void ViewFilesDialog::DisplayText(const WCHAR* fileName) if (fpOutput->GetOutputKind() == ReformatOutput::kOutputRTF) streamFormat = SF_RTF; if (fpOutput->GetTextLen() == 0) { - textBuf = "(file is empty)"; + if (zeroSourceLen) { + textBuf = "(file is empty)"; + EnableFormatSelection(FALSE); + } else { + textBuf = "(converted output is empty)"; + } textLen = strlen(textBuf); emptyFlg = true; - EnableFormatSelection(FALSE); } if (fpOutput->GetOutputKind() == ReformatOutput::kOutputErrorMsg) EnableFormatSelection(FALSE); @@ -638,7 +657,8 @@ void ViewFilesDialog::OnFviewNext(void) id = ConfigureFormatSel(part); Reformat(pSelEntry->GetEntry(), part, id); - DisplayText(pSelEntry->GetEntry()->GetDisplayName()); + DisplayText(pSelEntry->GetEntry()->GetDisplayName(), + IsSourceEmpty(pSelEntry->GetEntry(), part)); } void ViewFilesDialog::OnFviewPrev(void) @@ -681,7 +701,7 @@ void ViewFilesDialog::OnFviewPrev(void) id = ConfigureFormatSel(part); Reformat(pEntry, part, id); - DisplayText(pEntry->GetDisplayName()); + DisplayText(pEntry->GetDisplayName(), IsSourceEmpty(pEntry, part)); } void ViewFilesDialog::ConfigurePartButtons(const GenericEntry* pEntry) @@ -936,7 +956,7 @@ void ViewFilesDialog::OnFormatSelChange(void) id = (ReformatHolder::ReformatID) pCombo->GetItemData(pCombo->GetCurSel()); Reformat(pEntry, part, id); - DisplayText(pEntry->GetDisplayName()); + DisplayText(pEntry->GetDisplayName(), IsSourceEmpty(pEntry, part)); } void ViewFilesDialog::OnFviewData(void) @@ -969,7 +989,7 @@ void ViewFilesDialog::ForkSelectCommon(ReformatHolder::ReformatPart part) id = ConfigureFormatSel(part); Reformat(pEntry, part, id); - DisplayText(pEntry->GetDisplayName()); + DisplayText(pEntry->GetDisplayName(), IsSourceEmpty(pEntry, part)); } void ViewFilesDialog::OnFviewFmtBest(void) diff --git a/app/ViewFilesDialog.h b/app/ViewFilesDialog.h index 622da76..e658758 100644 --- a/app/ViewFilesDialog.h +++ b/app/ViewFilesDialog.h @@ -120,15 +120,23 @@ private: //void StretchControl(int id, int deltaX, int deltaY); void NewFontSelected(bool resetBold); + /* + * Determines whether the specified part is an empty fork/comment. + */ + bool IsSourceEmpty(const GenericEntry* pEntry, + ReformatHolder::ReformatPart part); + /* * Display a buffer of text in the RichEdit control. * * The RichEdit dialog will hold its own copy of the data, so "pHolder" can * be safely destroyed after this returns. * - * "fileName" is for display only. + * "fileName" is for display only. "zeroSourceLen" allows the function to + * tell the difference between an empty file and a non-empty file that + * generated empty output. */ - void DisplayText(const WCHAR* fileName); + void DisplayText(const WCHAR* fileName, bool zeroSourceLen); /* * Set up the fpHolder. Does not reformat the data, just loads the source