base.configmap.bmx 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. ' Copyright (c) 2013-2017 Ronny Otto
  2. '
  3. ' This software is provided 'as-is', without any express or implied
  4. ' warranty. In no event will the authors be held liable for any damages
  5. ' arising from the use of this software.
  6. '
  7. ' Permission is granted to anyone to use this software for any purpose,
  8. ' including commercial applications, and to alter it and redistribute it
  9. ' freely, subject to the following restrictions:
  10. '
  11. ' 1. The origin of this software must not be misrepresented; you must not
  12. ' claim that you wrote the original software. If you use this software
  13. ' in a product, an acknowledgment in the product documentation would be
  14. ' appreciated but is not required.
  15. '
  16. ' 2. Altered source versions must be plainly marked as such, and must not be
  17. ' misrepresented as being the original software.
  18. '
  19. ' 3. This notice may not be removed or altered from any source
  20. ' distribution.
  21. '
  22. SuperStrict
  23. Import BRL.Map
  24. Import BRL.FileSystem
  25. Import BRL.Standardio
  26. Type TConfigMap
  27. Field values:TMap = CreateMap()
  28. Field fileUri:String = ""
  29. Method Init:TConfigMap( configFile:String="" )
  30. If configFile <> "" Then LoadFromFile(configFile)
  31. Return Self
  32. End Method
  33. 'clear all key->value pairs
  34. Method Reset:Int()
  35. values.Clear()
  36. Return True
  37. End Method
  38. 'create another configMap with the same values
  39. Method Copy:TConfigMap()
  40. Local copyObj:TConfigMap = New TConfigMap
  41. 'copy values
  42. For Local key:String = EachIn values.Keys()
  43. copyObj.Add(key, Get(key))
  44. Next
  45. Return copyObj
  46. End Method
  47. 'create a merged configMap of all given configurations (eg. base + extension)
  48. Function CreateMerged:TConfigMap( configs:TConfigMap[], reversed:Int = False )
  49. If configs.length = 0 Then Return Null
  50. If reversed
  51. Local newConfigs:TConfigMap[]
  52. For Local i:Int = 1 To configs.length
  53. newConfigs :+ [configs[configs.length - i]]
  54. Next
  55. configs = newConfigs
  56. EndIf
  57. Local result:TConfigMap = configs[0].copy()
  58. For Local i:Int = 1 To configs.length-1
  59. 'overwrite values or add new if not existing
  60. For Local key:String = EachIn configs[i].values.Keys()
  61. Local value:Object = configs[i].Get(key)
  62. If value Then result.Add(key, value)
  63. Next
  64. Next
  65. Return result
  66. End Function
  67. 'try to load the configuration from a file
  68. Method LoadFromFile:Int( fileUri:String )
  69. 'skip resetting and loading if the file is not existing
  70. If FileSize(fileUri) < 0 Then Return False
  71. Self.fileUri = fileUri
  72. 'remove old values
  73. Reset()
  74. Local file:TStream = ReadFile(fileUri)
  75. If Not file
  76. 'RuntimeError("ERROR: could not open file ~q"+fileUri+"~q for reading.")
  77. Print "ERROR: could not open file ~q"+fileUri+"~q for reading."
  78. Return False
  79. EndIf
  80. Local line:String = ""
  81. Local splitPos:Int = 0
  82. Local key:String, value:String
  83. While Not Eof(file)
  84. line = ReadLine(file)
  85. 'skip #comments
  86. If line.Trim().Find("#") = 0 Then Continue
  87. 'find first "=" (later ones could come from arguments/params)
  88. splitPos = line.Find("=")
  89. 'no splitter means no assignment
  90. If splitPos < 0 Then Continue
  91. key = line[..splitPos].Trim()
  92. value = line[splitPos+1..].Trim()
  93. Add(key, value)
  94. Wend
  95. file.Close()
  96. Return True
  97. End Method
  98. Method ToString:String()
  99. Local result:String = "TConfigMap"+"~n"
  100. result :+ "-> file: "+Self.fileUri+"~n"
  101. result :+ "-> keys:"+"~n"
  102. For Local key:String = EachIn values.Keys()
  103. result :+ " -> "+key+" : "+String(values.ValueForKey(key))+"~n"
  104. Next
  105. Return result
  106. End Method
  107. Method Add:TConfigMap( key:String, data:Object )
  108. values.insert(key, data)
  109. Return Self
  110. End Method
  111. Method AddString:TConfigMap( key:String, data:String )
  112. Add(key, Object(data))
  113. Return Self
  114. End Method
  115. Method AddNumber:TConfigMap( key:String, data:Float )
  116. Add( key, Object( String(data) ) )
  117. Return Self
  118. End Method
  119. Method Get:Object( key:String, defaultValue:Object=Null )
  120. Local result:Object = values.ValueForKey(key)
  121. If result Then Return result
  122. Return defaultValue
  123. End Method
  124. Method GetString:String( key:String, defaultValue:String=Null )
  125. Local result:Object = Get(key)
  126. If result Then Return String( result )
  127. Return defaultValue
  128. End Method
  129. Method GetInt:Int( key:String, defaultValue:Int = Null )
  130. Local result:Object = Get(key)
  131. If result Then Return Int( Float( String( result ) ) )
  132. Return defaultValue
  133. End Method
  134. End Type