-
Notifications
You must be signed in to change notification settings - Fork 1
/
clsINI.cls
327 lines (280 loc) · 10.1 KB
/
clsINI.cls
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
VERSION 1.0 CLASS
BEGIN
MultiUse = -1 'True
Persistable = 0 'NotPersistable
DataBindingBehavior = 0 'vbNone
DataSourceBehavior = 0 'vbNone
MTSTransactionMode = 0 'NotAnMTSObject
END
Attribute VB_Name = "clsINI"
Attribute VB_GlobalNameSpace = False
Attribute VB_Creatable = True
Attribute VB_PredeclaredId = False
Attribute VB_Exposed = False
Option Explicit
Private Declare Function GetPrivateProfileSection Lib "Kernel32" Alias _
"GetPrivateProfileSectionA" (ByVal Section As String, ByVal Buffer As String, ByVal Size As Long, ByVal Filename As String) As Long
Private Declare Function GetPrivateProfileSectionNames Lib "Kernel32" Alias _
"GetPrivateProfileSectionNamesA" (ByVal lpszReturnBuffer As String, ByVal nSize As Long, ByVal lpFileName As String) As Long
Private Declare Function GetPrivateProfileString Lib "Kernel32" Alias _
"GetPrivateProfileStringA" (ByVal Section As String, ByVal Key As String, ByVal Default As String, ByVal Buffer As String, ByVal Size As Long, ByVal Filename As String) As Long
Private Declare Function WritePrivateProfileSection Lib "Kernel32" Alias _
"WritePrivateProfileSectionA" (ByVal Section As String, ByVal Key As String, ByVal Filename As String) As Long
Private Declare Function WritePrivateProfileString Lib "Kernel32" Alias _
"WritePrivateProfileStringA" (ByVal Section As String, ByVal Key As String, ByVal Setting As String, ByVal Filename As String) As Long
Private iFilename As String, iSection As String, iKey As String, _
iErrCode As Long, iErrVar(1 To 2) As String
Public Property Let Key(ByVal vData As String)
iKey = vData
End Property
Public Property Get Key() As String
Key = iKey
End Property
Public Property Let Section(ByVal vData As String)
iSection = vData
End Property
Public Property Get Section() As String
Section = iSection
End Property
Public Property Let Filename(ByVal vData As String)
iFilename = Trim$(vData)
End Property
Public Property Get Filename() As String
'Attribute Filename.VB_UserMemId = 0
Filename = iFilename
End Property
Public Function CheckIniFile(Optional Filename As String) As Boolean
Dim tFilename As String
CheckIniFile = False
tFilename = GetFilename(Filename)
If Len(tFilename) = 0 Then
Exit Function
End If
If Len(Dir(tFilename)) = 0 Then
Exit Function
End If
CheckIniFile = True
End Function
Public Function GetAllSectionsFromInifile(Optional ByVal Filename As String, Optional Sorted As Boolean = False) As Collection
Dim tFilename As String, i As Long, j As Long, nBuffer As String, _
res As Long, tStr As String, nItems() As String
Const kBufferSize = 32767
tFilename = GetFilename(Filename)
If CheckIniFile(tFilename) = True Then
Set GetAllSectionsFromInifile = New Collection
nBuffer = Space$(kBufferSize)
res = GetPrivateProfileSectionNames(nBuffer, kBufferSize, tFilename & vbNullString)
If res = 0 Then
Exit Function
End If
nItems = Split(Left$(nBuffer, res), Chr$(0))
If Sorted = True Then
For i = LBound(nItems) To UBound(nItems) - 1
For j = i To UBound(nItems)
If StrComp(nItems(i), nItems(j), vbTextCompare) = 1 Then
tStr = nItems(i): nItems(i) = nItems(j): nItems(j) = tStr
End If
Next j
Next i
End If
For i = 0 To UBound(nItems)
If Trim$(nItems(i)) <> "" Then
GetAllSectionsFromInifile.Add nItems(i)
End If
Next i
End If
End Function
Public Function CheckSection(Optional Section As String, Optional Filename As String) As Boolean
Dim tFilename As String, tSection As String, i As Long, tColl As Collection
CheckSection = False
tFilename = GetFilename(Filename)
tSection = GetSectionname(Section)
Set tColl = GetAllSectionsFromInifile(tFilename)
For i = 1 To tColl.Count
If UCase$(tSection) = UCase$(tColl.Item(i)) Then
CheckSection = True
Set tColl = Nothing
Exit Function
End If
Next i
Set tColl = Nothing
End Function
Public Function GetAllKeysFromSection(Optional Section As String, Optional Filename As String, Optional WithComments As Boolean = False, Optional Sorted As Boolean = False) As Collection
Const kBufferSize = 32767
Dim tFilename As String, tSection As String, nBuffer As String, res As Long, _
nItems() As String, nItem() As String, i As Long, tColl As Collection, _
ts(1) As String, j As Long, tStr As String
tFilename = GetFilename(Filename)
tSection = GetSectionname(Section)
Set tColl = New Collection
Set GetAllKeysFromSection = tColl
If CheckSection(tSection, tFilename) = True Then
nBuffer = Space$(kBufferSize)
res = GetPrivateProfileSection(tSection, nBuffer, kBufferSize, tFilename)
If res = 0 Then
Exit Function
End If
nItems = Split(Left$(nBuffer, res), Chr$(0))
If Sorted = True Then
For i = LBound(nItems) To UBound(nItems) - 1
For j = i To UBound(nItems)
If StrComp(nItems(i), nItems(j), vbTextCompare) = 1 Then
tStr = nItems(i): nItems(i) = nItems(j): nItems(j) = tStr
End If
Next j
Next i
End If
For i = LBound(nItems) To UBound(nItems)
If Len(Trim$(nItems(i))) > 0 Then
If WithComments = True Then
If InStr(nItems(i), "=") > 0 Then
nItem = Split(nItems(i), "=")
ts(0) = nItem(0): ts(1) = nItem(1)
tColl.Add ts
Else
ts(0) = nItems(i): ts(1) = vbNullString
tColl.Add ts
End If
Else
If InStr(Trim$(nItems(i)), ";") <> 1 Then
If InStr(nItems(i), "=") > 0 Then
nItem = Split(nItems(i), "=")
ts(0) = nItem(0): ts(1) = nItem(1)
tColl.Add ts
Else
ts(0) = nItems(i): ts(1) = vbNullString
tColl.Add ts
End If
End If
End If
End If
Next i
Set GetAllKeysFromSection = tColl
End If
End Function
Public Function CheckKey(Optional Key As String, Optional Section As String, Optional Filename As String) As Boolean
Dim tFilename As String, tSection As String, tKey As String, _
i As Long, tColl As Collection
CheckKey = False
tFilename = GetFilename(Filename)
tSection = GetSectionname(Section)
tKey = GetKeyname(Key)
Set tColl = GetAllKeysFromSection(tSection, tFilename)
For i = 1 To tColl.Count
If UCase$(tKey) = UCase$(tColl.Item(i)(0)) Then
CheckKey = True
Set tColl = Nothing
Exit Function
End If
Next i
End Function
Public Function GetKeyFromSection(Optional Key As String, Optional Section As String, Optional Filename As String) As String
Const kBufferSize = 1024
Dim nBuffer As String, tKey As String, tSection As String, tFilename As String, _
tColl As Collection, i As Long, res As Long
tFilename = GetFilename(Filename)
tSection = GetSectionname(Section)
tKey = GetKeyname(Key)
GetKeyFromSection = vbNullString
If CheckSection(tSection, tFilename) = True Then
Set tColl = GetAllKeysFromSection(tSection, tFilename)
For i = 1 To tColl.Count
If UCase$(tKey) = UCase$(tColl.Item(i)(0)) Then
GetKeyFromSection = tColl.Item(i)(1)
Set tColl = Nothing
Exit Function
End If
Next i
End If
End Function
Public Sub DeleteIniFile(Optional Filename As String)
Dim tFilename As String
tFilename = GetFilename(Filename)
If Dir(tFilename) <> "" Then
Kill tFilename
End If
End Sub
Public Sub CreateIniFile(Optional Filename As String)
Dim tFilename As String, fn As Long
tFilename = GetFilename(Filename)
fn = FreeFile
Open tFilename For Output As #fn
Close #fn
FlushInifile
End Sub
Public Sub CreateSection(Optional Section As String, Optional Filename As String)
Dim tFilename As String, tSection As String, res As Long, fn As Long
tFilename = GetFilename(Filename)
tSection = GetSectionname(Section)
If CheckSection(tSection, tFilename) = False Then
fn = FreeFile
Open tFilename For Append As #fn
Print #fn, "[" & tSection & "]"
Close #fn
End If
FlushInifile
End Sub
Public Sub DeleteAllSectionFromInifile(Optional Filename As String)
Dim tKey As String, tSection As String, tFilename As String, res As Long, _
i As Long, tColl As Collection
tFilename = GetFilename(Filename)
If CheckIniFile(tFilename) = True Then
res = WritePrivateProfileString(vbNullString, vbNullString, vbNullString, tFilename)
End If
End Sub
Public Sub DeleteSectionFromInifile(Optional Section As String, Optional Filename As String)
Dim tKey As String, tSection As String, tFilename As String, res As Long, _
i As Long, tColl As Collection
tFilename = GetFilename(Filename)
tSection = GetSectionname(Section)
If CheckSection(tSection, tFilename) = True Then
res = WritePrivateProfileString(tSection, vbNullString, vbNullString, tFilename)
End If
End Sub
Public Sub DeleteKeyFromSection(Optional Key As String, Optional Section As String, Optional Filename As String)
Dim tKey As String, tSection As String, tFilename As String, res As Long
tFilename = GetFilename(Filename)
tSection = GetSectionname(Section)
tKey = GetKeyname(Key)
If CheckKey(tKey, tSection, tFilename) = True Then
res = WritePrivateProfileString(tSection, tKey, vbNullString, tFilename)
End If
End Sub
Public Function SaveKey(Value As String, Optional Key As String, Optional Section As String, Optional Filename As String) As Boolean
Dim tKey As String, tSection As String, tFilename As String, res As Long
tFilename = GetFilename(Filename)
tSection = GetSectionname(Section)
tKey = GetKeyname(Key)
res = WritePrivateProfileString(tSection & vbNullString, tKey & vbNullString, _
Value & vbNullString, tFilename & vbNullString)
FlushInifile
End Function
Public Sub FlushInifile(Optional Filename As String)
Dim res As Long, tFilename As String
tFilename = GetFilename(Filename)
res = WritePrivateProfileString(vbNullString, vbNullString, vbNullString, tFilename & vbNullString)
End Sub
Private Function GetFilename(ByVal Filename As String) As String
Filename = Trim$(Filename)
If Len(Filename) = 0 Then
GetFilename = iFilename
Else
GetFilename = Filename
End If
End Function
Private Function GetSectionname(ByVal Sectionname As String) As String
Sectionname = Trim$(Sectionname)
If Len(Sectionname) = 0 Then
GetSectionname = iSection
Else
GetSectionname = Sectionname
End If
End Function
Private Function GetKeyname(ByVal Keyname As String) As String
Keyname = Trim$(Keyname)
If Len(Keyname) = 0 Then
GetKeyname = iKey
Else
GetKeyname = Keyname
End If
End Function