-
Notifications
You must be signed in to change notification settings - Fork 35
/
Copy pathmodConvertUtils.bas
114 lines (99 loc) · 3.24 KB
/
modConvertUtils.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
Attribute VB_Name = "modConvertUtils"
Option Explicit
' Basic conversion utilities
Private EOLComment As String
Private mStrings As Collection
Private nStringCnt As Long
Private Const DeStringToken_Base1 As String = "STRING_"
Private Const DeStringToken_Base2 As String = "TOKEN_"
Public Const DeStringToken_Base As String = DeStringToken_Base1 & DeStringToken_Base2
' remove any comment from line
Public Function DeComment(ByVal Str As String, Optional ByVal Discard As Boolean = False) As String
Dim A As Long
Dim T As String, U As String
Dim C As String
DeComment = Str
A = InStr(Str, "'")
If A = 0 Then Exit Function
Do While True
T = Left(Str, A - 1)
U = Replace(T, """", "")
If (Len(T) - Len(U)) Mod 2 = 0 Then Exit Do
A = InStr(A + 1, Str, "'")
If A = 0 Then Exit Function
Loop
If Not Discard Then EOLComment = Mid(Str, A + 1)
DeComment = RTrim(Left(Str, A - 1))
End Function
' replace comments on line
Public Function ReComment(ByVal Str As String, Optional ByVal KeepVBComments As Boolean = False) As String
Dim C As String
Dim Pr As String
Pr = IIf(KeepVBComments, "'", "//")
If EOLComment = "" Then ReComment = Str: Exit Function
C = Pr & EOLComment
EOLComment = ""
If Not IsInStr(Str, vbCrLf) Then
ReComment = Str & IIf(Len(Str) = 0, "", " ") & C
Else
ReComment = Replace(Str, vbCrLf, C & vbCrLf, , 1) ' Always leave on end of first line...
End If
If Left(LTrim(ReComment), 2) = Pr Then ReComment = LTrim(ReComment)
End Function
' initialize de stringing
Public Sub InitDeString()
Set mStrings = New Collection
nStringCnt = 0
End Sub
' token for destringing
Private Function DeStringToken(ByVal N As Long) As String
DeStringToken = DeStringToken_Base & Format(N, "00000")
End Function
' destring a line. destringing before
Public Function DeString(ByVal S As String) As String
Const Q As String = """"
Dim Token As String
Dim A As Long, B As Long, C As Long
Dim K As String
If mStrings Is Nothing Then InitDeString
'If IsInStr(S, """ArCheck.chkShowB") Then Stop
A = InStr(S, Q)
C = A
If A > 0 Then
MidQuote:
B = InStr(C + 1, S, Q)
If B > 0 Then
If Mid(S, B + 1, 1) = Q Then
C = B + 1
GoTo MidQuote
End If
nStringCnt = nStringCnt + 1
Token = DeStringToken(nStringCnt)
K = Mid(S, A, B - A + 1)
mStrings.Add K, Token
S = Left(S, A - 1) & Token & Mid(S, B + 1)
DeString = DeString(S)
Exit Function
End If
End If
DeString = S
End Function
Public Function ReString(ByVal Str As String, Optional ByVal doConvertString As Boolean = False) As String
Dim I As Long, T As String, V As String
For I = 1 To nStringCnt
T = DeStringToken(I)
V = mStrings.Item(T)
If V <> "" And doConvertString Then
If Left(V, 1) = """" And Right(V, 1) = """" Then
V = """" & InternalConvertString(Mid(V, 2, Len(V) - 2)) & """"
End If
End If
Str = Replace(Str, T, V)
Next
ReString = Str
End Function
Private Function InternalConvertString(ByVal S As String) As String
S = Replace(S, "\", "\\")
S = Replace(S, """""", "\""")
InternalConvertString = S
End Function