瀏覽代碼

Added stream append write mode.

woollybah 6 年之前
父節點
當前提交
7c14de0faa

+ 3 - 3
endianstream.mod/endianstream.bmx

@@ -145,12 +145,12 @@ Function LittleEndianStream:TStream( stream:TStream )
 End Function
 End Function
 
 
 Type TXEndianStreamFactory Extends TStreamFactory
 Type TXEndianStreamFactory Extends TStreamFactory
-	Method CreateStream:TStream( url:Object,proto$,path$,readable:Int,writeable:Int ) Override
+	Method CreateStream:TStream( url:Object,proto$,path$,readable:Int,writeMode:Int ) Override
 		Select proto$
 		Select proto$
 		Case "bigendian"
 		Case "bigendian"
-			Return TXEndianStream.BigEndian( OpenStream(path,readable,writeable) )
+			Return TXEndianStream.BigEndian( OpenStream(path,readable,writeMode) )
 		Case "littleendian"
 		Case "littleendian"
-			Return TXEndianStream.LittleEndian( OpenStream(path,readable,writeable) )
+			Return TXEndianStream.LittleEndian( OpenStream(path,readable,writeMode) )
 		End Select
 		End Select
 	End Method
 	End Method
 End Type
 End Type

+ 1 - 1
httpstream.mod/httpstream.bmx

@@ -17,7 +17,7 @@ Import BRL.SocketStream
 
 
 Type THTTPStreamFactory Extends TStreamFactory
 Type THTTPStreamFactory Extends TStreamFactory
 
 
-	Method CreateStream:TStream( url:Object,proto$,path$,readable:Int,writeable:Int ) Override
+	Method CreateStream:TStream( url:Object,proto$,path$,readable:Int,writeMode:Int ) Override
 		If proto="http"
 		If proto="http"
 
 
 			Local i:Int=path.Find( "/",0 ),server$,file$
 			Local i:Int=path.Find( "/",0 ),server$,file$

+ 8 - 7
ramstream.mod/ramstream.bmx

@@ -59,13 +59,13 @@ Type TRamStream Extends TStream
 		Return count
 		Return count
 	End Method
 	End Method
 
 
-	Function Create:TRamStream( buf:Byte Ptr,size:Long,readable,writeable )
+	Function Create:TRamStream( buf:Byte Ptr,size:Long,readable,writeMode )
 		Local stream:TRamStream=New TRamStream
 		Local stream:TRamStream=New TRamStream
 		stream._pos=0
 		stream._pos=0
 		stream._size=size
 		stream._size=size
 		stream._buf=buf
 		stream._buf=buf
 		stream._read=readable
 		stream._read=readable
-		stream._write=writeable
+		stream._write=writeMode
 		Return stream
 		Return stream
 	End Function
 	End Function
 
 
@@ -80,17 +80,18 @@ A ram stream extends a stream object so can be used anywhere a stream is expecte
 Be careful when working with ram streams, as any attempt to access memory
 Be careful when working with ram streams, as any attempt to access memory
 which has not been allocated to your application can result in a runtime crash.
 which has not been allocated to your application can result in a runtime crash.
 End Rem
 End Rem
-Function CreateRamStream:TRamStream( ram:Byte Ptr,size:Long,readable,writeable )
-	Return TRamStream.Create( ram,size,readable,writeable )
+Function CreateRamStream:TRamStream( ram:Byte Ptr,size:Long,readable,writeMode )
+	Assert writeMode <> WRITE_MODE_APPEND, "Ram Streams cannot be appended"
+	Return TRamStream.Create( ram,size,readable,writeMode )
 End Function
 End Function
 
 
 Type TRamStreamFactory Extends TStreamFactory
 Type TRamStreamFactory Extends TStreamFactory
-	Method CreateStream:TRamStream( url:Object,proto$,path$,readable,writeable ) Override
-		If proto="incbin" And writeable=False
+	Method CreateStream:TRamStream( url:Object,proto$,path$,readable,writeMode ) Override
+		If proto="incbin" And Not writeMode
 			Local buf:Byte Ptr=IncbinPtr( path )
 			Local buf:Byte Ptr=IncbinPtr( path )
 			If Not buf Return
 			If Not buf Return
 			Local size:Long=IncbinLen( path )
 			Local size:Long=IncbinLen( path )
-			Return TRamStream.Create( buf,size,readable,writeable )
+			Return TRamStream.Create( buf,size,readable,writeMode )
 		EndIf
 		EndIf
 	End Method
 	End Method
 End Type
 End Type

+ 1 - 1
socketstream.mod/socketstream.bmx

@@ -77,7 +77,7 @@ Type TSocketStream Extends TStream
 End Type
 End Type
 
 
 Type TSocketStreamFactory Extends TStreamFactory
 Type TSocketStreamFactory Extends TStreamFactory
