iniファイルの読み書きのロジック

 iniファイルの読み書きは、「セクション」「キー」「値」という3階層の構造を扱うだけなので、とてもシンプルです。


🧩 iniファイルの基本構造

[SectionName]
Key1=Value1
Key2=Value2
  • [Section] … グループ名
  • Key=Value … 設定項目と値
  • 空行や ; で始まる行はコメント扱い

🔍 読み込みロジック(基本アルゴリズム)

ini を読むときの典型的な流れはこうなります。

  1. ファイルを1行ずつ読む
  2. 行頭の空白をトリムする
  3. 行が空行 or コメントならスキップ
  4. [ で始まり ] で終わる行 → セクション名として記録
  5. = を含む行 → キーと値に分割して現在のセクションに登録
  6. セクションが無い状態でキーが来たら「無名セクション」として扱う実装もある

データ構造としては、以下のような辞書が一般的です。

Dictionary<string, Dictionary<string, string>>

✏️ 書き込みロジック(基本アルゴリズム)

書くときは逆に、

  1. セクションごとに [Section] を書く
  2. その下に Key=Value を列挙する
  3. セクション間は空行を入れることが多い

例:

[General]
UserName=tam2
Theme=Dark

[Window]
Width=800
Height=600






📘 Small Basic での ini 読み込みロジック

Small Basic は辞書型がないので、
Section_Key のように連結したキー名で保存するのが一番扱いやすい。


🔍 読み込みコード例(ini → 配列に格納)

file = "config.ini"
contents = File.ReadContents(file)
lines = Text.Split(contents, Text.GetCharacter(10))

currentSection = ""

For i = 1 To Array.GetItemCount(lines)
  line = Text.Trim(lines[i])

  ' 空行・コメントはスキップ
  If line = "" Then
    Continue For
  EndIf
  If Text.StartsWith(line, ";") Then
    Continue For
  EndIf

  ' セクション判定
  If Text.StartsWith(line, "[") And Text.EndsWith(line, "]") Then
    currentSection = Text.GetSubText(line, 2, Text.GetLength(line) - 2)
    Continue For
  EndIf

  ' キー=値
  pos = Text.GetIndexOf(line, "=")
  If pos > 0 Then
    key = Text.GetSubText(line, 1, pos - 1)
    value = Text.GetSubText(line, pos + 1, Text.GetLength(line) - pos)

    fullKey = currentSection + "_" + key
    ini[fullKey] = value
  EndIf
EndFor

使い方例

TextWindow.WriteLine(ini["General_UserName"])
TextWindow.WriteLine(ini["Window_Width"])

✏️ 書き込みロジック(配列 → ini ファイル)

Small Basic は辞書の列挙ができないので、
自分でセクションとキーを管理するリストを作るのがコツ。

例として、以下のように手動で書き出す方法を示すね。

text = ""

text = text + "[General]" + Text.GetCharacter(10)
text = text + "UserName=" + ini["General_UserName"] + Text.GetCharacter(10)
text = text + "Theme=" + ini["General_Theme"] + Text.GetCharacter(10)
text = text + Text.GetCharacter(10)

text = text + "[Window]" + Text.GetCharacter(10)
text = text + "Width=" + ini["Window_Width"] + Text.GetCharacter(10)
text = text + "Height=" + ini["Window_Height"] + Text.GetCharacter(10)

File.WriteContents("config.ini", text)

🧠 Small Basic での実装ポイント

  • Small Basic は辞書が弱いので
    「セクション_キー」形式で配列に保存する」のが最も扱いやすい
  • 読み込みは File.ReadContents → 行分割 → パース
  • 書き込みは文字列を組み立てて File.WriteContents






📦 Small Basic 用「INI モジュール」

セクションとキーを Section_Key 形式で内部保存する方式で作ってある。


🔍 IniRead(section, key)

Function IniRead
  section = IniRead_Section
  key = IniRead_Key
  fullKey = section + "_" + key

  If ini[fullKey] = "" Then
    IniRead = ""
  Else
    IniRead = ini[fullKey]
  EndIf
EndFunction

✏️ IniWrite(section, key, value)

Sub IniWrite
  section = IniWrite_Section
  key = IniWrite_Key
  value = IniWrite_Value

  fullKey = section + "_" + key
  ini[fullKey] = value
EndSub

📘 IniLoad(filename)(ini → 配列)

Sub IniLoad
  file = IniLoad_File
  contents = File.ReadContents(file)
  lines = Text.Split(contents, Text.GetCharacter(10))

  currentSection = ""

  For i = 1 To Array.GetItemCount(lines)
    line = Text.Trim(lines[i])

    If line = "" Then
      Continue For
    EndIf
    If Text.StartsWith(line, ";") Then
      Continue For
    EndIf

    If Text.StartsWith(line, "[") And Text.EndsWith(line, "]") Then
      currentSection = Text.GetSubText(line, 2, Text.GetLength(line) - 2)
      Continue For
    EndIf

    pos = Text.GetIndexOf(line, "=")
    If pos > 0 Then
      key = Text.GetSubText(line, 1, pos - 1)
      value = Text.GetSubText(line, pos + 1, Text.GetLength(line) - pos)

      fullKey = currentSection + "_" + key
      ini[fullKey] = value
    EndIf
  EndFor
EndSub

💾 IniSave(filename)(配列 → ini)

Small Basic は辞書列挙ができないので、
セクション一覧とキー一覧を自分で管理する方式が一番安定する。

テンプレートとしてこう書ける:

Sub IniSave
  file = IniSave_File
  text = ""

  ' General セクション
  text = text + "[General]" + Text.GetCharacter(10)
  text = text + "UserName=" + ini["General_UserName"] + Text.GetCharacter(10)
  text = text + "Theme=" + ini["General_Theme"] + Text.GetCharacter(10)
  text = text + Text.GetCharacter(10)

  ' Window セクション
  text = text + "[Window]" + Text.GetCharacter(10)
  text = text + "Width=" + ini["Window_Width"] + Text.GetCharacter(10)
  text = text + "Height=" + ini["Window_Height"] + Text.GetCharacter(10)

  File.WriteContents(file, text)
EndSub

🎮 使い方例

IniLoad_File = "config.ini"
IniLoad()

name = IniRead("General", "UserName")
width = IniRead("Window", "Width")

IniWrite_Section = "General"
IniWrite_Key = "UserName"
IniWrite_Value = "tam2"
IniWrite()

IniSave_File = "config.ini"
IniSave()

コメント

このブログの人気の投稿

宇宙的にみると地球はどのくらいの速さで宇宙を移動しているの?

HTA (HTML Application)

HTA+JScriptでウインドウのサイズ変更するプログラム

教えてGemini先生、各国の言語によって頭の良さというか脳に効率の良い言語はありますか?

【VBScript】文字列の前後の空白を削除

バイトテロとバカッターの対決

Googleドライブにエッチな動画入れておくとバンされると聞きましたが?

ツイフェミってなに?おじさんでもわかるように教えてGemini先生

右翼・左翼とは?教えてGemini先生