Framework Asiatant and Unicode source

BlitzMax Forums/BlitzMax Programming/Framework Asiatant and Unicode source

The forum is locked. These are the original blitzbasic.com forums, kept so they can still be read. No new topics, replies or edits can be made here. To join in, head over to the Blitz3D+ forums.

@Jim Brown. You have to use textstream, and enable the correct encoding. fore more info on how to detect encoding, you can see the loadtext function in the blitzmax modules, it's a very easy to understand function, and works like a charm loading unicode or ansi encoded files.
i made a little module to read streams ecognizing the encoding, base on the loadtext function:

Type TextFileReader
	Function CreateStream:TTextStream(url:Object,DetectEncoding:Int = True,ForceEncoding:Int = 0 )
		Local Stream:TStream=ReadStream(url) 'This is the base stream
		Local TxStream:TTextStream
		If Stream = Null Then Return Null 'Unable to open URL
		If detectencoding = False Then
			TxStream = TTextStream.Create( Stream,forceencoding)
			Return TxStream
		Else
			TxStream = TTextStream.Create( Stream,EncodingDetector(url))
			Return TxStream
		EndIf
		
	End Function
	Function EncodingDetector:Int(url:Object)
		Local format:Int = 1  'This is to store the encoding, by default LATIN1
		Local Stream:TStream=ReadStream(url) 'This is the base stream
		If not Stream.Eof() 'check if there is data to read
			Local c:Byte=Stream.ReadByte()
			If not Stream.Eof()
				Local d:Byte=Stream.ReadByte()  'Get the firt byte to get the encoding
				If c=$fe and d=$ff  'if its FF and FE (Big endian byte order)
					format=TTextStream.UTF16BE
				Else If c=$ff and d=$fe  'Little endian byte order
					format=TTextStream.UTF16LE
				Else If c=$ef and d=$bb
					If not Stream.Eof()
						Local e:Byte=Stream.ReadByte()
						If e=$bf format=TTextStream.UTF8
					EndIf
				EndIf
			EndIf
		EndIf
		If format = 1
   			 Try
        				Stream.Seek(0) 'this will only work on an unbuffered serial stream
    			Catch err$
    			End Try
  		EndIf


		Return format
	End Function
End Type


making something as simple as:

Local pepe:TTextStream = TextFileReader.CreateStream("file.txt")

will create an apropiate text stream, with the correct encoding. (it will not work for non local files (internet or whatever))