|
@@ -25,12 +25,16 @@ bbdoc: An INI reader/writer.
|
|
End Rem
|
|
End Rem
|
|
Module Text.Ini
|
|
Module Text.Ini
|
|
|
|
|
|
-ModuleInfo "Version: 1.01"
|
|
|
|
|
|
+ModuleInfo "Version: 1.02"
|
|
ModuleInfo "Author: Bruce A Henderson"
|
|
ModuleInfo "Author: Bruce A Henderson"
|
|
ModuleInfo "License: MIT"
|
|
ModuleInfo "License: MIT"
|
|
ModuleInfo "ini.h - Copyright (c) 2015 Mattias Gustavsson"
|
|
ModuleInfo "ini.h - Copyright (c) 2015 Mattias Gustavsson"
|
|
ModuleInfo "Copyright: 2022-2023 Bruce A Henderson"
|
|
ModuleInfo "Copyright: 2022-2023 Bruce A Henderson"
|
|
|
|
|
|
|
|
+ModuleInfo "History: 1.02"
|
|
|
|
+ModuleInfo "History: Fixed SetName update the name copy"
|
|
|
|
+ModuleInfo "History: Added some helper methods"
|
|
|
|
+ModuleInfo "History: Added tests"
|
|
ModuleInfo "History: 1.01"
|
|
ModuleInfo "History: 1.01"
|
|
ModuleInfo "History: Fixed missing terminator"
|
|
ModuleInfo "History: Fixed missing terminator"
|
|
ModuleInfo "History: 1.00"
|
|
ModuleInfo "History: 1.00"
|
|
@@ -77,6 +81,7 @@ Type TIni
|
|
|
|
|
|
Rem
|
|
Rem
|
|
bbdoc: Loads an ini file at the given @path.
|
|
bbdoc: Loads an ini file at the given @path.
|
|
|
|
+ returns: The loaded ini file, or #Null if the file could not be loaded.
|
|
End Rem
|
|
End Rem
|
|
Function Load:TIni(path:String)
|
|
Function Load:TIni(path:String)
|
|
Local stream:TStream = ReadStream(path)
|
|
Local stream:TStream = ReadStream(path)
|
|
@@ -89,6 +94,7 @@ Type TIni
|
|
|
|
|
|
Rem
|
|
Rem
|
|
bbdoc: Loads an ini file from the given @stream.
|
|
bbdoc: Loads an ini file from the given @stream.
|
|
|
|
+ returns: The loaded ini file from the stream, or #Null if the file could not be loaded.
|
|
about: Does not close the #TStream.
|
|
about: Does not close the #TStream.
|
|
End Rem
|
|
End Rem
|
|
Function Load:TIni(stream:TStream)
|
|
Function Load:TIni(stream:TStream)
|
|
@@ -140,6 +146,8 @@ Type TIni
|
|
|
|
|
|
Rem
|
|
Rem
|
|
bbdoc: Returns the number of sections.
|
|
bbdoc: Returns the number of sections.
|
|
|
|
+ about: There's at least one section in an ini file (the global section), but there can be many more,
|
|
|
|
+ each specified in the file by the section name wrapped in square brackets `[ ]`
|
|
End Rem
|
|
End Rem
|
|
Method CountSections:Int()
|
|
Method CountSections:Int()
|
|
Return ini_section_count(iniPtr)
|
|
Return ini_section_count(iniPtr)
|
|
@@ -147,17 +155,19 @@ Type TIni
|
|
|
|
|
|
Rem
|
|
Rem
|
|
bbdoc: Returns the section at the given @index, or #Null if the index is out of bounds.
|
|
bbdoc: Returns the section at the given @index, or #Null if the index is out of bounds.
|
|
|
|
+ about: #INI_GLOBAL_SECTION can be used to get the global section.
|
|
End Rem
|
|
End Rem
|
|
Method GetSection:TIniSection(index:Int)
|
|
Method GetSection:TIniSection(index:Int)
|
|
If index < 0 Or index >= ini_section_count(iniPtr) Then
|
|
If index < 0 Or index >= ini_section_count(iniPtr) Then
|
|
Return Null
|
|
Return Null
|
|
End If
|
|
End If
|
|
|
|
+
|
|
Return sections[index]
|
|
Return sections[index]
|
|
End Method
|
|
End Method
|
|
|
|
|
|
Rem
|
|
Rem
|
|
bbdoc: Finds a section by @name.
|
|
bbdoc: Finds a section by @name.
|
|
- returns: The named section, or Null if not found.
|
|
|
|
|
|
+ returns: The named section, or #Null if not found.
|
|
End Rem
|
|
End Rem
|
|
Method FindSection:TIniSection(name:String)
|
|
Method FindSection:TIniSection(name:String)
|
|
name = name.ToUpper()
|
|
name = name.ToUpper()
|
|
@@ -165,6 +175,7 @@ Type TIni
|
|
If Not section.upperName Then
|
|
If Not section.upperName Then
|
|
section.upperName = section.name.ToUpper()
|
|
section.upperName = section.name.ToUpper()
|
|
End If
|
|
End If
|
|
|
|
+
|
|
If name = section.upperName Then
|
|
If name = section.upperName Then
|
|
Return section
|
|
Return section
|
|
End If
|
|
End If
|
|
@@ -193,6 +204,12 @@ Type TIni
|
|
Return
|
|
Return
|
|
End If
|
|
End If
|
|
|
|
|
|
|
|
+ If index = 0 Then ' we just clear the global section, rather than remove it.
|
|
|
|
+ Local s:TIniSection = GetSection(0)
|
|
|
|
+ s.Clear()
|
|
|
|
+ Return
|
|
|
|
+ End If
|
|
|
|
+
|
|
ini_section_remove(iniPtr, index)
|
|
ini_section_remove(iniPtr, index)
|
|
|
|
|
|
' not the last section? We'll need to fix the array
|
|
' not the last section? We'll need to fix the array
|
|
@@ -213,6 +230,67 @@ Type TIni
|
|
End If
|
|
End If
|
|
End Method
|
|
End Method
|
|
|
|
|
|
|
|
+ Rem
|
|
|
|
+ bbdoc: Sets the value for the property with the given @section and @name.
|
|
|
|
+ about: If the section or property does not exist, it is created.
|
|
|
|
+ End Rem
|
|
|
|
+ Method Set(section:String, name:String, value:String)
|
|
|
|
+ Local s:TIniSection
|
|
|
|
+ If section.Trim() = Null Then
|
|
|
|
+ s = GetSection(INI_GLOBAL_SECTION)
|
|
|
|
+ Else
|
|
|
|
+ s = FindSection(section)
|
|
|
|
+ End If
|
|
|
|
+
|
|
|
|
+ If Not s Then
|
|
|
|
+ s = AddSection(section)
|
|
|
|
+ End If
|
|
|
|
+
|
|
|
|
+ If s Then
|
|
|
|
+ Local p:TIniProperty = s.FindProperty(name)
|
|
|
|
+ If Not p Then
|
|
|
|
+ p = s.AddProperty(name, value)
|
|
|
|
+ End If
|
|
|
|
+ If p Then
|
|
|
|
+ p.SetValue(value)
|
|
|
|
+ End If
|
|
|
|
+ End If
|
|
|
|
+ End Method
|
|
|
|
+
|
|
|
|
+ Rem
|
|
|
|
+ bbdoc: Sets the value for the property with the given @name in the global section.
|
|
|
|
+ about: If the property does not exist, it is created.
|
|
|
|
+ End Rem
|
|
|
|
+ Method Set(name:String, value:String)
|
|
|
|
+ Set(Null, name, value)
|
|
|
|
+ End Method
|
|
|
|
+
|
|
|
|
+ Rem
|
|
|
|
+ bbdoc: Returns the value for the property with the given @section and @name, or #Null if not found.
|
|
|
|
+ End Rem
|
|
|
|
+ Method Get:String(section:String, name:String)
|
|
|
|
+ Local s:TIniSection
|
|
|
|
+ If section = Null Then
|
|
|
|
+ s = GetSection(INI_GLOBAL_SECTION)
|
|
|
|
+ Else
|
|
|
|
+ s = FindSection(section)
|
|
|
|
+ End If
|
|
|
|
+
|
|
|
|
+ If s Then
|
|
|
|
+ Local p:TIniProperty = s.FindProperty(name)
|
|
|
|
+ If p Then
|
|
|
|
+ Return p.GetValue()
|
|
|
|
+ End If
|
|
|
|
+ End If
|
|
|
|
+ End Method
|
|
|
|
+
|
|
|
|
+ Rem
|
|
|
|
+ bbdoc: Returns the value for the property with the given @name in the global section, or #Null if not found.
|
|
|
|
+ End Rem
|
|
|
|
+ Method Get:String(name:String)
|
|
|
|
+ Return Get(Null, name)
|
|
|
|
+ End Method
|
|
|
|
+
|
|
Rem
|
|
Rem
|
|
bbdoc: Frees instance and associated data.
|
|
bbdoc: Frees instance and associated data.
|
|
End Rem
|
|
End Rem
|
|
@@ -239,6 +317,11 @@ End Type
|
|
|
|
|
|
Rem
|
|
Rem
|
|
bbdoc: An ini section.
|
|
bbdoc: An ini section.
|
|
|
|
+about: Represents a distinct section within an INI file.
|
|
|
|
+In the structure of an INI file, sections are used to group related key-value pairs.
|
|
|
|
+A section can either be global, meaning it applies to the entire INI file, or named, identified by a unique header enclosed in
|
|
|
|
+square brackets (e.g., `[section name]`). This type facilitates the parsing, manipulation, and storage of these sections,
|
|
|
|
+allowing for organized access to the contained data.
|
|
End Rem
|
|
End Rem
|
|
Type TIniSection
|
|
Type TIniSection
|
|
|
|
|
|
@@ -271,18 +354,26 @@ Type TIniSection
|
|
Public
|
|
Public
|
|
|
|
|
|
Rem
|
|
Rem
|
|
- bbdoc: Returns the name of the section, or Null if it is the global section.
|
|
|
|
|
|
+ bbdoc: Returns the name of the section, or #Null if it is the global section.
|
|
End Rem
|
|
End Rem
|
|
Method GetName:String()
|
|
Method GetName:String()
|
|
Return name
|
|
Return name
|
|
End Method
|
|
End Method
|
|
|
|
|
|
|
|
+ Rem
|
|
|
|
+ bbdoc: Returns the index of the section.
|
|
|
|
+ about: The global section is represented by the index #INI_GLOBAL_SECTION.
|
|
|
|
+ End Rem
|
|
|
|
+ Method GetIndex:Int()
|
|
|
|
+ Return index
|
|
|
|
+ End Method
|
|
|
|
+
|
|
Rem
|
|
Rem
|
|
bbdoc: Sets the name of the section.
|
|
bbdoc: Sets the name of the section.
|
|
End Rem
|
|
End Rem
|
|
Method SetName(name:String)
|
|
Method SetName(name:String)
|
|
bmx_ini_section_name_set(ini.iniPtr, index, name)
|
|
bmx_ini_section_name_set(ini.iniPtr, index, name)
|
|
- name = bmx_ini_section_name(ini.iniPtr, index)
|
|
|
|
|
|
+ Self.name = bmx_ini_section_name(ini.iniPtr, index)
|
|
upperName = Null
|
|
upperName = Null
|
|
End Method
|
|
End Method
|
|
|
|
|
|
@@ -294,7 +385,7 @@ Type TIniSection
|
|
End Method
|
|
End Method
|
|
|
|
|
|
Rem
|
|
Rem
|
|
- bbdoc: Returns a property at the given @index position, or Null if the @index is out of range.
|
|
|
|
|
|
+ bbdoc: Returns a property at the given @index position, or #Null if the @index is out of range.
|
|
End Rem
|
|
End Rem
|
|
Method GetProperty:TIniProperty(index:Int)
|
|
Method GetProperty:TIniProperty(index:Int)
|
|
If index < 0 Or index >= ini_property_count(ini.iniPtr, Self.index) Then
|
|
If index < 0 Or index >= ini_property_count(ini.iniPtr, Self.index) Then
|
|
@@ -303,6 +394,31 @@ Type TIniSection
|
|
Return properties[index]
|
|
Return properties[index]
|
|
End Method
|
|
End Method
|
|
|
|
|
|
|
|
+ Rem
|
|
|
|
+ bbdoc: Returns the property value with the given @name, or #Null if not found.
|
|
|
|
+ End Rem
|
|
|
|
+ Method Get:String(name:String)
|
|
|
|
+ Local p:TIniProperty = FindProperty(name)
|
|
|
|
+ If p Then
|
|
|
|
+ Return p.GetValue()
|
|
|
|
+ End If
|
|
|
|
+ End Method
|
|
|
|
+
|
|
|
|
+ Rem
|
|
|
|
+ bbdoc: Sets the property with the given @name to the specified @value.
|
|
|
|
+ about: If the property does not exist, it is created.
|
|
|
|
+ End Rem
|
|
|
|
+ Method Set(name:String, value:String)
|
|
|
|
+ Local p:TIniProperty = FindProperty(name)
|
|
|
|
+ If Not p Then
|
|
|
|
+ p = AddProperty(name, value)
|
|
|
|
+ End If
|
|
|
|
+
|
|
|
|
+ If p Then
|
|
|
|
+ p.SetValue(value)
|
|
|
|
+ End If
|
|
|
|
+ End Method
|
|
|
|
+
|
|
Rem
|
|
Rem
|
|
bbdoc: Finds the property with the given @name, or #Null if not found.
|
|
bbdoc: Finds the property with the given @name, or #Null if not found.
|
|
End Rem
|
|
End Rem
|
|
@@ -356,6 +472,23 @@ Type TIniSection
|
|
End If
|
|
End If
|
|
End Method
|
|
End Method
|
|
|
|
|
|
|
|
+ Rem
|
|
|
|
+ bbdoc: Removes all properties from the section.
|
|
|
|
+ End Rem
|
|
|
|
+ Method Clear()
|
|
|
|
+ While properties.Length > 0
|
|
|
|
+ RemoveProperty(properties.Length - 1)
|
|
|
|
+ Wend
|
|
|
|
+ End Method
|
|
|
|
+
|
|
|
|
+ Rem
|
|
|
|
+ bbdoc: Removes the section from the ini file.
|
|
|
|
+ about: On removal, the section is freed and this instance, and all referenced properties should no longer be used.
|
|
|
|
+ End Rem
|
|
|
|
+ Method Remove()
|
|
|
|
+ ini.RemoveSection(index)
|
|
|
|
+ End Method
|
|
|
|
+
|
|
Method Free()
|
|
Method Free()
|
|
properties = Null
|
|
properties = Null
|
|
End Method
|
|
End Method
|
|
@@ -364,6 +497,8 @@ End Type
|
|
|
|
|
|
Rem
|
|
Rem
|
|
bbdoc: An ini property, with a name and value.
|
|
bbdoc: An ini property, with a name and value.
|
|
|
|
+about: An individual key-value pair, commonly referred to as a "property", within an INI file.
|
|
|
|
+Each property comprises a distinct key and its associated value, serving as the basic data element in the INI file structure.
|
|
End Rem
|
|
End Rem
|
|
Type TIniProperty
|
|
Type TIniProperty
|
|
|
|
|
|
@@ -391,23 +526,31 @@ Type TIniProperty
|
|
Return name
|
|
Return name
|
|
End Method
|
|
End Method
|
|
|
|
|
|
|
|
+ Rem
|
|
|
|
+ bbdoc: Returns the index of the property.
|
|
|
|
+ End Rem
|
|
|
|
+ Method GetIndex:Int()
|
|
|
|
+ Return index
|
|
|
|
+ End Method
|
|
|
|
+
|
|
Rem
|
|
Rem
|
|
bbdoc: Sets the name the property.
|
|
bbdoc: Sets the name the property.
|
|
|
|
+ about: Names should not contain the `=` character, as it is used to separate the name from the value.
|
|
End Rem
|
|
End Rem
|
|
Method SetName(name:String)
|
|
Method SetName(name:String)
|
|
bmx_ini_property_name_set(section.ini.iniPtr, section.index, index, name)
|
|
bmx_ini_property_name_set(section.ini.iniPtr, section.index, index, name)
|
|
- name = bmx_ini_property_name(section.ini.iniPtr, section.index, index)
|
|
|
|
|
|
+ Self.name = bmx_ini_property_name(section.ini.iniPtr, section.index, index)
|
|
End Method
|
|
End Method
|
|
|
|
|
|
Rem
|
|
Rem
|
|
- bbdoc: Returns the value for the property
|
|
|
|
|
|
+ bbdoc: Returns the value for the property, or #Null if it is not set.
|
|
End Rem
|
|
End Rem
|
|
Method GetValue:String()
|
|
Method GetValue:String()
|
|
Return bmx_ini_property_value(section.ini.iniPtr, section.index, index)
|
|
Return bmx_ini_property_value(section.ini.iniPtr, section.index, index)
|
|
End Method
|
|
End Method
|
|
|
|
|
|
Rem
|
|
Rem
|
|
- bbdoc: Sets the value for the property
|
|
|
|
|
|
+ bbdoc: Sets the value for the property.
|
|
End Rem
|
|
End Rem
|
|
Method SetValue(value:String)
|
|
Method SetValue(value:String)
|
|
bmx_ini_property_value_set(section.ini.iniPtr, section.index, index, value)
|
|
bmx_ini_property_value_set(section.ini.iniPtr, section.index, index, value)
|
|
@@ -415,6 +558,7 @@ Type TIniProperty
|
|
|
|
|
|
Rem
|
|
Rem
|
|
bbdoc: Removes the property from its section.
|
|
bbdoc: Removes the property from its section.
|
|
|
|
+ about: On removal, the property is freed and this instance should no longer be used.
|
|
End Rem
|
|
End Rem
|
|
Method Remove()
|
|
Method Remove()
|
|
section.RemoveProperty(index)
|
|
section.RemoveProperty(index)
|