VCS_IE_Functions.bas 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. Option Compare Database
  2. Option Private Module
  3. Option Explicit
  4. Private Const AggressiveSanitize As Boolean = True
  5. Private Const StripPublishOption As Boolean = True
  6. ' Constants for Scripting.FileSystemObject API
  7. Public Const ForReading = 1, ForWriting = 2, ForAppending = 8
  8. Public Const TristateTrue = -1, TristateFalse = 0, TristateUseDefault = -2
  9. ' Can we export without closing the form?
  10. ' Export a database object with optional UCS2-to-UTF-8 conversion.
  11. Public Sub ExportObject(ByVal obj_type_num As Integer, ByVal obj_name As String, _
  12. ByVal file_path As String, Optional ByVal Ucs2Convert As Boolean = False)
  13. VCS_Dir.MkDirIfNotExist Left$(file_path, InStrRev(file_path, "\"))
  14. If Ucs2Convert Then
  15. Dim tempFileName As String
  16. tempFileName = VCS_File.TempFile()
  17. Application.SaveAsText obj_type_num, obj_name, tempFileName
  18. VCS_File.ConvertUcs2Utf8 tempFileName, file_path
  19. Else
  20. Application.SaveAsText obj_type_num, obj_name, file_path
  21. End If
  22. End Sub
  23. ' Import a database object with optional UTF-8-to-UCS2 conversion.
  24. Public Sub ImportObject(ByVal obj_type_num As Integer, ByVal obj_name As String, _
  25. ByVal file_path As String, Optional ByVal Ucs2Convert As Boolean = False)
  26. If Not VCS_Dir.FileExists(file_path) Then Exit Sub
  27. If Ucs2Convert Then
  28. Dim tempFileName As String
  29. tempFileName = VCS_File.TempFile()
  30. VCS_File.ConvertUtf8Ucs2 file_path, tempFileName
  31. Application.LoadFromText obj_type_num, obj_name, tempFileName
  32. Dim fso As Object
  33. Set fso = CreateObject("Scripting.FileSystemObject")
  34. fso.DeleteFile tempFileName
  35. Else
  36. Application.LoadFromText obj_type_num, obj_name, file_path
  37. End If
  38. End Sub
  39. 'shouldn't this be SanitizeTextFile (Singular)?
  40. ' For each *.txt in `Path`, find and remove a number of problematic but
  41. ' unnecessary lines of VB code that are inserted automatically by the
  42. ' Access GUI and change often (we don't want these lines of code in
  43. ' version control).
  44. Public Sub SanitizeTextFiles(ByVal Path As String, ByVal Ext As String)
  45. Dim fso As Object
  46. Set fso = CreateObject("Scripting.FileSystemObject")
  47. '
  48. ' Setup Block matching Regex.
  49. Dim rxBlock As Object
  50. Set rxBlock = CreateObject("VBScript.RegExp")
  51. rxBlock.ignoreCase = False
  52. '
  53. ' Match PrtDevNames / Mode with or without W
  54. Dim srchPattern As String
  55. srchPattern = "PrtDev(?:Names|Mode)[W]?"
  56. If (AggressiveSanitize = True) Then
  57. ' Add and group aggressive matches
  58. srchPattern = "(?:" & srchPattern
  59. srchPattern = srchPattern & "|GUID|""GUID""|NameMap|dbLongBinary ""DOL"""
  60. srchPattern = srchPattern & ")"
  61. End If
  62. ' Ensure that this is the begining of a block.
  63. srchPattern = srchPattern & " = Begin"
  64. 'Debug.Print srchPattern
  65. rxBlock.Pattern = srchPattern
  66. '
  67. ' Setup Line Matching Regex.
  68. Dim rxLine As Object
  69. Set rxLine = CreateObject("VBScript.RegExp")
  70. srchPattern = "^\s*(?:"
  71. srchPattern = srchPattern & "Checksum ="
  72. srchPattern = srchPattern & "|BaseInfo|NoSaveCTIWhenDisabled =1"
  73. If (StripPublishOption = True) Then
  74. srchPattern = srchPattern & "|dbByte ""PublishToWeb"" =""1"""
  75. srchPattern = srchPattern & "|PublishOption =1"
  76. End If
  77. srchPattern = srchPattern & ")"
  78. 'Debug.Print srchPattern
  79. rxLine.Pattern = srchPattern
  80. Dim filename As String
  81. filename = dir$(Path & "*." & Ext)
  82. Dim isReport As Boolean
  83. isReport = False
  84. Do Until Len(filename) = 0
  85. DoEvents
  86. Dim obj_name As String
  87. obj_name = Mid$(filename, 1, InStrRev(filename, ".") - 1)
  88. Dim InFile As Object
  89. Set InFile = fso.OpenTextFile(Path & obj_name & "." & Ext, iomode:=ForReading, create:=False, Format:=TristateFalse)
  90. Dim OutFile As Object
  91. Set OutFile = fso.CreateTextFile(Path & obj_name & ".sanitize", overwrite:=True, Unicode:=False)
  92. Dim getLine As Boolean
  93. getLine = True
  94. Do Until InFile.AtEndOfStream
  95. DoEvents
  96. Dim txt As String
  97. '
  98. ' Check if we need to get a new line of text
  99. If getLine = True Then
  100. txt = InFile.readline
  101. Else
  102. getLine = True
  103. End If
  104. '
  105. ' Skip lines starting with line pattern
  106. If rxLine.test(txt) Then
  107. Dim rxIndent As Object
  108. Set rxIndent = CreateObject("VBScript.RegExp")
  109. rxIndent.Pattern = "^(\s+)\S"
  110. '
  111. ' Get indentation level.
  112. Dim matches As Object
  113. Set matches = rxIndent.execute(txt)
  114. '
  115. ' Setup pattern to match current indent
  116. Select Case matches.count
  117. Case 0
  118. rxIndent.Pattern = "^" & vbNullString
  119. Case Else
  120. rxIndent.Pattern = "^" & matches(0).SubMatches(0)
  121. End Select
  122. rxIndent.Pattern = rxIndent.Pattern + "\S"
  123. '
  124. ' Skip lines with deeper indentation
  125. Do Until InFile.AtEndOfStream
  126. txt = InFile.readline
  127. If rxIndent.test(txt) Then Exit Do
  128. Loop
  129. ' We've moved on at least one line so do get a new one
  130. ' when starting the loop again.
  131. getLine = False
  132. '
  133. ' skip blocks of code matching block pattern
  134. ElseIf rxBlock.test(txt) Then
  135. Do Until InFile.AtEndOfStream
  136. txt = InFile.readline
  137. If InStr(txt, "End") Then Exit Do
  138. Loop
  139. ElseIf InStr(1, txt, "Begin Report") = 1 Then
  140. isReport = True
  141. OutFile.WriteLine txt
  142. ElseIf isReport = True And (InStr(1, txt, " Right =") Or InStr(1, txt, " Bottom =")) Then
  143. 'skip line
  144. If InStr(1, txt, " Bottom =") Then
  145. isReport = False
  146. End If
  147. Else
  148. OutFile.WriteLine txt
  149. End If
  150. Loop
  151. OutFile.Close
  152. InFile.Close
  153. fso.DeleteFile (Path & filename)
  154. Dim thisFile As Object
  155. Set thisFile = fso.GetFile(Path & obj_name & ".sanitize")
  156. thisFile.Move (Path & filename)
  157. filename = dir$()
  158. Loop
  159. End Sub