-	Method CreateStream:TSocketStream( url:Object,proto$,path$,readable:Int,writeable:Int ) Override
+	Method CreateStream:TSocketStream( url:Object,proto$,path$,readable:Int,writeMode:Int ) Override
 		If proto$="tcp"
 		If proto$="tcp"
 			Local i:Int=path.Find( ":",0 ),server$,port:Int
 			Local i:Int=path.Find( ":",0 ),server$,port:Int
 			If i>=0 Return TSocketStream.CreateClient( path[..i],Int(path[i+1..]) )
 			If i>=0 Return TSocketStream.CreateClient( path[..i],Int(path[i+1..]) )

+ 42 - 13
stream.mod/stream.bmx

@@ -6,12 +6,14 @@ bbdoc: Streams/Streams
 End Rem
 End Rem
 Module BRL.Stream
 Module BRL.Stream
 
 
-ModuleInfo "Version: 1.10"
+ModuleInfo "Version: 1.11"
 ModuleInfo "Author: Mark Sibly"
 ModuleInfo "Author: Mark Sibly"
 ModuleInfo "License: zlib/libpng"
 ModuleInfo "License: zlib/libpng"
 ModuleInfo "Copyright: Blitz Research Ltd"
 ModuleInfo "Copyright: Blitz Research Ltd"
 ModuleInfo "Modserver: BRL"
 ModuleInfo "Modserver: BRL"
 
 
+ModuleInfo "History: 1.11"
+ModuleInfo "History: Streams can now be opened for appending with the new WRITE_MODE_APPEND write mode."
 ModuleInfo "History: 1.10"
 ModuleInfo "History: 1.10"
 ModuleInfo "History: Module is now SuperStrict"
 ModuleInfo "History: Module is now SuperStrict"
 ModuleInfo "History: 1.09 Release"
 ModuleInfo "History: 1.09 Release"
@@ -587,6 +589,7 @@ Type TCStream Extends TStream
 
 
 	Const MODE_READ:Int=1
 	Const MODE_READ:Int=1
 	Const MODE_WRITE:Int=2
 	Const MODE_WRITE:Int=2
+	Const MODE_APPEND:Int = 4
 	
 	
 	Field _pos:Long,_size:Long,_mode:Int
 	Field _pos:Long,_size:Long,_mode:Int
 	Field _cstream:Byte Ptr
 	Field _cstream:Byte Ptr
@@ -643,14 +646,20 @@ Type TCStream Extends TStream
 	Rem
 	Rem
 	bbdoc: Create a TCStream from a 'C' filename
 	bbdoc: Create a TCStream from a 'C' filename
 	End Rem
 	End Rem
-	Function OpenFile:TCStream( path$,readable:Int,writeable:Int )
+	Function OpenFile:TCStream( path$,readable:Int,writeMode:Int )
 		Local Mode$,_mode:Int
 		Local Mode$,_mode:Int
-		If readable And writeable
+		If readable And writeMode = WRITE_MODE_OVERWRITE
 			Mode="r+b"
 			Mode="r+b"
 			_mode=MODE_READ|MODE_WRITE
 			_mode=MODE_READ|MODE_WRITE
-		Else If writeable
+		Else If readable And writeMode = WRITE_MODE_APPEND
+			Mode="a+b"
+			_mode=MODE_READ|MODE_APPEND
+		Else If writeMode = WRITE_MODE_OVERWRITE
 			Mode="wb"
 			Mode="wb"
 			_mode=MODE_WRITE
 			_mode=MODE_WRITE
+		Else If writeMode = WRITE_MODE_APPEND
+			Mode="ab"
+			_mode=MODE_APPEND
 		Else
 		Else
 			Mode="rb"
 			Mode="rb"
 			_mode=MODE_READ
 			_mode=MODE_READ
