-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathIniFile.vb
More file actions
209 lines (145 loc) · 5.22 KB
/
IniFile.vb
File metadata and controls
209 lines (145 loc) · 5.22 KB
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
Imports System.IO
Imports System.Text
Public Class IniFile
Dim m_aItems() As String
Dim m_nItemsCount As Int32
Dim m_sPathFileName As String = ""
Public Function LoadFile(ByVal FileName As String) As Boolean
Dim fs As FileStream
Dim sr As StreamReader
Dim sLine As String
FileName = Trim(FileName)
Try
If FileName = "" Then
Err.Raise(INIFILE_ERR_FILENAME_REQUIRED, , "Missing file name parameter")
End If
If InStr(FileName, "\") = 0 Then '-- Only filename without path
'-- Add current EXE path
m_sPathFileName = AppPath() & FileName
Else
m_sPathFileName = FileName
End If
'-- Initialize array items
m_nItemsCount = 0
If System.IO.File.Exists(m_sPathFileName) = False Then '-- Not found
Return False
End If
fs = New FileStream(m_sPathFileName, FileMode.OpenOrCreate, FileAccess.Read, FileShare.ReadWrite)
sr = New StreamReader(fs, Encoding.UTF8)
While True
sLine = Trim(sr.ReadLine())
m_nItemsCount = m_nItemsCount + 1
ReDim Preserve m_aItems(m_nItemsCount)
m_aItems(m_nItemsCount - 1) = sLine
If sr.EndOfStream Then
Exit While
End If
End While
sr.Close()
fs.Close()
sr = Nothing
fs = Nothing
Return True
Catch ex As Exception
sr = Nothing
fs = Nothing
Return False
End Try
End Function
Public Function SaveFile(Optional FileName As String = "") As Boolean
Dim fs As FileStream
Dim sw As StreamWriter
Dim sWrite As String
Dim i As Int32
FileName = Trim(FileName)
Try
If FileName = "" And m_sPathFileName = "" Then
Err.Raise(INIFILE_ERR_FILENAME_REQUIRED, , "Missing file name parameter")
End If
If FileName <> "" Then '-- Change INI file name
If InStr(FileName, "\") = 0 Then '-- Only filename without path
'-- Add current EXE path
m_sPathFileName = AppPath() & FileName
Else
m_sPathFileName = FileName
End If
End If
sWrite = ""
For i = 1 To m_nItemsCount
sWrite = sWrite & m_aItems(i - 1) & vbCrLf
Next
fs = New FileStream(m_sPathFileName, FileMode.Create, FileAccess.Write, FileShare.ReadWrite)
sw = New StreamWriter(fs, Encoding.UTF8)
sw.Write(sWrite)
sw.Close()
fs.Close()
sw = Nothing
fs = Nothing
Return True
Catch ex As Exception
sw = Nothing
fs = Nothing
Return False
End Try
End Function
Default Public Property Items(ByVal Key As String) As String
Get
Dim nIndex As Int32
Dim sRetVal As String = ""
Dim nPos As Int32
Key = Trim(Key)
If Key = "" Then
Err.Raise(INIFILE_ERR_KEY_REQUIRED, , "Missing key parameter")
End If
nIndex = _GetIndex(Key)
If nIndex = -1 Then '-- Not found
Return ""
End If
'-- Found
sRetVal = m_aItems(nIndex)
nPos = InStr(sRetVal, "=")
If (nPos > 0) Then
sRetVal = Trim(sRetVal.Substring(nPos))
End If
Return sRetVal
End Get
Set(value As String)
Dim nIndex As Integer
Key = Trim(Key)
value = Trim(value)
If Key = "" Then
Err.Raise(INIFILE_ERR_KEY_REQUIRED, , "Missing key parameter")
End If
nIndex = _GetIndex(Key)
If nIndex = -1 Then '-- Not found
'-- Add new. Put key value at the bottom
m_nItemsCount = m_nItemsCount + 1
ReDim Preserve m_aItems(m_nItemsCount)
nIndex = m_nItemsCount - 1
End If
m_aItems(nIndex) = Key & "=" & value
End Set
End Property
Private Function _GetIndex(ByVal Key As String) As Int32
Dim i As Integer
Dim sItem As String
Dim sCheckKey As String
Dim nPos As Int32
Key = LCase(Key)
For i = 1 To m_nItemsCount
sItem = m_aItems(i - 1)
If Left(sItem, 1) <> ";" Then '-- Ignore remark char ";"
nPos = InStr(sItem, "=") '-- Find "=" char
If nPos > 0 Then
'-- Get Key
sCheckKey = Trim(LCase(Left(m_aItems(i - 1), nPos - 1)))
If sCheckKey = Key Then
'-- Return actual array index (zero based array)
Return (i - 1)
End If
End If
End If
Next
Return -1 '-- Not Found
End Function
End Class