226 lines
6.9 KiB
Plaintext
226 lines
6.9 KiB
Plaintext
' Shared DxDesigner 7.9.5 bridge helpers. Keep this file ASCII-only.
|
|
|
|
Function DxReadUtf8(ByVal FilePath)
|
|
Dim Stream
|
|
Dim ErrorNumber
|
|
Dim ErrorDescription
|
|
Dim ErrorSource
|
|
|
|
Set Stream = CreateObject("ADODB.Stream")
|
|
Stream.Type = 2
|
|
Stream.Charset = "utf-8"
|
|
Stream.Open
|
|
|
|
On Error Resume Next
|
|
Stream.LoadFromFile CStr(FilePath)
|
|
ErrorNumber = Err.Number
|
|
ErrorDescription = Err.Description
|
|
ErrorSource = Err.Source
|
|
Err.Clear
|
|
On Error GoTo 0
|
|
If ErrorNumber <> 0 Then
|
|
Stream.Close
|
|
Err.Raise ErrorNumber, ErrorSource, ErrorDescription
|
|
End If
|
|
|
|
DxReadUtf8 = Stream.ReadText
|
|
Stream.Close
|
|
End Function
|
|
|
|
Sub DxWriteUtf8Atomic(ByVal FilePath, ByVal Text)
|
|
Dim Fso
|
|
Dim Stream
|
|
Dim Shell
|
|
Dim ParentPath
|
|
Dim TemporaryPath
|
|
Dim CommandLine
|
|
Dim ExitCode
|
|
Dim ErrorNumber
|
|
Dim ErrorDescription
|
|
Dim ErrorSource
|
|
|
|
Set Fso = CreateObject("Scripting.FileSystemObject")
|
|
ParentPath = Fso.GetParentFolderName(Fso.GetAbsolutePathName(CStr(FilePath)))
|
|
If Not Fso.FolderExists(ParentPath) Then
|
|
Err.Raise vbObjectError + 795, "DxWriteUtf8Atomic", _
|
|
"Destination directory does not exist"
|
|
End If
|
|
TemporaryPath = Fso.BuildPath(ParentPath, Fso.GetTempName)
|
|
|
|
Set Stream = CreateObject("ADODB.Stream")
|
|
Stream.Type = 2
|
|
Stream.Charset = "utf-8"
|
|
Stream.Open
|
|
Stream.WriteText CStr(Text)
|
|
|
|
On Error Resume Next
|
|
Stream.SaveToFile TemporaryPath, 2
|
|
ErrorNumber = Err.Number
|
|
ErrorDescription = Err.Description
|
|
ErrorSource = Err.Source
|
|
Err.Clear
|
|
On Error GoTo 0
|
|
Stream.Close
|
|
If ErrorNumber <> 0 Then
|
|
Err.Raise ErrorNumber, ErrorSource, ErrorDescription
|
|
End If
|
|
|
|
Set Shell = CreateObject("WScript.Shell")
|
|
CommandLine = DxCommandQuote( _
|
|
Shell.ExpandEnvironmentStrings("%ComSpec%")) & _
|
|
" /d /s /c " & Chr(34) & "move /y " & _
|
|
DxCommandQuote(TemporaryPath) & " " & _
|
|
DxCommandQuote(Fso.GetAbsolutePathName(CStr(FilePath))) & _
|
|
" >nul" & Chr(34)
|
|
ExitCode = Shell.Run(CommandLine, 0, True)
|
|
If ExitCode <> 0 Then
|
|
If Fso.FileExists(TemporaryPath) Then
|
|
Fso.DeleteFile TemporaryPath, True
|
|
End If
|
|
Err.Raise vbObjectError + 796, "DxWriteUtf8Atomic", _
|
|
"Cannot atomically replace destination file"
|
|
End If
|
|
End Sub
|
|
|
|
Function DxJsonEscape(ByVal Value)
|
|
Dim Text
|
|
Dim Result
|
|
Dim Index
|
|
Dim Character
|
|
Dim CodePoint
|
|
|
|
Text = CStr(Value)
|
|
Result = ""
|
|
For Index = 1 To Len(Text)
|
|
Character = Mid(Text, Index, 1)
|
|
CodePoint = AscW(Character)
|
|
If CodePoint < 0 Then CodePoint = CodePoint + 65536
|
|
Select Case CodePoint
|
|
Case 8
|
|
Result = Result & Chr(92) & "b"
|
|
Case 9
|
|
Result = Result & Chr(92) & "t"
|
|
Case 10
|
|
Result = Result & Chr(92) & "n"
|
|
Case 12
|
|
Result = Result & Chr(92) & "f"
|
|
Case 13
|
|
Result = Result & Chr(92) & "r"
|
|
Case 34
|
|
Result = Result & Chr(92) & Chr(34)
|
|
Case 92
|
|
Result = Result & Chr(92) & Chr(92)
|
|
Case Else
|
|
If CodePoint < 32 Then
|
|
Result = Result & Chr(92) & "u00" & _
|
|
Right("0" & Hex(CodePoint), 2)
|
|
Else
|
|
Result = Result & Character
|
|
End If
|
|
End Select
|
|
Next
|
|
DxJsonEscape = Result
|
|
End Function
|
|
|
|
Function DxInvariantNumber(ByVal Value)
|
|
Dim Text
|
|
Dim DecimalSample
|
|
Dim DecimalSeparator
|
|
|
|
If IsNull(Value) Or IsEmpty(Value) Or Not IsNumeric(Value) Then
|
|
Err.Raise vbObjectError + 797, "DxInvariantNumber", _
|
|
"Value is not numeric"
|
|
End If
|
|
Text = Trim(CStr(Value))
|
|
DecimalSample = Trim(CStr(1.5))
|
|
DecimalSeparator = Mid(DecimalSample, 2, Len(DecimalSample) - 2)
|
|
If Len(DecimalSeparator) > 0 And DecimalSeparator <> "." Then
|
|
Text = Replace(Text, DecimalSeparator, ".")
|
|
End If
|
|
DxInvariantNumber = Text
|
|
End Function
|
|
|
|
Function DxResolveRunPath(ByVal RootPath, ByVal RunRelativePath)
|
|
Dim Fso
|
|
Dim RootAbsolute
|
|
Dim Candidate
|
|
Dim RelativeText
|
|
Dim Prefix
|
|
|
|
Set Fso = CreateObject("Scripting.FileSystemObject")
|
|
RootAbsolute = Fso.GetAbsolutePathName(CStr(RootPath))
|
|
Do While Len(RootAbsolute) > 3 And Right(RootAbsolute, 1) = "\"
|
|
RootAbsolute = Left(RootAbsolute, Len(RootAbsolute) - 1)
|
|
Loop
|
|
If Not Fso.FolderExists(RootAbsolute) Then
|
|
Err.Raise vbObjectError + 798, "DxResolveRunPath", _
|
|
"Run root does not exist"
|
|
End If
|
|
DxValidateNoReparse Fso, RootAbsolute
|
|
|
|
RelativeText = CStr(RunRelativePath)
|
|
If Len(RelativeText) = 0 Or Left(RelativeText, 1) = "\" Or _
|
|
InStr(RelativeText, ":") > 0 Or _
|
|
InStr(RelativeText, "*") > 0 Or _
|
|
InStr(RelativeText, "?") > 0 Then
|
|
Err.Raise vbObjectError + 799, "DxResolveRunPath", _
|
|
"Run path must be a non-empty relative path"
|
|
End If
|
|
|
|
Candidate = Fso.GetAbsolutePathName( _
|
|
Fso.BuildPath(RootAbsolute, RelativeText))
|
|
Prefix = RootAbsolute
|
|
If Right(Prefix, 1) <> "\" Then Prefix = Prefix & "\"
|
|
If Len(Candidate) <= Len(Prefix) Or _
|
|
StrComp(Left(Candidate, Len(Prefix)), Prefix, _
|
|
vbTextCompare) <> 0 Then
|
|
Err.Raise vbObjectError + 800, "DxResolveRunPath", _
|
|
"Run path escapes the supplied root"
|
|
End If
|
|
DxValidateNoReparse Fso, Candidate
|
|
DxResolveRunPath = Candidate
|
|
End Function
|
|
|
|
Sub DxValidateNoReparse(ByRef Fso, ByVal FilePath)
|
|
Dim AbsolutePath
|
|
Dim DriveName
|
|
Dim CurrentPath
|
|
Dim Remaining
|
|
Dim Parts
|
|
Dim Part
|
|
Dim Attributes
|
|
|
|
AbsolutePath = Fso.GetAbsolutePathName(CStr(FilePath))
|
|
DriveName = Fso.GetDriveName(AbsolutePath)
|
|
If Len(DriveName) = 0 Then
|
|
Err.Raise vbObjectError + 801, "DxResolveRunPath", _
|
|
"Run paths must use a local drive"
|
|
End If
|
|
CurrentPath = DriveName & "\"
|
|
Remaining = Mid(AbsolutePath, Len(CurrentPath) + 1)
|
|
If Len(Remaining) = 0 Then Exit Sub
|
|
|
|
Parts = Split(Remaining, "\")
|
|
For Each Part In Parts
|
|
If Len(CStr(Part)) > 0 Then
|
|
CurrentPath = Fso.BuildPath(CurrentPath, CStr(Part))
|
|
If Fso.FolderExists(CurrentPath) Then
|
|
Attributes = Fso.GetFolder(CurrentPath).Attributes
|
|
ElseIf Fso.FileExists(CurrentPath) Then
|
|
Attributes = Fso.GetFile(CurrentPath).Attributes
|
|
Else
|
|
Exit For
|
|
End If
|
|
If (Attributes And 1024) <> 0 Then
|
|
Err.Raise vbObjectError + 802, "DxResolveRunPath", _
|
|
"Run path contains a reparse point"
|
|
End If
|
|
End If
|
|
Next
|
|
End Sub
|
|
|
|
Function DxCommandQuote(ByVal Value)
|
|
DxCommandQuote = Chr(34) & Replace(CStr(Value), Chr(34), _
|
|
Chr(92) & Chr(34)) & Chr(34)
|
|
End Function
|