Skip to content

Commit 4f2ef3e

Browse files
author
A9G-Data-Droid
committed
Console App dos2ux added
1 parent ba8e951 commit 4f2ef3e

4 files changed

Lines changed: 87 additions & 9 deletions

File tree

TextFileConvert.sln

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ VisualStudioVersion = 15.0.27428.2037
55
MinimumVisualStudioVersion = 10.0.40219.1
66
Project("{F184B08F-C81C-45F6-A57F-5ABD9991F28F}") = "TextFileConvert", "TextFileConvert\TextFileConvert.vbproj", "{C6D516A4-A83D-405A-8F46-DF37C9C25D4B}"
77
EndProject
8+
Project("{F184B08F-C81C-45F6-A57F-5ABD9991F28F}") = "dos2ux", "dos2ux\dos2ux.vbproj", "{AA1FC840-1A4A-4F07-BAFC-8B281CEE6F10}"
9+
EndProject
810
Global
911
GlobalSection(SolutionConfigurationPlatforms) = preSolution
1012
Debug|Any CPU = Debug|Any CPU
@@ -15,6 +17,10 @@ Global
1517
{C6D516A4-A83D-405A-8F46-DF37C9C25D4B}.Debug|Any CPU.Build.0 = Debug|Any CPU
1618
{C6D516A4-A83D-405A-8F46-DF37C9C25D4B}.Release|Any CPU.ActiveCfg = Release|Any CPU
1719
{C6D516A4-A83D-405A-8F46-DF37C9C25D4B}.Release|Any CPU.Build.0 = Release|Any CPU
20+
{AA1FC840-1A4A-4F07-BAFC-8B281CEE6F10}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
21+
{AA1FC840-1A4A-4F07-BAFC-8B281CEE6F10}.Debug|Any CPU.Build.0 = Debug|Any CPU
22+
{AA1FC840-1A4A-4F07-BAFC-8B281CEE6F10}.Release|Any CPU.ActiveCfg = Release|Any CPU
23+
{AA1FC840-1A4A-4F07-BAFC-8B281CEE6F10}.Release|Any CPU.Build.0 = Release|Any CPU
1824
EndGlobalSection
1925
GlobalSection(SolutionProperties) = preSolution
2026
HideSolutionNode = FALSE

TextFileConvert/ConvertLineEndings.vb

Lines changed: 23 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -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
8599
End Class

dos2ux/Program.vb

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
Imports System
2+
Imports TextFileConvert
3+
4+
Module Program
5+
Sub Main(args As String())
6+
Dim exitCode As Integer
7+
If args.LongCount() > 2 OrElse args(0) = Nothing OrElse args(1) = Nothing Then
8+
PrintInstructions()
9+
exitCode = 87
10+
Else
11+
Console.WriteLine("Perform DOS to UNIX conversion")
12+
Dim runAsync As Task(Of Integer) = ConvertLineEndings.Dos2Ux(args(0), args(1))
13+
Try
14+
runAsync.Wait()
15+
Catch ex As Exception
16+
' We handle failure later
17+
End Try
18+
19+
If runAsync.Status = TaskStatus.Faulted Then
20+
Console.WriteLine("Conversion failed.")
21+
exitCode = 188
22+
Else
23+
Console.WriteLine("Conversion complete.")
24+
exitCode = runAsync.Result
25+
End If
26+
End If
27+
28+
Environment.ExitCode = exitCode
29+
End Sub
30+
31+
Private Sub PrintInstructions()
32+
Dim helpMessage As String
33+
helpMessage =
34+
"NAME
35+
dos2ux - Convert ASCII file format
36+
37+
SYNOPSIS
38+
dos2ux oldfilename newfilename
39+
40+
DESCRIPTION
41+
dos2ux reads oldfilename and writes out newfilename, converting line endings from UNIX (LF) to DOS (CRLF)
42+
"
43+
Console.WriteLine(helpMessage)
44+
End Sub
45+
End Module

dos2ux/dos2ux.vbproj

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<OutputType>Exe</OutputType>
5+
<RootNamespace>dos2ux</RootNamespace>
6+
<TargetFramework>netcoreapp2.0</TargetFramework>
7+
</PropertyGroup>
8+
9+
<ItemGroup>
10+
<ProjectReference Include="..\TextFileConvert\TextFileConvert.vbproj" />
11+
</ItemGroup>
12+
13+
</Project>

0 commit comments

Comments
 (0)