mirror of
https://github.com/nim-lang/Nim.git
synced 2026-04-21 06:45:27 +00:00
Update tparscfg.nim
This commit is contained in:
@@ -1,25 +1,37 @@
|
||||
import parsecfg
|
||||
|
||||
import
|
||||
os, parsecfg, strutils, streams
|
||||
## Creating a configuration file.
|
||||
var dict1=newConfig()
|
||||
dict1.setSectionKey("","charset","utf-8")
|
||||
dict1.setSectionKey("Package","name","hello")
|
||||
dict1.setSectionKey("Package","--threads","on")
|
||||
dict1.setSectionKey("Author","name","lihf8515")
|
||||
dict1.setSectionKey("Author","qq","10214028")
|
||||
dict1.setSectionKey("Author","email","lihaifeng@wxm.com")
|
||||
dict1.writeConfig("config.ini")
|
||||
|
||||
## Reading a configuration file.
|
||||
var dict2 = loadConfig("config.ini")
|
||||
var charset = dict2.getSectionValue("","charset")
|
||||
var threads = dict2.getSectionValue("Package","--threads")
|
||||
var pname = dict2.getSectionValue("Package","name")
|
||||
var name = dict2.getSectionValue("Author","name")
|
||||
var qq = dict2.getSectionValue("Author","qq")
|
||||
var email = dict2.getSectionValue("Author","email")
|
||||
echo charset
|
||||
echo threads
|
||||
echo pname
|
||||
echo name
|
||||
echo qq
|
||||
echo email
|
||||
|
||||
## Modifying a configuration file.
|
||||
var dict3 = loadConfig("config.ini")
|
||||
dict3.setSectionKey("Author","name","lhf")
|
||||
dict3.writeConfig("config.ini")
|
||||
|
||||
## Deleting a section key in a configuration file.
|
||||
var dict4 = loadConfig("config.ini")
|
||||
dict4.delSectionKey("Author","email")
|
||||
dict4.writeConfig("config.ini")
|
||||
|
||||
var f = newFileStream(paramStr(1), fmRead)
|
||||
if f != nil:
|
||||
var p: TCfgParser
|
||||
open(p, f, paramStr(1))
|
||||
while true:
|
||||
var e = next(p)
|
||||
case e.kind
|
||||
of cfgEof:
|
||||
echo("EOF!")
|
||||
break
|
||||
of cfgSectionStart: ## a ``[section]`` has been parsed
|
||||
echo("new section: " & e.section)
|
||||
of cfgKeyValuePair:
|
||||
echo("key-value-pair: " & e.key & ": " & e.value)
|
||||
of cfgOption:
|
||||
echo("command: " & e.key & ": " & e.value)
|
||||
of cfgError:
|
||||
echo(e.msg)
|
||||
close(p)
|
||||
else:
|
||||
echo("cannot open: " & paramStr(1))
|
||||
|
||||
Reference in New Issue
Block a user