VCS_IE_Functions.bas 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  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. ' constants for names conversion
  10. Public Const ForbiddenCars = "34,42,47,58,60,62,63,92,124"
  11. ' Can we export without closing the form?
  12. ' Export a database object with optional UCS2-to-UTF-8 conversion.
  13. Public Sub ExportObject(ByVal obj_type_num As Integer, ByVal obj_name As String, _
  14. ByVal file_path As String, Optional ByVal Ucs2Convert As Boolean = False)
  15. VCS_Dir.MkDirIfNotExist Left$(file_path, InStrRev(file_path, "\"))
  16. If Ucs2Convert Then
  17. Dim tempFileName As String
  18. tempFileName = VCS_File.TempFile()
  19. Application.SaveAsText obj_type_num, obj_name, tempFileName
  20. VCS_File.ConvertUcs2Utf8 tempFileName, file_path
  21. Dim fso As Object
  22. Set fso = CreateObject("Scripting.FileSystemObject")
  23. fso.DeleteFile tempFileName
  24. Else
  25. Application.SaveAsText obj_type_num, obj_name, file_path
  26. End If
  27. End Sub
  28. ' Import a database object with optional UTF-8-to-UCS2 conversion.
  29. Public Sub ImportObject(ByVal obj_type_num As Integer, ByVal obj_name As String, _
  30. ByVal file_path As String, Optional ByVal Ucs2Convert As Boolean = False)
  31. If Not VCS_Dir.FileExists(file_path) Then
  32. logger "ImportObject", "ERROR", "Can't find the file " & file_path
  33. Exit Sub
  34. End If
  35. On Error GoTo err
  36. If Ucs2Convert Then
  37. Dim tempFileName As String
  38. tempFileName = VCS_File.TempFile()
  39. VCS_File.ConvertUtf8Ucs2 file_path, tempFileName
  40. Application.LoadFromText obj_type_num, obj_name, tempFileName
  41. Dim fso As Object
  42. Set fso = CreateObject("Scripting.FileSystemObject")
  43. fso.DeleteFile tempFileName
  44. Else
  45. Application.LoadFromText obj_type_num, obj_name, file_path
  46. End If
  47. Exit Sub
  48. err:
  49. logger "ImportObject", "CRITICAL", "Unable to import the object " & obj_name & " (" & obj_type_num & ")" & "[" & err.Description & "]"
  50. End Sub
  51. 'shouldn't this be SanitizeTextFile (Singular)?
  52. ' For each *.txt in `Path`, find and remove a number of problematic but
  53. ' unnecessary lines of VB code that are inserted automatically by the
  54. ' Access GUI and change often (we don't want these lines of code in
  55. ' version control).
  56. Public Sub SanitizeTextFiles(ByVal path As String, ByVal Ext As String)
  57. Dim fso As Object
  58. Set fso = CreateObject("Scripting.FileSystemObject")
  59. '
  60. ' Setup Block matching Regex.
  61. Dim rxBlock As Object
  62. Set rxBlock = CreateObject("VBScript.RegExp")
  63. rxBlock.ignoreCase = False
  64. '
  65. ' Match PrtDevNames / Mode with or without W
  66. Dim srchPattern As String
  67. srchPattern = "PrtDev(?:Names|Mode)[W]?"
  68. If (AggressiveSanitize = True) Then
  69. ' Add and group aggressive matches
  70. srchPattern = "(?:" & srchPattern
  71. srchPattern = srchPattern & "|GUID|""GUID""|NameMap|dbLongBinary ""DOL"""
  72. srchPattern = srchPattern & ")"
  73. End If
  74. ' Ensure that this is the begining of a block.
  75. srchPattern = srchPattern & " = Begin"
  76. 'Debug.Print srchPattern
  77. rxBlock.Pattern = srchPattern
  78. '
  79. ' Setup Line Matching Regex.
  80. Dim rxLine As Object
  81. Set rxLine = CreateObject("VBScript.RegExp")
  82. srchPattern = "^\s*(?:"
  83. srchPattern = srchPattern & "Checksum ="
  84. srchPattern = srchPattern & "|BaseInfo|NoSaveCTIWhenDisabled =1"
  85. If (StripPublishOption = True) Then
  86. srchPattern = srchPattern & "|dbByte ""PublishToWeb"" =""1"""
  87. srchPattern = srchPattern & "|PublishOption =1"
  88. End If
  89. srchPattern = srchPattern & ")"
  90. 'Debug.Print srchPattern
  91. rxLine.Pattern = srchPattern
  92. Dim filename As String
  93. filename = dir$(path & "*." & Ext)
  94. Dim isReport As Boolean
  95. isReport = False
  96. Do Until Len(filename) = 0
  97. DoEvents
  98. Dim obj_name As String
  99. obj_name = Mid$(filename, 1, InStrRev(filename, ".") - 1)
  100. Dim InFile As Object
  101. Set InFile = fso.OpenTextFile(path & obj_name & "." & Ext, iomode:=ForReading, create:=False, Format:=TristateFalse)
  102. Dim OutFile As Object
  103. Set OutFile = fso.CreateTextFile(path & obj_name & ".sanitize", overwrite:=True, unicode:=False)
  104. Dim getLine As Boolean
  105. getLine = True
  106. Do Until InFile.AtEndOfStream
  107. DoEvents
  108. Dim txt As String
  109. '
  110. ' Check if we need to get a new line of text
  111. If getLine = True Then
  112. txt = InFile.readline
  113. Else
  114. getLine = True
  115. End If
  116. '
  117. ' Skip lines starting with line pattern
  118. If rxLine.test(txt) Then
  119. Dim rxIndent As Object
  120. Set rxIndent = CreateObject("VBScript.RegExp")
  121. rxIndent.Pattern = "^(\s+)\S"
  122. '
  123. ' Get indentation level.
  124. Dim matches As Object
  125. Set matches = rxIndent.execute(txt)
  126. '
  127. ' Setup pattern to match current indent
  128. Select Case matches.count
  129. Case 0
  130. rxIndent.Pattern = "^" & vbNullString
  131. Case Else
  132. rxIndent.Pattern = "^(\s{0," & Len(matches(0).SubMatches(0)) & "})"
  133. End Select
  134. rxIndent.Pattern = rxIndent.Pattern + "\S"
  135. '
  136. ' Skip lines with deeper indentation
  137. Do Until InFile.AtEndOfStream
  138. txt = InFile.readline
  139. If rxIndent.test(txt) Then Exit Do
  140. Loop
  141. ' We've moved on at least one line so do get a new one
  142. ' when starting the loop again.
  143. getLine = False
  144. '
  145. ' skip blocks of code matching block pattern
  146. ElseIf rxBlock.test(txt) Then
  147. Do Until InFile.AtEndOfStream
  148. txt = InFile.readline
  149. If InStr(txt, "End") Then Exit Do
  150. Loop
  151. ElseIf InStr(1, txt, "Begin Report") = 1 Then
  152. isReport = True
  153. OutFile.WriteLine txt
  154. ElseIf isReport = True And (InStr(1, txt, " Right =") Or InStr(1, txt, " Bottom =")) Then
  155. 'skip line
  156. If InStr(1, txt, " Bottom =") Then
  157. isReport = False
  158. End If
  159. Else
  160. OutFile.WriteLine txt
  161. End If
  162. Loop
  163. OutFile.Close
  164. InFile.Close
  165. fso.DeleteFile (path & filename)
  166. Dim thisFile As Object
  167. Set thisFile = fso.GetFile(path & obj_name & ".sanitize")
  168. thisFile.Move (path & filename)
  169. filename = dir$()
  170. Loop
  171. End Sub
  172. Public Function to_filename(object_name As String) As String
  173. ' return a file name for the object's name
  174. ' 1- access does not accept brackets for object's names
  175. ' 2- file's names can not contain those caracters:
  176. ' \ [92]
  177. ' / [47]
  178. ' : [58]
  179. ' * [42]
  180. ' ? [63]
  181. ' " [34]
  182. ' < [60]
  183. ' > [62]
  184. ' | [124]
  185. '
  186. ' this function replaces caracters which are not allowed for file names by [x],
  187. 'where x is the ascii code of the character
  188. ' test: "test_\_/_:_*_?_""_<_>_|" should become test_[92]_[47]_[58]_[42]_[63]_[34]_[60]_[62]_[124]
  189. ' to convert back the string, use to_accessname
  190. Dim result As String
  191. Dim ascii_code As Variant
  192. result = object_name
  193. For Each ascii_code In Split(ForbiddenCars, ",")
  194. result = Replace(result, Chr(CInt(ascii_code)), "[" & ascii_code & "]")
  195. Next
  196. to_filename = result
  197. Exit Function
  198. err:
  199. Call logger("to_accessname", "ERROR", "Unable to convert object's name " & object_name & " to file's name")
  200. to_filename = object_name
  201. End Function
  202. Public Function to_accessname(file_name As String) As String
  203. On Error GoTo err
  204. ' return an object name from a file's name
  205. ' see function 'to_filename' for more informations
  206. ' test: "test_[92]_[47]_[58]_[42]_[63]_[34]_[60]_[62]_[124]" should become test_\_/_:_*_?_"_<_>_|
  207. Dim result As String
  208. Dim ascii_code As Variant
  209. result = file_name
  210. For Each ascii_code In Split(ForbiddenCars, ",")
  211. result = Replace(result, "[" & ascii_code & "]", Chr(CInt(ascii_code)))
  212. Next
  213. to_accessname = result
  214. Exit Function
  215. err:
  216. Call logger("to_accessname", "ERROR", "Unable to convert file's name " & file_name & " to access object's name")
  217. to_accessname = file_name
  218. End Function