Skip to content

Commit a4b63c7

Browse files
author
A9G-Data-Droid
committed
This is the first version that produces correct output.
1 parent 1808e38 commit a4b63c7

15 files changed

Lines changed: 538 additions & 17 deletions

TextFileConvert.sln

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,11 @@ Microsoft Visual Studio Solution File, Format Version 12.00
33
# Visual Studio 15
44
VisualStudioVersion = 15.0.27428.2037
55
MinimumVisualStudioVersion = 10.0.40219.1
6-
Project("{F184B08F-C81C-45F6-A57F-5ABD9991F28F}") = "TextFileConvert", "TextFileConvert\TextFileConvert.vbproj", "{C6D516A4-A83D-405A-8F46-DF37C9C25D4B}"
6+
Project("{778DAE3C-4631-46EA-AA77-85C1314464D9}") = "TextFileConvert", "TextFileConvert\TextFileConvert.vbproj", "{C6D516A4-A83D-405A-8F46-DF37C9C25D4B}"
77
EndProject
8-
Project("{F184B08F-C81C-45F6-A57F-5ABD9991F28F}") = "dos2ux", "dos2ux\dos2ux.vbproj", "{AA1FC840-1A4A-4F07-BAFC-8B281CEE6F10}"
8+
Project("{778DAE3C-4631-46EA-AA77-85C1314464D9}") = "dos2ux", "dos2ux\dos2ux.vbproj", "{AA1FC840-1A4A-4F07-BAFC-8B281CEE6F10}"
9+
EndProject
10+
Project("{F184B08F-C81C-45F6-A57F-5ABD9991F28F}") = "ux2dos", "ux2dos\ux2dos.vbproj", "{C596EC73-1634-429E-A20C-CD73DB6D416C}"
911
EndProject
1012
Global
1113
GlobalSection(SolutionConfigurationPlatforms) = preSolution
@@ -21,6 +23,10 @@ Global
2123
{AA1FC840-1A4A-4F07-BAFC-8B281CEE6F10}.Debug|Any CPU.Build.0 = Debug|Any CPU
2224
{AA1FC840-1A4A-4F07-BAFC-8B281CEE6F10}.Release|Any CPU.ActiveCfg = Release|Any CPU
2325
{AA1FC840-1A4A-4F07-BAFC-8B281CEE6F10}.Release|Any CPU.Build.0 = Release|Any CPU
26+
{C596EC73-1634-429E-A20C-CD73DB6D416C}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
27+
{C596EC73-1634-429E-A20C-CD73DB6D416C}.Debug|Any CPU.Build.0 = Debug|Any CPU
28+
{C596EC73-1634-429E-A20C-CD73DB6D416C}.Release|Any CPU.ActiveCfg = Release|Any CPU
29+
{C596EC73-1634-429E-A20C-CD73DB6D416C}.Release|Any CPU.Build.0 = Release|Any CPU
2430
EndGlobalSection
2531
GlobalSection(SolutionProperties) = preSolution
2632
HideSolutionNode = FALSE

TextFileConvert/ConvertLineEndings.vb

Lines changed: 41 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,8 @@ Public Class ConvertLineEndings
1414
Ux2Mac
1515
End Enum
1616

17-
Const CR = ChrW(13)
18-
Const LF = ChrW(10)
17+
Const CR As Char = ChrW(13)
18+
Const LF As Char = ChrW(10)
1919

