<?xml version="1.0" encoding="utf-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom">
	<channel>
		<title><![CDATA[GMan's Mods & Stuff — TIrrStream and TIrrStreamReader]]></title>
		<link>https://gprogs.com/viewtopic.php?id=255</link>
		<atom:link href="https://gprogs.com/extern.php?action=feed&amp;tid=255&amp;type=rss" rel="self" type="application/rss+xml" />
		<description><![CDATA[The most recent posts in TIrrStream and TIrrStreamReader.]]></description>
		<lastBuildDate>Fri, 27 Jul 2007 01:41:46 +0000</lastBuildDate>
		<generator>PunBB</generator>
		<item>
			<title><![CDATA[Re: TIrrStream and TIrrStreamReader]]></title>
			<link>https://gprogs.com/viewtopic.php?pid=1061#p1061</link>
			<description><![CDATA[<p>Whee!&nbsp; I just made some massive changes, but for the better.&nbsp; I&#039;m almost done with the stream writer too.&nbsp; Stick around.&nbsp; :-3</p>]]></description>
			<author><![CDATA[null@example.com (ninjarat)]]></author>
			<pubDate>Fri, 27 Jul 2007 01:41:46 +0000</pubDate>
			<guid>https://gprogs.com/viewtopic.php?pid=1061#p1061</guid>
		</item>
		<item>
			<title><![CDATA[TIrrStream and TIrrStreamReader]]></title>
			<link>https://gprogs.com/viewtopic.php?pid=1059#p1059</link>
			<description><![CDATA[<p>Check it out!&nbsp; More excerpts from my engine.&nbsp; This particular class doesn&#039;t use any other objects in my engine, so you can use it as is.&nbsp; Oh, except it does get the file system manager from my TIrrContainer object.&nbsp; You can get around that by replacing &#039;IrrContainer.IrrFileSys&#039; with &#039;device.getFileSystem()&#039;.&nbsp; It basically creates an advanced blitz-like stream reader based in the Irrlicht file system manager.</p><p>Special thanks to Mark and Simon.&nbsp; I could never have made this if I hadn&#039;t had a good look at their stream module first.</p><p>EDIT:&nbsp; Fixed an issue with ReadLine.&nbsp; When reading last line of text from the read target, an attempt to access null memory error occurred.&nbsp; I found a spot where I was freeing some memory when I shouldn&#039;t have.&nbsp; All cleared up now.&nbsp; I had to fix it the hard way, because the debug build worked for some reason.&nbsp; Probably a difference in garbage collection.</p><p>EDIT:&nbsp; Restructured for the addition of a stream writer object.&nbsp; Yay!&nbsp; Same basic use, mostly just stuff behind the scenes changed.&nbsp; You&#039;ll find that with the new version you&#039;ll have to use TIrrStreamReader(irrstreamobject) to get access to the functions, but these changes will make it more efficient in the long run.&nbsp; I swear.</p><div class="codebox"><pre><code>&#039;RCD Software (TM)
&#039;by ninjarat

&#039;This is part of the RCD Irrlicht Addons


Const STREAM_TARGET_UNDEFINED=-1
Const STREAM_TARGET_FILE=0
Const STREAM_TARGET_RAM=1


&#039;irrlicht stream base class
Type TIrrStreamBase
    Field irrstream:TIrr_IUnknown
    Field pos,size,fname$
    Field endoffile=False
    Field irrstreamtype=-1
    
    Function create:TIrrStreamBase(file$)
        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 Destroy()
        If irrstream Then irrstream.drop
    End Method
    
    Method ToString$()
        str$=&quot;Stream Object~n~r&quot;
        If IReadFile(irrstream) Then
            str:+&quot;type:STREAM_TYPE_READ~n~r&quot;
        Else If IWriteFile(irrstream) Then
            str:+&quot;type:STREAM_TYPE_WRITE~n~r&quot;
        Else
            str:+&quot;type:STREAM_TYPE_NULLOBJECT~n~r&quot;
        End If
        str:+&quot;pos:&quot;+pos+&quot;~n~rsize:&quot;+size+&quot;~n~rname:&quot;+..
         fname+&quot;~n~reof:&quot;+TrFs(endoffile)+&quot;~n~rtarget:&quot;
        If irrstreamtype=0 Then
            str:+&quot;STREAM_TARGET_FILE~n~r&quot;
        Else If irrstreamtype=1 Then
            str:+&quot;STREAM_TARGET_RAM~n~r&quot;
        Else
            str:+&quot;STREAM_TARGET_UNDEFINED~n~r&quot;
        End If
        Return str
    End Method
End Type

&#039;extends irrlicht stream base in to a stream reader
Type TIrrStreamReader Extends TIrrStreamBase
    Function create:TIrrStreamBase(file$)
        read:TIrrStreamReader=New TIrrStreamReader
        read._init1(file,0)
        Return read
    End Function
    
    Function createFromRAM:TIrrStreamBase(name$,buffer:Byte Ptr,buflength)
        read:TIrrStreamReader=New TIrrStreamReader
        read._init2(name,buffer,buflength,1)
        Return read
    End Function
    
    Method _init1(file$,mode)
        _setreader TIrrContainer.IrrFileSys.createAndOpenFile(file)
        fname=_reader().getFileName()
        pos=_reader().getPos()
        size=_reader().getSize()
        irrstreamtype=mode
    End Method
    
    Method _init2(name$,buffer:Byte Ptr,buflength,mode)
        _setreader TIrrContainer.IrrFileSys.createMemoryReadFile(buffer,buflength,name)
        fname=_reader().getFileName()
        pos=_reader().getPos()
        size=_reader().getSize()
        irrstreamtype=mode
    End Method
    
    Method Read(buffer:Byte Ptr,numbytes)
        bytesread=_reader().read(buffer,numbytes)
        pos=_reader().getPos()
        If bytesread&lt;numbytes Or pos&gt;=size Then
            endoffile=True
            Return False
        End If
        Return True
    End Method
    
    Method Seek(pos,relativemove=False)
        If Not _reader().seek(pos,relativemove) Then Return False
        Return True
    End Method
    
    Method Eof()
        Return endoffile
    End Method
    
    Method GetSize()
        size=_reader().getSize()
        Return size
    End Method
    
    Method GetPos()
        pos=_reader().getPos()
        Return pos
    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,4) Then n=Null
        Return n
    End Method
    
    Method ReadString$(numbytes)
        Assert numbytes&gt;0, &quot;Byte length must be greater than 0&quot;
        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&lt;&gt;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

&#039;extends irrlicht stream base in to a stream writer
&#039;not yet added, as you can see.
&#039;Type TIrrStreamWriter Extends TIrrStreamBase
&#039;    Function create:TIrrStreamWriter()
&#039;    End Function
&#039;End Type

Const STREAM_TYPE_READ=0
Const STREAM_TYPE_WRITE=1

Function CreateIrrStream:TIrrStreamBase(file$,mode=STREAM_TYPE_READ)
    Select mode
        Case STREAM_TYPE_READ  Return TIrrStreamReader.Create(file)
        Case STREAM_TYPE_WRITE Return Null &#039;not yet supported
    End Select
End Function

Function CreateIrrRAMStream:TIrrStreamBase(name$,buffer:Byte Ptr,buflength,mode=STREAM_TYPE_READ)
    Select mode
        Case STREAM_TYPE_READ  Return TIrrStreamReader.CreateFromRAM(name,buffer,buflength)
        Case STREAM_TYPE_WRITE Return Null &#039;not yet supported
    End Select
End Function</code></pre></div>]]></description>
			<author><![CDATA[null@example.com (ninjarat)]]></author>
			<pubDate>Thu, 26 Jul 2007 04:19:26 +0000</pubDate>
			<guid>https://gprogs.com/viewtopic.php?pid=1059#p1059</guid>
		</item>
	</channel>
</rss>
