|
| 1 | +Imports System.IO |
| 2 | +Imports System.Text |
| 3 | + |
| 4 | +Public Class ConvertLineEndings |
| 5 | + ''' <summary> |
| 6 | + ''' These are the different conversions we can handle in this library |
| 7 | + ''' </summary> |
| 8 | + Private Enum TextConvertMode |
| 9 | + Dos2Ux |
| 10 | + Ux2Dos |
| 11 | + Dos2Mac |
| 12 | + Mac2Dos |
| 13 | + Mac2Ux |
| 14 | + Ux2Mac |
| 15 | + End Enum |
| 16 | + |
| 17 | + Const CR = ChrW(13) |
| 18 | + Const LF = ChrW(10) |
| 19 | + |
| 20 | + ''' <summary> |
| 21 | + ''' Converts a DOS text file to have Unix line endings. |
| 22 | + ''' </summary> |
| 23 | + ''' <param name="originalFile">The file to convert.</param> |
| 24 | + ''' <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 |
| 28 | + |
| 29 | + ''' <summary> |
| 30 | + ''' Converts a DOS text file to have Unix line endings. |
| 31 | + ''' </summary> |
| 32 | + ''' <param name="originalFile">The file to convert.</param> |
| 33 | + ''' <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 |
| 37 | + |
| 38 | + ''' <summary> |
| 39 | + ''' Loads a whole text file in to memory, Performs a find\replace, and writes a new file. |
| 40 | + ''' </summary> |
| 41 | + ''' <param name="originalFile">The file to convert.</param> |
| 42 | + ''' <param name="newFile">The name of a new file to create.</param> |
| 43 | + ''' <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) |
| 45 | + Dim convertedText As New StringBuilder |
| 46 | + Dim oldFileStream As FileStream = Nothing |
| 47 | + Try |
| 48 | + oldFileStream = New FileStream(originalFile, FileMode.Open) |
| 49 | + Using oldFile As New StreamReader(oldFileStream) |
| 50 | + Do Until oldFile.EndOfStream |
| 51 | + Dim readBuffer(2) As Char |
| 52 | + Dim readChars As Integer = Await oldFile.ReadAsync(readBuffer, 0, 1) |
| 53 | + If readChars < 1 Then Exit Do |
| 54 | + Select Case convertMode |
| 55 | + Case TextConvertMode.Dos2Ux |
| 56 | + If readBuffer(0) = CR AndAlso oldFile.Peek() = 10 Then |
| 57 | + ' Strip out CR chars if followed by LF |
| 58 | + readBuffer(0) = Nothing |
| 59 | + End If |
| 60 | + Case TextConvertMode.Ux2Dos |
| 61 | + If readBuffer(0) = CR AndAlso oldFile.Peek() = 10 Then |
| 62 | + ' This is a DOS line ending, keep it. |
| 63 | + Dim tempBuffer(1) As Char |
| 64 | + Await oldFile.ReadAsync(tempBuffer, 0, 1) |
| 65 | + readBuffer(1) = tempBuffer(0) |
| 66 | + ElseIf readBuffer(0) = ChrW(10) Then |
| 67 | + ' Add preceeding CR |
| 68 | + readBuffer(0) = CR |
| 69 | + readBuffer(1) = LF |
| 70 | + End If |
| 71 | + Case Else |
| 72 | + Debug.Print("Unimplemented text conversion mode") |
| 73 | + Exit Sub |
| 74 | + End Select |
| 75 | + convertedText.Append(readBuffer) |
| 76 | + Loop |
| 77 | + End Using |
| 78 | + oldFileStream = Nothing |
| 79 | + Catch ex As Exception |
| 80 | + Debug.Print("Error: " & ex.Message & vbCrLf & "Number: " & ex.HResult) |
| 81 | + Finally |
| 82 | + If oldFileStream IsNot Nothing Then oldFileStream.Dispose() |
| 83 | + End Try |
| 84 | + End Sub |
| 85 | +End Class |
0 commit comments