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