tparsecfg.nim 4.4 KB

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