tcursor_on_localvar.nim 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. discard """
  2. output: '''Section: common
  3. Param: Floats1
  4. Section: local
  5. Param: Str
  6. Param: Bool
  7. Param: Floats2'''
  8. cmd: '''nim c --gc:arc $file'''
  9. """
  10. # bug #15325
  11. import tables
  12. import strutils
  13. const defaultSection = "***"
  14. type
  15. Config* = ref object
  16. table: OrderedTableRef[string, OrderedTable[string, string]]
  17. # ----------------------------------------------------------------------------------------------------------------------
  18. proc newConfig*(): Config =
  19. result = new(Config)
  20. result.table = newOrderedTable[string, OrderedTable[string, string]]()
  21. # ----------------------------------------------------------------------------------------------------------------------
  22. proc add*(self: Config, param, value, section: string) {.nosinks.} =
  23. let s = if section == "": defaultSection else: section
  24. if not self.table.contains(s):
  25. self.table[s] = initOrderedTable[string, string]()
  26. self.table[s][param] = value
  27. # ----------------------------------------------------------------------------------------------------------------------
  28. proc sections*(self: Config): seq[string] =
  29. for i in self.table.keys:
  30. let s = if i == defaultSection: "" else: i
  31. result.add(s)
  32. # ----------------------------------------------------------------------------------------------------------------------
  33. proc params*(self: Config, section: string): seq[string] =
  34. let s = if section == "": defaultSection else: section
  35. if self.table.contains(s):
  36. for i in self.table[s].keys:
  37. result.add(i)
  38. # ----------------------------------------------------------------------------------------------------------------------
  39. proc extract*(str, start, finish: string): string =
  40. let startPos = str.find(start)
  41. if startPos < 0:
  42. return ""
  43. let endPos = str.find(finish, startPos)
  44. if endPos < 0:
  45. return ""
  46. return str[startPos + start.len() ..< endPos]
  47. # ----------------------------------------------------------------------------------------------------------------------
  48. proc loadString*(self: Config, text: string): tuple[valid: bool, errorInLine: int] {.discardable.} =
  49. self.table.clear()
  50. var data = ""
  51. data = text
  52. var
  53. actualSection = ""
  54. lineCount = 0
  55. for i in splitLines(data):
  56. lineCount += 1
  57. var line = strip(i)
  58. if line.len() == 0:
  59. continue
  60. if line[0] == '#' or line[0] == ';':
  61. continue
  62. if line[0] == '[':
  63. let section = strip(extract(line, "[", "]"))
  64. if section.len() != 0:
  65. actualSection = section
  66. else:
  67. self.table.clear()
  68. return (false, lineCount)
  69. else:
  70. let equal = find(line, '=')
  71. if equal <= 0:
  72. self.table.clear()
  73. return (false, lineCount)
  74. else:
  75. let
  76. param = strip(line[0 .. equal - 1])
  77. value = strip(line[equal + 1 .. ^1])
  78. if param.len() == 0:
  79. self.table.clear()
  80. return (false, lineCount)
  81. else:
  82. self.add(param, value, actualSection)
  83. return (true, 0)
  84. # ----------------------------------------------------------------------------------------------------------------------
  85. when isMainModule:
  86. var cfg = newConfig()
  87. cfg.loadString("[common]\nFloats1 = 1,2,3\n[local]\nStr = \"String...\"\nBool = true\nFloats2 = 4, 5, 6\n")
  88. for s in cfg.sections():
  89. echo "Section: " & s
  90. for p in cfg.params(s):
  91. echo " Param: " & p