Another Tier 1 include for working with INI files, should work with all preexisting ini files and provides a full set of functions for manipulation.
Includes the following functions
INI_Create()
INI_Open()
INI_Save()
INI_Close()
INI_GetValue()
INI_SetValue()
INI_AddComment()
INI_SectionCount()
INI_KeyCount()
INI_Section()
INI_Key()
INI_Value()
ini_include.agc
// Project: INI File Include
// Created By: PartTimeCoder
Type tINI_KVPair
key as string
value as string
EndType
Type tINI_Section
is_comment as integer
name as string
pair as tINI_KVPair[]
EndType
Type tINI_File
path as string
section as tINI_Section[]
EndType
// Internal Functions
Function _INI_FindSectionIndex(ini as tINI_File, section as string)
// find the ini section
section_found=-1
section_count = ini.section.length
for index=0 to section_count
section_name$=ini.section[index].name
if section_name$ = section
section_found=index
exit
endif
next
EndFunction section_found
Function _INI_FindKeyPair(section as tINI_Section, key as string)
pair_found=-1
pair_count=section.pair.length
for index=0 to pair_count
key_name$=section.pair[index].key
if key_name$ = key
pair_found=index
exit
endif
next
EndFunction pair_found
Function _INI_AddSection(ini ref as tINI_File, section_name as string, is_comment)
new=ini.section.length+1
ini.section.length=new
ini.section[new].is_comment=is_comment
ini.section[new].name = section_name
EndFunction new
Function _INI_AddKeyPair(section ref as tINI_Section, key as string, value as string)
new=section.pair.length+1
section.pair.length=new
section.pair[new].key=key
section.pair[new].value=value
EndFunction new
// Public Functions
Function INI_Create(ini ref as tINI_File, file_path as string)
ini.path = file_path
EndFunction 1
Function INI_Open(ini ref as tINI_File, file_path as string)
file = OpenToRead(file_path)
if file
ini.path = file_path
while FileEOF(file) = 0
line as string
line=ReadLine(file)
Comment = FindString(line, ";")
Section_Open = FindString(line, "[")
Section_Close = FindString(line, "]")
Pair_Mid = FindString(line, "=")
if Comment >0
section_name$=Mid(line, Comment+1, -1)
INI_AddComment(ini, section_name$)
elseif Section_Open>0 and Section_Close>0
open = FindString(line, "[")
close = FindString(line, "]")
section_name$=Mid(line, open+1, close-2)
_INI_AddSection(ini, section_name$, 0)
elseif Pair_Mid>0
key$=Mid(line, 1, Pair_Mid-1)
value$=Mid(line, Pair_Mid+1, -1)
section_index=ini.section.length
_INI_AddKeyPair(ini.section[section_index], key$, value$)
endif
endwhile
CloseFile(file)
endif
EndFunction
Function INI_Save(ini as tINI_File)
file = OpenToWrite(ini.path, 0)
if file
section_count = ini.section.length
for section_index=0 to section_count
section_name$=ini.section[section_index].name
is_comment=ini.section[section_index].is_comment
if is_comment=1
WriteLine(file, "; "+section_name$)
else
WriteLine(file, "["+section_name$+"]")
endif
pair_count = ini.section[section_index].pair.length
for pair_index=0 to pair_count
key$=ini.section[section_index].pair[pair_index].key
value$=ini.section[section_index].pair[pair_index].value
WriteLine(file, key$ + "=" + value$)
next
next
CloseFile(file)
endif
EndFunction
Function INI_Close(ini ref as tINI_File)
ini.section.length=-1
ini.path=""
EndFunction
Function INI_GetValue(ini as tINI_File, section_name as string, key as string, def_value as string)
// find the ini section
section_found=-1
pair_found=-1
result as string
// if section found, find key pair
section_found = _INI_FindSectionIndex(ini, section_name)
if section_found>=0
pair_found=_INI_FindKeyPair(ini.section[section_found], key)
endif
if pair_found>=0
result = ini.section[section_found].pair[pair_found].value
else
result = def_value
endif
EndFunction result
Function INI_SetValue(ini ref as tINI_File, section_name as string, key as string, value as string)
// find the ini section
section_found=-1
pair_found=-1
// if section found, find key pair
section_found = _INI_FindSectionIndex(ini, section_name)
if section_found>=0
pair_found=_INI_FindKeyPair(ini.section[section_found], key)
if pair_found>=0
ini.section[section_found].pair[pair_found].value=value
else
// add a new key pair
_INI_AddKeyPair(ini.section[section_found], key, value)
endif
else
// add a new section
new = _INI_AddSection(ini, section_name, 0)
_INI_AddKeyPair(ini.section[new], key, value)
endif
EndFunction
Function INI_AddComment(ini ref as tINI_File, comment_text as string)
_INI_AddSection(ini, comment_text, 1)
EndFunction
Function INI_SectionCount(ini as tINI_File)
result=ini.section.length
EndFunction result
Function INI_KeyCount(ini as tINI_File, section_index)
result=-1
if section_index<=ini.section.length
result=ini.section[section_index].pair.length
endif
EndFunction result
Function INI_Section(ini as tINI_File, section_index)
result as string
if section_index<=ini.section.length
result=ini.section[section_index].name
endif
EndFunction result
Function INI_Key(ini as tINI_File, section_index, key_index)
result as string
section_max=ini.section.length
key_max=ini.section[section_index].pair.length
if section_index<=section_max and key_index<=key_max
result=ini.section[section_index].pair[key_index].key
endif
EndFunction result
Function INI_Value(ini as tINI_File, section_index, key_index)
result as string
section_max=ini.section.length
key_max=ini.section[section_index].pair.length
if section_index<=section_max and key_index<=key_max
result=ini.section[section_index].pair[key_index].value
endif
EndFunction result
Example
#Include "ini_include.agc"
// Exmaple
tmp_ini AS tINI_File
if INI_Create(tmp_ini, "test.ini")
// add 5 sections wach with 5 values
for i=1 to 5
INI_AddComment(tmp_ini, "This is a Comment")
for x=1 to 5
INI_SetValue(tmp_ini, "section"+Str(i), "key"+Str(x), "key"+Str(x)+" value")
next
next
// add 5 more values to each section
for i=1 to 5
for x=6 to 10
INI_SetValue(tmp_ini, "section"+Str(i), "key"+Str(x), "key"+Str(x)+" value")
next
next
INI_Save(tmp_ini)
INI_Close(tmp_ini)
endif
INI_Open(tmp_ini, "test.ini")
do
s$=INI_GetValue(tmp_ini, "section3", "key3", "def value")
Print(s$)
for section_index=0 to INI_SectionCount(tmp_ini)
for key_index=0 to INI_KeyCount(tmp_ini, section_index)
section_name$=INI_Section(tmp_ini, section_index)
key$=INI_Key(tmp_ini, section_index, key_index)
value$=INI_Value(tmp_ini, section_index, key_index)
text$ = "["+section_name$+"]"+key$+" = "+value$
Print(text$)
next
next
Sync()
loop
INI_Close(tmp_ini)