Read and Write easy your INI files with Blitz3D
Blitz3D Forums/Blitz3D Tutorials/Read and Write easy your INI files with Blitz3D
| ||
| With external DLL calls capability, Blitz can achieve an high grade of programmability. In this tutorial i try to explain how to manage easily you ini files. First you need to create in blitz 'userlibs' directory a decls file to import API routines. Save it as kernel32.decls ; Userlibs kernel32.decls .lib "kernel32.dll" GetPrivateProfileString%(lpApplicationName$,lpKeyName$,lpDefault$,lpReturnedString*,nSize%,lpFileName$):"GetPrivateProfileStringA" WritePrivateProfileString%(lpApplicationName$,lpKeyName$,lpString$,lpFileName$):"WritePrivateProfileStringA" Done? Well. Now you must include in your source code two functions to write and retrieve values in ini file:
Function SetIniString%(FileINI$, Section$, Key$, Value$)
; FileINI = file (must include path, for current dir use .\) to write into (eg: ".\myinifile.ini")
; Section$ = section that will include your key and value (eg: "GeneralSetup")
; Key$ = Key's name for the value (eg: "BackColor")
; Value$ = Value to write in Key (eg: "ff00ff")
; If the function succedeed it return True otherwhise False
; result in myinifile.ini:
; [GeneralSetup]
; BackColor=ff00ff
Ret%=WritePrivateProfileString(Section$, Key$, Value$, FileINI$)
If Ret% <> 1 Then
Return False
Else
Return True
End If
End Function
Function GetIniString$(FileINI$,Section$,Key$,DefaultValue$)
; FileINI = file (must include path, for current dir use .\) to read (eg: "c:\windows\myinifile.ini")
; Section$ = section where search your key (eg: "GeneralSetup")
; Key$ = Key's name to search (eg: "BackColor")
; DefaultValue$ = Value to return if key or file or section is not found (eg: "ff00ff")
; Function return value retrieved from searched Key or the user's default value
myString$=""
myBank=CreateBank(255)
LenString%=GetPrivateProfileString(Section$, Key$, DefaultValue$, myBank, 255, FileINI$)
For x=1 To LenString%
myString$=myString$+Chr(PeekByte(myBank,x-1))
Next
FreeBank myBank
Return myString$
End Function
You can manage also integer or floating number, you dont need to convert it to string before writing or after read. That's all! |
| ||
| Use of these functions. Imagine to load at start you preferred program variables, previuosly saved in you ini file. Code may be: const INIFILE$=".\myini.ini" WindowsSize$=GetIniString(INIFILE,"Window","Size","Maximized") WindowsCoords$=GetIniString(INIFILE,"Window","Coords","0,0,640,480") MaxScore%=GetIniString(INIFILE,"Players","MaxScore","9999") MaxScorePlayerName$=GetIniString(INIFILE,"Players","MaxScoreName","????") ... and so on To write: SetIniString(INIFILE,"Window","Size",WindowsSize$) SetIniString(INIFILE,"Window","Coords",WindowsCoords$) SetIniString(INIFILE,"Players","MaxScore",MaxScore%) SetIniString(INIFILE,"Players","MaxScoreName",MaxScorePlayerName$) Note that you can control if the write operation was successeful with: if not SetIniString(INIFILE,"Window","Size",WindowsSize$) then ... (manage here your error) |