intro.bbdoc 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. Ini files (short for initialization files) are simple text files used to store settings and configurations
  2. for applications. They are organized into sections, each containing key-value pairs known as properties. Ini
  3. files are a popular choice for storing settings because they are easy to read and modify by both humans and programs.
  4. In this introduction, we will discuss the Ini file format in detail, covering its typical use cases,
  5. properties and sections, and how to use the module in BlitzMax to load, save, and edit Ini files.
  6. By the end of this guide, you will have the knowledge to create your own Ini files, either with a separate editor or
  7. using the `text.ini` module in BlitzMax.
  8. ## Understanding the Ini File Format
  9. An Ini file is composed of sections, where each section starts with a section name enclosed in square brackets
  10. (e.g., [SectionName]). Underneath each section are properties, which are key-value pairs separated by an equals
  11. sign (=). For example:
  12. ```ini
  13. [Settings]
  14. Volume=80
  15. FullScreen=False
  16. Resolution=1920x1080
  17. [UserProfile]
  18. Username=JohnDoe
  19. [email protected]
  20. ```
  21. In this example, there are two sections, "Settings" and "UserProfile". The "Settings" section contains three
  22. properties: Volume, FullScreen, and Resolution.
  23. ## Using the module
  24. The `text.ini` module provides a simple and efficient way to work with Ini files in BlitzMax.
  25. The following subsections will guide you through loading, saving, and editing Ini files using the module.
  26. ### Loading an Ini File
  27. To load an Ini file from disk, use the `Load` function, providing the file path:
  28. ```blitzmax
  29. Local iniFile:TIni = TIni.Load("config.ini")
  30. ```
  31. Alternatively, to load an Ini file from a #TStream, use the Load function with the stream as the argument:
  32. ```blitzmax
  33. Local stream:TStream = ReadFile("config.ini")
  34. Local iniFile:TIni = TIni.Load(stream)
  35. ```
  36. ### Saving an Ini File
  37. To save an Ini file to disk, use the `Save` method and provide the file path:
  38. ```blitzmax
  39. iniFile.Save("config.ini")
  40. ```
  41. To save an Ini file to a #TStream, use the `Save` method with the stream as the argument:
  42. ```blitzmax
  43. Local stream:TStream = WriteFile("config.ini")
  44. iniFile.Save(stream)
  45. ```
  46. ### Working with Sections and Properties
  47. To create a new section, use the `AddSection` method:
  48. ```blitzmax
  49. Local section:TIniSection = iniFile.AddSection("Settings")
  50. ```
  51. To find a section by its name, use the `FindSection` method:
  52. ```blitzmax
  53. Local section:TIniSection = iniFile.FindSection("Settings")
  54. ```
  55. To add a new property to a section, use the `AddProperty` method:
  56. ```blitzmax
  57. section.AddProperty("Volume", "80")
  58. ```
  59. To find a property by its name, use the `FindProperty` method:
  60. ```blitzmax
  61. Local property:TIniProperty = section.FindProperty("Volume")
  62. ```
  63. To get or set a property's value, use the `GetValue` and `SetValue` methods:
  64. ```blitzmax
  65. Local volume:Int = Int(property.GetValue())
  66. property.SetValue("85")
  67. ```
  68. ### Cleaning Up
  69. When you are done working with an Ini file, call the Free method to release any resources used by the #TIni instance:
  70. ```blitzmax
  71. iniFile.Free()
  72. ```
  73. Here is a complete example that demonstrates how to create, edit, and save an Ini file using the text.ini module:
  74. ```blitzmax
  75. SuperStrict
  76. Framework Text.Ini
  77. ' Create a new empty TIni instance
  78. Local iniFile:TIni = New TIni
  79. ' Add sections and properties
  80. Local settingsSection:TIniSection = iniFile.AddSection("Settings")
  81. settingsSection.AddProperty("Volume", "80")
  82. settingsSection.AddProperty("FullScreen", "False")
  83. settingsSection.AddProperty("Resolution", "1920x1080")
  84. Local userProfileSection:TIniSection = iniFile.AddSection("UserProfile")
  85. userProfileSection.AddProperty("Username", "JohnDoe")
  86. userProfileSection.AddProperty("Email", "[email protected]")
  87. ' Save the Ini file to disk
  88. iniFile.Save("config.ini")
  89. ' Load the Ini file from disk
  90. Local loadedIniFile:TIni = TIni.Load("config.ini")
  91. ' Retrieve and modify a property
  92. Local section:TIniSection = loadedIniFile.FindSection("Settings")
  93. Local property:TIniProperty = section.FindProperty("Volume")
  94. Local volume:Int = Int(property.GetValue())
  95. property.SetValue(String(volume + 5))
  96. ' Save the modified Ini file to disk
  97. loadedIniFile.Save("config_modified.ini")
  98. ' Clean up
  99. iniFile.Free()
  100. loadedIniFile.Free()
  101. ```
  102. This example creates a new Ini file with two sections and several properties, saves it to disk, loads it back,
  103. modifies a property, and then saves the modified Ini file to a new file. The result is two Ini files: "config.ini"
  104. containing the original settings, and "config_modified.ini" containing the modified settings.