parsecfg: handle streams and strings when saving or loading configuration (#5554)
This commit is contained in:
parent
4ba6f9808e
commit
3374bcb373
2 changed files with 124 additions and 79 deletions
|
|
@ -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)
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue