forked from lee-soft/ViStart
-
Notifications
You must be signed in to change notification settings - Fork 0
/
StringHelper.bas
323 lines (232 loc) · 7.49 KB
/
StringHelper.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
Attribute VB_Name = "StringHelper"
Private m_logger As SeverityLogger
Private Property Get Logger() As SeverityLogger
If m_logger Is Nothing Then
Set m_logger = LogManager.GetLogger("StringHelper")
End If
Set Logger = m_logger
End Property
Function JustExe(ByVal szPath As String) As String
Dim extensionStart As Long
Dim extensionEnd As Long
extensionStart = InStrRev(szPath, ".")
If extensionStart < 0 Then extensionStart = 1
If extensionStart < Len(szPath) Then
extensionEnd = InStr(extensionStart, szPath, " ")
If extensionEnd = 0 Then
extensionEnd = Len(szPath)
End If
If extensionStart > 0 Then
szPath = Left$(szPath, extensionEnd)
End If
End If
'Kill Quotes
szPath = Replace(szPath, """", vbNullString)
szPath = Replace(szPath, "'", vbNullString)
JustExe = szPath
End Function
Private Function Unserialize_INode(szData As String) As INode
Dim returnNode As New INode
Set returnNode.Icon = New ViIcon
Dim sP() As String
sP = Split(szData, "&")
With returnNode
.Caption = URLDecode(sP(0))
.IconPosition = URLDecode(sP(1))
.Tag = URLDecode(sP(2))
.Icon.IconPath = URLDecode(sP(3))
End With
Set Unserialize_INode = returnNode
End Function
Private Function Serialize_INode(theINode As INode) As String
Dim szReturn As String
Dim szIcon As String
If Not theINode.Icon Is Nothing Then
szIcon = theINode.Icon.IconPath
End If
With theINode
szReturn = szReturn & _
"INode" & "?" & _
URLEncode(.Caption) & "&" & _
URLEncode(.IconPosition) & "&" & _
URLEncode(.Tag) & "&" & _
URLEncode(szIcon)
End With
Serialize_INode = szReturn
End Function
Function UnSerialize(szData As String)
Dim theType As String
Dim sP() As String
sP = Split(szData, "?")
If IsArrayInitialized(sP) Then
If UBound(sP) = 1 Then
Select Case UCase$(sP(0))
Case "INODE"
Set UnSerialize = Unserialize_INode(sP(1))
Case Else
MsgBox "'" & szData & "' cannot be unserialized!", vbCritical
End Select
End If
End If
End Function
Function Serialize(theObject As Object) As String
Select Case TypeName(theObject)
Case "INode"
Serialize = Serialize_INode(theObject)
End Select
End Function
Function StripTerminator(ByVal strString As String) As String
Dim intZeroPos As Integer
intZeroPos = InStr(strString, Chr$(0))
If intZeroPos > 0 Then
StripTerminator = Left$(strString, intZeroPos - 1)
Else
StripTerminator = strString
End If
End Function
Public Function ExtOrNot(szFileName As String) As String
Dim periodPosition As Long
periodPosition = InStrRev(szFileName, ".")
If periodPosition > 0 Then
ExtOrNot = Left$(szFileName, periodPosition - 1)
Else
ExtOrNot = szFileName
End If
End Function
Public Function StrEnd(sData As String, sDelim As String, Optional iOffset As Integer = 1)
If InStr(sData, sDelim) = 0 Then
'Delim not present
StrEnd = sData
Exit Function
End If
Dim iLen As Integer, iDLen As Integer
iLen = Len(sData) + 1
iDLen = Len(sDelim)
If iLen = 1 Or iDLen = 0 Then
StrEnd = False
Exit Function
End If
While Mid$(sData, iLen, iDLen) <> sDelim And iLen > 1
iLen = iLen - 1
Wend
If iLen = 0 Then
StrEnd = False
Exit Function
End If
StrEnd = Mid$(sData, iLen + iOffset)
End Function
Public Function GetDWord(Word As String, Optional Little As Boolean = False) As Double
If LenB(Word) = 4 Then
'Reverse if this is little
If Little Then
Dim i As Long, SStr As String
For i = Len(Word) To 1 Step -1
SStr = SStr & MidB$(Word, i, 1)
Next i
Word = SStr
End If
'Grab hex values
Dim J As Long, HStr As String, H As String
For J = 1 To Len(Word)
H = Hex$(AscB(MidB$(Word, J, 1)))
If Len(H) = 1 Then H = "0" & H
HStr = HStr & H
Next J
'Cut off padding (null characters)
Do
HStr = Left$(HStr, Len(HStr) - 2)
Loop While Right$(HStr, 2) = "00"
'Return the value
GetDWord = Val("&H" & IIf(HStr <> "", HStr, "00") & "&")
Else
'No correct Word supplied
GetDWord = 0
End If
End Function
Function StrToHex(ByRef str)
Dim length
Dim Max
Dim strHex
Max = Len(str)
For length = 1 To Max
strHex = strHex & Right$("0" & Hex$(Asc(Mid$(str, length, 1))), 2)
Next
StrToHex = strHex
End Function
Function GetStringByPosition(ByRef sSource As String, ByVal lngPos As Long) As String
Dim sNewString As String
If lngPos > 0 Then
sNewString = Mid$(sSource, 1, lngPos - 1)
GetStringByPosition = sNewString
sSource = MidB$(sSource, lngPos)
End If
End Function
Public Function ExtractBytes(ByRef strUniSource, lngBytes As Long) As String
Dim strBuffer As String
strBuffer = MidB$(strUniSource, 1, lngBytes)
strUniSource = MidB$(strUniSource, lngBytes + 1)
ExtractBytes = strBuffer
End Function
Function GetStringByString(ByRef sSource As String, ByVal sDelim As String) As String
Dim lngPos As Long
Dim sNewString As String
lngPos = InStr(sSource, sDelim)
If lngPos > 0 Then
sNewString = Mid$(sSource, 1, lngPos - 1)
GetStringByString = sNewString
sSource = Mid$(sSource, lngPos + Len(sDelim))
End If
End Function
Public Function CBol(ByRef vData) As Boolean
If vData = "1" Or vData = "True" Then
CBol = True
End If
End Function
Public Function HEXCOL2RGB(ByVal HexColor As String) As String
'The input at this point could be HexColor = "#00FF1F"
Dim Red As String
Dim Green As String
Dim Blue As String
On Error GoTo Handler
HexColor = Replace(HexColor, "#", "")
'Here HexColor = "00FF1F"
Red = Val("&H" & Mid$(HexColor, 1, 2))
'The red value is now the long version of "00"
Green = Val("&H" & Mid$(HexColor, 3, 2))
'The red value is now the long version of "FF"
Blue = Val("&H" & Mid$(HexColor, 5, 2))
'The red value is now the long version of "1F"
HEXCOL2RGB = RGB(Red, Green, Blue)
'The output is an RGB value
Exit Function
Handler:
HEXCOL2RGB = vbWhite
End Function
Public Function isChecked(ByRef bBol As Boolean) As Long
If bBol Then
isChecked = 1
Else
isChecked = 0
End If
End Function
Public Function HexToString(ByVal HexToStr As String) As String
Dim strTemp As String
Dim strReturn As String
Dim i As Long
For i = 1 To Len(HexToStr) Step 2
strTemp = ChrB$(Val("&H" & Mid$(HexToStr, i, 2)))
strReturn = strReturn & strTemp
Next i
HexToString = strReturn
End Function
Public Function ExistInStringArray(ByRef theArray() As String, ByVal theDelimiter As String) As Boolean
Dim arrayIndex As Long
theDelimiter = UCase$(theDelimiter)
If Not isset(theArray) Then Exit Function
For arrayIndex = LBound(theArray) To UBound(theArray)
If UCase$(theArray(arrayIndex)) = theDelimiter Then
ExistInStringArray = True
Exit For
End If
Next
End Function