Reading / Writing Unicode streams

BlitzMax Forums/BlitzMax Programming/Reading / Writing Unicode streams

neilo(Posted 2006) [#1]
Unfortunately, BlitzMAX doesn't encode text in streams as Unicode, which Mark says was a bit of an oversight.

Here, then, are two function that I use to write a string into a stream as unicode, and to retrieve it:

Function WriteStringW(stream:TStream,str:String)
	Local shorts:Short Ptr
	
	shorts=str.ToWString()
	WriteInt stream,Len(str)	' Number of shorts to write
	For Local i:Int=0 To Len(str)-1
		WriteShort stream,shorts[i]
	Next
	MemFree shorts
End Function

Function ReadStringW:String(stream:TStream)
	Local numShorts:Int
	Local str:String
	Local shorts:Short[]
	
	numShorts=ReadInt(stream)
	shorts=New Short[numShorts]
	For Local i:Int=0 To numShorts-1
		shorts[i]=ReadShort(stream)
	Next
	str=String.FromShorts(shorts,numShorts)
	Return str
End Function


There's nothing particularly fancy about these functions, other than they work.

Internally, all BlitzMAX strings are Unicode, so the functions are pretty transparent, as far as code in concerned. I write the number of unicode characters first, then the actual characters, rather than having a null (or double-null, in this case) terminated string. Mostly a matter of convenience for me, and it eliminates the possibility of buffer overflows.

Enjoy!

Neil


FlameDuck(Posted 2006) [#2]
Nice. You should probably add this to the code archives ASAP if you haven't already...


N(Posted 2006) [#3]
Revised version, doesn't make any unneccessary calls to the stream's methods and doesn't use wrapper functions.

Function WriteStringW( stream:TStream, str:String )
  stream.WriteInt( str.Length )
  If str.Length = 0 Then Return
  Local shorts:Short Ptr = str.ToWString( )
  stream.WriteBytes( shorts, str.Length*2 )
  MemFree( shorts )
End Function

Function ReadStringW$( stream:TStream )
  Local ln% = stream.ReadInt( )
  If ln = 0 Then Return ""
  Local shorts:Short[ln]
  stream.ReadBytes( shorts, ln*2 )
  Return String.FromShorts( shorts, ln )
End Function



skidracer(Posted 2006) [#4]
I was considering something similar with ReadLineW and WriteLineW commands to make it complete.


neilo(Posted 2006) [#5]
@Noel:

<copy> <paste>

Nice :)