2020
''' <summary>
2121
''' Converts a DOS text file to have Unix line endings.
@@ -45,28 +45,35 @@ Public Class ConvertLineEndings
4545
''' <param name="convertMode">This is the type of conversion we are going to perform</param>
4646
''' <returns>Exit code.</returns>
4747
Private Shared Async Function ReplaceLineEndings(originalFile As String, newFile As String, convertMode As TextConvertMode) As Task(Of Integer)
48+
' Attempt to detect encoding
49+
Dim fileEncoding As Encoding = GetEncoding(originalFile)
50+
If fileEncoding Is Nothing Then Return 4
51+
Debug.Print(fileEncoding.ToString())
52+
4853
Dim convertedText As New StringBuilder
4954
Dim oldFileStream As FileStream = Nothing
5055
Try
5156
oldFileStream = New FileStream(originalFile, FileMode.Open)
52-
Using oldFile As New StreamReader(oldFileStream)
57+
Using oldFile As New StreamReader(oldFileStream, fileEncoding, True)
5358
Do Until oldFile.EndOfStream
54-
Dim readBuffer(2) As Char
59+
Dim readBuffer(0) As Char
5560
Dim readChars As Integer = Await oldFile.ReadAsync(readBuffer, 0, 1)
5661
If readChars < 1 Then Exit Do
5762
Select Case convertMode
5863
Case TextConvertMode.Dos2Ux
5964
If readBuffer(0) = CR AndAlso oldFile.Peek() = 10 Then
6065
' Strip out CR chars if followed by LF
61-
readBuffer(0) = Nothing
66+
Await oldFile.ReadAsync(readBuffer, 0, 1)
6267
End If
6368
Case TextConvertMode.Ux2Dos
6469
If readBuffer(0) = CR AndAlso oldFile.Peek() = 10 Then
70+
ReDim Preserve readBuffer(1)
6571
' This is a DOS line ending, keep it.
6672
Dim tempBuffer(1) As Char
6773
Await oldFile.ReadAsync(tempBuffer, 0, 1)
6874
readBuffer(1) = tempBuffer(0)
69-
ElseIf readBuffer(0) = ChrW(10) Then
75+
ElseIf readBuffer(0) = LF Then
76+
ReDim readBuffer(1)
7077
' Add preceeding CR
7178
readBuffer(0) = CR
7279
readBuffer(1) = LF
@@ -80,20 +87,45 @@ Public Class ConvertLineEndings
8087
End Using
8188
oldFileStream = Nothing
8289
Catch ex As Exception
83-
Debug.Print("Error: " & ex.Message & vbCrLf & "Number: " & ex.HResult)
90+
Debug.Print("Error: " & ex.Message & Environment.NewLine & "Number: " & ex.HResult.ToString)
8491
Return ex.HResult
8592
Finally
8693
If oldFileStream IsNot Nothing Then oldFileStream.Dispose()
8794
End Try
8895

8996
'Write the result out to a new file
9097
Try
91-
Await File.WriteAllTextAsync(newFile, convertedText.ToString())
98+
File.WriteAllText(newFile, convertedText.ToString(), New UTF8Encoding(False))
9299
Catch ex As Exception
93-
Debug.Print("Error: " & ex.Message & vbCrLf & "Number: " & ex.HResult)
100+
Debug.Print("Error: " & ex.Message & Environment.NewLine & "Number: " & ex.HResult.ToString)
94101
Return ex.HResult
95102
End Try
96103

