Topic: Irrlicht Streams Complete 1.0
Yay! Finished. And with the memory reader you can even access your Incbin via IncbinPtr()!
'RCD Software (TM)
'by ninjarat
'This is part of the RCD Irrlicht Addons
Const STREAM_TARGET_UNDEFINED=-1
Const STREAM_TARGET_FILE=0
Const STREAM_TARGET_RAM=1
Const STREAM_TYPE_READ=0
Const STREAM_TYPE_WRITE=1
'irrlicht stream base class
Type TIrrStreamBase
Field irrstream:TIrr_IUnknown
Field pos,size,fname$
Field endoffile=False
Field irrstreamtype=-1
Function create:TIrrStreamBase(file$,append=False)
Return New TIrrStreamBase
End Function
Function createFromRAM:TIrrStreamBase(name$,buffer:Byte Ptr,buflength)
Return New TIrrStreamBase
End Function
Method _reader:IReadFile()
Return IReadFile(irrstream)
End Method
Method _writer:IWriteFile()
Return IWriteFile(irrstream)
End Method
Method _setreader(r:IReadFile)
irrstream=r
End Method
Method _setwriter(w:IWriteFile)
irrstream=w
End Method
Method GetType()
Return irrstreamtype
End Method
Method Eof()
Return endoffile
End Method
Method GetSize()
Return size
End Method
Method GetPos()
Return pos
End Method
Method GetFName$()
Return fname
End Method
Method Seek(pos,relativemove=False)
If _reader() Then
If Not _reader().seek(pos,relativemove) Then Return False Else pos=_reader().getPos()
Else If _writer() Then
If Not _writer().seek(pos,relativemove) Then Return False Else pos=_writer().getPos()
Else
Return False
End If
Return True
End Method
Method Destroy()
If irrstream Then irrstream.drop
End Method
Method ToString$()
str$="Stream Object~n~r"
If IReadFile(irrstream) Then
str:+"type:STREAM_TYPE_READ~n~r"
Else If IWriteFile(irrstream) Then
str:+"type:STREAM_TYPE_WRITE~n~r"
Else
str:+"type:STREAM_TYPE_NULLOBJECT~n~r"
End If
str:+"pos:"+pos
If IReadFile(irrstream) Then
str:+"~n~rsize:"+size
Else
str:+"~n~rsize:NON_TERMINATED"
End If
str:+"~n~rname:"+fname+"~n~reof:"+TrFs(endoffile)+"~n~rtarget:"
If irrstreamtype=0 Then
str:+"STREAM_TARGET_FILE~n~r"
Else If irrstreamtype=1 Then
str:+"STREAM_TARGET_RAM~n~r"
Else
str:+"STREAM_TARGET_UNDEFINED~n~r"
End If
Return str
End Method
End Type
'extends irrlicht stream base in to a stream reader
Type TIrrStreamReader Extends TIrrStreamBase
Function create:TIrrStreamBase(file$,append=False)
read:TIrrStreamReader=New TIrrStreamReader
read._initstd(file)
Return read
End Function
Function createFromRAM:TIrrStreamBase(name$,buffer:Byte Ptr,buflength)
read:TIrrStreamReader=New TIrrStreamReader
read._initram(name,buffer,buflength)
Return read
End Function
Method _initstd(file$)
_setreader TIrrContainer.IrrFileSys.createAndOpenFile(file)
fname=_reader().getFileName()
pos=_reader().getPos()
size=_reader().getSize()
irrstreamtype=0
End Method
Method _initram(name$,buffer:Byte Ptr,buflength)
_setreader TIrrContainer.IrrFileSys.createMemoryReadFile(buffer,buflength,name)
fname=_reader().getFileName()
pos=_reader().getPos()
size=_reader().getSize()
irrstreamtype=1
End Method
Method Read(buffer:Byte Ptr,numbytes)
bytesread=_reader().read(buffer,numbytes)
pos=_reader().getPos()
If bytesread<numbytes Or pos>=size Then
endoffile=True
Return False
End If
Return True
End Method
Method ReadByte:Byte()
If endoffile Then Return Null
n:Byte=0
If Not Read(Varptr n,1) Then n=Null
Return n
End Method
Method ReadShort:Short()
If endoffile Then Return Null
n:Short=0
If Not Read(Varptr n,2) Then n=Null
Return n
End Method
Method ReadInt()
If endoffile Then Return Null
n=0
If Not Read(Varptr n,4) Then n=Null
Return n
End Method
Method ReadLong:Long()
If endoffile Then Return Null
n:Long=0
If Not Read(Varptr n,8) Then n=Null
Return n
End Method
Method ReadFloat#()
If endoffile Then Return Null
n#=0
If Not Read(Varptr n,4) Then n=Null
Return n
End Method
Method ReadDouble!()
If endoffile Then Return Null
n!=0
If Not Read(Varptr n,8) Then n=Null
Return n
End Method
Method ReadString$(numbytes)
Assert numbytes>0, "Byte length must be greater than 0"
If endoffile Then Return Null
Local buffer:Byte[numbytes]
If Not Read(buffer,numbytes) Then n=Null
Return String.FromBytes(buffer,numbytes)
End Method
Method ReadLine$()
If endoffile Then Return Null
Local buffer:Byte[1024],str$,char:Byte
Repeat
If Not Read(Varptr char,1) Then Exit
If char=0 Then Exit
If char=10 Then Exit
If char=13 Then Continue
buffer[posi]=char
posi:+1
If p<>buffer.length Then Continue
str:+String.FromBytes(buffer,posi)
posi=0
Forever
If posi Then str:+String.FromBytes(buffer,posi)
Return str
End Method
End Type
'extends irrlicht stream base in to a stream writer
Type TIrrStreamWriter Extends TIrrStreamBase
Function create:TIrrStreamBase(file$,append=False)
write:TIrrStreamWriter=New TIrrStreamWriter
write._initstd(file,append)
Return write
End Function
'override with null returning function, because RAM file writing
'isn't supported by irrlicht.
Function createFromRAM:TIrrStreamBase(name$,buffer:Byte Ptr,buflength)
Return Null
End Function
Method _initstd(file$,append,maxsize=100000)
_setwriter TIrrContainer.IrrFileSys.createAndWriteFile(file,append)
fname=_writer().getFileName()
pos=_writer().getPos()
size=maxsize
irrstreamtype=0
End Method
Method Write(buffer:Byte Ptr,numbytes)
byteswritten=_writer().write(buffer,numbytes)
pos=_writer().getPos()
If byteswritten<numbytes Then Return False
Return True
End Method
Method WriteByte(n:Byte)
q:Byte=n
Return Write(Varptr q,1)
End Method
Method WriteShort(n:Short)
q:Short=n
Return Write(Varptr q,2)
End Method
Method WriteInt(n)
q=n
Return Write(Varptr q,4)
End Method
Method WriteLong(n:Long)
q:Long=n
Return Write(Varptr q,8)
End Method
Method WriteFloat(n#)
q#=n
Return Write(Varptr q,4)
End Method
Method WriteDouble(n!)
q!=n
Return Write(Varptr q,8)
End Method
Method WriteString(str$)
Local s:Byte Ptr
s=str.ToCString()
Return Write(s,str.length)
MemFree s
End Method
End Type
Function CreateIrrStream:TIrrStreamBase(file$,append=False,mode=STREAM_TYPE_READ)
Select mode
Case STREAM_TYPE_READ Return TIrrStreamReader.Create(file)
Case STREAM_TYPE_WRITE Return TIrrStreamWriter.Create(file,append)
End Select
End Function
Function CreateIrrRAMStreamReader:TIrrStreamBase(name$,buffer:Byte Ptr,buflength)
Return TIrrStreamReader.CreateFromRAM(name,buffer,buflength)
End Function