123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503 |
- Namespace std.stream
- Using libc
- Using std.memory
- Using std.collections
- #rem monkeydoc Stream class.
- #end
- Class Stream
- #rem monkeydoc True if no more data can be read from the stream.
- #end
- Property Eof:Bool() Abstract
-
- #rem monkeydoc Current stream position.
-
- In the case of non-seekable streams, `Position` will always be -1.
-
- #end
- Property Position:Int() Abstract
- #rem monkeydoc Current stream length.
-
- In the case of non-seekable streams, `Length` is the number of bytes that can be read from the stream without 'blocking'.
-
- #end
- Property Length:Int() Abstract
-
- #rem monkeydoc Closes the stream.
- #end
- Method Close:Void()
- OnClose()
- _tmpbuf.Discard()
- End
- #rem monkeydoc Seeks to a position in the stream.
-
- In debug builds, a runtime error will occur if the stream is not seekable or `position` is out of range.
-
- @param position The position to seek to.
-
- #end
- Method Seek( position:Int ) Abstract
-
- #rem monkeydoc Reads data from the stream into memory.
-
- Reads `count` bytes of data from the stream into either a raw memory pointer or a databuffer.
-
- Returns the number of bytes actually read.
-
- @param buf A pointer to the memory to read the data into.
-
- @param data The databuffer to read the data into.
-
- @param count The number of bytes to read from the stream.
-
- @return The number of bytes actually read.
-
- #end
- Method Read:Int( buf:Void Ptr,count:Int ) Abstract
-
- Method Read:Int( data:DataBuffer,offset:Int,count:Int )
- DebugAssert( offset>=0 And count>=0 And offset+count<=data.Length )
-
- Return Read( data.Data+offset,count )
- End
-
- #rem monkeydoc Writes data to the stream from memory.
-
- Writes `count` bytes of data to the stream from either a raw memory pointer or a databuffer.
-
- Returns the number of bytes actually written
-
- @param buf A pointer to the memory to write the data from.
- @param data The databuffer to write the data from.
-
- @param count The number of bytes to write to the stream.
-
- @return The number of bytes actually written.
-
- #end
- Method Write:Int( buf:Void Ptr,count:Int ) Abstract
- Method Write:Int( data:DataBuffer,offset:Int,count:Int )
- DebugAssert( offset>=0 And count>=0 And offset+count<=data.Length )
- Return Write( data.Data+offset,count )
- End
- #rem monkeydoc The byte order of the stream.
-
- The default byte order is ByteOrder.BigEndian.
-
- #end
- Property ByteOrder:ByteOrder()
- Return _tmpbuf.ByteOrder
- Setter( byteOrder:ByteOrder )
- _tmpbuf.ByteOrder=byteOrder
- End
-
- #rem monkeydoc Reads as many bytes as possible from a stream into memory.
-
- Continously reads data from a stream until either `count` bytes are read or the end of stream is reached.
-
- Returns the number of bytes read or the data read.
- @param buf memory to read bytes into.
-
- @param data data buffer to read bytes into.
-
- @param count number of bytes to read.
-
- #end
- Method ReadAll:Int( buf:Void Ptr,count:Int )
-
- Local pos:=0
-
- While pos<count
- Local n:=Read( Cast<Byte Ptr>( buf )+pos,count-pos )
- If n<=0 Exit
- pos+=n
- Wend
-
- Return pos
- End
-
- Method ReadAll:Int( data:DataBuffer,offset:Int,count:Int )
-
- Return ReadAll( data.Data+offset,count )
- End
-
- Method ReadAll:DataBuffer( count:Int )
-
- Local data:=New DataBuffer( count )
- Local n:=ReadAll( data,0,count )
- If n>=count Return data
- Local tmp:=data.Slice( 0,n )
- data.Discard()
- Return tmp
- End
-
- Method ReadAll:DataBuffer()
-
- If Length>=0 Return ReadAll( Length-Position )
- Local bufs:=New Stack<DataBuffer>
- Local buf:=New DataBuffer( 4096 ),pos:=0
- Repeat
- pos=ReadAll( buf,0,4096 )
- If pos<4096 Exit
- bufs.Push( buf )
- buf=New DataBuffer( 4096 )
- Forever
- Local len:=bufs.Length * 4096 + pos
- Local data:=New DataBuffer( len )
- pos=0
- For Local buf:=Eachin bufs
- buf.CopyTo( data,0,pos,4096 )
- buf.Discard()
- pos+=4096
- Next
- buf.CopyTo( data,0,pos,len-pos )
- buf.Discard()
- Return data
- End
-
- #rem monkeydoc Reads data from the filestream and throws it away.
- @param count The number of bytes to skip.
-
- @return The number of bytes actually skipped.
-
- #end
- Method Skip:Int( count:Int )
- Local tmp:=libc.malloc( count )
- Local n:=Read( tmp,count )
- libc.free( tmp )
- Return n
- End
-
- #rem monkeydoc Reads a byte from the stream.
-
- @return The byte read.
-
- #end
- Method ReadByte:Byte()
- If Read( _tmpbuf.Data,1 )=1 Return _tmpbuf.PeekByte( 0 )
-
- Return 0
- End
-
- #rem monkeydoc Reads an unsigned byte from the stream.
-
- @return The ubyte read.
-
- #end
- Method ReadUByte:UByte()
- If Read( _tmpbuf.Data,1 )=1 Return _tmpbuf.PeekUByte( 0 )
-
- Return 0
- End
-
- #rem monkeydoc Reads a 16 bit short from the stream.
-
- @return The short read.
-
- #end
- Method ReadShort:Short()
- If ReadAll( _tmpbuf.Data,2 )=2 Return _tmpbuf.PeekShort( 0 )
-
- Return 0
- End
-
- #rem monkeydoc Reads a 16 bit unsigned short from the stream.
-
- @return The ushort read.
-
- #end
- Method ReadUShort:UShort()
- If ReadAll( _tmpbuf.Data,2 )=2 Return _tmpbuf.PeekUShort( 0 )
-
- Return 0
- End
-
- #rem monkeydoc Reads a 32 bit int from the stream.
-
- @return The int read.
-
- #end
- Method ReadInt:Int()
- If ReadAll( _tmpbuf.Data,4 )=4 Return _tmpbuf.PeekInt( 0 )
-
- Return 0
- End
-
- #rem monkeydoc Reads a 32 bit unsigned int from the stream.
-
- @return The uint read.
-
- #end
- Method ReadUInt:UInt()
- If ReadAll( _tmpbuf.Data,4 )=4 Return _tmpbuf.PeekUInt( 0 )
-
- Return 0
- End
-
- #rem monkeydoc Reads a 32 bit long from the stream.
-
- @return The long read.
-
- #end
- Method ReadLong:Long()
- If ReadAll( _tmpbuf.Data,8 )=8 Return _tmpbuf.PeekLong( 0 )
- Return 0
- End
-
- #rem monkeydoc Reads a 32 bit unsigned long from the stream.
-
- @return The ulong read.
-
- #end
- Method ReadULong:ULong()
- If ReadAll( _tmpbuf.Data,8 )=8 Return _tmpbuf.PeekULong( 0 )
- Return 0
- End
-
- #rem monkeydoc Reads a 32 bit float from the stream.
-
- @return The float read.
-
- #end
- Method ReadFloat:Float()
- If ReadAll( _tmpbuf.Data,4 )=4 Return _tmpbuf.PeekFloat( 0 )
- Return 0
- End
-
- #rem monkeydoc Reads a 64 bit double from the stream.
-
- @return The double read.
-
- #end
- Method ReadDouble:Double()
- If ReadAll( _tmpbuf.Data,8 )=8 Return _tmpbuf.PeekDouble( 0 )
- Return 0
- End
-
- #rem monkeydoc Reads the entire stream into a string.
- #end
- Method ReadString:String()
- Local data:=ReadAll()
- Local str:=data.PeekString( 0 )
- data.Discard()
- Return str
- End
-
- #rem monkeydoc Reads a size prefixed string from the stream.
-
- Reads an int from the stream, then a string from that many bytes.
- @return the string read.
-
- #end
- Method ReadSizedString:String()
- Local n:=ReadInt()
- Local data:=ReadAll( n )
- Local str:=data.PeekString( 0 )
- data.Discard()
- Return str
- End
-
- #rem monkeydoc Reads a null terminated cstring from the stream.
-
- @return the string read.
-
- #end
- Method ReadCString:String()
- Local buf:=New Stack<Byte>
- While Not Eof
- Local chr:=ReadByte()
- If Not chr Exit
- buf.Push( chr )
- Wend
- Return String.FromCString( buf.Data.Data,buf.Length )
- End
-
- #rem monkeydoc Writes a byte to the stream.
-
- @param data The byte to write.
-
- #end
- Method WriteByte( data:Byte )
- _tmpbuf.PokeByte( 0,data )
- Write( _tmpbuf.Data,1 )
- End
-
- #rem monkeydoc Write an unsigned byte to the stream.
-
- @param data The ubyte to write.
- #end
- Method WriteUByte( data:UByte )
- _tmpbuf.PokeUByte( 0,data )
- Write( _tmpbuf.Data,1 )
- End
-
- #rem monkeydoc Writes a 16 bit short to the stream.
-
- @param data The short to write.
- #end
- Method WriteShort( data:Short )
- _tmpbuf.PokeShort( 0,data )
- Write( _tmpbuf.Data,2 )
- End
-
- #rem monkeydoc Writes a 16 bit unsigned short to the stream.
-
- @param data The ushort to write.
- #end
- Method WriteUShort( data:UShort )
- _tmpbuf.PokeUShort( 0,data )
- Write( _tmpbuf.Data,2 )
- End
-
- #rem monkeydoc Writes a 32 bit int to the stream.
-
- @param data The int to write.
- #end
- Method WriteInt( data:Int )
- _tmpbuf.PokeInt( 0,data )
- Write( _tmpbuf.Data,4 )
- End
-
- #rem monkeydoc Writes a 32 bit unsigned int to the stream.
-
- @param data The uint to write.
- #end
- Method WriteUInt( data:UInt )
- _tmpbuf.PokeUInt( 0,data )
- Write( _tmpbuf.Data,4 )
- End
-
- #rem monkeydoc Writes a 64 bit long to the stream.
-
- @param data The long to write.
- #end
- Method WriteLong( data:Long )
- _tmpbuf.PokeLong( 0,data )
- Write( _tmpbuf.Data,8 )
- End
-
- #rem monkeydoc Writes a 64 bit unsigned long to the stream.
-
- @param data The ulong to write.
- #end
- Method WriteULong( data:ULong )
- _tmpbuf.PokeULong( 0,data )
- Write( _tmpbuf.Data,8 )
- End
-
- #rem monkeydoc Writes a 32 bit float to the stream,
-
- @param data The float to write.
- #end
- Method WriteFloat:Void( data:Float )
- _tmpbuf.PokeFloat( 0,data )
- Write( _tmpbuf.Data,4 )
- End
-
- #rem monkeydoc Writes a 64 bit double to the stream.
-
- @param data The double to write.
- #end
- Method WriteDouble( data:Double )
- _tmpbuf.PokeDouble( 0,data )
- Write( _tmpbuf.Data,8 )
- End
-
- #rem monkeydoc Writes a string to the stream (NOT null terminated).
- @param str The string to write.
-
- #end
- Method WriteString( str:String )
- Local buf:=New DataBuffer( str.CStringLength )
- buf.PokeString( 0,str )
- Write( buf,0,buf.Length )
- buf.Discard()
- End
-
- #rem monkeydoc Writes a size prefixed string to the stream.
-
- Writes an int containing the size of the string to the stream, followed the string itself.
-
- #end
- Method WriteSizedString( str:String )
- WriteInt( str.CStringLength )
- WriteString( str )
- End
-
- #rem monkeydoc Writes a null terminated cstring to the stream.
-
- @param str The string to write.
-
- #end
- Method WriteCString( str:String )
- WriteString( str )
- WriteByte( 0 )
- End
-
- #rem monkeydoc Opens a stream
-
- `mode` should be "r" for read, "w" for write or "rw" for read/write.
-
- @param mode The mode to open the stream in: "r", "w" or "rw"
-
- #end
- Function Open:Stream( path:String,mode:String )
- Local i:=path.Find( "::" )
- If i=-1 Return FileStream.Open( path,mode )
-
- Local proto:=path.Slice( 0,i )
- Local ipath:=path.Slice( i+2 )
- Return OpenFuncs[proto]( proto,ipath,mode )
- End
-
- #rem monkeydoc @hidden
- #end
- Alias OpenFunc:Stream( proto:String,path:String,mode:String )
-
- #rem monkeydoc @hidden
- #end
- Const OpenFuncs:=New StringMap<OpenFunc>
-
- Protected
-
- Method New()
- _tmpbuf=New DataBuffer( 8,ByteOrder.LittleEndian )
- End
-
- Method OnClose() Abstract
-
- Private
-
- Field _tmpbuf:DataBuffer
- End
|