Hi guy's, quickly put together a user friendly system to create and read INI files. Here's an example of an INI file created with this system:
[Display]
iSizeW=1920
iSizeH=1200
bVSync=0
[Stuff]
sReadMe=This is a string your reading, etc...
fValue=0.015
Now here's how easy that was to make using the system:
OpenToWriteIni( "", "MyConfig" ) `//( Directory ,INI file name)
IniCreateHeader( "Display" )
IniCreateIntegerItem( "SizeW", 1920 )
IniCreateIntegerItem( "SizeH", 1200 )
IniCreateBooleanItem( "bVSync", 0 )
IniCreateHeader( "Stuff" )
IniCreateStringItem( "ReadMe", "This is a string your reading, etc..." )
IniCreateFloatItem( "Value", 0.015 )
CreateIni()
And finally, here's how easy it is to read them:
OpenToReadIni( "", "MyConfig" )
print "Size W: " + str$( ReadIniInteger( "Display", "SizeW" ) )
print "Size H: " + str$( ReadIniInteger( "Display", "SizeH" ) )
print "vSync: " + str$( ReadIniBoolean( "Display", "bVSync" ) )
print ""
print "A String: " + ReadIniString( "Stuff", "ReadMe" )
print "A Float: " + ReadIniFloat( "Stuff", "Value" )
CloseIni()
I've also added error codes for each variable just in case there are any read errors and will be working on a validate INI function soon. So here the code and enjoy:
Rem Project: INI Parsing
Rem Created: 17/07/2009 03:42:49
Rem ***** Main Source File *****
#constant c_MaxFiles 32 `Just incase
#constant c_IniMaxHeaderItems 32
#constant c_IniVariableType_Integer 0
#constant c_IniVariableType_Float 1
#constant c_IniVariableType_Boolean 2
#constant c_IniVariableType_String 3
#constant c_IniIntegerHeader "i" `Should change these to ascii me think's
#constant c_IniFloatHeader "f"
#constant c_IniBooleanHeader "b"
#constant c_IniStringHeader "s"
#constant c_IniFileExtension ".ini"
#constant c_IniHeaderOpenTag "["
#constant c_IniHeaderCloseTag "]"
`Errors Codes
`Because some of the read INI functions return values,
`you have to set some error values. Just make sure there
`values your never need. Also it's best to check if theres
`an error before setting any core variables and if there is,
`replace it with core preset deflauts. This can be and INI
`file just make sure there are no errors in the deflaut reads
`or chaos could accure.
#constant c_IniInteger_HeaderError 101101101
#constant c_IniInteger_KeyError 202202202
#constant c_IniFloat_HeaderError 10110.1101
#constant c_IniFloat_KeyError 20220.2202
#constant c_IniBoolean_HeaderError 2
#constant c_IniBoolean_KeyError 3
`And Strings
#constant c_IniString_HeaderError "Read Error: Header Not Found"
#constant c_IniString_KeyError "Read Error: Key Not Found"
InitializeIniParsing()
remstart
OpenToWriteIni( "", "ConfigTest" )
IniCreateHeader( "General" )
IniCreateBooleanItem( "EnableProfile", 0 )
IniCreateBooleanItem( "DrawSpellContact", 0 )
IniCreateHeader( "Display" )
IniCreateBooleanItem( "Full Screen", 1 )
IniCreateIntegerItem( "Size W", 1920 )
IniCreateIntegerItem( "Size H", 1200 )
IniCreateFloatItem( "NoLODFarDistancePct", 1.3453 )
IniCreateHeader( "Interface" )
IniCreateStringItem( "GeneralMasterMismatchWarning", "One or more plugins could not find the correct versions of the master files they depend on." )
IniCreateFloatItem( "FadeDistance", 12288.000 )
CreateIni()
remend
OpenToReadIni( "", "ConfigTest" )
print "EnableProfile: " + str$( ReadIniFloat( "Interface", "FadeDistance" ) )
print "Size H: " + str$( ReadIniInteger( "Display", "Size H" ) )
print ""
print "GeneralMasterMismatchWarning: " + ReadIniString( "Interface", "GeneralMasterMismatchWarning" )
CloseIni()
wait key
end
type UDT.IniHeader
HeaderName as string
ItemCount as integer
endtype
type UDT.IniItem
KeyType as dword
Key as string
IntegerItem as integer
FloatItem as float
BooleanItem as boolean
StringItem as string
endtype
function InitializeIniParsing()
global dim _IniCreateItem() as string
global dim _IniOpenHeader() as UDT.IniHeader
global dim _IniOpenItem(,) as UDT.IniItem
global g_bCreateIni as boolean `equals 1 if creating is in process
global g_bOpenIni as boolean
global g_IniFilePath as string
endfunction
function UnitializeIniParsing()
undim _IniCreateItem()
undim _IniOpenHeader()
undim _IniOpenItem(,)
endfunction
function OpenToReadIni( FileDir as string, FileName as string )
local IniFilePath as string
local StringLine as string
local HeaderName as string
local StrLength as integer
local bValidString as boolean
local FileNum as integer
if file exist( FileDir+FileName+c_IniFileExtension )
IniFilePath = FileDir+FileName+c_IniFileExtension
g_bOpenIni = 1
repeat : inc FileNum
if FileNum = c_MaxFiles then exitfunction
until not file open(FileNum)
open to read FileNum , IniFilePath
while not file end( FileNum )
read string FileNum , StringLine
bValidString = 1
if left$( StringLine, 1 ) = "" then bValidString = 0
if bValidString
if left$( StringLine, 1 ) = c_IniHeaderOpenTag and right$( StringLine, 1 ) = c_IniHeaderCloseTag
StrLength = len( StringLine )
HeaderName = left$( right$( StringLine, StrLength-1 ), StrLength-2 )
array insert at bottom _IniOpenHeader()
_IniOpenHeader().HeaderName = HeaderName
_IniOpenHeader().ItemCount = -1
HeaderIndex = array count(_IniOpenHeader())
global dim _IniOpenItem( HeaderIndex, c_IniMaxHeaderItems ) as UDT.IniItem
else
if HeaderName <> ""
inc _IniOpenHeader(HeaderIndex).ItemCount
ItemIndex = _IniOpenHeader(HeaderIndex).ItemCount
_IniOpenItem( HeaderIndex, ItemIndex ).Key = first token$( right$( StringLine, len(StringLine)-1 ), "=" )
select left$( StringLine, 1 )
case c_IniIntegerHeader
_IniOpenItem( HeaderIndex, ItemIndex ).KeyType = c_IniVariableType_Integer
_IniOpenItem( HeaderIndex, ItemIndex ).IntegerItem = val(next token$( "=" ))
endcase
case c_IniFloatHeader
_IniOpenItem( HeaderIndex, ItemIndex ).KeyType = c_IniVariableType_Float
_IniOpenItem( HeaderIndex, ItemIndex ).FloatItem = val(next token$( "=" ))
endcase
case c_IniBooleanHeader
_IniOpenItem( HeaderIndex, ItemIndex ).KeyType = c_IniVariableType_Boolean
_IniOpenItem( HeaderIndex, ItemIndex ).BooleanItem = val(next token$( "=" ))
endcase
case c_IniStringHeader
_IniOpenItem( HeaderIndex, ItemIndex ).KeyType = c_IniVariableType_String
_IniOpenItem( HeaderIndex, ItemIndex ).StringItem = next token$( "=" )
endcase
endselect
endif
endif
else
HeaderName = ""
endif
endwhile
close file FileNum
endif
endfunction
function CloseIni()
global dim _IniOpenHeader() as UDT.IniHeader
global dim _IniOpenItem(,) as UDT.IniItem
g_bOpenIni = 0
endfunction
function GetHeaderIndex( Header as string )
local Index as integer
for Index = 0 to array count(_IniOpenHeader())
if _IniOpenHeader(Index).HeaderName = Header
exitfunction Index
endif
next Index
endfunction -1
function ReadIniInteger( Header as string, Key as string )
local HeaderIndex as integer
local CountIndex as integer
local Index as integer
local Out as integer
if g_bOpenIni
HeaderIndex = GetHeaderIndex( Header )
if HeaderIndex = -1 then exitfunction c_IniInteger_HeaderError
CountIndex = _IniOpenHeader(HeaderIndex).ItemCount
for Index = 0 to CountIndex
if _IniOpenItem( HeaderIndex, Index ).KeyType = c_IniVariableType_Integer
if _IniOpenItem( HeaderIndex, Index ).Key = Key
Out = _IniOpenItem( HeaderIndex, Index ).IntegerItem
exitfunction Out
endif
endif
next Index
endif
endfunction c_IniInteger_KeyError
function ReadIniFloat( Header as string, Key as string )
local HeaderIndex as integer
local CountIndex as integer
local Index as integer
local Out as float
if g_bOpenIni
HeaderIndex = GetHeaderIndex( Header )
if HeaderIndex = -1 then exitfunction c_IniFloat_HeaderError
CountIndex = _IniOpenHeader(HeaderIndex).ItemCount
for Index = 0 to CountIndex
if _IniOpenItem( HeaderIndex, Index ).KeyType = c_IniVariableType_Float
if _IniOpenItem( HeaderIndex, Index ).Key = Key
Out = _IniOpenItem( HeaderIndex, Index ).FloatItem
exitfunction Out
endif
endif
next Index
endif
endfunction c_IniFloat_KeyError
function ReadIniBoolean( Header as string, Key as string )
local HeaderIndex as integer
local CountIndex as integer
local Index as integer
local Out as boolean
`Needs to be boolean so exitfunction/endfunction are the same datatypes
local Error as boolean
if g_bOpenIni
HeaderIndex = GetHeaderIndex( Header )
Error = c_IniBoolean_HeaderError
if HeaderIndex = -1 then exitfunction Error
CountIndex = _IniOpenHeader(HeaderIndex).ItemCount
for Index = 0 to CountIndex
if _IniOpenItem( HeaderIndex, Index ).KeyType = c_IniVariableType_Boolean
if _IniOpenItem( HeaderIndex, Index ).Key = Key
Out = _IniOpenItem( HeaderIndex, Index ).BooleanItem
exitfunction Out
endif
endif
next Index
endif
Error = c_IniBoolean_KeyError
endfunction Error
function ReadIniString( Header as string, Key as string )
local HeaderIndex as integer
local CountIndex as integer
local Index as integer
local Out as string
if g_bOpenIni
HeaderIndex = GetHeaderIndex( Header )
if HeaderIndex = -1 then exitfunction c_IniString_HeaderError
CountIndex = _IniOpenHeader(HeaderIndex).ItemCount
for Index = 0 to CountIndex
if _IniOpenItem( HeaderIndex, Index ).KeyType = c_IniVariableType_String
if _IniOpenItem( HeaderIndex, Index ).Key = Key
Out = _IniOpenItem( HeaderIndex, Index ).StringItem
exitfunction Out
endif
endif
next Index
endif
endfunction c_IniString_KeyError
function OpenToWriteIni( FileDir as string, FileName as string )
if file exist( FileDir+FileName+c_IniFileExtension )
delete file FileDir+FileName+c_IniFileExtension
endif
empty array _IniCreateItem()
g_IniFilePath = FileDir+FileName+c_IniFileExtension
g_bCreateIni = 1
endfunction
function CreateIni()
local FileNum as integer
local Index as integer
if g_bCreateIni = 1
repeat : inc FileNum
if FileNum = c_MaxFiles then exitfunction
until not file open(FileNum)
open to write FileNum, g_IniFilePath
for Index = 0 to array count(_IniCreateItem())
write string FileNum , _IniCreateItem(Index)
next Index
close file FileNum
empty array _IniCreateItem()
g_bCreateIni = 0
endif
endfunction
function IniCreateHeader( Header as string )
if g_bCreateIni
`Add as space betweem header sections
if array count(_IniCreateItem()) > 0
array insert at bottom _IniCreateItem()
endif
array insert at bottom _IniCreateItem()
_IniCreateItem() = c_IniHeaderOpenTag+Header+c_IniHeaderCloseTag
endif
endfunction
function IniCreateIntegerItem( Key as string, Item as integer )
if g_bCreateIni
array insert at bottom _IniCreateItem()
_IniCreateItem() = c_IniIntegerHeader + Key + "=" + str$(Item)
endif
endfunction
function IniCreateFloatItem( Key as string, Item as float )
if g_bCreateIni
array insert at bottom _IniCreateItem()
_IniCreateItem() = c_IniFloatHeader + Key + "=" + str$(Item,4)
endif
endfunction
function IniCreateBooleanItem( Key as string, Item as boolean )
if g_bCreateIni
array insert at bottom _IniCreateItem()
if Item < 0 then Item = 0
if Item > 1 then Item = 1
_IniCreateItem() = c_IniBooleanHeader + Key + "=" + str$(Item)
endif
endfunction
function IniCreateStringItem( Key as string, Item as string )
if g_bCreateIni
array insert at bottom _IniCreateItem()
_IniCreateItem() = c_IniStringHeader + Key + "=" + Item
endif
endfunction
Note: I dumped the code in a txt file and it messed the indentation up, but give me a sec and i'll fix soon, i'll also add comments aswell in the future version.
A dream is a fantasy, if you achieve that fantasy it was never a dream to begin with.