@@ -658,7 +667,7 @@ Type TCStream Extends TStream
 		path=path.Replace( "\","/" )
 		path=path.Replace( "\","/" )
 		Local cstream:Byte Ptr=fopen_( path,Mode )
 		Local cstream:Byte Ptr=fopen_( path,Mode )
 ?Linux
 ?Linux
-		If (Not cstream) And (Not writeable)
+		If (Not cstream) And (Not writeMode)
 			path=CasedFileName(path)
 			path=CasedFileName(path)
 			If path cstream=fopen_( path,Mode )
 			If path cstream=fopen_( path,Mode )
 		EndIf
 		EndIf
@@ -724,17 +733,17 @@ Type TStreamFactory
 	
 	
 	If @url is not a string, both @proto and @path will be Null.
 	If @url is not a string, both @proto and @path will be Null.
 	End Rem
 	End Rem
-	Method CreateStream:TStream( url:Object,proto$,path$,readable:Int,writeable:Int ) Abstract
+	Method CreateStream:TStream( url:Object,proto$,path$,readable:Int,writeMode:Int ) Abstract
 
 
 End Type
 End Type
 
 
 Rem
 Rem
-bbdoc: Open a stream for reading/writing
+bbdoc: Open a stream for reading/writing/appending
 returns: A stream object
 returns: A stream object
-about: All streams created by #OpenStream, #ReadStream or #WriteStream should eventually be
+about: All streams created by #OpenStream, #ReadStream, #WriteStream or #AppendStream should eventually be
 closed using #CloseStream.
 closed using #CloseStream.
 End Rem
 End Rem
-Function OpenStream:TStream( url:Object,readable:Int=True,writeable:Int=True )
+Function OpenStream:TStream( url:Object,readable:Int=True,writeMode:Int=WRITE_MODE_OVERWRITE )
 
 
 	Local stream:TStream=TStream( url )
 	Local stream:TStream=TStream( url )
 	If stream
 	If stream
@@ -744,7 +753,7 @@ Function OpenStream:TStream( url:Object,readable:Int=True,writeable:Int=True )
 	Local str$=String( url ),proto$,path$
 	Local str$=String( url ),proto$,path$
 	If str
 	If str
 		Local i:Int=str.Find( "::",0 )
 		Local i:Int=str.Find( "::",0 )
-		If i=-1 Return TCStream.OpenFile( str,readable,writeable )
+		If i=-1 Return TCStream.OpenFile( str,readable,writeMode )
 		proto$=str[..i].ToLower()
 		proto$=str[..i].ToLower()
 		path$=str[i+2..]
 		path$=str[i+2..]
 	EndIf
 	EndIf
@@ -752,7 +761,7 @@ Function OpenStream:TStream( url:Object,readable:Int=True,writeable:Int=True )
 	Local factory:TStreamFactory=stream_factories
 	Local factory:TStreamFactory=stream_factories
 	
 	
 	While factory
 	While factory
-		Local stream:TStream=factory.CreateStream( url,proto,path,readable,writeable )
+		Local stream:TStream=factory.CreateStream( url,proto,path,readable,writeMode )
 		If stream Return stream
 		If stream Return stream
 		factory=factory._succ
 		factory=factory._succ
 	Wend
 	Wend
@@ -771,11 +780,21 @@ End Function
 Rem
 Rem
 bbdoc: Open a stream for writing
 bbdoc: Open a stream for writing
 returns: A stream object
 returns: A stream object
-about: All streams created by #OpenStream, #ReadStream or #WriteStream should eventually
+about: All streams created by #OpenStream, #ReadStream, #WriteStream or #AppendStream should eventually
 be closed using #CloseStream.
 be closed using #CloseStream.
 End Rem
 End Rem
 Function WriteStream:TStream( url:Object )
 Function WriteStream:TStream( url:Object )
-	Return OpenStream( url,False,True )
+	Return OpenStream( url,False,WRITE_MODE_OVERWRITE )
+End Function
+
+Rem
+bbdoc: Open a stream for appending
+returns: A stream object
+about: All streams created by #OpenStream, #ReadStream, #WriteStream or #AppendStream should eventually
+be closed using #CloseStream.
+End Rem
+Function AppendStream:TStream( url:Object )
+	Return OpenStream( url,False,WRITE_MODE_APPEND )
 End Function
 End Function
 
 
 Rem
 Rem
@@ -1145,3 +1164,13 @@ Function CasedFileName$(path$)
 		closedir_(dir)
 		closedir_(dir)
 	EndIf
 	EndIf
 End Function
 End Function
+
+Rem
+bbdoc: Opens a file for output operations.
+End Rem
+Const WRITE_MODE_OVERWRITE:Int = 1
+Rem 
+bbdoc: Opens a file for appending with all output operations writing data at the end of the file.
+about: Repositioning operations such as #Seek affects the next input operations, but output operations move the position back to the end of file.
+End Rem
+Const WRITE_MODE_APPEND:Int = 2

+ 2 - 2
textstream.mod/textstream.bmx

@@ -321,7 +321,7 @@ End Type
 	
 	
 Type TTextStreamFactory Extends TStreamFactory
 Type TTextStreamFactory Extends TStreamFactory
 
 
-	Method CreateStream:TStream( url:Object,proto$,path$,readable:Int,writeable:Int ) Override
+	Method CreateStream:TStream( url:Object,proto$,path$,readable:Int,writeMode:Int ) Override
 		Local encoding:ETextStreamFormat
 		Local encoding:ETextStreamFormat
 		Select proto$
 		Select proto$
 		Case "latin1"
 		Case "latin1"
@@ -334,7 +334,7 @@ Type TTextStreamFactory Extends TStreamFactory
 			encoding=ETextStreamFormat.UTF16LE
 			encoding=ETextStreamFormat.UTF16LE
 		End Select
 		End Select
 		If Not encoding Return Null
 		If Not encoding Return Null
-		Local stream:TStream=OpenStream( path,readable,writeable )
+		Local stream:TStream=OpenStream( path,readable,writeMode )
 		If stream Return TTextStream.Create( stream,encoding )
 		If stream Return TTextStream.Create( stream,encoding )
 	End Method
 	End Method
 End Type
 End Type