summaryrefslogtreecommitdiff
path: root/basctl/source
diff options
context:
space:
mode:
authorAndreas Heinisch <andreas.heinisch@yahoo.de>2021-05-22 21:10:49 +0200
committerAndreas Heinisch <andreas.heinisch@yahoo.de>2021-05-25 22:20:10 +0200
commit178adcd8459af63ddb48927207baa5b4efbfda12 (patch)
tree938cb7a6e2a3f694cb36ebf9fe692c995511871a /basctl/source
parent60d216ed1a5ad74a93edfb288206ce9ae20380a4 (diff)
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 <andreas.heinisch@yahoo.de>
Diffstat (limited to 'basctl/source')
-rw-r--r--basctl/source/basicide/baside2.cxx22
1 files changed, 22 insertions, 0 deletions
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 <com/sun/star/ui/dialogs/XFilePickerControlAccess.hpp>
#include <comphelper/SetFlagContextHelper.hxx>
#include <comphelper/string.hxx>
+#include <unicode/ucsdet.h>
#include <svl/srchdefs.hxx>
#include <sfx2/bindings.hxx>
#include <sfx2/docfile.hxx>
@@ -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<const char*>(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();