mirror of
https://github.com/nim-lang/Nim.git
synced 2026-04-18 13:30:33 +00:00
parsecfg: handle streams and strings when saving or loading configuration (#5554)
This commit is contained in:
committed by
Andreas Rumpf
parent
4ba6f9808e
commit
3374bcb373
@@ -1,4 +1,31 @@
|
||||
import parsecfg
|
||||
discard """
|
||||
output: '''
|
||||
utf-8
|
||||
on
|
||||
hello
|
||||
lihf8515
|
||||
10214028
|
||||
lihaifeng@wxm.com
|
||||
===
|
||||
charset=utf-8
|
||||
[Package]
|
||||
name=hello
|
||||
--threads:on
|
||||
[Author]
|
||||
name=lhf
|
||||
qq=10214028
|
||||
email="lihaifeng@wxm.com"
|
||||
===
|
||||
charset=utf-8
|
||||
[Package]
|
||||
name=hello
|
||||
--threads:on
|
||||
[Author]
|
||||
name=lihf8515
|
||||
qq=10214028
|
||||
'''
|
||||
"""
|
||||
import parsecfg, streams
|
||||
|
||||
## Creating a configuration file.
|
||||
var dict1=newConfig()
|
||||
@@ -8,10 +35,11 @@ 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")
|
||||
var ss = newStringStream()
|
||||
dict1.writeConfig(ss)
|
||||
|
||||
## Reading a configuration file.
|
||||
var dict2 = loadConfig("config.ini")
|
||||
var dict2 = loadConfig(newStringStream(ss.data))
|
||||
var charset = dict2.getSectionValue("","charset")
|
||||
var threads = dict2.getSectionValue("Package","--threads")
|
||||
var pname = dict2.getSectionValue("Package","name")
|
||||
@@ -25,13 +53,17 @@ echo name
|
||||
echo qq
|
||||
echo email
|
||||
|
||||
echo "==="
|
||||
|
||||
## Modifying a configuration file.
|
||||
var dict3 = loadConfig("config.ini")
|
||||
var dict3 = loadConfig(newStringStream(ss.data))
|
||||
dict3.setSectionKey("Author","name","lhf")
|
||||
dict3.writeConfig("config.ini")
|
||||
write(stdout, $dict3)
|
||||
|
||||
echo "==="
|
||||
|
||||
## Deleting a section key in a configuration file.
|
||||
var dict4 = loadConfig("config.ini")
|
||||
var dict4 = loadConfig(newStringStream(ss.data))
|
||||
dict4.delSectionKey("Author","email")
|
||||
dict4.writeConfig("config.ini")
|
||||
write(stdout, $dict4)
|
||||
|
||||
|
||||
Reference in New Issue
Block a user