Skip to content

Commit

Permalink
New Encoding method
Browse files Browse the repository at this point in the history
  • Loading branch information
A9G-Data-Droid committed Mar 4, 2021
1 parent aa1afe3 commit 9aea695
Show file tree
Hide file tree
Showing 10 changed files with 200 additions and 175 deletions.
4 changes: 2 additions & 2 deletions Testing/Testing.accdb.src/dbs-properties.json
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
{
{
"Info": {
"Class": "clsDbProperty",
"Description": "Database Properties (DAO)"
Expand Down Expand Up @@ -117,7 +117,7 @@
"Type": 4
},
"ProjVer": {
"Value": 119,
"Value": 142,
"Type": 3
},
"QueryTimeout": {
Expand Down
254 changes: 127 additions & 127 deletions Testing/Testing.accdb.src/modules/basExtendedChars.bas

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion Testing/Testing.accdb.src/vcs-options.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"Info": {
"AddinVersion": "3.3.1",
"AddinVersion": "3.3.3",
"AccessVersion": "16.0 64-bit"
},
"Options": {
Expand Down
6 changes: 3 additions & 3 deletions Version Control.accda.src/dbs-properties.json
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@
"Type": 10
},
"AppVersion": {
"Value": "3.3.6",
"Value": "3.3.3",
"Type": 10
},
"Auto Compact": {
Expand Down Expand Up @@ -117,7 +117,7 @@
"Type": 4
},
"ProjVer": {
"Value": 119,
"Value": 142,
"Type": 3
},
"QueryTimeout": {
Expand Down Expand Up @@ -197,7 +197,7 @@
"Type": 2
},
"Version": {
"Value": "14.0",
"Value": "12.0",
"Type": 12
},
"WebDesignMode": {
Expand Down
78 changes: 55 additions & 23 deletions Version Control.accda.src/modules/modEncoding.bas
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
'---------------------------------------------------------------------------------------
'---------------------------------------------------------------------------------------
' Module : modEncoding
' Author : Adam Waller
' Date : 12/4/2020
Expand Down Expand Up @@ -206,7 +206,7 @@ Public Sub ConvertUtf8Ucs2(strSourceFile As String, strDestinationFile As String
strText = Utf8BytesToString(utf8Bytes)

' Write as UCS-2 LE (BOM)
With FSO.CreateTextFile(strDestinationFile, True, TristateTrue)

This comment has been minimized.

Copy link
@A9G-Data-Droid

A9G-Data-Droid Mar 4, 2021

Author Owner

CreateTextFile is not Tristate, it's binary.

With FSO.CreateTextFile(strDestinationFile, True, True)
.Write strText
.Close
End With
Expand All @@ -230,9 +230,8 @@ End Sub
Public Sub ConvertAnsiUtf8(strSourceFile As String, strDestinationFile As String, _
Optional blnDeleteSourceFileAfterConversion As Boolean = True)

' Convert the ANSI content to UTF-8, and write to a new file.
' (Adds UTF-8 BOM if extended characters are used.)
WriteFile ReadFile(strSourceFile, "_autodetect_all"), strDestinationFile
ReEncodeFile strSourceFile, "_autodetect_all", strDestinationFile, "UTF-8", adSaveCreateOverWrite

If blnDeleteSourceFileAfterConversion Then DeleteFile strSourceFile

End Sub
Expand All @@ -250,13 +249,7 @@ Public Sub ConvertUtf8Ansi(strSourceFile As String, strDestinationFile As String

' Perform file conversion
Perf.OperationStart "ANSI Conversion"
With New ADODB.Stream
.Charset = "_autodetect_all"
.Open
.WriteText ReadFile(strSourceFile)
.SaveToFile strDestinationFile, adSaveCreateOverWrite
.Close
End With
ReEncodeFile strSourceFile, "UTF-8", strDestinationFile, "_autodetect_all", adSaveCreateOverWrite
Perf.OperationEnd

' Remove original file if specified.
Expand Down Expand Up @@ -421,7 +414,6 @@ End Function
'
Public Function ReadFile(strPath As String, Optional strCharset As String = "UTF-8") As String

Dim stm As ADODB.Stream
Dim strText As String
Dim cData As clsConcat
Dim strBom As String
Expand All @@ -435,8 +427,7 @@ Public Function ReadFile(strPath As String, Optional strCharset As String = "UTF
If FSO.FileExists(strPath) Then
Perf.OperationStart "Read File"
Set cData = New clsConcat
Set stm = New ADODB.Stream
With stm
With New ADODB.Stream
.Charset = strCharset
.Open
.LoadFromFile strPath
Expand All @@ -453,7 +444,6 @@ Public Function ReadFile(strPath As String, Optional strCharset As String = "UTF
Loop
.Close
End With
Set stm = Nothing
Perf.OperationEnd
End If

Expand Down Expand Up @@ -485,7 +475,7 @@ Public Sub WriteFile(strText As String, strPath As String)
bteUtf8 = Utf8BytesFromString(strContent)

' Write binary content to file.
WriteBinaryFile bteUtf8, StringHasUnicode(strContent), strPath
WriteBinaryFile bteUtf8, False, strPath

End Sub

Expand All @@ -499,12 +489,10 @@ End Sub
'
Public Sub WriteBinaryFile(bteContent() As Byte, blnUtf8Bom As Boolean, strPath As String)

Dim stm As ADODB.Stream
Dim bteBOM(0 To 2) As Byte

' Write to a binary file using a Stream object
Set stm = New ADODB.Stream
With stm
With New ADODB.Stream
.Type = adTypeBinary
.Open
If blnUtf8Bom Then
Expand All @@ -513,25 +501,69 @@ Public Sub WriteBinaryFile(bteContent() As Byte, blnUtf8Bom As Boolean, strPath
bteBOM(2) = &HBF
.Write bteBOM
End If

.Write bteContent
VerifyPath strPath
Perf.OperationStart "Write to Disk"
.SaveToFile strPath, adSaveCreateOverWrite
Perf.OperationEnd
.Close
End With

End Sub


'---------------------------------------------------------------------------------------
' Procedure : StringHasUnicode
' Procedure : StringHasExtendedASCII
' Author : Adam Waller
' Date : 3/6/2020
' Purpose : Returns true if the string contains non-ASCI characters.
'---------------------------------------------------------------------------------------
'
Public Function StringHasUnicode(strText As String) As Boolean
Public Function StringHasExtendedASCII(strText As String) As Boolean

StringHasUnicode = (StrConv(strText, vbUnicode) = strText)
Perf.OperationStart "Unicode Check"
With New VBScript_RegExp_55.RegExp
' Include extended ASCII characters here.
.Pattern = "[^\u0000-\u007F]"
StringHasExtendedASCII = .Test(strText)
End With
Perf.OperationEnd

End Function


'---------------------------------------------------------------------------------------
' Procedure : ReEncodeFile
' Author : Adam Kauffman
' Date : 3/4/2021
' Purpose : Change File Encoding. It reads and writes at the same time so the files must be different.
' intOverwriteMode will take the following values:
' Const adSaveCreateOverWrite = 2
' Const adSaveCreateNotExist = 1
'---------------------------------------------------------------------------------------
'
Public Sub ReEncodeFile(strInputFile, strInputCharset, strOutputFile, strOutputCharset, intOverwriteMode)

Dim objOutputStream As ADODB.Stream
Set objOutputStream = New ADODB.Stream

With New ADODB.Stream
.Open
.Type = adTypeBinary
.LoadFromFile strInputFile
.Type = adTypeText
.Charset = strInputCharset
objOutputStream.Open
objOutputStream.Charset = strOutputCharset
Do While .EOS <> True
' Read from one file and write to the other a line at a time
objOutputStream.WriteText .ReadText(adReadLine), adWriteLine
Loop

.Close
End With

objOutputStream.SaveToFile strOutputFile, intOverwriteMode
objOutputStream.Close
End Sub
14 changes: 4 additions & 10 deletions Version Control.accda.src/modules/modUnitTesting.bas
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ Public Sub TestUCS2toUTF8RoundTrip()

'Arrange:
Dim queryName As String
queryName = "Temp_Test_Query_Delete_Me_Æ_ø_Å"
queryName = "Temp_Test_Query_Delete_Me_Æ_ø_Å"
Dim tempFileName As String
tempFileName = GetTempFile()

Expand All @@ -53,7 +53,7 @@ Public Sub TestUCS2toUTF8RoundTrip()
UTFtoUCS = tempFileName & "UTF-8toUCS-2"

' Use temporary query to export example file
CurrentDb.CreateQueryDef queryName, "SELECT * FROM TEST WHERE TESTING='ÆØÅ'"
CurrentDb.CreateQueryDef queryName, "SELECT * FROM TEST WHERE TESTING='ÆØÅ'"
Application.SaveAsText acQuery, queryName, tempFileName
CurrentDb.QueryDefs.Delete queryName

Expand Down Expand Up @@ -106,7 +106,7 @@ Private Sub TestParseSpecialCharsInJson()

Set FSO = CreateObject("Scripting.FileSystemObject")
With FSO.CreateTextFile(strPath, True)
.WriteLine "{""Test"":""ÆØÅ are special?""}"
.WriteLine "{""Test"":""ÆØÅ are special?""}"
.Close
End With

Expand All @@ -130,12 +130,6 @@ TestFail:
Assert.Fail "Test raised an error: #" & Err.Number & " - " & Err.Description
End Sub

'@TestMethod("TextConversion")
Private Sub TestStringHasUnicode()
Assert.IsTrue StringHasUnicode("aß?de?????µ???p?st?f?")
Assert.IsFalse StringHasUnicode("'——————————————————————————————————————————————————————————————————————————————————————————")
End Sub



'@TestMethod("Sorting")
Expand Down Expand Up @@ -220,4 +214,4 @@ TestExit:
Exit Sub
TestFail:
Assert.Fail "Test raised an error: #" & Err.Number & " - " & Err.Description
End Sub
End Sub
4 changes: 2 additions & 2 deletions Version Control.accda.src/modules/modVCSUtility.bas
Original file line number Diff line number Diff line change
Expand Up @@ -179,8 +179,8 @@ Public Sub SaveComponentAsText(intType As AcObjectType, _
Case acModule '(ANSI text file)
' Modules may contain extended characters that need UTF-8 conversion
' to display correctly in some editors.
If StringHasUnicode(ReadFile(strTempFile, "_autodetect_all")) Then
' Convert to UTF-8 BOM
If StringHasExtendedASCII(ReadFile(strTempFile, "_autodetect_all")) Then
' Convert to UTF-8
ConvertAnsiUtf8 strTempFile, strFile
Else
' Leave as ANSI
Expand Down
4 changes: 2 additions & 2 deletions Version Control.accda.src/vbe-project.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@
},
"Items": {
"Name": "MSAccessVCS",
"Description": "Version 3.3.6 deployed on 3/3/2021",
"Description": "Version 3.3.3 deployed on 3/4/2021",
"FileName": "rel:Version Control.accda",
"HelpFile": "",
"HelpFile": "100746350",
"HelpContextId": 0,
"Mode": 0,
"Protection": 0,
Expand Down
5 changes: 2 additions & 3 deletions Version Control.accda.src/vbe-references.json
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
{
"Info": {
"Class": "clsDbVbeReference",
"Description": "VBE References",
"VCS Version": "3.1.76"
"Description": "VBE References"
},
"Items": {
"stdole": {
Expand All @@ -23,7 +22,7 @@
},
"Office": {
"GUID": "{2DF8D04C-5BFA-101B-BDE5-00AA0044DE52}",
"Version": "2.5"
"Version": "2.8"
},
"Scripting": {
"GUID": "{420B2830-E718-11CF-893D-00A0C9054228}",
Expand Down
4 changes: 2 additions & 2 deletions Version Control.accda.src/vcs-options.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"Info": {
"AddinVersion": "3.3.6",
"AccessVersion": "14.0 32-bit"
"AddinVersion": "3.3.3",
"AccessVersion": "16.0 64-bit"
},
"Options": {
"ExportFolder": "",
Expand Down

0 comments on commit 9aea695

Please sign in to comment.