tparscfg.nim 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. discard """
  2. output: '''
  3. utf-8
  4. on
  5. hello
  6. lihf8515
  7. 10214028
  8. lihaifeng@wxm.com
  9. ===
  10. charset=utf-8
  11. [Package]
  12. name=hello
  13. --threads:on
  14. [Author]
  15. name=lhf
  16. qq=10214028
  17. email="lihaifeng@wxm.com"
  18. ===
  19. charset=utf-8
  20. [Package]
  21. name=hello
  22. --threads:on
  23. [Author]
  24. name=lihf8515
  25. qq=10214028
  26. '''
  27. """
  28. import parsecfg, streams
  29. ## Creating a configuration file.
  30. var dict1=newConfig()
  31. dict1.setSectionKey("","charset","utf-8")
  32. dict1.setSectionKey("Package","name","hello")
  33. dict1.setSectionKey("Package","--threads","on")
  34. dict1.setSectionKey("Author","name","lihf8515")
  35. dict1.setSectionKey("Author","qq","10214028")
  36. dict1.setSectionKey("Author","email","lihaifeng@wxm.com")
  37. var ss = newStringStream()
  38. dict1.writeConfig(ss)
  39. ## Reading a configuration file.
  40. var dict2 = loadConfig(newStringStream(ss.data))
  41. var charset = dict2.getSectionValue("","charset")
  42. var threads = dict2.getSectionValue("Package","--threads")
  43. var pname = dict2.getSectionValue("Package","name")
  44. var name = dict2.getSectionValue("Author","name")
  45. var qq = dict2.getSectionValue("Author","qq")
  46. var email = dict2.getSectionValue("Author","email")
  47. echo charset
  48. echo threads
  49. echo pname
  50. echo name
  51. echo qq
  52. echo email
  53. echo "==="
  54. ## Modifying a configuration file.
  55. var dict3 = loadConfig(newStringStream(ss.data))
  56. dict3.setSectionKey("Author","name","lhf")
  57. write(stdout, $dict3)
  58. echo "==="
  59. ## Deleting a section key in a configuration file.
  60. var dict4 = loadConfig(newStringStream(ss.data))
  61. dict4.delSectionKey("Author","email")
  62. write(stdout, $dict4)