diff options
author | elfmz <fenix1905@tut.by> | 2022-02-07 22:29:37 +0300 |
---|---|---|
committer | elfmz <fenix1905@tut.by> | 2022-02-07 22:29:37 +0300 |
commit | b748240f56afbdae94336442d7e7cb05ec50bfc7 (patch) | |
tree | a3b91a242ec1d52edf0ae9411beb17b91d850d9a /far2l/src | |
parent | 0feb97bb3edddae8a91fd67105f40f80d2c0ee85 (diff) |
editor: refactor recent changes
Diffstat (limited to 'far2l/src')
-rw-r--r-- | far2l/src/fileedit.cpp | 102 | ||||
-rw-r--r-- | far2l/src/fileedit.hpp | 10 |
2 files changed, 49 insertions, 63 deletions
diff --git a/far2l/src/fileedit.cpp b/far2l/src/fileedit.cpp index 2ed83b3f..9e9fe53f 100644 --- a/far2l/src/fileedit.cpp +++ b/far2l/src/fileedit.cpp @@ -1318,12 +1318,14 @@ int FileEditor::ReProcessKey(int Key,int CalledFromControl) || IsUTF16(m_codepage) != IsUTF16(codepage) || IsUTF32(m_codepage) != IsUTF32(codepage); if (!IsFileModified() || !need_reload) { - SetCodePage(codepage); Flags.Set(FFILEEDIT_CODEPAGECHANGEDBYUSER); if (need_reload) { + m_codepage = codepage; int UserBreak = 0; SaveToCache(); LoadFile(strLoadedFileName, UserBreak); + } else { + SetCodePage(codepage); } ChangeEditKeyBar(); } else @@ -1649,7 +1651,7 @@ int FileEditor::LoadFile(const wchar_t *Name,int &UserBreak) } //TextFormat и Codepage используются ТОЛЬКО, если bSaveAs = true! -void FileEditor::SaveContent(const wchar_t *Name, IContentWriter *Writer, bool bSaveAs, int TextFormat, UINT codepage, bool AddSignature, int Phase) +void FileEditor::SaveContent(const wchar_t *Name, BaseContentWriter *Writer, bool bSaveAs, int TextFormat, UINT codepage, bool AddSignature, int Phase) { DWORD dwSignature = 0; DWORD SignLength = 0; @@ -1687,9 +1689,6 @@ void FileEditor::SaveContent(const wchar_t *Name, IContentWriter *Writer, bool b DWORD StartTime=WINPORT(GetTickCount)(); size_t LineNumber=0; - std::string tmpstr; - std::vector<unsigned char> tmpbuf; - for (Edit *CurPtr=m_editor->TopList; CurPtr; CurPtr=CurPtr->m_next,LineNumber++) { DWORD CurTime=WINPORT(GetTickCount)(); @@ -1718,56 +1717,49 @@ void FileEditor::SaveContent(const wchar_t *Name, IContentWriter *Writer, bool b CurPtr->SetEOL(EndSeq); } - int EndLength=StrLength(EndSeq); + Writer->EncodeAndWrite(codepage, SaveStr, Length); + Writer->EncodeAndWrite(codepage, EndSeq, StrLength(EndSeq)); + } +} + + +void FileEditor::BaseContentWriter::EncodeAndWrite(UINT codepage, const wchar_t *Str, size_t Length) +{ + if (!Length) + return; + if (codepage == CP_WIDE_LE) { - if (Length) - Writer->Write(SaveStr,Length*sizeof(wchar_t)); - if (EndLength) - Writer->Write(EndSeq,EndLength*sizeof(wchar_t)); + Write(Str, Length * sizeof(wchar_t)); } else if (codepage == CP_UTF8) { - Wide2MB(SaveStr, Length, tmpstr); - if (EndLength) - Wide2MB(EndSeq, EndLength, tmpstr, true); - if (!tmpstr.empty()) - Writer->Write(tmpstr.data(),tmpstr.size()); + Wide2MB(Str, Length, _tmpstr); + Write(_tmpstr.data(), _tmpstr.size()); } - else + else if (codepage == CP_WIDE_BE) { - if (Length) - { - const DWORD bufsize = (codepage == CP_WIDE_BE) ? Length * sizeof(wchar_t) - : WINPORT(WideCharToMultiByte)(codepage, 0, SaveStr, Length, nullptr, 0, nullptr, nullptr); - tmpbuf.resize(bufsize); + if (_tmpwstr.size() < Length) + _tmpwstr.resize(Length + 0x20); - if (codepage == CP_WIDE_BE) { - WideReverse(SaveStr, (wchar_t *)tmpbuf.data(), Length); - } else - WINPORT(WideCharToMultiByte)(codepage, 0, SaveStr, Length, (char *)tmpbuf.data(), bufsize, nullptr, nullptr); + WideReverse(Str, (wchar_t *)_tmpwstr.data(), Length); + Write(_tmpwstr.data(), Length * sizeof(wchar_t)); - Writer->Write(tmpbuf.data(), bufsize); - } + } else { + int cnt = WINPORT(WideCharToMultiByte)(codepage, 0, Str, Length, nullptr, 0, nullptr, nullptr); + if (cnt <= 0) + return; - if (EndLength) - { - const DWORD bufsize = (codepage == CP_WIDE_BE ? EndLength*sizeof(wchar_t) - : WINPORT(WideCharToMultiByte)(codepage, 0, EndSeq, EndLength, nullptr, 0, nullptr, nullptr)); - tmpbuf.resize(bufsize); - if (codepage == CP_WIDE_BE) - WideReverse(EndSeq, (wchar_t *)tmpbuf.data(), EndLength); - else - WINPORT(WideCharToMultiByte)(codepage, 0, EndSeq, EndLength, (char *)tmpbuf.data(), bufsize, nullptr, nullptr); + if (_tmpstr.size() < (size_t)cnt) + _tmpstr.resize(cnt + 0x20); - Writer->Write(tmpbuf.data(), bufsize); - } + cnt = WINPORT(WideCharToMultiByte)(codepage, 0, Str, Length, (char *)_tmpstr.data(), _tmpstr.size(), nullptr, nullptr); + if (cnt > 0) + Write(_tmpstr.data(), cnt); } - } } - -struct ContentMeasurer : FileEditor::IContentWriter +struct ContentMeasurer : FileEditor::BaseContentWriter { INT64 MeasuredSize = 0; @@ -1777,7 +1769,7 @@ struct ContentMeasurer : FileEditor::IContentWriter } }; -class ContentSaver : public FileEditor::IContentWriter +class ContentSaver : public FileEditor::BaseContentWriter { CachedWrite CW; @@ -2036,30 +2028,21 @@ int FileEditor::SaveFile(const wchar_t *Name,int Ask, bool bSaveAs, int TextForm SetCursorType(FALSE,0); TPreRedrawFuncGuard preRedrawFuncGuard(Editor::PR_EditorShowMsg); - try { + try + { ContentMeasurer cm; SaveContent(Name, &cm, bSaveAs, TextFormat, codepage, AddSignature, 0); - try { + try + { File EditFile; if (!EditFile.Open(Name, GENERIC_WRITE, FILE_SHARE_READ, nullptr, OPEN_ALWAYS, FILE_ATTRIBUTE_ARCHIVE|FILE_FLAG_SEQUENTIAL_SCAN)) throw WINPORT(GetLastError)(); - if (!Flags.Check(FFILEEDIT_NEW)) { - UINT64 OriginalSize = 0; - EditFile.GetSize(OriginalSize); - - if (!EditFile.SetPointer(cm.MeasuredSize, nullptr, FILE_BEGIN) || !EditFile.SetEnd()) + if (!Flags.Check(FFILEEDIT_NEW)) + { + if (!EditFile.AllocationRequire(cm.MeasuredSize)) throw WINPORT(GetLastError)(); - - if (!EditFile.AllocationRequire(cm.MeasuredSize)) { - auto ErrorCode = WINPORT(GetLastError)(); - EditFile.SetPointer(OriginalSize, nullptr, FILE_BEGIN); - EditFile.SetEnd(); - throw ErrorCode; - } - - EditFile.SetPointer(0, nullptr, FILE_BEGIN); } ContentSaver cs(EditFile); @@ -2087,9 +2070,8 @@ int FileEditor::SaveFile(const wchar_t *Name,int Ask, bool bSaveAs, int TextForm } } - if (FileHolder && RetCode != SAVEFILE_ERROR) { + if (FileHolder && RetCode != SAVEFILE_ERROR) FileHolder->OnFileEdited(Name); - } if (FileUnmakeWritable) { diff --git a/far2l/src/fileedit.hpp b/far2l/src/fileedit.hpp index ba632b9e..f831103a 100644 --- a/far2l/src/fileedit.hpp +++ b/far2l/src/fileedit.hpp @@ -83,11 +83,15 @@ enum FFILEEDIT_FLAGS class FileEditor : public Frame { public: - struct IContentWriter + struct BaseContentWriter { + void EncodeAndWrite(UINT codepage, const wchar_t *Str, size_t Length); virtual void Write(const void *Data, size_t Length) = 0; - }; + private: + std::string _tmpstr; + std::wstring _tmpwstr; + }; FileEditor(const wchar_t *Name, UINT codepage, DWORD InitFlags,int StartLine=-1,int StartChar=-1,const wchar_t *PluginData=nullptr,int OpenModeExstFile=FEOPMODE_QUERY); FileEditor(const wchar_t *Name, UINT codepage, DWORD InitFlags,int StartLine,int StartChar,const wchar_t *Title,int X1,int Y1,int X2,int Y2,int DeleteOnClose=0,int OpenModeExstFile=FEOPMODE_QUERY); @@ -171,7 +175,7 @@ class FileEditor : public Frame int LoadFile(const wchar_t *Name, int &UserBreak); //TextFormat, Codepage и AddSignature используются ТОЛЬКО, если bSaveAs = true! - void SaveContent(const wchar_t *Name, IContentWriter *Writer, bool bSaveAs, int TextFormat, UINT codepage, bool AddSignature, int Phase); + void SaveContent(const wchar_t *Name, BaseContentWriter *Writer, bool bSaveAs, int TextFormat, UINT codepage, bool AddSignature, int Phase); int SaveFile(const wchar_t *Name, int Ask, bool bSaveAs, int TextFormat = 0, UINT Codepage = CP_UTF8, bool AddSignature=false); void SetTitle(const wchar_t *Title); virtual FARString &GetTitle(FARString &Title,int SubLen=-1,int TruncSize=0); |