11Imports System.IO
22Imports System.Text
33
4+
5+
46Public Class ConvertLineEndings
57 ''' <summary>
68 ''' These are the different conversions that this library can perform.
@@ -37,10 +39,10 @@ Public Class ConvertLineEndings
3739 ''' <summary>
3840 ''' Loads a whole text file in to memory, Performs a find\replace, and writes a new file.
3941 ''' </summary>
40- ''' <param name="originalFile">The file to convert.</param>
42+ ''' <param name="originalFile">The file path to convert.</param>
4143 ''' <param name="newFile">The name of a new file to create.</param>
42- ''' <param name="convertMode">This is the type of conversion we are going to perform</param>
43- ''' <returns>Exit code.</returns>
44+ ''' <param name="convertMode">This is the type of conversion we are going to perform. </param>
45+ ''' <returns>Exit code. 0 is success. -1 is a symbolic link. </returns>
4446 Private Shared Async Function ReplaceLineEndings(originalFile As String , newFile As String ,
4547 convertMode As TextConvertMode) As Task( Of Integer )
4648
@@ -65,44 +67,62 @@ Public Class ConvertLineEndings
6567 Return ex.HResult
6668 End Try
6769
68- Return 0 ' Exit status 0 is a good thing
70+ Return 0
6971 End Function
7072
73+ ''' <summary>
74+ ''' This is where the actual conversion logic lives.
75+ ''' </summary>
76+ ''' <param name="originalFile">The file you want to convert.</param>
77+ ''' <param name="fileEncoding">The encoding you want to read that file as.</param>
78+ ''' <param name="convertMode">The type of conversion you want to perform.</param>
79+ ''' <returns>The full text of the file with new line endings.</returns>
7180 Private Shared Async Function GetConvertedText(originalFile As FileStream, fileEncoding As Encoding, convertMode As TextConvertMode) As Task( Of String )
72- Const CR As Char = ChrW( 13 )
73- Const LF As Char = ChrW( 10 )
81+ Const CR As Char = ChrW( 13 ) ' Carriage Return
82+ Const LF As Char = ChrW( 10 ) ' Line Feed
7483
7584 Dim convertedLines As New StringBuilder
7685 Using oldFile As New StreamReader(originalFile, fileEncoding, True )
77- Do Until oldFile.EndOfStream ' Read through the whole file
78- Dim readBuffer( 0 ) As Char
79- Dim readChars As Integer = Await oldFile.ReadAsync(readBuffer, 0 , 1 )
80- If readChars >= 1 Then
81- Select Case convertMode
82- Case TextConvertMode.Dos2Ux
86+ Select Case convertMode
87+ Case TextConvertMode.Dos2Ux
88+ Do Until oldFile.EndOfStream
89+ Dim readBuffer( 0 ) As Char
90+ Dim readChars As Integer = Await oldFile.ReadAsync(readBuffer, 0 , 1 )
91+ If readChars >= 1 Then
92+ ' Look for Dos line endings
8393 If readBuffer( 0 ) = CR AndAlso oldFile.Peek() = 10 Then
8494 ' Strip out CR chars if followed by LF
8595 Await oldFile.ReadAsync(readBuffer, 0 , 1 )
8696 End If
87- Case TextConvertMode.Ux2Dos
97+
98+ 'Yield readBuffer
99+ convertedLines.Append(readBuffer)
100+ End If
101+ Loop
102+ Case TextConvertMode.Ux2Dos
103+ Do Until oldFile.EndOfStream
104+ Dim readBuffer( 0 ) As Char
105+ Dim readChars As Integer = Await oldFile.ReadAsync(readBuffer, 0 , 1 )
106+ If readChars >= 1 Then
107+ ' Check for CR first to avoid doubling the CR character when LF is found
88108 If readBuffer( 0 ) = CR AndAlso oldFile.Peek() = 10 Then
89- ReDim Preserve readBuffer( 1 )
90109 ' This is a DOS line ending, keep it.
110+ ReDim Preserve readBuffer( 1 )
91111 Dim tempBuffer( 1 ) As Char
92112 Await oldFile.ReadAsync(tempBuffer, 0 , 1 )
93113 readBuffer( 1 ) = tempBuffer( 0 )
94114 ElseIf readBuffer( 0 ) = LF Then
115+ ' This is a Unix line ending. Add preceeding CR.
95116 ReDim readBuffer( 1 )
96- ' Add preceeding CR
97117 readBuffer( 0 ) = CR
98118 readBuffer( 1 ) = LF
99119 End If
100- End Select
101120
102- 'Yield readBuffer
103- convertedLines.Append(readBuffer)
104- End If
105- Loop
121+ 'Yield readBuffer
122+ convertedLines.Append(readBuffer)
123+ End If
124+ Loop
125+ End Select
106126 End Using
107127
108128 Return convertedLines.ToString
0 commit comments