@@ -22,26 +22,29 @@ Public Class ConvertLineEndings
2222 ''' </summary>
2323 ''' <param name="originalFile">The file to convert.</param>
2424 ''' <param name="newFile">The name of a new file to create.</param>
25- Public Sub Dos2Ux(originalFile As String , newFile As String )
26- ReplaceLineEndings(originalFile, newFile, TextConvertMode.Dos2Ux)
27- End Sub
25+ ''' <returns>Exit code.</returns>
26+ Public Shared Async Function Dos2Ux(originalFile As String , newFile As String ) As Task( Of Integer )
27+ Return Await ReplaceLineEndings(originalFile, newFile, TextConvertMode.Dos2Ux)
28+ End Function
2829
2930 ''' <summary>
3031 ''' Converts a DOS text file to have Unix line endings.
3132 ''' </summary>
3233 ''' <param name="originalFile">The file to convert.</param>
3334 ''' <param name="newFile">The name of a new file to create.</param>
34- Public Sub Ux2Dos(originalFile As String , newFile As String )
35- ReplaceLineEndings(originalFile, newFile, TextConvertMode.Ux2Dos)
36- End Sub
35+ ''' <returns>Exit code.</returns>
36+ Public Shared Async Function Ux2Dos(originalFile As String , newFile As String ) As Task( Of Integer )
37+ Return Await ReplaceLineEndings(originalFile, newFile, TextConvertMode.Ux2Dos)
38+ End Function
3739
3840 ''' <summary>
3941 ''' Loads a whole text file in to memory, Performs a find\replace, and writes a new file.
4042 ''' </summary>
4143 ''' <param name="originalFile">The file to convert.</param>
4244 ''' <param name="newFile">The name of a new file to create.</param>
4345 ''' <param name="convertMode">This is the type of conversion we are going to perform</param>
44- Private Async Sub ReplaceLineEndings(originalFile As String , newFile As String , convertMode As TextConvertMode)
46+ ''' <returns>Exit code.</returns>
47+ Private Shared Async Function ReplaceLineEndings(originalFile As String , newFile As String , convertMode As TextConvertMode) As Task( Of Integer )
4548 Dim convertedText As New StringBuilder
4649 Dim oldFileStream As FileStream = Nothing
4750 Try
@@ -70,16 +73,27 @@ Public Class ConvertLineEndings
7073 End If
7174 Case Else
7275 Debug.Print( "Unimplemented text conversion mode" )
73- Exit Sub
76+ Return - 1
7477 End Select
7578 convertedText.Append(readBuffer)
7679 Loop
7780 End Using
7881 oldFileStream = Nothing
7982 Catch ex As Exception
8083 Debug.Print( "Error: " & ex.Message & vbCrLf & "Number: " & ex.HResult)
84+ Return ex.HResult
8185 Finally
8286 If oldFileStream IsNot Nothing Then oldFileStream.Dispose()
8387 End Try
84- End Sub
88+
89+ 'Write the result out to a new file
90+ Try
91+ Await File.WriteAllTextAsync(newFile, convertedText.ToString())
92+ Catch ex As Exception
93+ Debug.Print( "Error: " & ex.Message & vbCrLf & "Number: " & ex.HResult)
94+ Return ex.HResult
95+ End Try
96+
97+ Return 0 ' Exit status 0 is a good thing
98+ End Function
8599End Class
0 commit comments