-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathbase.util.data.bmx
470 lines (362 loc) · 11.1 KB
/
base.util.data.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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
Rem
====================================================================
Data container
====================================================================
====================================================================
If not otherwise stated, the following code is available under the
following licence:
LICENCE: zlib/libpng
Copyright (C) 2002-2016 Ronny Otto, digidea.de
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.
====================================================================
EndRem
SuperStrict
Import brl.Map
Import brl.Retro
Import brl.StringBuilder
Import "external/string_comp.bmx"
Type TData
Field data:TMap = New TMap
Method Init:TData(data:TMap=Null)
If data
Self.data.Clear()
For Local k:Object = EachIn data.Keys()
Local ls:TLowerString = TLowerString(k)
If Not ls Then
ls = TLowerString.Create(String(k))
End If
Self.data.Insert(ls, data.ValueForKey(k))
Next
EndIf
Return Self
End Method
Method Clear:Int()
If data Then data.Clear()
Return True
End Method
Method ToString:String()
Return ToStringFormat(0)
End Method
Method ToStringFormat:String(depth:Int = 0)
Local depthString:TStringBuilder = New TStringBuilder
'local depthString:string = ""
For Local i:Int = 0 Until depth
depthString.Append("| ")
Next
Local res:TStringBuilder = New TStringBuilder
res.Append("TData~n")
'local res:string = "TData~n"
For Local key:TLowerString = EachIn data.Keys()
If TData(data.ValueForKey(key))
res.AppendObject(depthString).Append("|- ").Append(key.orig).Append(" = ").Append(TData(data.ValueForKey(key)).ToStringFormat(depth + 1)).Append("~n")
ElseIf TData[](data.ValueForKey(key))
For Local d:TData = EachIn TData[](data.ValueForKey(key))
res.AppendObject(depthString).Append("|- ").Append(key.orig).Append(" = ").Append(d.ToStringFormat(depth + 1)).Append("~n")
Next
ElseIf Object[](data.ValueForKey(key))
For Local o:Object = EachIn Object[](data.ValueForKey(key))
res.AppendObject(depthString).Append("|- ").Append(key.orig).Append(" = ").Append(o.ToString()).Append("~n")
Next
ElseIf data.ValueForKey(key)
res.AppendObject(depthString).Append("|- ").Append(key.orig).Append(" = ").Append(data.ValueForKey(key).ToString()).Append("~n")
Else
res.AppendObject(depthString).Append("|- ").Append(key.orig).Append(" = NULL~n")
EndIf
Next
res.AppendObject(depthString).Append("'-------~n")
Return res.ToString()
End Method
Method Copy:TData()
Local dataCopy:TData = New TData
For Local key:TLowerString = EachIn data.Keys()
'key = key.ToLower()
Local value:Object = data.ValueForKey(key)
If TData(value)
dataCopy.Add(key, TData(value).Copy())
Else
dataCopy.Add(key, value)
EndIf
Next
Return dataCopy
End Method
Function JoinData:Int(dataSource:TData, dataTarget:TData)
If Not dataSource Then Return False
If Not dataTarget Then Return False
For Local key:TLowerString = EachIn dataSource.data.Keys()
'key = key.ToLower()
dataTarget.Add(key, dataSource.data.ValueForKey(key))
Next
Return True
End Function
'merge multiple TData with the current data object
Method AppendDataSets:Int(dataSets:TData[])
For Local set:TData = EachIn dataSets
JoinData(set, Self)
Next
Return True
End Method
'add keys->values from other data object (and overwrite own if also existing)
Method Append:TData(otherData:TData)
JoinData(otherData, Self)
Return Self
End Method
'appends own key->value pairs to given dataset
Method AppendTo:TData(otherData:TData Var)
JoinData(Self, otherData)
Return Self
End Method
'returns a data set containing only differing data
'1) original contains key->value, customized does not contain key->value
' -> SKIP key->value
'2) original contains key->value, customized contains key->value2
' -> add key->value2
'3) original contains key->value, customized contains same key->value
' -> SKIP key->value and key->value2
Function GetDataDifference:TData(original:TData, customized:TData)
Local result:TData = New TData
If Not original Then original = New TData
If Not customized Then customized = New TData
'add all data available in "customized" but not in "original"
For Local key:TLowerString = EachIn customized.data.Keys()
'skip if original contains value too
If original.Get(key) Then Continue
result.Add(key, customized.Get(key))
Next
'add all data differing in "customized" compared to "original"
'iterate through original to skip already "added ones"
For Local key:TLowerString = EachIn original.data.Keys()
Local newValue:Object = customized.Get(key)
'if customized does not contain that value yet - skip
If Not newValue Then Continue
'both contain a value for the given key
'only add the key->value if original and custom differ
If newValue <> original.Get(key)
If String(newValue) = String(original.Get(key)) Then Continue
'if it is another dataset, try to get their
'difference too
If TData(newValue)
newValue = GetDataDifference(TData(original.Get(key)), TData(newValue))
EndIf
result.Add(key, newValue)
EndIf
Next
Return result
End Function
Method GetDifferenceTo:TData(otherData:TData)
Return GetDataDifference(otherData, Self)
End Method
Method Add:TData(key:Object, data:Object)
Local ls:TLowerString = TLowerString(key)
If Not ls Then ls = TLowerString.Create(String(key))
Self.data.insert(ls, data)
Return Self
End Method
Method AddNumber:TData(key:Object, data:Double)
Local dd:TDoubleData = New TDoubleData
dd.value = data
Add( key, dd )
Return Self
rem
If Double(Long(data)) = data
Return AddLong(key, Long(data))
EndIf
Return AddDouble(key, data)
endrem
End Method
Method AddString:TData(key:Object, data:String)
Add( key, data )
Return Self
End Method
Method AddFloat:TData(key:Object, data:Float)
Return AddDouble(key, data)
End Method
Method AddDouble:TData(key:Object, data:Double)
Local dd:TDoubleData = New TDoubleData
dd.value = data
Add( key, dd )
Return Self
End Method
Method AddInt:TData(key:Object, data:int)
Return AddLong(key, data)
End Method
Method AddLong:TData(key:Object, data:Long)
Local ld:TLongData = New TLongData
ld.value = data
Add( key, ld )
Return Self
End Method
Method AddBoolString:TData(key:Object, data:String)
Select data.toLower()
Case "1", "true", "yes"
Add( key, "TRUE")
Default
Add( key, "FALSE")
End Select
Return Self
End Method
Method AddBool:TData(key:Object, bool:Int)
If bool
Add( key, "TRUE")
Else
Add( key, "FALSE")
EndIf
Return Self
End Method
Method Remove:Object(key:Object)
Local ls:TLowerString = TLowerString(key)
If Not ls Then ls = TLowerString.Create(String(key))
Local removed:Object = Get(ls)
data.Remove(ls)
Return removed
End Method
Method Has:Int(key:Object)
Local ls:TLowerString = TLowerString(key)
If Not ls Then ls = TLowerString.Create(String(key))
Return data.Contains(ls)
End Method
Method Get:Object(k:Object, defaultValue:Object=Null, groupsEnabled:Int = False)
Local ls:TLowerString = TLowerString(k)
If Not ls Then ls = TLowerString.Create(String(k))
'only react if the "::" is in the middle of something
If groupsEnabled
Local pos:Int = ls.Find("::")
If pos > 0
Local group:String = Left(ls.orig, pos)
Local groupData:TData = TData(Get(group))
If groupData
Return groupData.Get(Right(ls.orig, pos+1), defaultValue)
EndIf
EndIf
EndIf
Local result:Object = data.ValueForKey(ls)
If result Then
Return result
End If
Return defaultValue
End Method
Method GetString:String(key:Object, defaultValue:String=Null)
Local result:Object = Get(key)
If result Then
Return result.ToString()
End If
Return defaultValue
End Method
Method GetBool:Int(key:Object, defaultValue:Int=False)
Local result:Object = Get(key)
If Not result Then Return defaultValue
Select String(result).toLower()
Case "true", "yes"
Return True
Default
'also allow "1" / "1.00000" or "33"
If Int(String(result)) >= 1 Then Return True
Return False
End Select
Return False
End Method
Method GetDouble:Double(key:Object, defaultValue:Double = 0.0)
Local result:Object = Get(key)
If result Then
Local dd:TDoubleData = TDoubleData(result)
If dd Then
Return dd.value
Else
Local ld:TLongData = TLongData(result)
If ld Then
Return ld.value
EndIf
End If
Return Double( String( result ) )
End If
Return defaultValue
End Method
Method GetLong:Long(key:Object, defaultValue:Long = 0)
Local result:Object = Get(key)
If result Then
Local ld:TLongData = TLongData(result)
If ld Then
Return ld.value
Else
Local dd:TDoubleData = TDoubleData(result)
If dd Then
Return Long(dd.value)
EndIf
End If
Return Long( String( result ) )
End If
Return defaultValue
End Method
Method GetFloat:Float(key:Object, defaultValue:Float = 0.0)
Local result:Object = Get(key)
If result Then
Local dd:TDoubleData = TDoubleData(result)
If dd Then
Return Float(dd.value)
Local ld:TLongData = TLongData(result)
If ld Then
Return ld.value
EndIf
End If
Return Float( String( result ) )
End If
Return defaultValue
End Method
Method GetInt:Int(key:Object, defaultValue:Int = Null)
Local result:Object = Get(key)
If result Then
Local ld:TLongData = TLongData(result)
If ld Then
Return Int(ld.value)
Else
Local dd:TDoubleData = TDoubleData(result)
If dd Then
Return Int(dd.value)
End If
EndIf
Return Int( String( result ) )
End If
Return defaultValue
End Method
Method GetData:TData(key:String, defaultValue:TData = Null)
Return TData(Get(key, defaultValue))
End Method
End Type
Type TDoubleData
Field value:Double
Method ToString:String()
Return String(value)
End Method
Method SerializeTDoubleDataToString:String()
'fits into a long? skip the ".0000" values
If Double(Long(value)) = value Then Return String(Long(value))
Return String(value)
End Method
Method DeSerializeTDoubleDataFromString(text:String)
value = Double(text)
End Method
End Type
Type TLongData
Field value:Long
Method ToString:String()
Return String(value)
End Method
Method SerializeTLongDataToString:String()
Return String(value)
End Method
Method DeSerializeTLongDataFromString(text:String)
value = Long(text)
End Method
End Type