-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #4 from A9G-Data-Droid/csharp
Convert to Csharp
- Loading branch information
Showing
19 changed files
with
511 additions
and
386 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
using System.Diagnostics; | ||
using System.IO; | ||
using System.Threading.Tasks; | ||
using Microsoft.VisualStudio.TestTools.UnitTesting; | ||
using TextFileConvert; | ||
|
||
|
||
|
||
namespace TestLineEndingConversion; | ||
|
||
[TestClass] | ||
public class IntegrationTesting | ||
{ | ||
private const string TestUnixTxt = "testunix.txt"; | ||
private const string TestDosTxt = "testdos.txt"; | ||
|
||
[TestMethod] | ||
public async Task TestDos2Ux() | ||
{ | ||
// Where am I? | ||
Debug.WriteLine(Directory.GetCurrentDirectory()); | ||
if(File.Exists(TestUnixTxt)) | ||
File.Delete(TestUnixTxt); | ||
|
||
// ensure we start well | ||
var precondition = await File.ReadAllTextAsync("dos.txt"); | ||
Assert.IsTrue(precondition.Contains("\r\n")); | ||
|
||
// Do Conversion | ||
var exitCode = await ConvertLineEndings.Dos2Ux("dos.txt", TestUnixTxt); | ||
|
||
// Zero is success | ||
Assert.AreEqual(exitCode, 0); | ||
|
||
// Compare the test file to the artifact file | ||
var result = await File.ReadAllTextAsync(TestUnixTxt); | ||
Assert.IsFalse(result.Contains("\r\n")); | ||
Assert.AreEqual(await File.ReadAllTextAsync("unix.txt"), result); | ||
|
||
// Clean up | ||
File.Delete(TestUnixTxt); | ||
} | ||
|
||
[TestMethod] | ||
public async Task TestUx2Dos() | ||
{ | ||
// Where am I? | ||
Debug.WriteLine(Directory.GetCurrentDirectory()); | ||
|
||
if(File.Exists(TestDosTxt)) | ||
File.Delete(TestDosTxt); | ||
|
||
// ensure we start well | ||
var precondition = await File.ReadAllTextAsync("unix.txt"); | ||
Assert.IsFalse(precondition.Contains("\r\n")); | ||
|
||
// Do Conversion | ||
int exitCode = await ConvertLineEndings.Ux2Dos("unix.txt", TestDosTxt); | ||
|
||
// Zero is success | ||
Assert.AreEqual(exitCode, 0); | ||
|
||
// Compare the test file to the artifact file | ||
var result = await File.ReadAllTextAsync(TestDosTxt); | ||
Assert.IsTrue(result.Contains("\r\n")); | ||
Assert.AreEqual(await File.ReadAllTextAsync("dos.txt"), result); | ||
|
||
// Clean up | ||
File.Delete(TestDosTxt); | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
using System; | ||
using System.IO; | ||
using System.Text; | ||
|
||
namespace TextFileConvert; | ||
|
||
public static class Common | ||
{ | ||
public const byte Cr = 13; //'\r'; // Carriage Return | ||
public const byte Lf = 10; //'\n'; // Line Feed | ||
|
||
/// <summary> | ||
/// This is the command line help that is displayed when invalid parameters are given. | ||
/// </summary> | ||
/// <param name="programName"></param> | ||
public static void PrintUsage(string programName) | ||
{ | ||
Console.WriteLine(""" | ||
NAME | ||
{0} - Text file format converter | ||
SYNOPSIS | ||
{0} old-filename new-filename | ||
DESCRIPTION | ||
{0} reads old-filename and writes out new-filename, converting line endings. | ||
""", programName); | ||
} | ||
|
||
/// <summary> | ||
/// Attempt to detect the encoding of a file. | ||
/// </summary> | ||
/// <param name="theFile">The file to get the encoding pattern from.</param> | ||
/// <returns>Encoding type, defaults to ASCII.</returns> | ||
[Obsolete("No longer used internally. Prefer native methods.")] | ||
public static Encoding GetEncoding(FileStream theFile) | ||
{ | ||
var bom = new byte[4]; | ||
var count = theFile.Read(bom, 0, 4); | ||
|
||
// Detect BOM type | ||
if (count > 2 && bom[0] == 0x2B && bom[1] == 0x2F && bom[2] == 0x76) | ||
{ | ||
return Encoding.UTF7; | ||
} | ||
if (count > 2 && bom[0] == 0xEF && bom[1] == 0xBB && bom[2] == 0xBF) | ||
{ | ||
return Encoding.UTF8; | ||
} | ||
if (count > 1 && bom[0] == 0xFF && bom[1] == 0xFE) | ||
{ | ||
return Encoding.Unicode; | ||
} | ||
if (count > 1 && bom[0] == 0xFE && bom[1] == 0xFF) | ||
{ | ||
return Encoding.BigEndianUnicode; | ||
} | ||
if (count > 3 && bom[0] == 0 && bom[1] == 0 && bom[2] == 0xFE && bom[3] == 0xFF) | ||
{ | ||
return Encoding.UTF32; | ||
} | ||
|
||
// Default to | ||
return Encoding.ASCII; | ||
} | ||
|
||
/// <summary> | ||
/// Detect when the file in the given path is a symbolic link. | ||
/// WARNING: Could have false positive for any file with a reparse point that is not a symbolic link. | ||
/// </summary> | ||
/// <param name="path">Full path to file to test.</param> | ||
/// <returns>True if Reparse Point is found.</returns> | ||
public static bool IsSymbolic(string path) | ||
{ | ||
var pathInfo = new FileInfo(path); | ||
return pathInfo.Attributes.HasFlag(FileAttributes.ReparsePoint); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
using System; | ||
using System.IO; | ||
using System.Threading.Tasks; | ||
using TextFileConvert.Converters; | ||
|
||
namespace TextFileConvert; | ||
|
||
public class ConvertLineEndings | ||
{ | ||
/// <summary> | ||
/// Converts a DOS text file to have Unix line endings. | ||
/// </summary> | ||
/// <param name="originalFile">The file to convert.</param> | ||
/// <param name="newFile">The name of a new file to create.</param> | ||
/// <returns>Exit code.</returns> | ||
public static async Task<int> Dos2Ux(string originalFile, string newFile) | ||
{ | ||
return await ReplaceLineEndings(originalFile, newFile, new ConvertDos2Ux()); | ||
} | ||
|
||
/// <summary> | ||
/// Converts a DOS text file to have Unix line endings. | ||
/// </summary> | ||
/// <param name="originalFile">The file to convert.</param> | ||
/// <param name="newFile">The name of a new file to create.</param> | ||
/// <returns>Exit code.</returns> | ||
public static async Task<int> Ux2Dos(string originalFile, string newFile) | ||
{ | ||
return await ReplaceLineEndings(originalFile, newFile, new ConvertUx2Dos()); | ||
} | ||
|
||
/// <summary> | ||
/// Loads a whole text file in to memory, Performs a find\replace, and writes a new file. | ||
/// </summary> | ||
/// <param name="originalFile">The file path to convert.</param> | ||
/// <param name="newFile">The name of a new file to create.</param> | ||
/// <param name="convertMode">This is the type of conversion we are going to perform.</param> | ||
/// <returns>Exit code. 0 is success. -1 is a symbolic link.</returns> | ||
private static async Task<int> ReplaceLineEndings(string originalFile, string newFile, ITextConverter convertMode) | ||
{ | ||
try | ||
{ | ||
// Do not attempt to work on symbolic links | ||
if (Common.IsSymbolic(originalFile)) | ||
return -1; | ||
|
||
using var oldFileStream = new FileStream(originalFile, FileMode.Open, FileAccess.Read, FileShare.Read, 65536, FileOptions.Asynchronous); | ||
using var newFileStream = new FileStream(newFile, FileMode.Create, FileAccess.ReadWrite, FileShare.None, 65536, FileOptions.Asynchronous); | ||
|
||
// Reading and writing is done in this one line | ||
convertMode.WriteConvertedText(oldFileStream, newFileStream); | ||
await oldFileStream.FlushAsync(); | ||
await newFileStream.FlushAsync(); | ||
} | ||
catch (Exception ex) | ||
{ | ||
Console.WriteLine($"Error: {ex.Message}"); | ||
Console.WriteLine($"Number: {ex.HResult}"); | ||
return ex.HResult; | ||
} | ||
|
||
return 0; | ||
} | ||
} |
Oops, something went wrong.