Unicode Big Endian Reader

BlitzMax Forums/BlitzMax Programming/Unicode Big Endian Reader

ziggy(Posted 2006) [#1]
This is a unicode big endian stream reader for BlitzMax, as TStream seems to fail reading unicode big endian text files.

Here's the code:

SuperStrict
Type BigEndianReader 

	Field BaseStream:TStream
	Field ReadByteOrder:Int
	Method New()
		ReadByteOrder = True
	End Method
	Method ReadLine:String()
		Local Aux:Byte = 0
		Local str:String = ""
		If BaseStream.Eof() Then Return ""
		If BaseStream.Pos() = 0 Then
			If ReadByteOrder = False Then
				BaseStream.ReadByte()
				BaseStream.ReadByte()				
			End If
		End If
		While not BaseStream.Eof()
			aux = BaseStream.ReadByte()
			If aux=13 Then Exit 
			If aux<>0 Then str = str + Chr(aux)
		Wend
		While Left(str,1) = Chr(10) 
			str = Mid(str,2)
		Wend
		While Right(str,1) = Chr(10)
			str = Left(str,Len(str)-1)
		Wend
		Return str
	End Method
End Type


Here's a sample application:

Local MyStream:TStream = OpenStream("testfile.bmx") 'This opens the text file in unicode Big Endian.
Local Reader:BigEndianReader = New BigEndianReader 'Creates an instance of the Unicode reader
reader.BaseStream = mystream 'We tell the unicode reader wich stream to read of.
reader.ReadByteOrder = False 'We tell the unicode reader to ignore the first two bytes of 'Byte Order' mark

While not Eof(mystream)
  Local theString:String=reader.ReadLine() 'We read a line.
  Print theString
Wend