diff --git a/CSharp/Libraries/UniversalEditor.Essential/DataFormats/Text/Plain/ByteOrderMark.cs b/CSharp/Libraries/UniversalEditor.Essential/DataFormats/Text/Plain/ByteOrderMark.cs new file mode 100644 index 00000000..b12bee75 --- /dev/null +++ b/CSharp/Libraries/UniversalEditor.Essential/DataFormats/Text/Plain/ByteOrderMark.cs @@ -0,0 +1,31 @@ +// +// ByteOrderMark.cs +// +// Author: +// Mike Becker +// +// Copyright (c) 2019 Mike Becker +// +// This program is free software: you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// along with this program. If not, see . +using System; +namespace UniversalEditor.DataFormats.Text.Plain +{ + public enum ByteOrderMark + { + None, + UTF8, + UTF16LittleEndian, + UTF16BigEndian + } +} diff --git a/CSharp/Libraries/UniversalEditor.Essential/DataFormats/Text/Plain/PlainTextDataFormat.cs b/CSharp/Libraries/UniversalEditor.Essential/DataFormats/Text/Plain/PlainTextDataFormat.cs index 18611b9e..27f8ae5a 100644 --- a/CSharp/Libraries/UniversalEditor.Essential/DataFormats/Text/Plain/PlainTextDataFormat.cs +++ b/CSharp/Libraries/UniversalEditor.Essential/DataFormats/Text/Plain/PlainTextDataFormat.cs @@ -37,6 +37,8 @@ namespace UniversalEditor.DataFormats.Text.Plain return _dfr; } + public ByteOrderMark ByteOrderMark { get; set; } = ByteOrderMark.None; + protected override void LoadInternal(ref ObjectModel objectModel) { PlainTextObjectModel ptom = (objectModel as PlainTextObjectModel); @@ -44,6 +46,38 @@ namespace UniversalEditor.DataFormats.Text.Plain throw new ObjectModelNotSupportedException(); Reader reader = Accessor.Reader; + + // determine if we have BOM + if (reader.Accessor.Length >= 4) + { + byte b1 = reader.ReadByte(); + byte b2 = reader.ReadByte(); + byte b3 = reader.ReadByte(); + byte b4 = reader.ReadByte(); + if (b1 == 0xEF && b2 == 0xBB && b3 == 0xBF) + { + ByteOrderMark = ByteOrderMark.UTF8; + reader.Accessor.Seek(-1, SeekOrigin.Current); + } + else if ((b1 == 0xFE && b2 == 0xFF) || (b1 == 0xFF && b2 == 0xFE)) + { + if (b1 == 0xFE && b2 == 0xFF) + { + ByteOrderMark = ByteOrderMark.UTF16LittleEndian; + } + else + { + ByteOrderMark = ByteOrderMark.UTF16BigEndian; + } + reader.Accessor.Seek(-2, SeekOrigin.Current); + } + else + { + ByteOrderMark = ByteOrderMark.None; + reader.Accessor.Seek(-4, SeekOrigin.Current); + } + } + while (!reader.EndOfStream) { string line = reader.ReadLine(); diff --git a/CSharp/Libraries/UniversalEditor.Essential/UniversalEditor.Essential.csproj b/CSharp/Libraries/UniversalEditor.Essential/UniversalEditor.Essential.csproj index 2cb1a493..3b34843e 100644 --- a/CSharp/Libraries/UniversalEditor.Essential/UniversalEditor.Essential.csproj +++ b/CSharp/Libraries/UniversalEditor.Essential/UniversalEditor.Essential.csproj @@ -188,6 +188,7 @@ +