From 178adcd8459af63ddb48927207baa5b4efbfda12 Mon Sep 17 00:00:00 2001 From: Andreas Heinisch Date: Sat, 22 May 2021 21:10:49 +0200 Subject: tdf#139196 - Import/export macros using utf-8 including BOM In addition, try to detect the charset during the import of a *.bas file. Change-Id: I0dfe7f1b5349db409d90ed92b2e19c9946ae50cc Reviewed-on: https://gerrit.libreoffice.org/c/core/+/116004 Tested-by: Jenkins Reviewed-by: Andreas Heinisch --- basctl/source/basicide/baside2.cxx | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) (limited to 'basctl/source/basicide') diff --git a/basctl/source/basicide/baside2.cxx b/basctl/source/basicide/baside2.cxx index bebbe643874c..1b5cfce918bf 100644 --- a/basctl/source/basicide/baside2.cxx +++ b/basctl/source/basicide/baside2.cxx @@ -42,6 +42,7 @@ #include #include #include +#include #include #include #include @@ -436,6 +437,24 @@ void ModulWindow::LoadBasic() // nLines*4: ReadText/Formatting/Highlighting/Formatting GetEditorWindow().CreateProgress( IDEResId(RID_STR_GENERATESOURCE), nLines*4 ); GetEditEngine()->SetUpdateMode( false ); + // tdf#139196 - import macros using either default or utf-8 text encoding + constexpr size_t buffsize = 1024 * 1024; + sal_Int8 bytes[buffsize] = { 0 }; + sal_Int32 nRead = pStream->ReadBytes(bytes, buffsize); + UErrorCode uerr = U_ZERO_ERROR; + UCharsetDetector* ucd = ucsdet_open(&uerr); + ucsdet_setText(ucd, reinterpret_cast(bytes), nRead, &uerr); + if (const UCharsetMatch* match = ucsdet_detect(ucd, &uerr)) + { + const char* pEncodingName = ucsdet_getName(match, &uerr); + + if (U_SUCCESS(uerr) && !strcmp("UTF-8", pEncodingName)) + { + pStream->SetStreamCharSet(RTL_TEXTENCODING_UTF8); + } + } + ucsdet_close(ucd); + pStream->Seek(0); GetEditView()->Read( *pStream ); GetEditEngine()->SetUpdateMode( true ); GetEditorWindow().PaintImmediately(); @@ -483,6 +502,9 @@ void ModulWindow::SaveBasicSource() { EnterWait(); AssertValidEditEngine(); + // tdf#139196 - export macros using utf-8 including BOM + pStream->SetStreamCharSet(RTL_TEXTENCODING_UTF8); + pStream->WriteUChar(0xEF).WriteUChar(0xBB).WriteUChar(0xBF); GetEditEngine()->Write( *pStream ); aMedium.Commit(); LeaveWait(); -- cgit