Ini-File functions?
BlitzMax Forums/BlitzMax Programming/Ini-File functions?
| ||
Hi! Has someone created a set of Ini-File functions for bmx already? So that one can easily manage strings such as. Fullscreen=True Set_Resolution=800x600 Could not find them in the archives. |
| ||
Someone has put together a set of XML functions that would be great for this type of thing. They have been spoken about recently so take a look at recent posts. |
| ||
xml? wouldn't that be overkill? |
| ||
Id say yeah overkill lol. Best to code it and configure it to your needs, just parse out the commands you after like (ScreenRes=) etc and grap the info you need. Taking pre done functions from the community will mean having to use their tag's and that might not fit with what your doing. then again you could just edit the code lol.. |
| ||
heres what i did for my gui: testini.ini i'm have trouble with the arrays at the mo but once thats fixed it'll be done. and bmax doesnt like float/double ethier. Edit - Fixed it now. It didn't cause of how i was going through the file. i forgot i made it easy to read the file in one go. Edit 2 - you'll notice that you need a start and end tag. I put this in to make easy entry/exit code enjoy. |
| ||
...and another simple ini module. It can't handle arrays like the one from Diablo but does it's job quite good.Module pub.ini ModuleInfo "Version : 1.00" ModuleInfo "Author : d:bug / Future-Coding" ModuleInfo "License : Public Domain" ModuleInfo "History : 1.01 Module does not longer need BRL.Retro" ModuleInfo "History : 1.00 first released version" Import BRL.FileSystem Type TIni Field path:String Field line:String[1] Field linecount:Int=0 Function Create:TIni () Local ini:TIni = New TIni Return ini End Function Function Read:TIni (inipath:String) If FileType (inipath) Then Local ini:TIni = New TIni Local stream:TStream = ReadFile (inipath) While Not stream.EOF() ini.linecount :+1 ini.line = ini.line[..(ini.linecount+1)] ini.line[ini.linecount] = stream.ReadLine () Wend stream.close Return ini Else DebugLog "Ini.mod / Read : file "+inipath+" not found" Return Null EndIf End Function Method Write () Local stream:TStream = WriteFile(path) For Local i:Int = 0 To linecount stream.WriteLine (line[i]) Next stream.close End Method Method GetSeek:Int (group:String="") If group = "" Then Return -1 If group.find("[") = -1 Then group = "[" + group If group.find("]") = -1 Then group = group + "]" For Local i:Int = 0 To linecount If line[i].ToLower() = Group.ToLower() Then Return i + 1 EndIf Next DebugLog "Ini.mod / GetSeek : group <"+group+"> not found" Return -1 End Method Method GetGroup:String (seek:Int=-1) If (seek >= 0) And (seek <= linecount) If line[seek][..1] = "[" Return line[i] EndIf DebugLog "Ini.mod / GetGroup : seek <"+seek+"> not found" Return "" End Method Method SetGroup:Int (group:String) If group.find("[") = -1 Then group = "[" + group If group.find("]") = -1 Then group = group + "]" Local seek:Int = GetSeek (group) If seek = -1 line[linecount] = group linecount:+1 Return linecount Else line[seek] = group Return seek EndIf DebugLog "Ini.mod / SetGroup : unhandled error building group <"+group+">" Return -1 End Method Method GetValue:String (seek:Int,keyword:String,defaultvalue:String="") If seek = -1 Return defaultvalue For Local i:Int = seek To linecount If line[i][..1] = "[" Then Return defaultvalue If (line[i][..1] <> "#") And (line[i][..1] <> "") Local split:Int = line[i].find("=") Local key:String = (line[i][..split]).Replace(" ","") Local value:String = line[i][split+1..] If key.ToLower() = keyword.ToLower() While value[..1] = " " value = value[1..] Wend Return value EndIf EndIf Next DebugLog "Ini.mod / GetValue : keyword <"+keyword+"> not found" Return defaultvalue End Method Method SetValue:Byte (seek:Int,keyword:String,value:String) If seek = -1 Return defaultvalue For Local i:Int = seek To linecount If line[i][..1] = "[" Then Return False If (line[i][..1] <> "#") And (line[i][..1] <> "") Local split:Int = line[i].find("=") Local key:String = (line[i][..split]).Replace(" ","") If key.ToLower() = keyword.ToLower() line[i] = key + " = " + value Return True EndIf EndIf Next DebugLog "Ini.mod / SetValue : keyword <"+keyword+"> not found" Return True End Method End Type 'example Rem Graphics 640,480,0 local ini:tini = tini.Read ("blub.ini") local seek:Int = ini.GetSeek ("[TEST]") Repeat Cls DrawText (ini.GetValue(seek,"Blub","Muhahahahaha"),10,20) DrawText (ini.GetValue(seek,"Bla","Dummes Gelache"),10,40) If KeyHit (KEY_F1) local boing:Byte = ini.SetValue(seek,"Blub","Huhu") If boing = True ini.Write() Else Print "moep" EndIf EndIf Flip Until KeyHit(KEY_ESCAPE) End Rem This one reads the Ini into memory. You could manipulate it from there and write it back to disk. Simple stuff without any help. |
| ||
http://www.blitzwiki.org/index.php/Category:WB_Modules_%28Module_Collection%29 Wb.simpleconfig seemes nice. |
| ||
Here is one that i use in B3D that I got from Blitzcoder many moon ago. This is the bMax version. Include "bbtype.bmx" Include "bbvkey.bmx" Global INI_Type_list:TList=New TList ' Title: INI file manipulation functions ' Version: 2.1 ' Revised: September 28 2003 ' Author: Leigh Bowers ' Email: leigh.bowers@... ' Web: http://www.curvesoftware.co.uk/blitz ' History: ' 2.1 Fixed possible problem with locating keys. ' 2.01 Removed superfluous CR LF from file. ' 2.00 Redesigned and rewritten. ' Example usage: 'Include "Ini2.1.bb" ' 'INI_OpenFile("Archon.ini") 'strValue$ = INI_ReadValue("Config", "Mode", "C64") 'INI_WriteValue("Config", "Mode", "Atari") 'If Not(INI_CloseFile()) Then ' Print "Unable to commit INI file changes to file (" + INI_CurrentFile\strFilename + ")." 'End If Type bbINI_Type Extends TBBType Method New() Add(INI_Type_list) End Method Method After:bbINI_Type() Local t:TLink t=_link.NextLink() If t Return bbINI_Type(t.Value()) End Method Method Before:bbINI_Type() Local t:TLink t=_link.PrevLink() If t Return bbINI_Type(t.Value()) End Method Field strFilename$ = "" Field blnModified% = False Field strContents$ = "" End Type Global INI_CurrentFile:bbINI_Type = New bbINI_Type ' Core functions Function INI_OpenFile(INI_strFilename$) ' Description: Open the specified INI file for manipulation (if said file exists). ' Returns: [] n/a Local INI_lngFileHandle:tstream INI_CurrentFile.strFilename = Trim(INI_strFilename) ; If Right(Upper(INI_strFilename), 4) <> ".INI" Then INI_CurrentFile.strFilename = INI_CurrentFile.strFilename + ".ini" INI_CurrentFile.blnModified = False INI_CurrentFile.strContents = "" INI_lngFileHandle = ReadFile(CurrentDir$() + "\" + INI_CurrentFile.strFilename) If INI_lngFileHandle <> Null Then While Not(Eof(INI_lngFileHandle)) INI_CurrentFile.strContents = INI_CurrentFile.strContents + ReadLine$(INI_lngFileHandle) + Chr$($0D) + Chr$($0A) Wend CloseStream INI_lngFileHandle Return True Else Return False End If End Function Function INI_CloseFile%() ' Description: If INI modifications have been made (via INI_WriteValue), they are saved back to the original INI file. ' Returns: [boolean] True (success) or False (unable to save modifications back to the INI file, where applicable) Local INI_blnResult% = True If (INI_CurrentFile.blnModified = True) Then ' Commit internal changes back to the INI file Local INI_lngFileHandle:tstream = WriteFile(CurrentDir$() + "\" + INI_CurrentFile.strFilename) If INI_lngFileHandle <> Null Then WriteLine INI_lngFileHandle, Left$(INI_CurrentFile.strContents, Len(INI_CurrentFile.strContents) - 2) CloseFile INI_lngFileHandle INI_CurrentFile.blnModified = False Else INI_blnResult = False ' Create file failed End If End If Return INI_blnResult End Function Function INI_ReadValue$(INI_strSection$, INI_strKey$, INI_strDefault$ = "") ' Description: Retrieve the INI file value for the specified INI Section/Key combination. ' Returns: [string] INI Value - The optional INI_strDefault value is returned if no SECTION/KEY combination is found INI_strSection = "[" + Upper$(Trim$(INI_strSection)) + "]" INI_strKey = Upper$(Trim$(INI_strKey)) Local INI_strUpperContents$ = Upper$(INI_CurrentFile.strContents) ' Locate the SECTION, KEY and VALUE Local INI_strValue$ = "" Local INI_lngSectionPos% = Instr(INI_strUpperContents, INI_strSection) If (INI_lngSectionPos <> 0) Then Local INI_lngKeyPos% = Instr(INI_strUpperContents, INI_strKey, (INI_lngSectionPos + Len(INI_strSection) + 1)) If (INI_lngKeyPos <> 0) Then Local INI_NextSection% = Instr(INI_strUpperContents, "[", (INI_lngSectionPos + Len(INI_strSection) + 1)) If (INI_NextSection = 0) Or (INI_lngKeyPos < INI_NextSection) Then Local INI_lngStartPos% = Instr(INI_strUpperContents, "=", (INI_lngKeyPos + 1)) If (INI_lngStartPos <> 0) Then Local INI_lngEndPos% = Instr(INI_strUpperContents, Chr$($0D), (INI_lngStartPos + 1)) If (INI_lngEndPos <> 0) Then ' We have located the required INI key and it's corresponding value INI_strValue = Trim$(Mid$(INI_CurrentFile.strContents, INI_lngStartPos + 1, (INI_lngEndPos - INI_lngStartPos - 1))) End If End If End If End If End If ' Return the appropriate value If (INI_strValue <> "") Then Return INI_strValue Else Return INI_strDefault End Function Function INI_WriteValue(INI_strSection$, INI_strKey$, INI_strValue$) ' Description: Add/update key values within the current INI file (internally). ' Note: Changes are not committed back To file until you explicitly call INI_CloseFile. ' Returns: [] n/a INI_strSection = "[" + Trim$(INI_strSection) + "]" Local INI_strUpperSection$ = Upper$(INI_strSection) INI_strKey = Trim$(INI_strKey) INI_strValue = Trim$(INI_strValue) ' (Re)Create the INI contents updating/adding the SECTION, KEY and VALUE Local INI_blnWrittenKey% = False Local INI_blnSectionFound% = False Local INI_strCurrentSection$ = "" Local INI_strTempContents$ = INI_CurrentFile.strContents INI_CurrentFile.strContents = "" Local INI_lngOldPos% = 1 Local INI_lngPos% = Instr(INI_strTempContents, Chr$($0D)) While (INI_lngPos <> 0) Local INI_strTemp$ =Trim$(Mid$(INI_strTempContents, INI_lngOldPos, (INI_lngPos - INI_lngOldPos))) If (INI_strTemp <> "") Then If Left$(INI_strTemp, 1) = "[" And Right$(INI_strTemp, 1) = "]" Then ' Process SECTION If (INI_strCurrentSection = INI_strUpperSection) And (INI_blnWrittenKey = False) Then INI_blnWrittenKey = INI_CreateKey(INI_strKey, INI_strValue) End If INI_CreateSection INI_strTemp INI_strCurrentSection = Upper$(INI_strTemp) If (INI_strCurrentSection = INI_strUpperSection) Then INI_blnSectionFound = True Else ' KEY=VALUE Local INI_lngEqualsPos% = Instr(INI_strTemp, "=") If (INI_lngEqualsPos <> 0) Then If (INI_strCurrentSection = INI_strUpperSection) And (Upper$(Trim$(Left$(INI_strTemp, (INI_lngEqualsPos - 1)))) = Upper$(INI_strKey)) Then INI_blnWrittenKey = INI_CreateKey(INI_strKey, INI_strValue) Else INI_CurrentFile.strContents = INI_CurrentFile.strContents + INI_strTemp + Chr$($0D) + Chr$($0A) End If End If End If End If ' Move through the INI contents... INI_lngOldPos = INI_lngPos + 1 INI_lngPos% = Instr(INI_strTempContents, Chr$($0D), INI_lngOldPos) Wend ' KEY wasn't found in the INI contents - Append a new SECTION if required and create our KEY=VALUE line If (INI_strValue <> "") And (INI_blnWrittenKey = False) Then If (INI_blnSectionFound = False) Then INI_CreateSection INI_strSection End If INI_CreateKey INI_strKey, INI_strValue End If INI_CurrentFile.blnModified = True End Function Function INI_CreateSection(INI_strNewSection$) If INI_CurrentFile.strContents <> "" Then ' Blank line between sections INI_CurrentFile.strContents = INI_CurrentFile.strContents + Chr$($0D) + Chr$($0A) End If INI_CurrentFile.strContents = INI_CurrentFile.strContents + INI_strNewSection + Chr$($0D) + Chr$($0A) End Function Function INI_CreateKey%(INI_strKey$, INI_strValue$) If (INI_strValue <> "") Then INI_CurrentFile.strContents = INI_CurrentFile.strContents + INI_strKey + "=" + INI_strValue + Chr$($0D) + Chr$($0A) End If Return True End Function Its times like these that i really lament the loss of Blitzcoder.com. So much cool sourcecode lost.. :( |
| ||
Thanks everyone! I still try to sort out the functions I'll be using... :) |