i try to use the new framework all the way. I don’t have experiences using Xojo.IO.BinaryStream. What do i want? The User can select via Dialog any Textfile and load it into a TextArea. I don’t know the Xojo.Core.TextEncoding of the file. So using this:
Dim f As FolderItem
f = SpecialFolder.Documents.Child(“SaveData.txt”)
If Not f.Exists Then
’ Cannot read the file if it does not exist
Return
End If
Dim errorText As Text
Try
Dim input As TextInputStream
input = TextInputStream.Open(f, TextEncoding.UTF8) // don’t know the Encoding
TextFileArea.Text = input.ReadAll
input.Close
Catch e As IOException
errorText = "File IO Error: " + e.Reason
End Try[/code]
will not be an option. First i need to check the BOM (Byte Order Mark). I thought writing a Function returning Xojo.Core.TextEncoding would be the best. Can someone please help me to write such a Function?
After few hours, i got a working solution. It uses the new Xojo-Framework.
[code]Dim f As FolderItem = GetOpenFolderItem("")
If f <> Nil Then
Try
Dim openFile As New Xojo.IO.FolderItem(f.NativePath.ToText)
Dim b As Xojo.IO.BinaryStream
b = Xojo.IO.BinaryStream.Open(openFile, Xojo.IO.BinaryStream.LockModes.Read)
// first, check for a BOM
Dim startPosition As UInt64 = b.Position
Dim b1 As UInt8 = b.ReadUInt8
Dim b2 As UInt8 = b.ReadUInt8
Dim b3 As UInt8 = b.ReadUInt8
Dim b4 As UInt8 = b.ReadUInt8
Dim enc As Xojo.Core.TextEncoding
If b1 = &hEF And b2 = &hBB And b3 = &hBF Then
// UTF-8
enc = Xojo.Core.TextEncoding.UTF8
ElseIf b1 = &hFF And b2 = &hFE And b3 = &h00 And b4 = &h00 Then
// UTF-32 (LE)
enc = Xojo.Core.TextEncoding.UTF32LittleEndian
ElseIf b1 = &h00 And b2 = &h00 And b3 = &hFE And b4 = &hFF Then
// UTF-32 (BE)
enc = Xojo.Core.TextEncoding.UTF32BigEndian
ElseIf b1 = &hFF And b2 = &hFE And b3 <> &hFE And b4 <> &hFF Then
// UTF-16 (LE)
enc = Xojo.Core.TextEncoding.UTF16LittleEndian
ElseIf b1 = &hFE And b2 = &hFF And b3 <> &hFE And b4 <> &hFF Then
// UTF-16 (BE)
enc = Xojo.Core.TextEncoding.UTF16BigEndian
Else
// reset BinaryStream
b.Position = startPosition
// no BOM; see if it's entirely ASCII.
Dim mb As New Xojo.Core.MemoryBlock(b.Read(b.Length))
Dim i As Integer
Dim maxi As Integer = b.Length-1
For i = 0 To maxi
If mb.UInt8Value(i) > 127 Then Exit
Next
If i > maxi Then
// ASCII
enc = Xojo.Core.TextEncoding.ASCII
Else
// Default: UTF-8
enc = Xojo.Core.TextEncoding.UTF8
End If
End If
b.Close
// open Textfile
Dim input As Xojo.IO.TextInputStream
input = Xojo.IO.TextInputStream.Open(openFile, enc)
TextArea1.Text = input.ReadAll
input.Close
Catch e As IOException
MsgBox "File IO Error: " + e.Reason
End Try