Immer wieder im Einsatz: INI-Dateien.
Ich habe mal noch ergänzt um den Default-Wert beim Lesen eines Key.
Ich habe das gerade gebraucht für mein großes Projekt mit Nim (ein MP3-Player). Insofern schonmal: Vielen Dank für das Beispiel! ![]()
![]()
Allerdings kann man das mit dem Defaultwert auch einfacher hinkriegen:
Code
import parsecfg, os, strutils, tables
proc getSectionValueOrDefault*(dict: Config, section, key: string, default: string = ""): string =
result = default
if dict.hasKey(section):
if dict[section].hasKey(key): result = dict[section][key]
# Einen Anwendungsordner im ConfigDir erstellen
# Bei Windows: getConfigDir() = "c:\Users\<user>\AppData\Roaming\"
let appDir = getConfigDir() & "test\\"
if not existsDir(appDir): createDir(appDir) # wenn er nicht existiert, anlegen
let iniFile = appDir & "test.ini" # die Inidatei mit Pfad
var
dict: Config
test: int
try: # versuchen, die Inidatei zu laden
dict = loadConfig(iniFile)
except IOError: # wenn nicht vorhanden
echo "Inidatei nicht vorhanden! Erstelle neue Konfiguration."
dict = newConfig()
test = parseInt(getSectionValueOrDefault(dict, "config", "test", "10")) # Defaultwert = 10
echo test
test = 25 # vor dem speichern den Wert aendern
dict.setSectionKey("config", "test", $test)
dict.writeConfig(iniFile)
Alles anzeigen
Edit:
Man kann die Prozedur auch überladen und kann sie dann mit unterschiedlichen Datentypen als Defaultwert aufrufen:
Code
import parsecfg, strutils, tables
proc getSectionValueOrDefault*(dict: Config, section, key: string, default: string): string =
result = default
if dict.hasKey(section):
if dict[section].hasKey(key): result = dict[section][key]
proc getSectionValueOrDefault*(dict: Config, section, key: string): string =
result = getSectionValueOrDefault(dict, section, key, "")
proc getSectionValueOrDefault*(dict: Config, section, key: string, default: int): int =
result = parseInt(getSectionValueOrDefault(dict, section, key, $default))
proc getSectionValueOrDefault*(dict: Config, section, key: string, default: float): float =
result = parseFloat(getSectionValueOrDefault(dict, section, key, $default))
proc getSectionValueOrDefault*(dict: Config, section, key: string, default: bool): bool =
result = getSectionValueOrDefault(dict, section, key, $default) == "true"
var
dict = newConfig()
test0: string
test1: string
test2: int
test3: float
test4: bool
test0 = getSectionValueOrDefault(dict, "config", "test0") # ohne Defaultwert ist die Rueckgabe ein Leerstring
test1 = getSectionValueOrDefault(dict, "config", "test1", "35") # Defaultwert = 35 als String
test2 = getSectionValueOrDefault(dict, "config", "test2", 10) # Defaultwert = 10 als Int
test3 = getSectionValueOrDefault(dict, "config", "test3", 3.14) # Defaultwert = 3.14 als Float
test4 = getSectionValueOrDefault(dict, "config", "test4", false) # Defaultwert = false als Bool
echo "'", test0, "'", " -> ", test0.typeof()
echo test1, " -> ", test1.typeof()
echo test2, " -> ", test2.typeof()
echo test3, " -> ", test3.typeof()
echo test4, " -> ", test4.typeof()
Alles anzeigen