VCS_IE_Functions.bas 6.4 KB

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