diff --git a/source/DotnetStreams.sln b/source/DotnetStandardStreams.sln similarity index 73% rename from source/DotnetStreams.sln rename to source/DotnetStandardStreams.sln index a6e35f6..b6cc10d 100644 --- a/source/DotnetStreams.sln +++ b/source/DotnetStandardStreams.sln @@ -1,37 +1,37 @@ - -Microsoft Visual Studio Solution File, Format Version 12.00 -# Visual Studio Version 17 -VisualStudioVersion = 17.13.35919.96 d17.13 -MinimumVisualStudioVersion = 10.0.40219.1 -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "DotnetStreams", "DotnetStreams\DotnetStreams.csproj", "{42612FD1-4024-4091-B62F-3A897A4BBEF2}" -EndProject -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "DotnetStreamsTests", "DotnetStreamsTests\DotnetStreamsTests.csproj", "{1CE38FCB-E08F-8213-356A-F21626719E30}" -EndProject -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "DotnetStreamsApp", "DotnetStreamsApp\DotnetStreamsApp.csproj", "{7EF73074-AAA6-13D4-5EF8-6D91B220C690}" -EndProject -Global - GlobalSection(SolutionConfigurationPlatforms) = preSolution - Debug|Any CPU = Debug|Any CPU - Release|Any CPU = Release|Any CPU - EndGlobalSection - GlobalSection(ProjectConfigurationPlatforms) = postSolution - {42612FD1-4024-4091-B62F-3A897A4BBEF2}.Debug|Any CPU.ActiveCfg = Debug|Any CPU - {42612FD1-4024-4091-B62F-3A897A4BBEF2}.Debug|Any CPU.Build.0 = Debug|Any CPU - {42612FD1-4024-4091-B62F-3A897A4BBEF2}.Release|Any CPU.ActiveCfg = Release|Any CPU - {42612FD1-4024-4091-B62F-3A897A4BBEF2}.Release|Any CPU.Build.0 = Release|Any CPU - {1CE38FCB-E08F-8213-356A-F21626719E30}.Debug|Any CPU.ActiveCfg = Debug|Any CPU - {1CE38FCB-E08F-8213-356A-F21626719E30}.Debug|Any CPU.Build.0 = Debug|Any CPU - {1CE38FCB-E08F-8213-356A-F21626719E30}.Release|Any CPU.ActiveCfg = Release|Any CPU - {1CE38FCB-E08F-8213-356A-F21626719E30}.Release|Any CPU.Build.0 = Release|Any CPU - {7EF73074-AAA6-13D4-5EF8-6D91B220C690}.Debug|Any CPU.ActiveCfg = Debug|Any CPU - {7EF73074-AAA6-13D4-5EF8-6D91B220C690}.Debug|Any CPU.Build.0 = Debug|Any CPU - {7EF73074-AAA6-13D4-5EF8-6D91B220C690}.Release|Any CPU.ActiveCfg = Release|Any CPU - {7EF73074-AAA6-13D4-5EF8-6D91B220C690}.Release|Any CPU.Build.0 = Release|Any CPU - EndGlobalSection - GlobalSection(SolutionProperties) = preSolution - HideSolutionNode = FALSE - EndGlobalSection - GlobalSection(ExtensibilityGlobals) = postSolution - SolutionGuid = {56B0A117-876A-43B1-9E7E-3966279BC007} - EndGlobalSection -EndGlobal + +Microsoft Visual Studio Solution File, Format Version 12.00 +# Visual Studio Version 17 +VisualStudioVersion = 17.13.35919.96 +MinimumVisualStudioVersion = 10.0.40219.1 +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "DotnetStandardStreams", "DotnetStandardStreams\DotnetStandardStreams.csproj", "{42612FD1-4024-4091-B62F-3A897A4BBEF2}" +EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "DotnetStandardStreamsTests", "DotnetStandardStreamsTests\DotnetStandardStreamsTests.csproj", "{1CE38FCB-E08F-8213-356A-F21626719E30}" +EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "DotnetStandardStreamsApp", "DotnetStandardStreamsApp\DotnetStandardStreamsApp.csproj", "{7EF73074-AAA6-13D4-5EF8-6D91B220C690}" +EndProject +Global + GlobalSection(SolutionConfigurationPlatforms) = preSolution + Debug|Any CPU = Debug|Any CPU + Release|Any CPU = Release|Any CPU + EndGlobalSection + GlobalSection(ProjectConfigurationPlatforms) = postSolution + {42612FD1-4024-4091-B62F-3A897A4BBEF2}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {42612FD1-4024-4091-B62F-3A897A4BBEF2}.Debug|Any CPU.Build.0 = Debug|Any CPU + {42612FD1-4024-4091-B62F-3A897A4BBEF2}.Release|Any CPU.ActiveCfg = Release|Any CPU + {42612FD1-4024-4091-B62F-3A897A4BBEF2}.Release|Any CPU.Build.0 = Release|Any CPU + {1CE38FCB-E08F-8213-356A-F21626719E30}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {1CE38FCB-E08F-8213-356A-F21626719E30}.Debug|Any CPU.Build.0 = Debug|Any CPU + {1CE38FCB-E08F-8213-356A-F21626719E30}.Release|Any CPU.ActiveCfg = Release|Any CPU + {1CE38FCB-E08F-8213-356A-F21626719E30}.Release|Any CPU.Build.0 = Release|Any CPU + {7EF73074-AAA6-13D4-5EF8-6D91B220C690}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {7EF73074-AAA6-13D4-5EF8-6D91B220C690}.Debug|Any CPU.Build.0 = Debug|Any CPU + {7EF73074-AAA6-13D4-5EF8-6D91B220C690}.Release|Any CPU.ActiveCfg = Release|Any CPU + {7EF73074-AAA6-13D4-5EF8-6D91B220C690}.Release|Any CPU.Build.0 = Release|Any CPU + EndGlobalSection + GlobalSection(SolutionProperties) = preSolution + HideSolutionNode = FALSE + EndGlobalSection + GlobalSection(ExtensibilityGlobals) = postSolution + SolutionGuid = {56B0A117-876A-43B1-9E7E-3966279BC007} + EndGlobalSection +EndGlobal diff --git a/source/DotnetStreams/AnonOutputTarget.cs b/source/DotnetStandardStreams/AnonOutputTarget.cs similarity index 89% rename from source/DotnetStreams/AnonOutputTarget.cs rename to source/DotnetStandardStreams/AnonOutputTarget.cs index 0beef35..aee2353 100644 --- a/source/DotnetStreams/AnonOutputTarget.cs +++ b/source/DotnetStandardStreams/AnonOutputTarget.cs @@ -1,30 +1,30 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; - -namespace DotnetStreams -{ - public class AnonOutputTarget : IOutputTarget - { - private readonly Action outputProc; - public AnonOutputTarget(Action outputProc) - { - this.outputProc = outputProc; - } - - public virtual void Close() - { - } - - public virtual void Open() - { - } - - public virtual void Output(string line) - { - this.outputProc?.Invoke(line); - } - } -} +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace DotnetStandardStreams +{ + public class AnonOutputTarget : IOutputTarget + { + private readonly Action outputProc; + public AnonOutputTarget(Action outputProc) + { + this.outputProc = outputProc; + } + + public virtual void Close() + { + } + + public virtual void Open() + { + } + + public virtual void Output(string line) + { + this.outputProc?.Invoke(line); + } + } +} diff --git a/source/DotnetStreams/BinaryStdinReader.cs b/source/DotnetStandardStreams/BinaryStdinReader.cs similarity index 82% rename from source/DotnetStreams/BinaryStdinReader.cs rename to source/DotnetStandardStreams/BinaryStdinReader.cs index ab5ef9b..e1f34c8 100644 --- a/source/DotnetStreams/BinaryStdinReader.cs +++ b/source/DotnetStandardStreams/BinaryStdinReader.cs @@ -1,58 +1,58 @@ -using System; - -namespace DotnetStreams -{ - public class BytesReadEventArgs - { - public byte[] Data { get; } - public int Size { get; } - public BytesReadEventArgs(byte[] data, int size) - { - this.Data = data; - this.Size = size; - } - } - - public delegate void BytesReadEventHandler(object sender, BytesReadEventArgs e); - - public class BinaryStdinReader - { - protected readonly Action dataReceiverProc; - protected readonly Action doneProc; - public event BytesReadEventHandler OnBytesRead; - public event EventHandler OnDone; - public BinaryStdinReader(Action dataReceiverProc, Action doneProc) - { - this.dataReceiverProc = dataReceiverProc; - this.doneProc = doneProc; - } - - public BinaryStdinReader() - { - } - - public void ReadBytes(Action receiverProc, Action doneProc) - { - using System.IO.Stream stdinStream = System.Console.OpenStandardInput(); - - int totalBytesRead = 0; - int bufferSize = 2048; - byte[] buffer = new byte[bufferSize]; - int bytesRead = stdinStream.Read(buffer, 0, bufferSize); - while (bytesRead > 0) - { - OnBytesRead?.Invoke(this, new BytesReadEventArgs(buffer, bytesRead)); - receiverProc?.Invoke(buffer, bytesRead); - - if (bytesRead < bufferSize) - break; - - bytesRead = stdinStream.Read(buffer, 0, bufferSize); - totalBytesRead += bytesRead; - } - - OnDone?.Invoke(this, new EventArgs()); - doneProc?.Invoke(totalBytesRead); - } - } -} +using System; + +namespace DotnetStandardStreams +{ + public class BytesReadEventArgs + { + public byte[] Data { get; } + public int Size { get; } + public BytesReadEventArgs(byte[] data, int size) + { + this.Data = data; + this.Size = size; + } + } + + public delegate void BytesReadEventHandler(object sender, BytesReadEventArgs e); + + public class BinaryStdinReader + { + protected readonly Action? dataReceiverProc; + protected readonly Action? doneProc; + public event BytesReadEventHandler? OnBytesRead; + public event EventHandler? OnDone; + public BinaryStdinReader(Action dataReceiverProc, Action doneProc) + { + this.dataReceiverProc = dataReceiverProc; + this.doneProc = doneProc; + } + + public BinaryStdinReader() + { + } + + public void ReadBytes(Action receiverProc, Action doneProc) + { + using System.IO.Stream stdinStream = System.Console.OpenStandardInput(); + + int totalBytesRead = 0; + int bufferSize = 2048; + byte[] buffer = new byte[bufferSize]; + int bytesRead = stdinStream.Read(buffer, 0, bufferSize); + while (bytesRead > 0) + { + OnBytesRead?.Invoke(this, new BytesReadEventArgs(buffer, bytesRead)); + receiverProc?.Invoke(buffer, bytesRead); + + if (bytesRead < bufferSize) + break; + + bytesRead = stdinStream.Read(buffer, 0, bufferSize); + totalBytesRead += bytesRead; + } + + OnDone?.Invoke(this, new EventArgs()); + doneProc?.Invoke(totalBytesRead); + } + } +} diff --git a/source/DotnetStreams/ConsoleOutputTarget.cs b/source/DotnetStandardStreams/ConsoleOutputTarget.cs similarity index 87% rename from source/DotnetStreams/ConsoleOutputTarget.cs rename to source/DotnetStandardStreams/ConsoleOutputTarget.cs index 8108668..a1862a8 100644 --- a/source/DotnetStreams/ConsoleOutputTarget.cs +++ b/source/DotnetStandardStreams/ConsoleOutputTarget.cs @@ -1,22 +1,22 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; - -namespace DotnetStreams -{ - public class ConsoleOutputTarget : IOutputTarget - { - public virtual void Open() - { - } - public virtual void Output(string line) - { - Console.WriteLine(line); - } - public virtual void Close() - { - } - } -} +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace DotnetStandardStreams +{ + public class ConsoleOutputTarget : IOutputTarget + { + public virtual void Open() + { + } + public virtual void Output(string line) + { + Console.WriteLine(line); + } + public virtual void Close() + { + } + } +} diff --git a/source/DotnetStreams/DotnetStreams.csproj b/source/DotnetStandardStreams/DotnetStandardStreams.csproj similarity index 97% rename from source/DotnetStreams/DotnetStreams.csproj rename to source/DotnetStandardStreams/DotnetStandardStreams.csproj index 5648e5c..e54492d 100644 --- a/source/DotnetStreams/DotnetStreams.csproj +++ b/source/DotnetStandardStreams/DotnetStandardStreams.csproj @@ -1,16 +1,16 @@ - - - - net5.0 - enable - true - Phil Gilmore - Classes to seamlessly abstract StdIn, StdOut, coded lists or file streams in your console applications. - - MIT - true - true - snupkg - - - + + + + net5.0 + enable + true + Phil Gilmore + Classes to seamlessly abstract StdIn, StdOut, coded lists or file streams in your console applications. + + MIT + true + true + snupkg + + + diff --git a/source/DotnetStreams/FileTextSource.cs b/source/DotnetStandardStreams/FileTextSource.cs similarity index 92% rename from source/DotnetStreams/FileTextSource.cs rename to source/DotnetStandardStreams/FileTextSource.cs index 7633ef7..14cea55 100644 --- a/source/DotnetStreams/FileTextSource.cs +++ b/source/DotnetStandardStreams/FileTextSource.cs @@ -1,71 +1,71 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; -using System.IO; - -namespace DotnetStreams -{ - public class FileTextSource : ITextSource - { - protected FileStream? file; - protected StreamReader? reader; - protected string filename; - - public FileTextSource(string filename) - { - this.filename = filename; - file = null; - reader = null; - } - - public virtual void Open() - { - file = new FileStream(filename, FileMode.Open, FileAccess.Read, FileShare.Read); - reader = new StreamReader(file); - } - - public virtual IEnumerable ReadAll() - { - string? line = reader?.ReadLine(); - while (line != null) - { - yield return line; - line = reader?.ReadLine(); - } - } - - public virtual string Read() - { - if (!Eof()) - return reader?.ReadLine() ?? string.Empty; - else - return string.Empty; - } - - public virtual bool Eof() - { - return reader?.EndOfStream ?? true; - } - - public virtual void Close() - { - try - { - file?.Flush(); - } - finally - { - try - { - file?.Close(); - } - finally - { - file?.Dispose(); - } - } - } - } -} +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using System.IO; + +namespace DotnetStandardStreams +{ + public class FileTextSource : ITextSource + { + protected FileStream? file; + protected StreamReader? reader; + protected string filename; + + public FileTextSource(string filename) + { + this.filename = filename; + file = null; + reader = null; + } + + public virtual void Open() + { + file = new FileStream(filename, FileMode.Open, FileAccess.Read, FileShare.Read); + reader = new StreamReader(file); + } + + public virtual IEnumerable ReadAll() + { + string? line = reader?.ReadLine(); + while (line != null) + { + yield return line; + line = reader?.ReadLine(); + } + } + + public virtual string Read() + { + if (!Eof()) + return reader?.ReadLine() ?? string.Empty; + else + return string.Empty; + } + + public virtual bool Eof() + { + return reader?.EndOfStream ?? true; + } + + public virtual void Close() + { + try + { + file?.Flush(); + } + finally + { + try + { + file?.Close(); + } + finally + { + file?.Dispose(); + } + } + } + } +} diff --git a/source/DotnetStreams/IOutputTarget.cs b/source/DotnetStandardStreams/IOutputTarget.cs similarity index 85% rename from source/DotnetStreams/IOutputTarget.cs rename to source/DotnetStandardStreams/IOutputTarget.cs index 1a83e4b..1fee104 100644 --- a/source/DotnetStreams/IOutputTarget.cs +++ b/source/DotnetStandardStreams/IOutputTarget.cs @@ -1,15 +1,15 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; - -namespace DotnetStreams -{ - public interface IOutputTarget - { - void Open(); - void Output(string line); - void Close(); - } -} +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace DotnetStandardStreams +{ + public interface IOutputTarget + { + void Open(); + void Output(string line); + void Close(); + } +} diff --git a/source/DotnetStreams/ITextSource.cs b/source/DotnetStandardStreams/ITextSource.cs similarity index 86% rename from source/DotnetStreams/ITextSource.cs rename to source/DotnetStandardStreams/ITextSource.cs index aab3daa..2558b72 100644 --- a/source/DotnetStreams/ITextSource.cs +++ b/source/DotnetStandardStreams/ITextSource.cs @@ -1,17 +1,17 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; - -namespace DotnetStreams -{ - public interface ITextSource - { - void Open(); - IEnumerable ReadAll(); - string Read(); - bool Eof(); - void Close(); - } -} +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace DotnetStandardStreams +{ + public interface ITextSource + { + void Open(); + IEnumerable ReadAll(); + string Read(); + bool Eof(); + void Close(); + } +} diff --git a/source/DotnetStreams/ListOutputTarget.cs b/source/DotnetStandardStreams/ListOutputTarget.cs similarity index 89% rename from source/DotnetStreams/ListOutputTarget.cs rename to source/DotnetStandardStreams/ListOutputTarget.cs index f38700b..d5ce160 100644 --- a/source/DotnetStreams/ListOutputTarget.cs +++ b/source/DotnetStandardStreams/ListOutputTarget.cs @@ -1,30 +1,30 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; - -namespace DotnetStreams -{ - public class ListOutputTarget : IOutputTarget - { - public IList OutputList { get; } - public ListOutputTarget(IList outputList) - { - this.OutputList = outputList; - } - - public virtual void Open() - { - } - - public virtual void Output(string text) - { - OutputList.Add(text); - } - - public virtual void Close() - { - } - } -} +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace DotnetStandardStreams +{ + public class ListOutputTarget : IOutputTarget + { + public IList OutputList { get; } + public ListOutputTarget(IList outputList) + { + this.OutputList = outputList; + } + + public virtual void Open() + { + } + + public virtual void Output(string text) + { + OutputList.Add(text); + } + + public virtual void Close() + { + } + } +} diff --git a/source/DotnetStreams/ListTextSource.cs b/source/DotnetStandardStreams/ListTextSource.cs similarity index 93% rename from source/DotnetStreams/ListTextSource.cs rename to source/DotnetStandardStreams/ListTextSource.cs index 026e307..9388a03 100644 --- a/source/DotnetStreams/ListTextSource.cs +++ b/source/DotnetStandardStreams/ListTextSource.cs @@ -1,71 +1,71 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; - -namespace DotnetStreams -{ - public class ListTextSource : ITextSource - { - private bool isEof; - private readonly IEnumerable source; - private IEnumerator? enumerator; - //private string lastValue; - private bool firstIsRead; - private string nextLine; - public ListTextSource(IEnumerable source) - { - this.source = source; - enumerator = null; - firstIsRead = false; - nextLine = string.Empty; - } - - public virtual void Open() - { - } - - public virtual IEnumerable ReadAll() - { - return source.AsEnumerable(); - } - - public virtual string Read() - { - if (enumerator == null) - enumerator = source.GetEnumerator(); - - string thisLine; - if (!firstIsRead) - { - // Read the first, put it in the "last" buffer. - isEof = !enumerator.MoveNext(); - nextLine = enumerator.Current; - firstIsRead = true; - } - - thisLine = nextLine; - - if (!isEof) - { - isEof = !enumerator.MoveNext(); - if (!isEof) - nextLine = enumerator.Current; - else - nextLine = string.Empty; - - } - - return thisLine; - } - - public virtual bool Eof() => isEof; - - public virtual void Close() - { - enumerator = null; - isEof = false; - } - } -} +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace DotnetStandardStreams +{ + public class ListTextSource : ITextSource + { + private bool isEof; + private readonly IEnumerable source; + private IEnumerator? enumerator; + //private string lastValue; + private bool firstIsRead; + private string nextLine; + public ListTextSource(IEnumerable source) + { + this.source = source; + enumerator = null; + firstIsRead = false; + nextLine = string.Empty; + } + + public virtual void Open() + { + } + + public virtual IEnumerable ReadAll() + { + return source.AsEnumerable(); + } + + public virtual string Read() + { + if (enumerator == null) + enumerator = source.GetEnumerator(); + + string thisLine; + if (!firstIsRead) + { + // Read the first, put it in the "last" buffer. + isEof = !enumerator.MoveNext(); + nextLine = enumerator.Current; + firstIsRead = true; + } + + thisLine = nextLine; + + if (!isEof) + { + isEof = !enumerator.MoveNext(); + if (!isEof) + nextLine = enumerator.Current; + else + nextLine = string.Empty; + + } + + return thisLine; + } + + public virtual bool Eof() => isEof; + + public virtual void Close() + { + enumerator = null; + isEof = false; + } + } +} diff --git a/source/DotnetStreams/ProcessedConsoleOutputTarget.cs b/source/DotnetStandardStreams/ProcessedConsoleOutputTarget.cs similarity index 92% rename from source/DotnetStreams/ProcessedConsoleOutputTarget.cs rename to source/DotnetStandardStreams/ProcessedConsoleOutputTarget.cs index c462011..7768eb5 100644 --- a/source/DotnetStreams/ProcessedConsoleOutputTarget.cs +++ b/source/DotnetStandardStreams/ProcessedConsoleOutputTarget.cs @@ -1,26 +1,26 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; - -namespace DotnetStreams -{ - public class ProcessedConsoleOutputTarget : ConsoleOutputTarget - { - protected Func processorFunc; - public ProcessedConsoleOutputTarget(Func processorFunc) : base() - { - if (processorFunc != null) - this.processorFunc = processorFunc; - else - this.processorFunc = line => line; - } - - public override void Output(string line) - { - string output = processorFunc?.Invoke(line) ?? string.Empty; - Console.WriteLine(output); - } - } -} +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace DotnetStandardStreams +{ + public class ProcessedConsoleOutputTarget : ConsoleOutputTarget + { + protected Func processorFunc; + public ProcessedConsoleOutputTarget(Func processorFunc) : base() + { + if (processorFunc != null) + this.processorFunc = processorFunc; + else + this.processorFunc = line => line; + } + + public override void Output(string line) + { + string output = processorFunc?.Invoke(line) ?? string.Empty; + Console.WriteLine(output); + } + } +} diff --git a/source/DotnetStreams/StdInTextSource.cs b/source/DotnetStandardStreams/StdInTextSource.cs similarity index 90% rename from source/DotnetStreams/StdInTextSource.cs rename to source/DotnetStandardStreams/StdInTextSource.cs index 543755e..a9aa4f9 100644 --- a/source/DotnetStreams/StdInTextSource.cs +++ b/source/DotnetStandardStreams/StdInTextSource.cs @@ -1,47 +1,47 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; - -namespace DotnetStreams -{ - public class StdInTextSource : ITextSource - { - protected bool isEof; - - public virtual void Open() - { - isEof = false; - } - - public virtual IEnumerable ReadAll() - { - string? s = Console.ReadLine(); - while (s != null) - { - yield return s; - s = Console.ReadLine(); - } - isEof = true; - } - - public virtual string Read() - { - string? s = Console.ReadLine(); - if (s == null) - { - isEof = true; - return string.Empty; - } - else - return s; - } - - public virtual bool Eof() => isEof; - - public virtual void Close() - { - } - } -} +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace DotnetStandardStreams +{ + public class StdInTextSource : ITextSource + { + protected bool isEof; + + public virtual void Open() + { + isEof = false; + } + + public virtual IEnumerable ReadAll() + { + string? s = Console.ReadLine(); + while (s != null) + { + yield return s; + s = Console.ReadLine(); + } + isEof = true; + } + + public virtual string Read() + { + string? s = Console.ReadLine(); + if (s == null) + { + isEof = true; + return string.Empty; + } + else + return s; + } + + public virtual bool Eof() => isEof; + + public virtual void Close() + { + } + } +} diff --git a/source/DotnetStreamsApp/DotnetStreamsApp.csproj b/source/DotnetStandardStreamsApp/DotnetStandardStreamsApp.csproj similarity index 62% rename from source/DotnetStreamsApp/DotnetStreamsApp.csproj rename to source/DotnetStandardStreamsApp/DotnetStandardStreamsApp.csproj index be9b27e..9af55b8 100644 --- a/source/DotnetStreamsApp/DotnetStreamsApp.csproj +++ b/source/DotnetStandardStreamsApp/DotnetStandardStreamsApp.csproj @@ -1,14 +1,14 @@ - - - - Exe - net5.0 - enable - nullable - - - - - - - + + + + Exe + net8.0 + enable + nullable + + + + + + + diff --git a/source/DotnetStreamsApp/DotnetStreams.cs b/source/DotnetStandardStreamsApp/DotnetStreams.cs similarity index 94% rename from source/DotnetStreamsApp/DotnetStreams.cs rename to source/DotnetStandardStreamsApp/DotnetStreams.cs index 5f76fa7..34f0675 100644 --- a/source/DotnetStreamsApp/DotnetStreams.cs +++ b/source/DotnetStandardStreamsApp/DotnetStreams.cs @@ -1,297 +1,297 @@ -using System; -using System.Collections.Generic; -using System.IO; -using System.Linq; -using System.Text; -using System.Threading.Tasks; - -namespace DotnetStreams -{ - //public interface ITextSource - //{ - // void Open(); - // IEnumerable ReadAll(); - // string Read(); - // bool Eof(); - // void Close(); - //} - - //public class StdInTextSource : ITextSource - //{ - // protected bool isEof; - - // public virtual void Open() - // { - // isEof = false; - // } - - // public virtual IEnumerable ReadAll() - // { - // string? s = Console.ReadLine(); - // while (s != null) - // { - // yield return s; - // s = Console.ReadLine(); - // } - // isEof = true; - // } - - // public virtual string Read() - // { - // string? s = Console.ReadLine(); - // if (s == null) - // { - // isEof = true; - // return string.Empty; - // } - // else - // return s; - // } - - // public virtual bool Eof() => isEof; - - // public virtual void Close() - // { - // } - //} - - //public class FileTextSource : ITextSource - //{ - // protected FileStream? file; - // protected StreamReader? reader; - // protected string filename; - - // public FileTextSource(string filename) - // { - // this.filename = filename; - // file = null; - // reader = null; - // } - - // public virtual void Open() - // { - // file = new FileStream(filename, FileMode.Open, FileAccess.Read, FileShare.Read); - // reader = new StreamReader(file); - // } - - // public virtual IEnumerable ReadAll() - // { - // string? line = reader?.ReadLine(); - // while (line != null) - // { - // yield return line; - // line = reader?.ReadLine(); - // } - // } - - // public virtual string Read() - // { - // if (!Eof()) - // return reader?.ReadLine() ?? string.Empty; - // else - // return string.Empty; - // } - - // public virtual bool Eof() - // { - // return reader?.EndOfStream ?? true; - // } - - // public virtual void Close() - // { - // try - // { - // file?.Flush(); - // } - // finally - // { - // try - // { - // file?.Close(); - // } - // finally - // { - // file?.Dispose(); - // } - // } - // } - //} - - //public class ListTextSource : ITextSource - //{ - // private bool isEof; - // private readonly IEnumerable source; - // private IEnumerator? enumerator; - // //private string lastValue; - // private bool firstIsRead; - // private string nextLine; - // public ListTextSource(IEnumerable source) - // { - // this.source = source; - // enumerator = null; - // firstIsRead = false; - // nextLine = string.Empty; - // } - - // public virtual void Open() - // { - // } - - // public virtual IEnumerable ReadAll() - // { - // return source.AsEnumerable(); - // } - - // public virtual string Read() - // { - // if (enumerator == null) - // enumerator = source.GetEnumerator(); - - // string thisLine; - // if (!firstIsRead) - // { - // // Read the first, put it in the "last" buffer. - // isEof = !enumerator.MoveNext(); - // nextLine = enumerator.Current; - // firstIsRead = true; - // } - - // thisLine = nextLine; - - // if (!isEof) - // { - // isEof = !enumerator.MoveNext(); - // if (!isEof) - // nextLine = enumerator.Current; - // else - // nextLine = string.Empty; - - // } - - // return thisLine; - // } - - // public virtual bool Eof() => isEof; - - // public virtual void Close() - // { - // enumerator = null; - // isEof = false; - // } - //} - - //public interface IOutputTarget - //{ - // void Open(); - // void Output(string line); - // void Close(); - //} - - //public class ConsoleOutputTarget : IOutputTarget - //{ - // public virtual void Open() - // { - // } - // public virtual void Output(string line) - // { - // Console.WriteLine(line); - // } - // public virtual void Close() - // { - // } - //} - - //public class ProcessedConsoleOutputTarget : ConsoleOutputTarget - //{ - // protected Func processorFunc; - // public ProcessedConsoleOutputTarget(Func processorFunc) : base() - // { - // if (processorFunc != null) - // this.processorFunc = processorFunc; - // else - // this.processorFunc = line => line; - // } - - // public override void Output(string line) - // { - // string output = processorFunc?.Invoke(line) ?? string.Empty; - // Console.WriteLine(output); - // } - //} - - //public class ListOutputTarget : IOutputTarget - //{ - // public IList OutputList { get; } - // public ListOutputTarget(IList outputList) - // { - // this.OutputList = outputList; - // } - - // public virtual void Open() - // { - // } - - // public virtual void Output(string text) - // { - // OutputList.Add(text); - // } - - // public virtual void Close() - // { - // } - //} - - //public class BytesReadEventArgs - //{ - // public byte[] Data { get; } - // public int Size { get; } - // public BytesReadEventArgs(byte[] data, int size) - // { - // this.Data = data; - // this.Size = size; - // } - //} - - //public delegate void BytesReadEventHandler(object sender, BytesReadEventArgs e); - - //public class BinaryStdinReader - //{ - // protected readonly Action dataReceiverProc; - // protected readonly Action doneProc; - // public event BytesReadEventHandler OnBytesRead; - // public event EventHandler OnDone; - // public BinaryStdinReader(Action dataReceiverProc, Action doneProc) - // { - // this.dataReceiverProc = dataReceiverProc; - // this.doneProc = doneProc; - // } - - // public BinaryStdinReader() - // { - // } - - // public void ReadBytes(Action receiverProc, Action doneProc) - // { - // using System.IO.Stream stdinStream = System.Console.OpenStandardInput(); - - // int totalBytesRead = 0; - // int bufferSize = 2048; - // byte[] buffer = new byte[bufferSize]; - // int bytesRead = stdinStream.Read(buffer, 0, bufferSize); - // while (bytesRead > 0) - // { - // OnBytesRead?.Invoke(this, new BytesReadEventArgs(buffer, bytesRead)); - // receiverProc?.Invoke(buffer, bytesRead); - - // if (bytesRead < bufferSize) - // break; - - // bytesRead = stdinStream.Read(buffer, 0, bufferSize); - // totalBytesRead += bytesRead; - // } - - // OnDone?.Invoke(this, new EventArgs()); - // doneProc?.Invoke(totalBytesRead); - // } - //} -} +using System; +using System.Collections.Generic; +using System.IO; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace DotnetStandardStreams +{ + //public interface ITextSource + //{ + // void Open(); + // IEnumerable ReadAll(); + // string Read(); + // bool Eof(); + // void Close(); + //} + + //public class StdInTextSource : ITextSource + //{ + // protected bool isEof; + + // public virtual void Open() + // { + // isEof = false; + // } + + // public virtual IEnumerable ReadAll() + // { + // string? s = Console.ReadLine(); + // while (s != null) + // { + // yield return s; + // s = Console.ReadLine(); + // } + // isEof = true; + // } + + // public virtual string Read() + // { + // string? s = Console.ReadLine(); + // if (s == null) + // { + // isEof = true; + // return string.Empty; + // } + // else + // return s; + // } + + // public virtual bool Eof() => isEof; + + // public virtual void Close() + // { + // } + //} + + //public class FileTextSource : ITextSource + //{ + // protected FileStream? file; + // protected StreamReader? reader; + // protected string filename; + + // public FileTextSource(string filename) + // { + // this.filename = filename; + // file = null; + // reader = null; + // } + + // public virtual void Open() + // { + // file = new FileStream(filename, FileMode.Open, FileAccess.Read, FileShare.Read); + // reader = new StreamReader(file); + // } + + // public virtual IEnumerable ReadAll() + // { + // string? line = reader?.ReadLine(); + // while (line != null) + // { + // yield return line; + // line = reader?.ReadLine(); + // } + // } + + // public virtual string Read() + // { + // if (!Eof()) + // return reader?.ReadLine() ?? string.Empty; + // else + // return string.Empty; + // } + + // public virtual bool Eof() + // { + // return reader?.EndOfStream ?? true; + // } + + // public virtual void Close() + // { + // try + // { + // file?.Flush(); + // } + // finally + // { + // try + // { + // file?.Close(); + // } + // finally + // { + // file?.Dispose(); + // } + // } + // } + //} + + //public class ListTextSource : ITextSource + //{ + // private bool isEof; + // private readonly IEnumerable source; + // private IEnumerator? enumerator; + // //private string lastValue; + // private bool firstIsRead; + // private string nextLine; + // public ListTextSource(IEnumerable source) + // { + // this.source = source; + // enumerator = null; + // firstIsRead = false; + // nextLine = string.Empty; + // } + + // public virtual void Open() + // { + // } + + // public virtual IEnumerable ReadAll() + // { + // return source.AsEnumerable(); + // } + + // public virtual string Read() + // { + // if (enumerator == null) + // enumerator = source.GetEnumerator(); + + // string thisLine; + // if (!firstIsRead) + // { + // // Read the first, put it in the "last" buffer. + // isEof = !enumerator.MoveNext(); + // nextLine = enumerator.Current; + // firstIsRead = true; + // } + + // thisLine = nextLine; + + // if (!isEof) + // { + // isEof = !enumerator.MoveNext(); + // if (!isEof) + // nextLine = enumerator.Current; + // else + // nextLine = string.Empty; + + // } + + // return thisLine; + // } + + // public virtual bool Eof() => isEof; + + // public virtual void Close() + // { + // enumerator = null; + // isEof = false; + // } + //} + + //public interface IOutputTarget + //{ + // void Open(); + // void Output(string line); + // void Close(); + //} + + //public class ConsoleOutputTarget : IOutputTarget + //{ + // public virtual void Open() + // { + // } + // public virtual void Output(string line) + // { + // Console.WriteLine(line); + // } + // public virtual void Close() + // { + // } + //} + + //public class ProcessedConsoleOutputTarget : ConsoleOutputTarget + //{ + // protected Func processorFunc; + // public ProcessedConsoleOutputTarget(Func processorFunc) : base() + // { + // if (processorFunc != null) + // this.processorFunc = processorFunc; + // else + // this.processorFunc = line => line; + // } + + // public override void Output(string line) + // { + // string output = processorFunc?.Invoke(line) ?? string.Empty; + // Console.WriteLine(output); + // } + //} + + //public class ListOutputTarget : IOutputTarget + //{ + // public IList OutputList { get; } + // public ListOutputTarget(IList outputList) + // { + // this.OutputList = outputList; + // } + + // public virtual void Open() + // { + // } + + // public virtual void Output(string text) + // { + // OutputList.Add(text); + // } + + // public virtual void Close() + // { + // } + //} + + //public class BytesReadEventArgs + //{ + // public byte[] Data { get; } + // public int Size { get; } + // public BytesReadEventArgs(byte[] data, int size) + // { + // this.Data = data; + // this.Size = size; + // } + //} + + //public delegate void BytesReadEventHandler(object sender, BytesReadEventArgs e); + + //public class BinaryStdinReader + //{ + // protected readonly Action dataReceiverProc; + // protected readonly Action doneProc; + // public event BytesReadEventHandler OnBytesRead; + // public event EventHandler OnDone; + // public BinaryStdinReader(Action dataReceiverProc, Action doneProc) + // { + // this.dataReceiverProc = dataReceiverProc; + // this.doneProc = doneProc; + // } + + // public BinaryStdinReader() + // { + // } + + // public void ReadBytes(Action receiverProc, Action doneProc) + // { + // using System.IO.Stream stdinStream = System.Console.OpenStandardInput(); + + // int totalBytesRead = 0; + // int bufferSize = 2048; + // byte[] buffer = new byte[bufferSize]; + // int bytesRead = stdinStream.Read(buffer, 0, bufferSize); + // while (bytesRead > 0) + // { + // OnBytesRead?.Invoke(this, new BytesReadEventArgs(buffer, bytesRead)); + // receiverProc?.Invoke(buffer, bytesRead); + + // if (bytesRead < bufferSize) + // break; + + // bytesRead = stdinStream.Read(buffer, 0, bufferSize); + // totalBytesRead += bytesRead; + // } + + // OnDone?.Invoke(this, new EventArgs()); + // doneProc?.Invoke(totalBytesRead); + // } + //} +} diff --git a/source/DotnetStreamsApp/Program.cs b/source/DotnetStandardStreamsApp/Program.cs similarity index 94% rename from source/DotnetStreamsApp/Program.cs rename to source/DotnetStandardStreamsApp/Program.cs index a60607d..d518cea 100644 --- a/source/DotnetStreamsApp/Program.cs +++ b/source/DotnetStandardStreamsApp/Program.cs @@ -1,87 +1,87 @@ -using System; -using System.IO; -using System.Linq; -using DotnetStreams; - -namespace StreamsTest -{ - class Program - { - - static void Main(string[] args) - { - //Program p = new(); - - ITextSource textSource; - IOutputTarget textTarget = new ConsoleOutputTarget(); - - if (args.Length > 0) - textSource = new FileTextSource(args[0]); - else - textSource = new StdInTextSource(); - - - Execute( - textSource, - textTarget); - } - - // Works, but needs to be more compact. - //static void Main(string[] args) - //{ - // Program p = new Program(); - - // ITextSource textSource = null; - - // if (args.Length > 0) - // { - // var filename = args[0]; - // if (!File.Exists(filename)) - // { - // Console.WriteLine($"File not found ({filename})."); - // Environment.Exit(1); - // } - // else - // textSource = new FileTextSource(filename); - // } - // else - // textSource = new StdInTextSource(); - - // p.Execute( - // textSource, - // new ConsoleOutputTarget()); - //} - - - //public void ExecuteReadLine(string[] args) - //{ - // string s = Console.ReadLine(); - // // This terminates on blank lines, no surprise - // //while (!string.IsNullOrEmpty(s)) - // while (s != null) - // { - // s = s.Replace("\r", "{CR}") - // .Replace("\n", "{LF}") - // .Replace("\t", "{TAB}"); - // if (s == string.Empty) - // s = "{EMPTYSTRING}"; - // Console.WriteLine($"{s}"); - // s = Console.ReadLine(); - // } - // if (s == null) - // Console.WriteLine("/s/ is null"); - //} - - public static void Execute(ITextSource source, IOutputTarget target) - { - source.Open(); - target.Open(); - - foreach (string line in source.ReadAll()) - target.Output(line); - - target.Close(); - source.Close(); - } - } -} +using System; +using System.IO; +using System.Linq; +using DotnetStandardStreams; + +namespace StreamsTest +{ + class Program + { + + static void Main(string[] args) + { + //Program p = new(); + + ITextSource textSource; + IOutputTarget textTarget = new ConsoleOutputTarget(); + + if (args.Length > 0) + textSource = new FileTextSource(args[0]); + else + textSource = new StdInTextSource(); + + + Execute( + textSource, + textTarget); + } + + // Works, but needs to be more compact. + //static void Main(string[] args) + //{ + // Program p = new Program(); + + // ITextSource textSource = null; + + // if (args.Length > 0) + // { + // var filename = args[0]; + // if (!File.Exists(filename)) + // { + // Console.WriteLine($"File not found ({filename})."); + // Environment.Exit(1); + // } + // else + // textSource = new FileTextSource(filename); + // } + // else + // textSource = new StdInTextSource(); + + // p.Execute( + // textSource, + // new ConsoleOutputTarget()); + //} + + + //public void ExecuteReadLine(string[] args) + //{ + // string s = Console.ReadLine(); + // // This terminates on blank lines, no surprise + // //while (!string.IsNullOrEmpty(s)) + // while (s != null) + // { + // s = s.Replace("\r", "{CR}") + // .Replace("\n", "{LF}") + // .Replace("\t", "{TAB}"); + // if (s == string.Empty) + // s = "{EMPTYSTRING}"; + // Console.WriteLine($"{s}"); + // s = Console.ReadLine(); + // } + // if (s == null) + // Console.WriteLine("/s/ is null"); + //} + + public static void Execute(ITextSource source, IOutputTarget target) + { + source.Open(); + target.Open(); + + foreach (string line in source.ReadAll()) + target.Output(line); + + target.Close(); + source.Close(); + } + } +} diff --git a/source/DotnetStreamsApp/Properties/launchSettings.json b/source/DotnetStandardStreamsApp/Properties/launchSettings.json similarity index 75% rename from source/DotnetStreamsApp/Properties/launchSettings.json rename to source/DotnetStandardStreamsApp/Properties/launchSettings.json index f7e67bc..7c123e2 100644 --- a/source/DotnetStreamsApp/Properties/launchSettings.json +++ b/source/DotnetStandardStreamsApp/Properties/launchSettings.json @@ -1,9 +1,9 @@ -{ - "profiles": { - "StreamsTest": { - "commandName": "Project", - "commandLineArgs": "C:\\Users\\Phil\\source\\repos\\DotnetStreams\\DotnetStreamsApp\\delme.txt", - "sqlDebugging": true - } - } +{ + "profiles": { + "StreamsTest": { + "commandName": "Project", + "commandLineArgs": "C:\\Users\\Phil\\source\\repos\\DotnetStandardStreams\\DotnetStandardStreamsApp\\delme.txt", + "sqlDebugging": true + } + } } \ No newline at end of file diff --git a/source/DotnetStreamsApp/delme.txt b/source/DotnetStandardStreamsApp/delme.txt similarity index 87% rename from source/DotnetStreamsApp/delme.txt rename to source/DotnetStandardStreamsApp/delme.txt index 6afc597..8bc6a34 100644 --- a/source/DotnetStreamsApp/delme.txt +++ b/source/DotnetStandardStreamsApp/delme.txt @@ -1,4 +1,4 @@ -Line 1 -Line 2 - +Line 1 +Line 2 + Line 3 \ No newline at end of file diff --git a/source/DotnetStreamsTests/ConsoleOutputTargetTests.cs b/source/DotnetStandardStreamsTests/ConsoleOutputTargetTests.cs similarity index 85% rename from source/DotnetStreamsTests/ConsoleOutputTargetTests.cs rename to source/DotnetStandardStreamsTests/ConsoleOutputTargetTests.cs index 8016cff..f274882 100644 --- a/source/DotnetStreamsTests/ConsoleOutputTargetTests.cs +++ b/source/DotnetStandardStreamsTests/ConsoleOutputTargetTests.cs @@ -1,38 +1,38 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; -using Xunit; -using Shouldly; -using DotnetStreams; -using System.IO; -using DotnetStreamsTests.Testables; - -namespace DotnetStreamsTests -{ - public class ConsoleOutputTargetTests - { - private IOutputTarget outputTarget; - - [Fact] - public void WritesToOutputStream() - { - ListWriter writer = new(); - outputTarget = new TestableConsoleOutputTarget(writer); - - outputTarget.Open(); - outputTarget.Output("1"); - outputTarget.Output("2"); - outputTarget.Output(""); - outputTarget.Output("3"); - outputTarget.Close(); - - writer.Lines.Count.ShouldBe(4); - writer.Lines[0].ShouldBe("1"); - writer.Lines[1].ShouldBe("2"); - writer.Lines[2].ShouldBe(string.Empty); - writer.Lines[3].ShouldBe("3"); - } - } -} +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using Xunit; +using Shouldly; +using DotnetStandardStreams; +using System.IO; +using DotnetStandardStreamsTests.Testables; + +namespace DotnetStandardStreamsTests +{ + public class ConsoleOutputTargetTests + { + private IOutputTarget outputTarget; + + [Fact] + public void WritesToOutputStream() + { + ListWriter writer = new(); + outputTarget = new TestableConsoleOutputTarget(writer); + + outputTarget.Open(); + outputTarget.Output("1"); + outputTarget.Output("2"); + outputTarget.Output(""); + outputTarget.Output("3"); + outputTarget.Close(); + + writer.Lines.Count.ShouldBe(4); + writer.Lines[0].ShouldBe("1"); + writer.Lines[1].ShouldBe("2"); + writer.Lines[2].ShouldBe(string.Empty); + writer.Lines[3].ShouldBe("3"); + } + } +} diff --git a/source/DotnetStreamsTests/DotnetStreamsTests.csproj b/source/DotnetStandardStreamsTests/DotnetStandardStreamsTests.csproj similarity index 85% rename from source/DotnetStreamsTests/DotnetStreamsTests.csproj rename to source/DotnetStandardStreamsTests/DotnetStandardStreamsTests.csproj index f44f485..0d27252 100644 --- a/source/DotnetStreamsTests/DotnetStreamsTests.csproj +++ b/source/DotnetStandardStreamsTests/DotnetStandardStreamsTests.csproj @@ -1,27 +1,27 @@ - - - - net5.0 - - false - - - - - - - - runtime; build; native; contentfiles; analyzers; buildtransitive - all - - - runtime; build; native; contentfiles; analyzers; buildtransitive - all - - - - - - - - + + + + net8.0 + + false + + + + + + + + runtime; build; native; contentfiles; analyzers; buildtransitive + all + + + runtime; build; native; contentfiles; analyzers; buildtransitive + all + + + + + + + + diff --git a/source/DotnetStreamsTests/ListOutputTargetTests.cs b/source/DotnetStandardStreamsTests/ListOutputTargetTests.cs similarity index 87% rename from source/DotnetStreamsTests/ListOutputTargetTests.cs rename to source/DotnetStandardStreamsTests/ListOutputTargetTests.cs index bec3d9e..865fa36 100644 --- a/source/DotnetStreamsTests/ListOutputTargetTests.cs +++ b/source/DotnetStandardStreamsTests/ListOutputTargetTests.cs @@ -1,27 +1,27 @@ -using DotnetStreams; -using System; -using System.Collections.Generic; -using Xunit; -using Shouldly; - -namespace DotnetStreamsTests -{ - public class ListOutputTargetTests - { - [Fact] - public void WritesToList() - { - ListOutputTarget target = new(new List()); - target.Output("1"); - target.Output("2"); - target.Output(""); - target.Output("3"); - - target.OutputList.Count.ShouldBe(4); - target.OutputList[0].ShouldBe("1"); - target.OutputList[1].ShouldBe("2"); - target.OutputList[2].ShouldBe(string.Empty); - target.OutputList[3].ShouldBe("3"); - } - } -} +using DotnetStandardStreams; +using System; +using System.Collections.Generic; +using Xunit; +using Shouldly; + +namespace DotnetStandardStreamsTests +{ + public class ListOutputTargetTests + { + [Fact] + public void WritesToList() + { + ListOutputTarget target = new(new List()); + target.Output("1"); + target.Output("2"); + target.Output(""); + target.Output("3"); + + target.OutputList.Count.ShouldBe(4); + target.OutputList[0].ShouldBe("1"); + target.OutputList[1].ShouldBe("2"); + target.OutputList[2].ShouldBe(string.Empty); + target.OutputList[3].ShouldBe("3"); + } + } +} diff --git a/source/DotnetStreamsTests/ListTextSourceTests.cs b/source/DotnetStandardStreamsTests/ListTextSourceTests.cs similarity index 89% rename from source/DotnetStreamsTests/ListTextSourceTests.cs rename to source/DotnetStandardStreamsTests/ListTextSourceTests.cs index fb24e42..8130eb4 100644 --- a/source/DotnetStreamsTests/ListTextSourceTests.cs +++ b/source/DotnetStandardStreamsTests/ListTextSourceTests.cs @@ -1,39 +1,39 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; -using Xunit; -using Shouldly; -using DotnetStreams; - -namespace DotnetStreamsTests -{ - public class ListTextSourceTests - { - [Fact] - public void ReadsFromList() - { - ITextSource reader = new ListTextSource(new[] { "1", "2", "", "3" }); - string s0 = reader.Read(); - s0.ShouldNotBeNull(); - s0.ShouldBe("1"); - reader.Eof().ShouldBe(false); - - string s1 = reader.Read(); - s1.ShouldNotBeNull(); - s1.ShouldBe("2"); - reader.Eof().ShouldBe(false); - - string s2 = reader.Read(); - s2.ShouldNotBeNull(); - s2.ShouldBe(string.Empty); - reader.Eof().ShouldBe(false); - - string s3 = reader.Read(); - s3.ShouldNotBeNull(); - s3.ShouldBe("3"); - reader.Eof().ShouldBe(true); - } - } -} +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using Xunit; +using Shouldly; +using DotnetStandardStreams; + +namespace DotnetStandardStreamsTests +{ + public class ListTextSourceTests + { + [Fact] + public void ReadsFromList() + { + ITextSource reader = new ListTextSource(new[] { "1", "2", "", "3" }); + string s0 = reader.Read(); + s0.ShouldNotBeNull(); + s0.ShouldBe("1"); + reader.Eof().ShouldBe(false); + + string s1 = reader.Read(); + s1.ShouldNotBeNull(); + s1.ShouldBe("2"); + reader.Eof().ShouldBe(false); + + string s2 = reader.Read(); + s2.ShouldNotBeNull(); + s2.ShouldBe(string.Empty); + reader.Eof().ShouldBe(false); + + string s3 = reader.Read(); + s3.ShouldNotBeNull(); + s3.ShouldBe("3"); + reader.Eof().ShouldBe(true); + } + } +} diff --git a/source/DotnetStreamsTests/StdInTextSourceTests.cs b/source/DotnetStandardStreamsTests/StdInTextSourceTests.cs similarity index 92% rename from source/DotnetStreamsTests/StdInTextSourceTests.cs rename to source/DotnetStandardStreamsTests/StdInTextSourceTests.cs index ab54acb..1f3791f 100644 --- a/source/DotnetStreamsTests/StdInTextSourceTests.cs +++ b/source/DotnetStandardStreamsTests/StdInTextSourceTests.cs @@ -1,210 +1,210 @@ -using DotnetStreamsTests.Testables; -using System; -using System.Collections.Generic; -using System.IO; -using System.Linq; -using System.Text; -using System.Threading.Tasks; -using Xunit; -using Shouldly; -using DotnetStreams; - -namespace DotnetStreamsTests -{ - public class StdInTextSourceTests - { - private static TextReader CreateStdIn() - { - return new ListTextReader(new[] { "1", "2", "", "3" }); - } - - private static void WrapStdInTest(Action testCode) - { - var oldInputReader = System.Console.In; - try - { - TextReader reader = CreateStdIn(); - System.Console.SetIn(reader); - - ITextSource stdin = new StdInTextSource(); - stdin.Open(); - try - { - testCode?.Invoke(stdin); - } - finally - { - stdin.Close(); - } - } - finally - { - System.Console.SetIn(oldInputReader); - } - } - - [Fact] - public void CanReadAllFromStandardIn() - { - WrapStdInTest(stdin => - { - var actual = stdin.ReadAll().ToList(); - actual.Count.ShouldBe(4); - actual[0].ShouldBe("1"); - actual[1].ShouldBe("2"); - actual[2].ShouldBe(string.Empty); - actual[3].ShouldBe("3"); - }); - } - - //{ - - // // Fake the StdIn - // var oldInputReader = System.Console.In; - // try - // { - // TextReader reader = CreateStdIn(); - // System.Console.SetIn(reader); - - // ITextSource stdin = new StdInTextSource(); - // stdin.Open(); - // try - // { - // var actual = stdin.ReadAll().ToList(); - // actual.Count.ShouldBe(4); - // actual[0].ShouldBe("1"); - // actual[1].ShouldBe("2"); - // actual[2].ShouldBe(string.Empty); - // actual[3].ShouldBe("3"); - // } - // finally - // { - // stdin.Close(); - // } - // } - // finally - // { - // System.Console.SetIn(oldInputReader); - // } - //} - - [Fact] - public void CanReadIndividualLinesFromStandardIn() - { - WrapStdInTest(stdin => - { - string s; - s = stdin.Read(); - stdin.Eof().ShouldBe(false); - s.ShouldBe("1"); - - s = stdin.Read(); - stdin.Eof().ShouldBe(false); - s.ShouldBe("2"); - - s = stdin.Read(); - stdin.Eof().ShouldBe(false); - s.ShouldBe(string.Empty); - - s = stdin.Read(); - stdin.Eof().ShouldBe(false); - s.ShouldBe("3"); - - s = stdin.Read(); - stdin.Eof().ShouldBe(true); - }); - - //// Fake the StdIn - //var oldInputReader = System.Console.In; - //try - //{ - // TextReader reader = CreateStdIn(); - // System.Console.SetIn(reader); - - // ITextSource stdin = new StdInTextSource(); - // stdin.Open(); - // try - // { - // string s; - // s = stdin.Read(); - // stdin.Eof().ShouldBe(false); - // s.ShouldBe("1"); - - // s = stdin.Read(); - // stdin.Eof().ShouldBe(false); - // s.ShouldBe("2"); - - // s = stdin.Read(); - // stdin.Eof().ShouldBe(false); - // s.ShouldBe(string.Empty); - - // s = stdin.Read(); - // stdin.Eof().ShouldBe(false); - // s.ShouldBe("3"); - - // s = stdin.Read(); - // stdin.Eof().ShouldBe(true); - // } - // finally - // { - // stdin.Close(); - // } - //} - //finally - //{ - // System.Console.SetIn(oldInputReader); - //} - } - - [Fact] - public void CanWhileLoopThroughStdInProperly() - { - WrapStdInTest(stdin => - { - int lineCount = 0; - var s = stdin.Read(); - while (!stdin.Eof()) - { - lineCount++; - s = stdin.Read(); - } - - lineCount.ShouldBe(4); - }); - } - - //{ - // // Fake the StdIn - // var oldInputReader = System.Console.In; - // try - // { - // TextReader reader = CreateStdIn(); - // System.Console.SetIn(reader); - - // ITextSource stdin = new StdInTextSource(); - // stdin.Open(); - // try - // { - // int lineCount = 0; - // var s = stdin.Read(); - // while (!stdin.Eof()) - // { - // // Do stuff with s here normally. - // lineCount++; - // s = stdin.Read(); - // } - - // lineCount.ShouldBe(4); - // } - // finally - // { - // stdin.Close(); - // } - // } - // finally - // { - // System.Console.SetIn(oldInputReader); - // } - //} - } -} +using DotnetStandardStreamsTests.Testables; +using System; +using System.Collections.Generic; +using System.IO; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using Xunit; +using Shouldly; +using DotnetStandardStreams; + +namespace DotnetStandardStreamsTests +{ + public class StdInTextSourceTests + { + private static TextReader CreateStdIn() + { + return new ListTextReader(new[] { "1", "2", "", "3" }); + } + + private static void WrapStdInTest(Action testCode) + { + var oldInputReader = System.Console.In; + try + { + TextReader reader = CreateStdIn(); + System.Console.SetIn(reader); + + ITextSource stdin = new StdInTextSource(); + stdin.Open(); + try + { + testCode?.Invoke(stdin); + } + finally + { + stdin.Close(); + } + } + finally + { + System.Console.SetIn(oldInputReader); + } + } + + [Fact] + public void CanReadAllFromStandardIn() + { + WrapStdInTest(stdin => + { + var actual = stdin.ReadAll().ToList(); + actual.Count.ShouldBe(4); + actual[0].ShouldBe("1"); + actual[1].ShouldBe("2"); + actual[2].ShouldBe(string.Empty); + actual[3].ShouldBe("3"); + }); + } + + //{ + + // // Fake the StdIn + // var oldInputReader = System.Console.In; + // try + // { + // TextReader reader = CreateStdIn(); + // System.Console.SetIn(reader); + + // ITextSource stdin = new StdInTextSource(); + // stdin.Open(); + // try + // { + // var actual = stdin.ReadAll().ToList(); + // actual.Count.ShouldBe(4); + // actual[0].ShouldBe("1"); + // actual[1].ShouldBe("2"); + // actual[2].ShouldBe(string.Empty); + // actual[3].ShouldBe("3"); + // } + // finally + // { + // stdin.Close(); + // } + // } + // finally + // { + // System.Console.SetIn(oldInputReader); + // } + //} + + [Fact] + public void CanReadIndividualLinesFromStandardIn() + { + WrapStdInTest(stdin => + { + string s; + s = stdin.Read(); + stdin.Eof().ShouldBe(false); + s.ShouldBe("1"); + + s = stdin.Read(); + stdin.Eof().ShouldBe(false); + s.ShouldBe("2"); + + s = stdin.Read(); + stdin.Eof().ShouldBe(false); + s.ShouldBe(string.Empty); + + s = stdin.Read(); + stdin.Eof().ShouldBe(false); + s.ShouldBe("3"); + + s = stdin.Read(); + stdin.Eof().ShouldBe(true); + }); + + //// Fake the StdIn + //var oldInputReader = System.Console.In; + //try + //{ + // TextReader reader = CreateStdIn(); + // System.Console.SetIn(reader); + + // ITextSource stdin = new StdInTextSource(); + // stdin.Open(); + // try + // { + // string s; + // s = stdin.Read(); + // stdin.Eof().ShouldBe(false); + // s.ShouldBe("1"); + + // s = stdin.Read(); + // stdin.Eof().ShouldBe(false); + // s.ShouldBe("2"); + + // s = stdin.Read(); + // stdin.Eof().ShouldBe(false); + // s.ShouldBe(string.Empty); + + // s = stdin.Read(); + // stdin.Eof().ShouldBe(false); + // s.ShouldBe("3"); + + // s = stdin.Read(); + // stdin.Eof().ShouldBe(true); + // } + // finally + // { + // stdin.Close(); + // } + //} + //finally + //{ + // System.Console.SetIn(oldInputReader); + //} + } + + [Fact] + public void CanWhileLoopThroughStdInProperly() + { + WrapStdInTest(stdin => + { + int lineCount = 0; + var s = stdin.Read(); + while (!stdin.Eof()) + { + lineCount++; + s = stdin.Read(); + } + + lineCount.ShouldBe(4); + }); + } + + //{ + // // Fake the StdIn + // var oldInputReader = System.Console.In; + // try + // { + // TextReader reader = CreateStdIn(); + // System.Console.SetIn(reader); + + // ITextSource stdin = new StdInTextSource(); + // stdin.Open(); + // try + // { + // int lineCount = 0; + // var s = stdin.Read(); + // while (!stdin.Eof()) + // { + // // Do stuff with s here normally. + // lineCount++; + // s = stdin.Read(); + // } + + // lineCount.ShouldBe(4); + // } + // finally + // { + // stdin.Close(); + // } + // } + // finally + // { + // System.Console.SetIn(oldInputReader); + // } + //} + } +} diff --git a/source/DotnetStreamsTests/Testables/ListTextReader.cs b/source/DotnetStandardStreamsTests/Testables/ListTextReader.cs similarity index 87% rename from source/DotnetStreamsTests/Testables/ListTextReader.cs rename to source/DotnetStandardStreamsTests/Testables/ListTextReader.cs index 829256c..c7d8e6e 100644 --- a/source/DotnetStreamsTests/Testables/ListTextReader.cs +++ b/source/DotnetStandardStreamsTests/Testables/ListTextReader.cs @@ -1,26 +1,26 @@ -using System; -using System.Collections.Generic; -using System.IO; -using System.Linq; -using System.Text; -using System.Threading.Tasks; - -namespace DotnetStreamsTests.Testables -{ - public class ListTextReader : TextReader - { - private readonly IEnumerator enumerator; - public ListTextReader(IEnumerable data) - { - enumerator = data.GetEnumerator(); - } - - public override string ReadLine() - { - if (enumerator.MoveNext()) - return enumerator.Current; - else - return null; - } - } -} +using System; +using System.Collections.Generic; +using System.IO; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace DotnetStandardStreamsTests.Testables +{ + public class ListTextReader : TextReader + { + private readonly IEnumerator enumerator; + public ListTextReader(IEnumerable data) + { + enumerator = data.GetEnumerator(); + } + + public override string ReadLine() + { + if (enumerator.MoveNext()) + return enumerator.Current; + else + return null; + } + } +} diff --git a/source/DotnetStreamsTests/Testables/ListWriter.cs b/source/DotnetStandardStreamsTests/Testables/ListWriter.cs similarity index 87% rename from source/DotnetStreamsTests/Testables/ListWriter.cs rename to source/DotnetStandardStreamsTests/Testables/ListWriter.cs index 14cad3c..bd545c9 100644 --- a/source/DotnetStreamsTests/Testables/ListWriter.cs +++ b/source/DotnetStandardStreamsTests/Testables/ListWriter.cs @@ -1,30 +1,30 @@ -using System; -using System.Collections.Generic; -using System.IO; -using System.Linq; -using System.Text; -using System.Threading.Tasks; - -namespace DotnetStreamsTests.Testables -{ - public class ListWriter : TextWriter - { - public List Lines { get; } - public override Encoding Encoding => Encoding.UTF8; - - public ListWriter() - { - Lines = new(); - } - - public override void Write(string value) - { - Lines.Add(value); - } - - public override void WriteLine(string value) - { - Lines.Add(value); - } - } -} +using System; +using System.Collections.Generic; +using System.IO; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace DotnetStandardStreamsTests.Testables +{ + public class ListWriter : TextWriter + { + public List Lines { get; } + public override Encoding Encoding => Encoding.UTF8; + + public ListWriter() + { + Lines = new(); + } + + public override void Write(string value) + { + Lines.Add(value); + } + + public override void WriteLine(string value) + { + Lines.Add(value); + } + } +} diff --git a/source/DotnetStreamsTests/Testables/TestableConsoleOutputTarget.cs b/source/DotnetStandardStreamsTests/Testables/TestableConsoleOutputTarget.cs similarity index 84% rename from source/DotnetStreamsTests/Testables/TestableConsoleOutputTarget.cs rename to source/DotnetStandardStreamsTests/Testables/TestableConsoleOutputTarget.cs index df3fb3c..a80a8e1 100644 --- a/source/DotnetStreamsTests/Testables/TestableConsoleOutputTarget.cs +++ b/source/DotnetStandardStreamsTests/Testables/TestableConsoleOutputTarget.cs @@ -1,24 +1,24 @@ -using System; -using System.Collections.Generic; -using System.IO; -using System.Text; -using DotnetStreams; - -namespace DotnetStreamsTests.Testables -{ - public class TestableConsoleOutputTarget : ConsoleOutputTarget, IDisposable - { - private readonly TextWriter previousOutWriter; - public TestableConsoleOutputTarget(TextWriter outWriter) : base() - { - previousOutWriter = System.Console.Out; - System.Console.SetOut(outWriter); - } - - public void Dispose() - { - - System.Console.SetOut(previousOutWriter); - } - } -} +using System; +using System.Collections.Generic; +using System.IO; +using System.Text; +using DotnetStandardStreams; + +namespace DotnetStandardStreamsTests.Testables +{ + public class TestableConsoleOutputTarget : ConsoleOutputTarget, IDisposable + { + private readonly TextWriter previousOutWriter; + public TestableConsoleOutputTarget(TextWriter outWriter) : base() + { + previousOutWriter = System.Console.Out; + System.Console.SetOut(outWriter); + } + + public void Dispose() + { + + System.Console.SetOut(previousOutWriter); + } + } +}