Firstly here is the code:
#CONSTANT configFilePath "gamedata/"
type configDetails
configFile AS STRING
numLines AS INTEGER
endtype
GLOBAL currentConfig AS configDetails
currentConfig.configFile=""
currentConfig.numLines=0
#CONSTANT maxConfigLines 100
GLOBAL DIM configArray(maxConfigLines) AS STRING
LOCAL ScreenName AS STRING
LOCAL IPAddress AS STRING
`sample usages
` Get information from config file main section screen
ScreenName=GetConfigString("main.screen.name")
ScreenWidth=GetConfigInt("main.screen.width")
ScreenHeight=GetConfigInt("main.screen.height")
ScreenDepth=GetConfigInt("main.screen.depth")
CLS
PRINT "Screen Name = ";ScreenName
PRINT "Screen width = ";ScreenWidth
PRINT "Screen height= ";ScreenHeight
PRINT "Screen Depth = ";ScreenDepth
PRINT
`Now get from section runtime
DebugMode=GetConfigInt("main.runtime.debug")
PRINT "Debug Mode =";DebugMode
` Now get from config file network
IPAddress=GetConfigString("network.defaults.ip")
PRINT
PRINT "IP Address =";IPAddress
WAIT KEY
function GetConfigInt(stringRequest AS STRING)
LOCAL result AS INTEGER
result=VAL(GetConfigString(stringRequest))
endfunction result
function GetConfigString(stringRequest AS STRING)
LOCAL result AS STRING
LOCAL lp AS INTEGER
LOCAL currentLine AS STRING
LOCAL doLoop AS INTEGER
LOCAL pos AS INTEGER
LOCAL configFile AS STRING
LOCAL section AS STRING
LOCAL sectionValue AS STRING
` first need to work what config file is being requested
configFile =FIRST TOKEN$(stringRequest,".")
section =":"+next TOKEN$(".")+":"
sectionValue=next TOKEN$(".")
` make sure the correct config file is loaded.
LoadConfigFile(configFile)
` now search array for section
lp=0
pos=-1
doLoop=1
while doLoop
currentLine=ConfigArray(lp)
if FIND SUB STRING$(currentLine, section)>=0
pos=lp
doLoop=0
endif
lp=lp+1
if lp>currentConfig.numLines then doLoop=0
endwhile
if pos=-1
CLS
PRINT "FATAL ERROR: in config file ";configFile;" failed to find section ";section
WAIT KEY
END
endif
result="!@!@!@!"
doLoop=1
while doLoop
pos=pos+1
currentLine=ConfigArray(pos)
if FIND SUB STRING$(currentLine, sectionValue)>=0
if FIRST TOKEN$(currentLine,"=")=sectionValue
result=next TOKEN$("=")
doLoop=0
else
CLS
PRINT "FATAL ERROR: TOKEN or FIND SUB STRING$ is broken in GetConfigInt()"
WAIT KEY
END
endif
endif
if currentLine=";" OR pos>currentConfig.numLines then doLoop=0
endwhile
if result="!@!@!@!"
CLS
PRINT "FATAL ERROR: in config file ";configFile;" section ";section;" failed to find ";sectionValue
WAIT KEY
END
endif
endfunction result
function LoadConfigFile(configFile AS STRING)
LOCAL lp AS INTEGER
LOCAL readLine AS STRING
LOCAL fileName AS STRING
fileName=configFilePath+configFile+".conf"
if currentConfig.configFile<>configFile
if FILE EXIST(fileName)
lp=0
OPEN TO READ 1,fileName
while (FILE END(1)=0)
READ STRING 1,readLine
if readLine<>""
ConfigArray(lp)=readLine
lp=lp+1
if lp>MaxConfigLines
CLS
PRINT "FATAL ERROR, config file ";fileName;"is too large to fit in config buffer of ";MaxConfigLine;" lines"
PRINT "Blank Lines are ignored"
WAIT KEY
END
endif
endif
endwhile
CLOSE FILE 1
else
CLS
PRINT "FATAL ERROR, config file ";fileName;" does not exist."
WAIT KEY
END
endif
currentConfig.configFile=configFile
currentConfig.numLines=lp-1
endif
endfunction
This contains 2 main functions for geting information out of the config files.
Int=GetConfigInt("Details")
and
String=GetConfigString("Details")
The details part is a single string seperated by .'s
In the first position is the config file name
In the second postion is the section in the config file
in the third postion is the parameter you want to read. So in the above code there is the following example
Int=GetConfigInt("main.screen.width")
This will open a file called "main.conf" look for a section called screen and return the contents of parameter width.
The way the above code works means the config file needs to have a :screen: piece of text and it will look from that point until it reaches a ; for the string width. The config files required for the example above are:
main.conf
:screen:
name=Test Screen
width=1024
height=768
depth=32
;
:window:
name=Test Window
width=100
height=100
;
:runtime:
debug=1
;
network.conf
:defaults:
ip=127.0.0.0
hostname=localhost
gameport=1111
;
for the above code you need to create a sub directory in your project folder called gamedata and put the 2 above text config files in this directory.
I hope some people find this useful. Any problems with it then let me know.
Jas
----
"What is this talk of 'release'? Klingons do not'release' software. It escapes leaving a bloody trail of developers and quality assurance people in its wake!"