More out of text files?
BlitzMax Forums/BlitzMax Beginners Area/More out of text files?
| ||
I've used text files to load a few variables for a config file, but is there a tutorial or guide on how to read and write more advanced things to text files? I would like to keep my text files readable so you can edit them in a text editor. I don't want a big list of numbers. |
| ||
I think there are modules out there for reading/writing XML files, which are in a human-readable form similar to HTML. I think there is even a module to automatically allow saving/loading of objects to XML files. I'm not sure where, check Brucey ;) |
| ||
What you are looking for is a file with INI-structure. e.g. TEST.INI:[Player] Name=Tarzan Age=23 X=120 [Level 1] Geld=100.90 ... [Level 2] Geld=80.70 ... [Start] ScaleX=1280 .... The structure is always the same: There is a Section Headline in brackets and below the named values: [Section HeadLine] Value1= Value2= In the german Blitzforum I wrote a tutorial http://www.blitzforum.de/forum/viewtopic.php?t=33651 where you can find a source code that might help you: This part is only how to read an ini file. You need to have a hand made INI-File. Write it with the BlitzMax-Ide, but save it as "Test.INI" in the folder, where the source code is. To use the INI-File you only need to call one function of this code Value = ReadValue ("Player", "X") Here "Player" is the section name and "X" is the name of the value you want to load. All other functions in the code are for internal use. |
| ||
Thanks I will try and get my head round your code once ive converted it to english. |
| ||
There is although Muttleys very good mod for inifile handling: http://www.muttleyville.org/forum/ |
| ||
Here's a type I wrote for simple handling of config files:Framework BRL.System Import BRL.LinkedList Import BRL.StandardIO Type TConfig Field CR:String Field Name:String Field FileName:String Method Init ( ) CR = Chr ( 13 ) + Chr ( 10 ) FileName = CurrentDir ( ) + "/" + Name + ".cfg" If FileType ( FileName ) = False CreateFile ( FileName ) EndIf End Method Method Write ( Entry:String, Contents:String ) Local strm:TStream = OpenStream ( FileName, 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 ( FileName ) CreateFile ( FileName ) strm = OpenStream ( FileName, 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 ( FileName, 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 ( FileName, 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. |
| ||
It helps alot thanks. If tempstring [ .. Len ( Entry ) ] = Entry result = tempstring [ Len ( Entry ) + 3 .. ] found = True EndIf I know what this does, but what does the [..Len( entry )] and [ Len (entry) + 3..] bits mean syntax wise? |
| ||
Have a look in the Bmax docs under Language->Slices. The examples there should make it clearer. It's a way of extracting a part of tempstring by treating the tempstring string as an array of characters. You could do the same type of things with Mid$, Left$ and Right$. I just find the Slice way of doing it cleaner. 'tempstring [ .. Len ( Entry ) ]' will extract all characters from the beginning of the string up to 'Len( Entry )', so Left$ could have been used here instead. You could also use 'tempstring [ 0 .. Len ( Entry ) ]' to make it more readable. 'tempstring [ Len ( Entry ) + 3 .. ]' extracts all the characters from 'Len ( Entry ) + 3' up to the end of the string. You could also use 'tempstring [ Len ( Entry ) + 3 .. Len ( tempstring ) ]' to make that one more readable. Hope that makes some kind of sense. I just tidied that code up a little bit too. |
| ||
thanks for the help its the cutting up the strings to get the bit you want that was confusing me, so this has cleared it up a bit for me. |
| ||
Please delete this post, I didn't realize this was the BlitzMax forum. Unless a Blitz3D suggestion would help. If so, it's http://www.blitzbasic.com/Community/posts.php?topic=84249#952561 |