| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181 |
- Option Compare Database
- Option Private Module
- Option Explicit
- Private Const AggressiveSanitize As Boolean = True
- Private Const StripPublishOption As Boolean = True
- ' Constants for Scripting.FileSystemObject API
- Public Const ForReading = 1, ForWriting = 2, ForAppending = 8
- Public Const TristateTrue = -1, TristateFalse = 0, TristateUseDefault = -2
- ' Can we export without closing the form?
- ' Export a database object with optional UCS2-to-UTF-8 conversion.
- Public Sub ExportObject(ByVal obj_type_num As Integer, ByVal obj_name As String, _
- ByVal file_path As String, Optional ByVal Ucs2Convert As Boolean = False)
- VCS_Dir.MkDirIfNotExist Left$(file_path, InStrRev(file_path, "\"))
- If Ucs2Convert Then
- Dim tempFileName As String
- tempFileName = VCS_File.TempFile()
- Application.SaveAsText obj_type_num, obj_name, tempFileName
- VCS_File.ConvertUcs2Utf8 tempFileName, file_path
- Else
- Application.SaveAsText obj_type_num, obj_name, file_path
- End If
- End Sub
- ' Import a database object with optional UTF-8-to-UCS2 conversion.
- Public Sub ImportObject(ByVal obj_type_num As Integer, ByVal obj_name As String, _
- ByVal file_path As String, Optional ByVal Ucs2Convert As Boolean = False)
-
- If Not VCS_Dir.FileExists(file_path) Then Exit Sub
-
- If Ucs2Convert Then
- Dim tempFileName As String
- tempFileName = VCS_File.TempFile()
- VCS_File.ConvertUtf8Ucs2 file_path, tempFileName
- Application.LoadFromText obj_type_num, obj_name, tempFileName
-
- Dim fso As Object
- Set fso = CreateObject("Scripting.FileSystemObject")
- fso.DeleteFile tempFileName
- Else
- Application.LoadFromText obj_type_num, obj_name, file_path
- End If
- End Sub
- 'shouldn't this be SanitizeTextFile (Singular)?
- ' For each *.txt in `Path`, find and remove a number of problematic but
- ' unnecessary lines of VB code that are inserted automatically by the
- ' Access GUI and change often (we don't want these lines of code in
- ' version control).
- Public Sub SanitizeTextFiles(ByVal Path As String, ByVal Ext As String)
- Dim fso As Object
- Set fso = CreateObject("Scripting.FileSystemObject")
- '
- ' Setup Block matching Regex.
- Dim rxBlock As Object
- Set rxBlock = CreateObject("VBScript.RegExp")
- rxBlock.ignoreCase = False
- '
- ' Match PrtDevNames / Mode with or without W
- Dim srchPattern As String
- srchPattern = "PrtDev(?:Names|Mode)[W]?"
- If (AggressiveSanitize = True) Then
- ' Add and group aggressive matches
- srchPattern = "(?:" & srchPattern
- srchPattern = srchPattern & "|GUID|""GUID""|NameMap|dbLongBinary ""DOL"""
- srchPattern = srchPattern & ")"
- End If
- ' Ensure that this is the begining of a block.
- srchPattern = srchPattern & " = Begin"
- 'Debug.Print srchPattern
- rxBlock.Pattern = srchPattern
- '
- ' Setup Line Matching Regex.
- Dim rxLine As Object
- Set rxLine = CreateObject("VBScript.RegExp")
- srchPattern = "^\s*(?:"
- srchPattern = srchPattern & "Checksum ="
- srchPattern = srchPattern & "|BaseInfo|NoSaveCTIWhenDisabled =1"
- If (StripPublishOption = True) Then
- srchPattern = srchPattern & "|dbByte ""PublishToWeb"" =""1"""
- srchPattern = srchPattern & "|PublishOption =1"
- End If
- srchPattern = srchPattern & ")"
- 'Debug.Print srchPattern
- rxLine.Pattern = srchPattern
- Dim filename As String
- filename = dir$(Path & "*." & Ext)
- Dim isReport As Boolean
- isReport = False
-
- Do Until Len(filename) = 0
- DoEvents
- Dim obj_name As String
- obj_name = Mid$(filename, 1, InStrRev(filename, ".") - 1)
- Dim InFile As Object
- Set InFile = fso.OpenTextFile(Path & obj_name & "." & Ext, iomode:=ForReading, create:=False, Format:=TristateFalse)
- Dim OutFile As Object
- Set OutFile = fso.CreateTextFile(Path & obj_name & ".sanitize", overwrite:=True, Unicode:=False)
-
- Dim getLine As Boolean
- getLine = True
-
- Do Until InFile.AtEndOfStream
- DoEvents
- Dim txt As String
- '
- ' Check if we need to get a new line of text
- If getLine = True Then
- txt = InFile.readline
- Else
- getLine = True
- End If
- '
- ' Skip lines starting with line pattern
- If rxLine.test(txt) Then
- Dim rxIndent As Object
- Set rxIndent = CreateObject("VBScript.RegExp")
- rxIndent.Pattern = "^(\s+)\S"
- '
- ' Get indentation level.
- Dim matches As Object
- Set matches = rxIndent.execute(txt)
- '
- ' Setup pattern to match current indent
- Select Case matches.count
- Case 0
- rxIndent.Pattern = "^" & vbNullString
- Case Else
- rxIndent.Pattern = "^" & matches(0).SubMatches(0)
- End Select
- rxIndent.Pattern = rxIndent.Pattern + "\S"
- '
- ' Skip lines with deeper indentation
- Do Until InFile.AtEndOfStream
- txt = InFile.readline
- If rxIndent.test(txt) Then Exit Do
- Loop
- ' We've moved on at least one line so do get a new one
- ' when starting the loop again.
- getLine = False
- '
- ' skip blocks of code matching block pattern
- ElseIf rxBlock.test(txt) Then
- Do Until InFile.AtEndOfStream
- txt = InFile.readline
- If InStr(txt, "End") Then Exit Do
- Loop
- ElseIf InStr(1, txt, "Begin Report") = 1 Then
- isReport = True
- OutFile.WriteLine txt
- ElseIf isReport = True And (InStr(1, txt, " Right =") Or InStr(1, txt, " Bottom =")) Then
- 'skip line
- If InStr(1, txt, " Bottom =") Then
- isReport = False
- End If
- Else
- OutFile.WriteLine txt
- End If
- Loop
- OutFile.Close
- InFile.Close
- fso.DeleteFile (Path & filename)
- Dim thisFile As Object
- Set thisFile = fso.GetFile(Path & obj_name & ".sanitize")
- thisFile.Move (Path & filename)
- filename = dir$()
- Loop
- End Sub
|