Appendable Stream

BlitzMax Forums/Brucey's Modules/Appendable Stream

Brucey(Posted 2009) [#1]
Here's some code to make an appendable stream (yes, I made up that word) :

Import BRL.Stream

Type TAppendableCStream Extends TCStream

	Function OpenFile:TCStream( path$, readable:Int, writeable:Int )
		Local mode$,_mode:Int
		If readable And writeable
			mode="a+b"
			_mode=MODE_READ|MODE_WRITE
		Else If writeable
			mode="ab"
			_mode=MODE_WRITE
		Else
			mode="rb"
			_mode=MODE_READ
		EndIf
		path=path.Replace( "\","/" )
		Local cstream:Int = fopen_( path,mode )
?Linux
		If (Not cstream) And (Not writeable)
			path=CasedFileName(path)
			If path cstream=fopen_( path,mode )
		EndIf
?
		If cstream Return CreateWithCStream( cstream,_mode )
	End Function

End Type

Type TAppendableCStreamFactory Extends TStreamFactory

	Method CreateStream:TStream( url:Object,proto$,path$,readable:Int, writeable:Int )
		If proto="append"
			Return TAppendableCStream.OpenFile( path,readable,writeable )
		EndIf
	End Method

End Type

New TAppendableCStreamFactory



To use it, you simply add the "append::" proto to a filename.
For example :
Local file:String = "myfile.txt"
Local fileAppend:String = "append::myfile.txt"

Local stream:TStream = WriteStream(file)
stream.WriteLine("Hello!")
stream.Close()

stream = WriteStream(fileAppend)
stream.WriteLine("There!")
stream.Close()


Would be nice to have as standard... oh well... :-)


xlsior(Posted 2009) [#2]
Oooooo. Nice one!

would be nice if it became part of the standard...


DavidDC(Posted 2009) [#3]
would be nice if it became part of the standard...

Agreed.


plash(Posted 2009) [#4]
would be nice if it became part of the standard...
You know.. There is this rare occurrence, called the 'BlitzMax Update'.

I bet if we plead (or beg!) enough, it might just reach the overlords!


Retimer(Posted 2009) [#5]
Hurray for no more seeking to the eof.


Brucey(Posted 2009) [#6]
Hurray for no more seeking to the eof.

Yeah... I can't believe some people have been doing that! :-p