-
-
Notifications
You must be signed in to change notification settings - Fork 18
/
Dictionary.bas
335 lines (295 loc) · 9.51 KB
/
Dictionary.bas
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
'###############################################################################
'# Dictionary.bi #
'# This file is part of MyFBFramework #
'# Authors: Nastase Eodor, Xusinboy Bekchanov, Liu XiaLin #
'# Based on: #
'# TStringList.bi #
'# FreeBasic Windows GUI ToolKit #
'# Copyright (c) 2007-2008 Nastase Eodor #
'# Version 1.0.0 #
'# Adapted to Dictionary by Xusinboy Bekchanov(2018-2019) Liu XiaLin #
'###############################################################################
#include once "Dictionary.bi"
'DictionaryItem
Private Property DictionaryItem.Key ByRef As WString
Return WGet(FKey)
End Property
Private Property DictionaryItem.Key(ByRef V As WString)
WLet(FKey, V)
End Property
Private Property DictionaryItem.Text ByRef As WString
Return WGet(FText)
End Property
Private Property DictionaryItem.Text(ByRef V As WString)
WLet(FText, V)
End Property
Private Constructor DictionaryItem
Key = ""
Text = ""
Object = 0
End Constructor
Private Destructor DictionaryItem
If FKey Then Deallocate_( FKey)
If FText Then Deallocate_( FText)
End Destructor
Private Operator DictionaryItem.Cast As Any Ptr
Return @This
End Operator
Private Property Dictionary.Count As Integer
Return FItems.Count
End Property
Private Property Dictionary.Count(Value As Integer)
End Property
Private Property Dictionary.Item(Index As Integer) As DictionaryItem Ptr
If Index >= 0 And Index <= Count -1 Then
Return FItems.Items[Index]
End If
Return 0
End Property
Private Property Dictionary.Item(Index As Integer, FItem As DictionaryItem Ptr)
If Index >= 0 And Index <= Count -1 Then
FItems.Items[Index] = FItem
If OnChange Then OnChange(This)
End If
End Property
Private Property Dictionary.Item(ByRef Key As WString) As DictionaryItem Ptr
Return Item(IndexOfKey(Key))
End Property
Private Property Dictionary.Item(ByRef Key As WString, FItem As DictionaryItem Ptr)
Item(IndexOfKey(Key)) = FItem
If OnChange Then OnChange(This)
End Property
Private Sub Dictionary.Add(ByRef Key As WString = "", ByRef wText As WString = "", Object As Any Ptr = 0)
Dim As DictionaryItem Ptr nItem = New_( DictionaryItem)
With *nItem
.Key = Key
.Text = wText
.Object = Object
End With
FItems.Add nItem
If OnChange Then OnChange(This)
End Sub
Private Sub Dictionary.Set(ByRef Key As WString, ByRef wText As WString = "", Object As Any Ptr = 0)
If Not ContainsKey(Key) Then
This.Add Key, wText, Object
Else
Item(Key)->Text = wText
Item(Key)->Object = Object
End If
If OnChange Then OnChange(This)
End Sub
Private Function Dictionary.Get(ByRef Key As WString, ByRef DefaultText As WString = "") ByRef As WString
If Not ContainsKey(Key) Then
Return DefaultText
Else
Return Item(Key)->Text
End If
End Function
Private Function Dictionary.Get(Index As Integer, ByRef DefaultText As WString = "") ByRef As WString
If Index >= 0 And Index <= Count - 1 Then
Return Item(Index)->Text
Else
Return DefaultText
End If
End Function
Private Sub Dictionary.Insert(Index As Integer, ByRef Key As WString = "", ByRef wText As WString = "", Object As Any Ptr = 0)
Dim As DictionaryItem Ptr nItem = New_( DictionaryItem)
With *nItem
.Key = Key
.Text = wText
.Object = Object
End With
FItems.Insert Index, nItem
If OnChange Then OnChange(This)
End Sub
Private Sub Dictionary.Exchange(Index1 As Integer, Index2 As Integer)
FItems.Exchange(Index1, Index2)
If OnChange Then OnChange(This)
End Sub
Private Sub Dictionary.Remove(Index As Integer)
Delete_( Cast(DictionaryItem Ptr, FItems.Items[Index]))
FItems.Remove Index
If OnChange Then OnChange(This)
End Sub
Private Sub Dictionary.Sort
Dim As Integer i, j
For i = 1 To Count - 1
For j = Count - 1 To i Step -1
If (Item(j)->Text < Item(j - 1)->Text) Then
Exchange j - 1, j
End If
Next
Next
If OnChange Then OnChange(This)
End Sub
Private Sub Dictionary.SortKeys
Dim As Integer i, j
For i = 1 To Count - 1
For j = Count - 1 To i Step -1
If (Item(j)->Key < Item(j - 1)->Key) Then
Exchange j - 1, j
End If
Next
Next
If OnChange Then OnChange(This)
End Sub
Private Sub Dictionary.Clear
For i As Integer = Count - 1 To 0 Step -1
Delete_( Cast(DictionaryItem Ptr, FItems.Items[i]))
Next i
FItems.Clear
If OnChange Then OnChange(This)
End Sub
Private Sub Dictionary.SaveToFile(ByRef FileName As WString)
Dim As Integer Fn = FreeFile
'If Open(FileName For Binary Access Write As #F) = 0 Then
If Open(FileName For Output Encoding "utf-8" As #Fn) = 0 Then 'David Change
For i As Integer = 0 To Count - 1
Print #Fn, Item(i)->Key & ": " & Item(i)->Text 'David Change
Next
End If
Close #Fn
End Sub
Private Sub Dictionary.LoadFromFile(ByRef FileName As WString)
Dim As Integer Fn = FreeFile, Result = -1, Pos1 = -1
Dim Buff As WString * 2000 'David Change for V1.07 Line Input not working fine
'If Open(FileName For Binary Access Read As #Fn) = 0 Then
Result = Open(FileName For Input Encoding "utf-8" As #Fn)
If Result <> 0 Then Result = Open(FileName For Input Encoding "utf-16" As #Fn)
If Result <> 0 Then Result = Open(FileName For Input Encoding "utf-32" As #Fn)
If Result <> 0 Then Result = Open(FileName For Input As #Fn)
If Result = 0 Then 'David Change
'Dim FText As WString Ptr
'WReallocate FText, LOF(F) + 1
This.Clear
While Not EOF(Fn)
Line Input #Fn, Buff
If Trim(Buff, Any !"\t ") <> "" Then 'David Change
Pos1 = InStr(Buff, ": ")
If Pos1 > 0 Then
Dim As DictionaryItem Ptr nItem = New_( DictionaryItem)
With *nItem
If Pos1 > 0 Then
.Key = ..Left(*FText, Pos1 - 1)
.Text = Mid(*FText, Pos1 + 2)
Else
.Key = *FText
End If
End With
FItems.Add nItem
End If
End If
Wend
End If
Close #Fn
If OnChange Then OnChange(This)
End Sub
Private Function Dictionary.IndexOf(ByRef wText As WString) As Integer
For i As Integer = 0 To Count - 1
If QDictionaryItem(FItems.Items[i]).Text = wText Then Return i
Next i
Return -1
End Function
Private Function Dictionary.IndexOfKey(ByRef Key As WString, Object As Any Ptr = 0) As Integer
If Object = 0 Then
For i As Integer = 0 To Count - 1
If QDictionaryItem(FItems.Items[i]).Key = Key Then Return i
Next i
Else
For i As Integer = 0 To Count - 1
With QDictionaryItem(FItems.Items[i])
If .Key = Key AndAlso .Object = Object Then Return i
End With
Next i
End If
Return -1
End Function
Private Function Dictionary.IndexOfObject(Object As Any Ptr) As Integer
For i As Integer = 0 To Count - 1
If QDictionaryItem(FItems.Items[i]).Object = Object Then Return i
Next i
Return -1
End Function
Private Function Dictionary.GetText(ByRef Key As WString) ByRef As WString
For i As Integer = 0 To Count - 1
If QDictionaryItem(FItems.Items[i]).Key = Key Then Return QDictionaryItem(FItems.Items[i]).Text
Next i
Return ""
End Function
Private Function Dictionary.GetObject(ByRef Key As WString) As Any Ptr
For i As Integer = 0 To Count - 1
If QDictionaryItem(FItems.Items[i]).Key = Key Then Return QDictionaryItem(FItems.Items[i]).Object
Next i
Return 0
End Function
Private Function Dictionary.GetKey(ByRef wText As WString) ByRef As WString
For i As Integer = 0 To Count - 1
If QDictionaryItem(FItems.Items[i]).Text = wText Then Return QDictionaryItem(FItems.Items[i]).Key
Next i
Return ""
End Function
Private Function Dictionary.GetKey(Object As Any Ptr) ByRef As WString
For i As Integer = 0 To Count - 1
If QDictionaryItem(FItems.Items[i]).Object = Object Then Return QDictionaryItem(FItems.Items[i]).Key
Next i
Return ""
End Function
Private Property Dictionary.Text ByRef As WString
WLet(FText, "")
For i As Integer = 0 To FItems.Count - 1
If i <> FItems.Count - 1 Then
WLet(FText, *FText & Item(i)->Key & ": " & Item(i)->Text & Chr(13) & Chr(10))
Else
WLet(FText, *FText & Item(i)->Key & ": " & Item(i)->Text)
End If
Next i
Return *FText
End Property
Private Property Dictionary.Text(ByRef Value As WString)
WLet(FText, "")
This.Clear
Dim As Integer Pos1
For i As Integer = 0 To Len(Value)
If Value[i] = 10 Or Value[i] = 0 Then
WLet(*FText, Trim(Mid(*FText, 1, Len(*FText)), Any WChr(13) & WChr(10)))
Pos1 = InStr(*FText, ": ")
Dim As DictionaryItem Ptr nItem = New_( DictionaryItem)
With *nItem
If Pos1 > 0 Then
.Key = ..Left(*FText, Pos1 - 1)
.Text = Mid(*FText, Pos1 + 2)
Else
.Key = *FText
End If
End With
FItems.Add nItem
WLet(FText, "")
Else
WLet(FText, *FText & WChr(Value[i]))
End If
Next i
If OnChange Then OnChange(This)
End Property
Private Function Dictionary.Contains(ByRef wText As WString) As Boolean
Return IndexOf(wText) <> -1
End Function
Private Function Dictionary.ContainsKey(ByRef Key As WString, Object As Any Ptr = 0) As Boolean
Return IndexOfKey(Key, Object) <> -1
End Function
Private Function Dictionary.ContainsObject(Object As Any Ptr) As Boolean
Return IndexOfObject(Object) <> -1
End Function
Private Operator Dictionary.Let(ByRef Value As WString)
This.Text = Value
End Operator
Private Constructor Dictionary
FItems.Clear
End Constructor
Private Destructor Dictionary
' For i As Integer = Count - 1 To 0 Step -1
' Delete_( Cast(DictionaryItem Ptr, FItems.Items[i]))
' Next i
' FItems.Clear
This.Clear
If FText Then Deallocate_( FText)
End Destructor