Skip to content

Commit ba8e951

Browse files
author
A9G-Data-Droid
committed
Add project files.
1 parent 941fade commit ba8e951

3 files changed

Lines changed: 118 additions & 0 deletions

File tree

TextFileConvert.sln

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
2+
Microsoft Visual Studio Solution File, Format Version 12.00
3+
# Visual Studio 15
4+
VisualStudioVersion = 15.0.27428.2037
5+
MinimumVisualStudioVersion = 10.0.40219.1
6+
Project("{F184B08F-C81C-45F6-A57F-5ABD9991F28F}") = "TextFileConvert", "TextFileConvert\TextFileConvert.vbproj", "{C6D516A4-A83D-405A-8F46-DF37C9C25D4B}"
7+
EndProject
8+
Global
9+
GlobalSection(SolutionConfigurationPlatforms) = preSolution
10+
Debug|Any CPU = Debug|Any CPU
11+
Release|Any CPU = Release|Any CPU
12+
EndGlobalSection
13+
GlobalSection(ProjectConfigurationPlatforms) = postSolution
14+
{C6D516A4-A83D-405A-8F46-DF37C9C25D4B}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
15+
{C6D516A4-A83D-405A-8F46-DF37C9C25D4B}.Debug|Any CPU.Build.0 = Debug|Any CPU
16+
{C6D516A4-A83D-405A-8F46-DF37C9C25D4B}.Release|Any CPU.ActiveCfg = Release|Any CPU
17+
{C6D516A4-A83D-405A-8F46-DF37C9C25D4B}.Release|Any CPU.Build.0 = Release|Any CPU
18+
EndGlobalSection
19+
GlobalSection(SolutionProperties) = preSolution
20+
HideSolutionNode = FALSE
21+
EndGlobalSection
22+
GlobalSection(ExtensibilityGlobals) = postSolution
23+
SolutionGuid = {F276CEC7-656F-404C-BB9F-FE0CC0739DAA}
24+
EndGlobalSection
25+
EndGlobal
Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
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
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<RootNamespace>TextFileConvert</RootNamespace>
5+
<TargetFramework>netcoreapp2.0</TargetFramework>
6+
</PropertyGroup>
7+
8+
</Project>

0 commit comments

Comments
 (0)