@@ -3,19 +3,19 @@ Imports System.Text
33
44Public Class ConvertLineEndings
55 ''' <summary>
6- ''' These are the different conversions we can handle in this library
6+ ''' These are the different conversions that this library can perform.
77 ''' </summary>
88 Private Enum TextConvertMode
99 Dos2Ux
1010 Ux2Dos
11- Dos2Mac
12- Mac2Dos
13- Mac2Ux
14- Ux2Mac
11+ ' Dos2Mac
12+ ' Mac2Dos
13+ ' Mac2Ux
14+ ' Ux2Mac
1515 End Enum
1616
1717 ''' <summary>
18- ''' Converts a DOS text file to have Unix line endings.
18+ ''' Converts a DOS text file to have Unix line endings.
1919 ''' </summary>
2020 ''' <param name="originalFile">The file to convert.</param>
2121 ''' <param name="newFile">The name of a new file to create.</param>
@@ -25,7 +25,7 @@ Public Class ConvertLineEndings
2525 End Function
2626
2727 ''' <summary>
28- ''' Converts a DOS text file to have Unix line endings.
28+ ''' Converts a DOS text file to have Unix line endings.
2929 ''' </summary>
3030 ''' <param name="originalFile">The file to convert.</param>
3131 ''' <param name="newFile">The name of a new file to create.</param>
@@ -35,13 +35,14 @@ Public Class ConvertLineEndings
3535 End Function
3636
3737 ''' <summary>
38- ''' Loads a whole text file in to memory, Performs a find\replace, and writes a new file.
38+ ''' Loads a whole text file in to memory, Performs a find\replace, and writes a new file.
3939 ''' </summary>
4040 ''' <param name="originalFile">The file to convert.</param>
4141 ''' <param name="newFile">The name of a new file to create.</param>
4242 ''' <param name="convertMode">This is the type of conversion we are going to perform</param>
4343 ''' <returns>Exit code.</returns>
44- Private Shared Async Function ReplaceLineEndings(originalFile As String , newFile As String , convertMode As TextConvertMode) As Task( Of Integer )
44+ Private Shared Async Function ReplaceLineEndings(originalFile As String , newFile As String ,
45+ convertMode As TextConvertMode) As Task( Of Integer )
4546 Const cr As Char = ChrW( 13 )
4647 Const lf As Char = ChrW( 10 )
4748
@@ -55,10 +56,10 @@ Public Class ConvertLineEndings
5556 Try
5657 oldFileStream = New FileStream(originalFile, FileMode.Open)
5758 Using oldFile As New StreamReader(oldFileStream, fileEncoding, True )
58- Do Until oldFile.EndOfStream
59+ Do Until oldFile.EndOfStream ' Read through the whole file
5960 Dim readBuffer( 0 ) As Char
6061 Dim readChars As Integer = Await oldFile.ReadAsync(readBuffer, 0 , 1 )
61- If readChars < 1 Then Exit Do
62+ If readChars < 1 Then Exit Do ' Short circuit
6263 Select Case convertMode
6364 Case TextConvertMode.Dos2Ux
6465 If readBuffer( 0 ) = cr AndAlso oldFile.Peek() = 10 Then
@@ -82,9 +83,11 @@ Public Class ConvertLineEndings
8283 Debug.Print( "Unimplemented text conversion mode" )
8384 Return - 1
8485 End Select
86+
8587 convertedText.Append(readBuffer)
8688 Loop
8789 End Using
90+
8891 oldFileStream = Nothing
8992 Catch ex As Exception
9093 Debug.Print( "Error: " & ex.Message & Environment.NewLine & "Number: " & ex.HResult.ToString)
@@ -105,14 +108,13 @@ Public Class ConvertLineEndings
105108 End Function
106109
107110 ''' <summary>
108- ''' Attempt to detect the encoding of a file.
111+ ''' Attempt to detect the encoding of a file.
109112 ''' </summary>
110113 ''' <param name="filename">The file to get the encoding pattern from.</param>
111- ''' <returns>Encoding type, defaults to ASCII</returns>
114+ ''' <returns>Encoding type, defaults to ASCII. </returns>
112115 Public Shared Function GetEncoding(filename As String ) As Encoding
113116 Dim bom = New Byte ( 3 ) {}
114-
115- Try
117+ Try ' to read BOM
116118 Using file = New FileStream(filename, FileMode.Open, FileAccess.Read)
117119 file.Read(bom, 0 , 4 )
118120 End Using
@@ -121,11 +123,14 @@ Public Class ConvertLineEndings
121123 Return Nothing
122124 End Try
123125
126+ ' Detect BOM type
124127 If bom( 0 ) = &H2B AndAlso bom( 1 ) = &H2F AndAlso bom( 2 ) = &H76 Then Return Encoding.UTF7
125128 If bom( 0 ) = &HEF AndAlso bom( 1 ) = &HBB AndAlso bom( 2 ) = &HBF Then Return Encoding.UTF8
126129 If bom( 0 ) = &HFF AndAlso bom( 1 ) = &HFE Then Return Encoding.Unicode
127130 If bom( 0 ) = &HFE AndAlso bom( 1 ) = &HFF Then Return Encoding.BigEndianUnicode
128131 If bom( 0 ) = 0 AndAlso bom( 1 ) = 0 AndAlso bom( 2 ) = &HFE AndAlso bom( 3 ) = &HFF Then Return Encoding.UTF32
132+
133+ ' Default to
129134 Return Encoding.ASCII
130135 End Function
131136End Class
0 commit comments