97104
Return 0 ' Exit status 0 is a good thing
98105
End Function
106+
107+
''' <summary>
108+
''' Attempt to detect the encoding of a file.
109+
''' </summary>
110+
''' <param name="filename">The file to get the encoding pattern from.</param>
111+
''' <returns>Encoding type, defaults to ASCII</returns>
112+
Public Shared Function GetEncoding(ByVal filename As String) As Encoding
113+
Dim bom = New Byte(3) {}
114+
115+
Try
116+
Using file = New FileStream(filename, FileMode.Open, FileAccess.Read)
117+
file.Read(bom, 0, 4)
118+
End Using
119+
Catch ex As Exception
120+
Debug.Print("Error: " & ex.Message & Environment.NewLine & "Number: " & ex.HResult.ToString)
121+
Return Nothing
122+
End Try
123+
124+
If bom(0) = &H2B AndAlso bom(1) = &H2F AndAlso bom(2) = &H76 Then Return Encoding.UTF7
125+
If bom(0) = &HEF AndAlso bom(1) = &HBB AndAlso bom(2) = &HBF Then Return Encoding.UTF8
126+
If bom(0) = &HFF AndAlso bom(1) = &HFE Then Return Encoding.Unicode
127+
If bom(0) = &HFE AndAlso bom(1) = &HFF Then Return Encoding.BigEndianUnicode
128+
If bom(0) = 0 AndAlso bom(1) = 0 AndAlso bom(2) = &HFE AndAlso bom(3) = &HFF Then Return Encoding.UTF32
129+
Return Encoding.ASCII
130+
End Function
99131
End Class

TextFileConvert/TextFileConvert.vbproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
<PropertyGroup>
44
<RootNamespace>TextFileConvert</RootNamespace>
5-
<TargetFramework>netcoreapp2.0</TargetFramework>
5+
<TargetFramework>netstandard2.0</TargetFramework>
66
</PropertyGroup>
77

88
</Project>

dos2ux/dos2ux.vb

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
Imports TextFileConvert
22

3-
Module dos2ux
3+
Module Dos2Ux
4+
45
Sub Main(args As String())
56
Dim exitCode As Integer
67
If args.Length <> 2 OrElse args(0) = Nothing OrElse args(1) = Nothing Then
@@ -37,7 +38,7 @@ SYNOPSIS
3738
dos2ux oldfilename newfilename
3839

3940
DESCRIPTION
40-
dos2ux reads oldfilename and writes out newfilename, converting line endings from UNIX (LF) to DOS (CRLF)
41+
dos2ux reads oldfilename and writes out newfilename, converting line endings from DOS (CRLF) to UNIX (LF)
4142
"
4243
Console.WriteLine(helpMessage)
4344
End Sub

dos2ux/dos2ux.vbproj

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,9 @@
33
<PropertyGroup>
44
<OutputType>Exe</OutputType>
55
<RootNamespace>dos2ux</RootNamespace>
6-
<TargetFramework>netcoreapp2.0</TargetFramework>
6+
<TargetFramework>net461</TargetFramework>
77
<StartupObject>Sub Main</StartupObject>
88
<ApplicationManifest>My Project\app.manifest</ApplicationManifest>
9-
<RuntimeFrameworkVersion>2.0.0-*</RuntimeFrameworkVersion>
10-
<RuntimeIdentifiers>win7-x32</RuntimeIdentifiers>
119
</PropertyGroup>
1210

1311
<ItemGroup>

ux2dos/App.config

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
<?xml version="1.0" encoding="utf-8" ?>
2+
<configuration>
3+
<startup>
4+
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.6.1" />
5+
</startup>
6+
</configuration>

ux2dos/My Project/Application.Designer.vb

Lines changed: 13 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<MyApplicationData xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
3+
<MySubMain>false</MySubMain>
4+
<SingleInstance>false</SingleInstance>
5+
<ShutdownMode>0</ShutdownMode>
6+
<EnableVisualStyles>true</EnableVisualStyles>
7+
<AuthenticationMode>0</AuthenticationMode>
8+
<ApplicationType>2</ApplicationType>
9+
<SaveMySettingsOnExit>true</SaveMySettingsOnExit>
10+
</MyApplicationData>

ux2dos/My Project/AssemblyInfo.vb

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
Imports System
2+
Imports System.Reflection
3+
Imports System.Runtime.InteropServices
4+
5+
' General Information about an assembly is controlled through the following
6+
' set of attributes. Change these attribute values to modify the information
7+
' associated with an assembly.
8+
9+
' Review the values of the assembly attributes
10+
11+
<Assembly: AssemblyTitle("ux2dos")>
12+
<Assembly: AssemblyDescription("")>
13+
<Assembly: AssemblyCompany("Microsoft")>
14+
<Assembly: AssemblyProduct("ux2dos")>
15+
<Assembly: AssemblyCopyright("Copyright © Microsoft 2018")>
16+
<Assembly: AssemblyTrademark("")>
17+
18+
<Assembly: ComVisible(False)>
19+
20+
'The following GUID is for the ID of the typelib if this project is exposed to COM
21+
<Assembly: Guid("4ed6d1ad-c413-44a6-9f33-d6a313f75f09")>
22+
23+
' Version information for an assembly consists of the following four values:
24+
'
25+
' Major Version
26+
' Minor Version
27+
' Build Number
28+
' Revision
29+
'
30+
' You can specify all the values or you can default the Build and Revision Numbers
31+
' by using the '*' as shown below:
32+
' <Assembly: AssemblyVersion("1.0.*")>
33+
34+
<Assembly: AssemblyVersion("1.0.0.0")>
35+
<Assembly: AssemblyFileVersion("1.0.0.0")>

ux2dos/My Project/Resources.Designer.vb

Lines changed: 62 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)