VCS_IE_Functions.bas 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334
  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. Dim p_source_dir As String
  12. Public Function source_dir() As String
  13. If Len(p_source_dir) = 0 Then
  14. ' get the source directory's path
  15. p_source_dir = norm_dir_path(CurrentProject.path) & "source\"
  16. logger "source_dir", "DEBUG", "> Source's directory defined: " & p_source_dir
  17. End If
  18. source_dir = p_source_dir
  19. End Function
  20. 'returns true if named module is NOT part of the VCS / OA code
  21. Public Function IsNotVCS(ByVal name As String) As Boolean
  22. '*** if OA addin is used from its developement version (OA exporting itself)
  23. If CurrentProject.name = "openaccess.accda" Then
  24. IsNotVCS = True
  25. Exit Function
  26. End If
  27. '****
  28. If name <> "OA_Controls" And _
  29. name <> "OA_Log" And _
  30. name <> "OA_Main" And _
  31. name <> "OA_Optimizer" And _
  32. name <> "OA_Properties" And _
  33. name <> "OA_Shell" And _
  34. name <> "OA_Utils" And _
  35. name <> "VCS_DataMacro" And _
  36. name <> "VCS_Dir" And _
  37. name <> "VCS_File" And _
  38. name <> "VCS_IE_Functions" And _
  39. name <> "VCS_ImportExport" And _
  40. name <> "VCS_Reference" And _
  41. name <> "VCS_Relation" And _
  42. name <> "VCS_Report" And _
  43. name <> "VCS_String" And _
  44. name <> "VCS_Table" Then
  45. IsNotVCS = True
  46. Else
  47. IsNotVCS = False
  48. End If
  49. End Function
  50. '[DEPRECATED]
  51. ' Export a database object with optional UCS2-to-UTF-8 conversion.
  52. 'Public Sub ExportObject(ByVal obj_type_num As Integer, ByVal obj_name As String, _
  53. ' ByVal file_path As String, Optional ByVal Ucs2Convert As Boolean = False)
  54. '
  55. ' VCS_Dir.MkDirIfNotExist Left$(file_path, InStrRev(file_path, "\"))
  56. '
  57. ' If Ucs2Convert Then
  58. ' Dim tempFileName As String
  59. ' tempFileName = VCS_File.TempFile()
  60. ' Application.SaveAsText obj_type_num, obj_name, tempFileName
  61. ' VCS_File.ConvertUcs2Utf8 tempFileName, file_path
  62. '
  63. ' Dim FSO As Object
  64. ' Set FSO = CreateObject("Scripting.FileSystemObject")
  65. ' FSO.DeleteFile tempFileName
  66. ' Else
  67. ' Application.SaveAsText obj_type_num, obj_name, file_path
  68. ' End If
  69. '
  70. ' logger "ExportObject", "DEBUG", "Object of type " & obj_type_num & " named " & obj_name & " exported to " & file_path
  71. '
  72. ' If obj_type_num <> acModule Then
  73. ' SanitizeFile file_path
  74. ' End If
  75. '
  76. 'End Sub
  77. '[DEPRECATED]
  78. ' Import a database object with optional UTF-8-to-UCS2 conversion.
  79. 'Public Sub ImportObject(ByVal obj_type_num As Integer, ByVal obj_name As String, _
  80. ' ByVal file_path As String, Optional ByVal Ucs2Convert As Boolean = False)
  81. ' Dim tempFileName As String
  82. ' tempFileName = ""
  83. '
  84. ' logger "ImportObject", "DEBUG", "Try to import " & obj_name & "(type " & obj_type_num & ") from: " & file_path
  85. '
  86. ' If Not VCS_Dir.FileExists(file_path) Then
  87. ' logger "ImportObject", "ERROR", "Can't find the file " & file_path
  88. ' GoTo end_
  89. ' End If
  90. '
  91. ' On Error GoTo err
  92. '
  93. ' If Ucs2Convert Then
  94. '
  95. ' tempFileName = VCS_File.TempFile()
  96. ' VCS_File.ConvertUtf8Ucs2 file_path, tempFileName
  97. '
  98. ' logger "ImportObject", "DEBUG", "Load data from " & tempFileName
  99. ' Application.LoadFromText obj_type_num, obj_name, tempFileName
  100. '
  101. ' Else
  102. '
  103. ' logger "ImportObject", "DEBUG", "Load data from " & file_path
  104. ' Application.LoadFromText obj_type_num, obj_name, file_path
  105. ' End If
  106. '
  107. ' logger "ImportObject", "DEBUG", "> imported"
  108. '
  109. 'end_:
  110. ' If Len(tempFileName) > 0 Then
  111. ' DelIfExist tempFileName
  112. ' End If
  113. '
  114. ' Exit Sub
  115. 'err:
  116. ' logger "ImportObject", "CRITICAL", "Unable to import " & obj_name & "[" & err.Description & "]"
  117. ' 'GoTo end_ ' > on error, don't delete the file (debugging purpose)
  118. 'End Sub
  119. Public Sub SanitizeFile(ByVal filePath As String)
  120. ' cleans the file from unnecessary lines
  121. Dim fso As Object
  122. Set fso = CreateObject("Scripting.FileSystemObject")
  123. ' Setup Block matching Regex.
  124. Dim rxBlock As Object
  125. Set rxBlock = CreateObject("VBScript.RegExp")
  126. rxBlock.ignoreCase = False
  127. ' Match PrtDevNames / Mode with or without W
  128. Dim srchPattern As String
  129. srchPattern = "PrtDev(?:Names|Mode)[W]?"
  130. If (AggressiveSanitize = True) Then
  131. ' Add and group aggressive matches
  132. srchPattern = "(?:" & srchPattern
  133. srchPattern = srchPattern & "|GUID|""GUID""|NameMap|dbLongBinary ""DOL"""
  134. srchPattern = srchPattern & ")"
  135. End If
  136. ' Ensure that this is the begining of a block.
  137. srchPattern = srchPattern & " = Begin"
  138. rxBlock.Pattern = srchPattern
  139. ' Setup Line Matching Regex.
  140. Dim rxLine As Object
  141. Set rxLine = CreateObject("VBScript.RegExp")
  142. srchPattern = "^\s*(?:"
  143. srchPattern = srchPattern & "Checksum ="
  144. srchPattern = srchPattern & "|BaseInfo|NoSaveCTIWhenDisabled =1"
  145. If (StripPublishOption = True) Then
  146. srchPattern = srchPattern & "|dbByte ""PublishToWeb"" =""1"""
  147. srchPattern = srchPattern & "|PublishOption =1"
  148. End If
  149. srchPattern = srchPattern & ")"
  150. rxLine.Pattern = srchPattern
  151. Dim isReport As Boolean
  152. isReport = False
  153. ' Dim dir_name, file_name, obj_name As String
  154. '
  155. ' obj_name = Mid$(filename, 1, InStrRev(filename, ".") - 1)
  156. Dim InFile As Object
  157. Set InFile = fso.OpenTextFile(filePath, iomode:=ForReading, Create:=False, Format:=TristateFalse)
  158. Dim OutFile As Object
  159. Set OutFile = fso.CreateTextFile(filePath & ".sanitize", overwrite:=True, unicode:=False)
  160. Dim getLine As Boolean
  161. getLine = True
  162. Do Until InFile.AtEndOfStream
  163. DoEvents
  164. Dim txt As String
  165. '
  166. ' Check if we need to get a new line of text
  167. If getLine = True Then
  168. txt = InFile.readline
  169. Else
  170. getLine = True
  171. End If
  172. ' Skip lines starting with line pattern
  173. If rxLine.test(txt) Then
  174. Dim rxIndent As Object
  175. Set rxIndent = CreateObject("VBScript.RegExp")
  176. rxIndent.Pattern = "^(\s+)\S"
  177. '
  178. ' Get indentation level.
  179. Dim matches As Object
  180. Set matches = rxIndent.Execute(txt)
  181. '
  182. ' Setup pattern to match current indent
  183. Select Case matches.count
  184. Case 0
  185. rxIndent.Pattern = "^" & vbNullString
  186. Case Else
  187. rxIndent.Pattern = "^(\s{0," & Len(matches(0).SubMatches(0)) & "})"
  188. End Select
  189. rxIndent.Pattern = rxIndent.Pattern + "\S"
  190. '
  191. ' Skip lines with deeper indentation
  192. Do Until InFile.AtEndOfStream
  193. txt = InFile.readline
  194. If rxIndent.test(txt) Then Exit Do
  195. Loop
  196. ' We've moved on at least one line so do get a new one
  197. ' when starting the loop again.
  198. getLine = False
  199. '
  200. ' skip blocks of code matching block pattern
  201. ElseIf rxBlock.test(txt) Then
  202. Do Until InFile.AtEndOfStream
  203. txt = InFile.readline
  204. If InStr(txt, "End") Then Exit Do
  205. Loop
  206. ElseIf InStr(1, txt, "Begin Report") = 1 Then
  207. isReport = True
  208. OutFile.WriteLine txt
  209. ElseIf isReport = True And (InStr(1, txt, " Right =") Or InStr(1, txt, " Bottom =")) Then
  210. 'skip line
  211. If InStr(1, txt, " Bottom =") Then
  212. isReport = False
  213. End If
  214. Else
  215. OutFile.WriteLine txt
  216. End If
  217. Loop
  218. OutFile.Close
  219. InFile.Close
  220. fso.DeleteFile (filePath)
  221. Dim thisFile As Object
  222. Set thisFile = fso.GetFile(filePath & ".sanitize")
  223. thisFile.Move (filePath)
  224. logger "SanitizeFile", "DEBUG", "> File " & filePath & " sanitized"
  225. End Sub
  226. ' Close all open forms.
  227. Public Sub CloseFormsReports()
  228. On Error GoTo errorHandler
  229. logger "CloseFormsReports", "DEBUG", "Close any opened form or report"
  230. Dim threshold As Integer
  231. threshold = 0
  232. Do While Forms.count > threshold
  233. If Forms(0).name = "OpenAccess" Then
  234. threshold = 1
  235. Else
  236. DoCmd.Close acForm, Forms(threshold).name
  237. End If
  238. 'DoEvents
  239. Loop
  240. Do While Reports.count > 0
  241. DoCmd.Close acReport, Reports(0).name
  242. 'DoEvents
  243. Loop
  244. DoEvents
  245. Exit Sub
  246. errorHandler:
  247. logger "CloseFormsReports", "CRITICAL", "Error #" & err.Number & err.Description
  248. End Sub
  249. 'errno 457 - duplicate key (& item)
  250. Public Function StrSetToCol(ByVal strSet As String, ByVal delimiter As String) As Collection 'throws errors
  251. Dim strSetArray() As String
  252. Dim col As Collection
  253. Set col = New Collection
  254. strSetArray = Split(strSet, delimiter)
  255. Dim item As Variant
  256. For Each item In strSetArray
  257. col.Add item, item
  258. Next
  259. Set StrSetToCol = col
  260. End Function
  261. ' Check if an item or key is in a collection
  262. Public Function InCollection(col As Collection, Optional vItem, Optional vKey) As Boolean
  263. On Error Resume Next
  264. Dim vColItem As Variant
  265. InCollection = False
  266. If Not IsMissing(vKey) Then
  267. col.item vKey
  268. '5 if not in collection, it is 91 if no collection exists
  269. If err.Number <> 5 And err.Number <> 91 Then
  270. InCollection = True
  271. End If
  272. ElseIf Not IsMissing(vItem) Then
  273. For Each vColItem In col
  274. If vColItem = vItem Then
  275. InCollection = True
  276. GoTo Exit_Proc
  277. End If
  278. Next vColItem
  279. End If
  280. Exit_Proc:
  281. Exit Function
  282. Err_Handle:
  283. Resume Exit_Proc
  284. End Function