File writing

BlitzMax Forums/BlitzMax Beginners Area/File writing

The forum is locked. These are the original blitzbasic.com forums, kept so they can still be read. No new topics, replies or edits can be made here. To join in, head over to the Blitz3D+ forums.

Here's a type I wrote for simple handling of config files:

	Import BRL.System														'Import BMax System Module
	Import PUB.Win32														'Import Public Win32 Module
	Import BRL.Keycodes														'Import BMax Keycodes Module
	Import BRL.LinkedList													'Import BMax LinkedList Module
	Import BRL.FileSystem													'Import BMax FileSystem Module
	Import BRL.Retro														'Import BMax Retro Module
	Import BRL.StandardIO
	
	Type TConfig

		Field CR:String
		Field Name:String
		
		Method Init ( )

			CR = Chr ( 13 ) + Chr ( 10 )
			If FileType ( CurrentDir ( ) + "/" + Name + ".cfg" ) = False
				CreateFile ( CurrentDir ( ) + "/" + Name + ".cfg" )
			EndIf

		End Method
		
		Method Write ( Entry:String, Contents:String )
		
			Local strm:TStream = OpenStream ( CurrentDir ( ) + "/" + Name + ".cfg", True, True )
			Local tempcfg:TList = New TList
			Local found = False
			Local result:String
			While ( Eof ( strm ) = False )
				Local tempstring:String = ReadLine$ ( strm )
				If tempstring [ .. Len ( Entry ) ] = Entry
					Local newstring:String = Entry + " = " + Contents
					tempcfg.AddLast newstring
					found = True
				Else
					tempcfg.AddLast tempstring
				EndIf
			Wend
			CloseStream ( strm )
			DeleteFile ( CurrentDir ( ) + "/" + Name + ".cfg" )
			CreateFile ( CurrentDir ( ) + "/" + Name + ".cfg" )
			strm = OpenStream ( CurrentDir ( ) + "/" + Name + ".cfg", True, True )
			For Local t$ = EachIn tempcfg
				WriteLine ( strm, t )
			Next
			If found = False
				WriteString ( strm, Entry + " = " + Contents + CR )
			EndIf
			CloseStream ( strm )
			
		End Method
		
		Method Read:String ( Entry:String, DefaultContents:String )
			
			Local strm:TStream = OpenStream ( CurrentDir ( ) + "/" + Name + ".cfg", True, True )
			Local found = False
			Local result:String
			While ( Eof ( strm ) = False ) And ( found = False )
				Local tempstring:String = ReadLine$ ( strm )
				If tempstring [ .. Len ( Entry ) ] = Entry
					result = tempstring [ Len ( Entry ) + 3 .. ]
					found = True
				EndIf
			Wend
			CloseStream ( strm )
			If found = False
				strm = OpenStream ( CurrentDir ( ) + "/" + Name + ".cfg", True, True )
				SeekStream ( strm, StreamSize ( strm ) )
				WriteString ( strm, Entry + " = " + DefaultContents + CR )
				result = DefaultContents
				CloseStream ( strm )
			EndIf
			Return result
	
		End Method
		
	End Type
	
Config:TConfig = New TConfig
Config.Name = "Display"
Config.Init ( )
Config.Write ( "Res.X", String ( 800 ) )
Config.Write ( "Res.Y", String ( 600 ) )
print Int ( Config.Read ( "Res.X", "" ) )




Note that the Read method can also be used to add new entry's. If it finds that the entry does not exist, then it will create the entry and fill it with DefaultContents. If the entry already exists then it will return the current contents. If you want to change the contents just use the Write method.

It's not optimized for speed or massive config files as I didn't need these qualities for the project I am using it in.

Hope this helps.