I've a project were I need the end user to create an INI file. I could only find a DarkBasic set of functions for this and failed at getting that working, so cracked on with my implementation.
My code loads the INI file into a string array for reading/writing.
There is also a function for writing it back out.
To keep the code small, I only cover String and Numerical (Float) data types.
There is some basic diagnostics, so if you ask to read a section/parameter that doesn't exist, it will flag it.
It should be fairly fast, I'm assuming most INI files won't be massive.
One thing I've love to trap is attempting to read a Numerical Value where the actual data is a string... AppGameKit reports "na", but I'm struggling to know the best way to detect that.
config.ini
[Jason]
was=1
[Pete]
jas=1971
ere=AGK Rocks
timestamp=16:32:26
datestamp=2019-10-02
config_data as string[] // A local store for the config file
// Load config.ini into the config_data[] string array
config_data=read_config() // Load the config.ini into a string array
// Read and write some values...
Log(INIReadString(config_data,"Pete","jas")) // Extract the "jas" parameter from the "Pete" section as a string
valu=INIReadNumber(config_data,"Pete","jas")
config_data=INIWriteNumber(config_data,"Pete","jas",1971)
config_data=INIWriteString(config_data,"Pete","ere","AGK Rocks")
config_data=INIWriteString(config_data,"Pete","timestamp",GetCurrentTime())
// Dump the config_data[] array to the config.ini file.
write_config(config_data) // Write the changes back to the file
///////////////////////////////////////////////////////////////////
// Read a string value from the cached ini file
///////////////////////////////////////////////////////////////////
function INIReadString(config_data as string[],section_find$,param_find$)
out_value$ as string
for i = 0 to config_data.length-1
read$=config_data[i] // Read a line
if mid(read$,1,1)="[" // if 1st char is [ we have a section
sectionname$=mid(read$,2,len(read$)-2)
if sectionname$=section_find$ // We have found the section
for j=i to config_data.length-1
read$=config_data[j]
eq_pos=FindString(read$,"=") // Where is the = ?
if eq_pos>0 // The line contains =
param$=TrimString(mid(read$,1,eq_pos-1)," ")
value$=TrimString(mid(read$,eq_pos+1,len(read$))," ")
if value$<>"" and param$=param_find$
value_found=1
out_value$=value$
exit // exit the for loop
else
value_found=0
endif
endif
next j
if value_found=1 then exit
endif
endif
next i
if value_found=0 then log("Unable to locate Section ("+section_find$+") or Parameter ("+param_find$+")")
endfunction out_value$
///////////////////////////////////////////////////////////////////
// Read a numerical value from the cached ini file
///////////////////////////////////////////////////////////////////
function INIReadNumber(config_data as string[],section_find$,param_find$)
out_value as float
for i = 0 to config_data.length-1
read$=config_data[i] // Read a line
if mid(read$,1,1)="[" // if 1st char is [ we have a section
sectionname$=mid(read$,2,len(read$)-2)
if sectionname$=section_find$ // We have found the section
for j=i to config_data.length-1
read$=config_data[j]
eq_pos=FindString(read$,"=") // Where is the = ?
if eq_pos>0 // The line contains =
param$=TrimString(mid(read$,1,eq_pos-1)," ")
value$=TrimString(mid(read$,eq_pos+1,len(read$))," ")
if value$<>"" and param$=param_find$
value_found=1
out_value=valfloat(value$)
exit // exit the for loop
else
value_found=0
endif
endif
next j
if value_found=1 then exit
endif
endif
next i
if value_found=0 then log("Unable to locate Section ("+section_find$+") or Parameter ("+param_find$+")")
endfunction out_value
///////////////////////////////////////////////////////////////////
// Write a numerical value to the cached ini file
///////////////////////////////////////////////////////////////////
function INIWriteNumber(config_data as string[],section_find$,param_find$,new_value)
for i = 0 to config_data.length-1
read$=config_data[i] // Read a line
if mid(read$,1,1)="[" // if 1st char is [ we have a section
sectionname$=mid(read$,2,len(read$)-2)
if sectionname$=section_find$ // We have found the section
for j=i to config_data.length-1
read$=config_data[j]
eq_pos=FindString(read$,"=") // Where is the = ?
if eq_pos>0 // The line contains =
param$=TrimString(mid(read$,1,eq_pos-1)," ")
value$=TrimString(mid(read$,eq_pos+1,len(read$))," ")
if value$<>"" and param$=param_find$
value_found=1
new_data$=param$+"="+str(new_value)
config_data[j]=new_data$
exit // exit the for loop
else
value_found=0
endif
endif
next j
if value_found=1 then exit
endif
endif
next i
if value_found=0 then log("Unable to locate Section ("+section_find$+") or Parameter ("+param_find$+")")
endfunction config_data
///////////////////////////////////////////////////////////////////
// Write a string value to the cached ini file
///////////////////////////////////////////////////////////////////
function INIWriteString(config_data as string[],section_find$,param_find$,new_value$)
for i = 0 to config_data.length-1
read$=config_data[i] // Read a line
if mid(read$,1,1)="[" // if 1st char is [ we have a section
sectionname$=mid(read$,2,len(read$)-2)
if sectionname$=section_find$ // We have found the section
for j=i to config_data.length-1
read$=config_data[j]
eq_pos=FindString(read$,"=") // Where is the = ?
if eq_pos>0 // The line contains =
param$=TrimString(mid(read$,1,eq_pos-1)," ")
value$=TrimString(mid(read$,eq_pos+1,len(read$))," ")
if value$<>"" and param$=param_find$
value_found=1
new_data$=param$+"="+new_value$
config_data[j]=new_data$
exit // exit the for loop
else
value_found=0
endif
endif
next j
if value_found=1 then exit
endif
endif
next i
if value_found=0 then log("Unable to locate Section ("+section_find$+") or Parameter ("+param_find$+")")
endfunction config_data
///////////////////////////////////////////////////////////////////
// Load the entire config into a string array
///////////////////////////////////////////////////////////////////
function read_config()
start_time=Timer()
config_data as string[]
if GetFileExists("config.ini")
fid=OpenToRead("config.ini")
repeat
read$=ReadLine(fid) // Read a line
config_data.insert(read$)
until FileEOF(fid)
CloseFile(fid)
else
log("config.ini not found")
endif
endfunction config_data
///////////////////////////////////////////////////////////////////
// Save the entire config into a string array
///////////////////////////////////////////////////////////////////
function write_config(config_data as string[])
start_time=Timer()
fid=OpenToWrite("config.ini")
for i = 0 to config_data.length-1
write$=config_data[i]
WriteLine(fid,write$) // Read a line
next i
CloseFile(fid)
endfunction config_data
do
Print( ScreenFPS() )
Sync()
loop