tparsecfg.nim 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. discard """
  2. targets: "c js"
  3. """
  4. import parsecfg, streams, sequtils
  5. import std/assertions
  6. when not defined(js):
  7. from stdtest/specialpaths import buildDir
  8. import os
  9. # bug #6046
  10. block:
  11. var config = newConfig()
  12. config.setSectionKey("foo", "bar", "-1")
  13. config.setSectionKey("foo", "foo", "abc")
  14. const file = buildDir / "tparsecfg.ini"
  15. config.writeConfig(file)
  16. # file now contains
  17. # [foo]
  18. # bar=-1
  19. # foo=abc
  20. var config2 = loadConfig(file)
  21. let bar = config2.getSectionValue("foo", "bar")
  22. let foo = config2.getSectionValue("foo", "foo")
  23. doAssert(bar == "-1")
  24. doAssert(foo == "abc")
  25. ## Creating a configuration file.
  26. var dict1 = newConfig()
  27. dict1.setSectionKey("", "charset", "utf-8")
  28. dict1.setSectionKey("Package", "name", "hello")
  29. dict1.setSectionKey("Package", "--threads", "on")
  30. dict1.setSectionKey("Author", "name", "lihf8515")
  31. dict1.setSectionKey("Author", "qq", "10214028")
  32. dict1.setSectionKey("Author", "email", "lihaifeng@wxm.com")
  33. var ss = newStringStream()
  34. dict1.writeConfig(ss)
  35. ## Reading a configuration file.
  36. let dict2 = loadConfig(newStringStream(ss.data))
  37. doAssert dict2.getSectionValue("", "charset") == "utf-8"
  38. doAssert dict2.getSectionValue("Package", "--threads") == "on"
  39. doAssert dict2.getSectionValue("Package", "name") == "hello"
  40. doAssert dict2.getSectionValue("Author", "name") == "lihf8515"
  41. doAssert dict2.getSectionValue("Author", "qq") == "10214028"
  42. doAssert dict2.getSectionValue("Author", "email") == "lihaifeng@wxm.com"
  43. doAssert toSeq(dict2.sections) == @["", "Package", "Author"]
  44. ## Modifying a configuration file.
  45. var dict3 = loadConfig(newStringStream(ss.data))
  46. dict3.setSectionKey("Author", "name", "lhf")
  47. doAssert $dict3 == """charset=utf-8
  48. [Package]
  49. name=hello
  50. --threads:on
  51. [Author]
  52. name=lhf
  53. qq=10214028
  54. email="lihaifeng@wxm.com"
  55. """
  56. ## Deleting a section key in a configuration file.
  57. var dict4 = loadConfig(newStringStream(ss.data))
  58. dict4.delSectionKey("Author", "email")
  59. doAssert $dict4 == """charset=utf-8
  60. [Package]
  61. name=hello
  62. --threads:on
  63. [Author]
  64. name=lihf8515
  65. qq=10214028
  66. """
  67. block:
  68. var dict = loadConfig(newStringStream("""[Simple Values]
  69. key=value
  70. spaces in keys=allowed
  71. spaces in values=allowed as well
  72. spaces around the delimiter = obviously
  73. you can also use : to delimit keys from values
  74. [All Values Are Strings]
  75. values like this: 19990429
  76. or this: 3.14159265359
  77. are they treated as numbers : no
  78. integers floats and booleans are held as: strings
  79. can use the API to get converted values directly: true
  80. [No Values]
  81. key_without_value
  82. # empty string value is not allowed =
  83. [ Seletion A ]
  84. space around section name will be ignored
  85. [You can use comments]
  86. # like this
  87. ; or this
  88. # By default only in an empty line.
  89. # Inline comments can be harmful because they prevent users
  90. # from using the delimiting characters as parts of values.
  91. # That being said, this can be customized.
  92. [Sections Can Be Indented]
  93. can_values_be_as_well = True
  94. does_that_mean_anything_special = False
  95. purpose = formatting for readability
  96. # Did I mention we can indent comments, too?
  97. """)
  98. )
  99. let section1 = "Simple Values"
  100. doAssert dict.getSectionValue(section1, "key") == "value"
  101. doAssert dict.getSectionValue(section1, "spaces in keys") == "allowed"
  102. doAssert dict.getSectionValue(section1, "spaces in values") == "allowed as well"
  103. doAssert dict.getSectionValue(section1, "spaces around the delimiter") == "obviously"
  104. doAssert dict.getSectionValue(section1, "you can also use") == "to delimit keys from values"
  105. let section2 = "All Values Are Strings"
  106. doAssert dict.getSectionValue(section2, "values like this") == "19990429"
  107. doAssert dict.getSectionValue(section2, "or this") == "3.14159265359"
  108. doAssert dict.getSectionValue(section2, "are they treated as numbers") == "no"
  109. doAssert dict.getSectionValue(section2, "integers floats and booleans are held as") == "strings"
  110. doAssert dict.getSectionValue(section2, "can use the API to get converted values directly") == "true"
  111. let section3 = "Seletion A"
  112. doAssert dict.getSectionValue(section3,
  113. "space around section name will be ignored", "not an empty value") == ""
  114. let section4 = "Sections Can Be Indented"
  115. doAssert dict.getSectionValue(section4, "can_values_be_as_well") == "True"
  116. doAssert dict.getSectionValue(section4, "does_that_mean_anything_special") == "False"
  117. doAssert dict.getSectionValue(section4, "purpose") == "formatting for readability"