Renamed from DotnetStreams to DotnetStandardStreams.
This commit is contained in:
parent
1e9844143c
commit
1d7cefb207
@ -1,37 +1,37 @@
|
|||||||
|
|
||||||
Microsoft Visual Studio Solution File, Format Version 12.00
|
Microsoft Visual Studio Solution File, Format Version 12.00
|
||||||
# Visual Studio Version 17
|
# Visual Studio Version 17
|
||||||
VisualStudioVersion = 17.13.35919.96 d17.13
|
VisualStudioVersion = 17.13.35919.96
|
||||||
MinimumVisualStudioVersion = 10.0.40219.1
|
MinimumVisualStudioVersion = 10.0.40219.1
|
||||||
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "DotnetStreams", "DotnetStreams\DotnetStreams.csproj", "{42612FD1-4024-4091-B62F-3A897A4BBEF2}"
|
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "DotnetStandardStreams", "DotnetStandardStreams\DotnetStandardStreams.csproj", "{42612FD1-4024-4091-B62F-3A897A4BBEF2}"
|
||||||
EndProject
|
EndProject
|
||||||
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "DotnetStreamsTests", "DotnetStreamsTests\DotnetStreamsTests.csproj", "{1CE38FCB-E08F-8213-356A-F21626719E30}"
|
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "DotnetStandardStreamsTests", "DotnetStandardStreamsTests\DotnetStandardStreamsTests.csproj", "{1CE38FCB-E08F-8213-356A-F21626719E30}"
|
||||||
EndProject
|
EndProject
|
||||||
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "DotnetStreamsApp", "DotnetStreamsApp\DotnetStreamsApp.csproj", "{7EF73074-AAA6-13D4-5EF8-6D91B220C690}"
|
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "DotnetStandardStreamsApp", "DotnetStandardStreamsApp\DotnetStandardStreamsApp.csproj", "{7EF73074-AAA6-13D4-5EF8-6D91B220C690}"
|
||||||
EndProject
|
EndProject
|
||||||
Global
|
Global
|
||||||
GlobalSection(SolutionConfigurationPlatforms) = preSolution
|
GlobalSection(SolutionConfigurationPlatforms) = preSolution
|
||||||
Debug|Any CPU = Debug|Any CPU
|
Debug|Any CPU = Debug|Any CPU
|
||||||
Release|Any CPU = Release|Any CPU
|
Release|Any CPU = Release|Any CPU
|
||||||
EndGlobalSection
|
EndGlobalSection
|
||||||
GlobalSection(ProjectConfigurationPlatforms) = postSolution
|
GlobalSection(ProjectConfigurationPlatforms) = postSolution
|
||||||
{42612FD1-4024-4091-B62F-3A897A4BBEF2}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
|
{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}.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.ActiveCfg = Release|Any CPU
|
||||||
{42612FD1-4024-4091-B62F-3A897A4BBEF2}.Release|Any CPU.Build.0 = 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.ActiveCfg = Debug|Any CPU
|
||||||
{1CE38FCB-E08F-8213-356A-F21626719E30}.Debug|Any CPU.Build.0 = 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.ActiveCfg = Release|Any CPU
|
||||||
{1CE38FCB-E08F-8213-356A-F21626719E30}.Release|Any CPU.Build.0 = 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.ActiveCfg = Debug|Any CPU
|
||||||
{7EF73074-AAA6-13D4-5EF8-6D91B220C690}.Debug|Any CPU.Build.0 = 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.ActiveCfg = Release|Any CPU
|
||||||
{7EF73074-AAA6-13D4-5EF8-6D91B220C690}.Release|Any CPU.Build.0 = Release|Any CPU
|
{7EF73074-AAA6-13D4-5EF8-6D91B220C690}.Release|Any CPU.Build.0 = Release|Any CPU
|
||||||
EndGlobalSection
|
EndGlobalSection
|
||||||
GlobalSection(SolutionProperties) = preSolution
|
GlobalSection(SolutionProperties) = preSolution
|
||||||
HideSolutionNode = FALSE
|
HideSolutionNode = FALSE
|
||||||
EndGlobalSection
|
EndGlobalSection
|
||||||
GlobalSection(ExtensibilityGlobals) = postSolution
|
GlobalSection(ExtensibilityGlobals) = postSolution
|
||||||
SolutionGuid = {56B0A117-876A-43B1-9E7E-3966279BC007}
|
SolutionGuid = {56B0A117-876A-43B1-9E7E-3966279BC007}
|
||||||
EndGlobalSection
|
EndGlobalSection
|
||||||
EndGlobal
|
EndGlobal
|
||||||
@ -1,30 +1,30 @@
|
|||||||
using System;
|
using System;
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
using System.Linq;
|
using System.Linq;
|
||||||
using System.Text;
|
using System.Text;
|
||||||
using System.Threading.Tasks;
|
using System.Threading.Tasks;
|
||||||
|
|
||||||
namespace DotnetStreams
|
namespace DotnetStandardStreams
|
||||||
{
|
{
|
||||||
public class AnonOutputTarget : IOutputTarget
|
public class AnonOutputTarget : IOutputTarget
|
||||||
{
|
{
|
||||||
private readonly Action<string> outputProc;
|
private readonly Action<string> outputProc;
|
||||||
public AnonOutputTarget(Action<string> outputProc)
|
public AnonOutputTarget(Action<string> outputProc)
|
||||||
{
|
{
|
||||||
this.outputProc = outputProc;
|
this.outputProc = outputProc;
|
||||||
}
|
}
|
||||||
|
|
||||||
public virtual void Close()
|
public virtual void Close()
|
||||||
{
|
{
|
||||||
}
|
}
|
||||||
|
|
||||||
public virtual void Open()
|
public virtual void Open()
|
||||||
{
|
{
|
||||||
}
|
}
|
||||||
|
|
||||||
public virtual void Output(string line)
|
public virtual void Output(string line)
|
||||||
{
|
{
|
||||||
this.outputProc?.Invoke(line);
|
this.outputProc?.Invoke(line);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -1,58 +1,58 @@
|
|||||||
using System;
|
using System;
|
||||||
|
|
||||||
namespace DotnetStreams
|
namespace DotnetStandardStreams
|
||||||
{
|
{
|
||||||
public class BytesReadEventArgs
|
public class BytesReadEventArgs
|
||||||
{
|
{
|
||||||
public byte[] Data { get; }
|
public byte[] Data { get; }
|
||||||
public int Size { get; }
|
public int Size { get; }
|
||||||
public BytesReadEventArgs(byte[] data, int size)
|
public BytesReadEventArgs(byte[] data, int size)
|
||||||
{
|
{
|
||||||
this.Data = data;
|
this.Data = data;
|
||||||
this.Size = size;
|
this.Size = size;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public delegate void BytesReadEventHandler(object sender, BytesReadEventArgs e);
|
public delegate void BytesReadEventHandler(object sender, BytesReadEventArgs e);
|
||||||
|
|
||||||
public class BinaryStdinReader
|
public class BinaryStdinReader
|
||||||
{
|
{
|
||||||
protected readonly Action<byte[], int> dataReceiverProc;
|
protected readonly Action<byte[], int>? dataReceiverProc;
|
||||||
protected readonly Action<int> doneProc;
|
protected readonly Action<int>? doneProc;
|
||||||
public event BytesReadEventHandler OnBytesRead;
|
public event BytesReadEventHandler? OnBytesRead;
|
||||||
public event EventHandler OnDone;
|
public event EventHandler? OnDone;
|
||||||
public BinaryStdinReader(Action<byte[], int> dataReceiverProc, Action<int> doneProc)
|
public BinaryStdinReader(Action<byte[], int> dataReceiverProc, Action<int> doneProc)
|
||||||
{
|
{
|
||||||
this.dataReceiverProc = dataReceiverProc;
|
this.dataReceiverProc = dataReceiverProc;
|
||||||
this.doneProc = doneProc;
|
this.doneProc = doneProc;
|
||||||
}
|
}
|
||||||
|
|
||||||
public BinaryStdinReader()
|
public BinaryStdinReader()
|
||||||
{
|
{
|
||||||
}
|
}
|
||||||
|
|
||||||
public void ReadBytes(Action<byte[], int> receiverProc, Action<int> doneProc)
|
public void ReadBytes(Action<byte[], int> receiverProc, Action<int> doneProc)
|
||||||
{
|
{
|
||||||
using System.IO.Stream stdinStream = System.Console.OpenStandardInput();
|
using System.IO.Stream stdinStream = System.Console.OpenStandardInput();
|
||||||
|
|
||||||
int totalBytesRead = 0;
|
int totalBytesRead = 0;
|
||||||
int bufferSize = 2048;
|
int bufferSize = 2048;
|
||||||
byte[] buffer = new byte[bufferSize];
|
byte[] buffer = new byte[bufferSize];
|
||||||
int bytesRead = stdinStream.Read(buffer, 0, bufferSize);
|
int bytesRead = stdinStream.Read(buffer, 0, bufferSize);
|
||||||
while (bytesRead > 0)
|
while (bytesRead > 0)
|
||||||
{
|
{
|
||||||
OnBytesRead?.Invoke(this, new BytesReadEventArgs(buffer, bytesRead));
|
OnBytesRead?.Invoke(this, new BytesReadEventArgs(buffer, bytesRead));
|
||||||
receiverProc?.Invoke(buffer, bytesRead);
|
receiverProc?.Invoke(buffer, bytesRead);
|
||||||
|
|
||||||
if (bytesRead < bufferSize)
|
if (bytesRead < bufferSize)
|
||||||
break;
|
break;
|
||||||
|
|
||||||
bytesRead = stdinStream.Read(buffer, 0, bufferSize);
|
bytesRead = stdinStream.Read(buffer, 0, bufferSize);
|
||||||
totalBytesRead += bytesRead;
|
totalBytesRead += bytesRead;
|
||||||
}
|
}
|
||||||
|
|
||||||
OnDone?.Invoke(this, new EventArgs());
|
OnDone?.Invoke(this, new EventArgs());
|
||||||
doneProc?.Invoke(totalBytesRead);
|
doneProc?.Invoke(totalBytesRead);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -1,22 +1,22 @@
|
|||||||
using System;
|
using System;
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
using System.Linq;
|
using System.Linq;
|
||||||
using System.Text;
|
using System.Text;
|
||||||
using System.Threading.Tasks;
|
using System.Threading.Tasks;
|
||||||
|
|
||||||
namespace DotnetStreams
|
namespace DotnetStandardStreams
|
||||||
{
|
{
|
||||||
public class ConsoleOutputTarget : IOutputTarget
|
public class ConsoleOutputTarget : IOutputTarget
|
||||||
{
|
{
|
||||||
public virtual void Open()
|
public virtual void Open()
|
||||||
{
|
{
|
||||||
}
|
}
|
||||||
public virtual void Output(string line)
|
public virtual void Output(string line)
|
||||||
{
|
{
|
||||||
Console.WriteLine(line);
|
Console.WriteLine(line);
|
||||||
}
|
}
|
||||||
public virtual void Close()
|
public virtual void Close()
|
||||||
{
|
{
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -1,16 +1,16 @@
|
|||||||
<Project Sdk="Microsoft.NET.Sdk">
|
<Project Sdk="Microsoft.NET.Sdk">
|
||||||
|
|
||||||
<PropertyGroup>
|
<PropertyGroup>
|
||||||
<TargetFramework>net5.0</TargetFramework>
|
<TargetFramework>net5.0</TargetFramework>
|
||||||
<Nullable>enable</Nullable>
|
<Nullable>enable</Nullable>
|
||||||
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
|
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
|
||||||
<Authors>Phil Gilmore</Authors>
|
<Authors>Phil Gilmore</Authors>
|
||||||
<Description>Classes to seamlessly abstract StdIn, StdOut, coded lists or file streams in your console applications.</Description>
|
<Description>Classes to seamlessly abstract StdIn, StdOut, coded lists or file streams in your console applications.</Description>
|
||||||
<PackageTags />
|
<PackageTags />
|
||||||
<PackageLicenseExpression>MIT</PackageLicenseExpression>
|
<PackageLicenseExpression>MIT</PackageLicenseExpression>
|
||||||
<PublishRepositoryUrl>true</PublishRepositoryUrl>
|
<PublishRepositoryUrl>true</PublishRepositoryUrl>
|
||||||
<IncludeSymbols>true</IncludeSymbols>
|
<IncludeSymbols>true</IncludeSymbols>
|
||||||
<SymbolPackageFormat>snupkg</SymbolPackageFormat>
|
<SymbolPackageFormat>snupkg</SymbolPackageFormat>
|
||||||
</PropertyGroup>
|
</PropertyGroup>
|
||||||
|
|
||||||
</Project>
|
</Project>
|
||||||
@ -1,71 +1,71 @@
|
|||||||
using System;
|
using System;
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
using System.Linq;
|
using System.Linq;
|
||||||
using System.Text;
|
using System.Text;
|
||||||
using System.Threading.Tasks;
|
using System.Threading.Tasks;
|
||||||
using System.IO;
|
using System.IO;
|
||||||
|
|
||||||
namespace DotnetStreams
|
namespace DotnetStandardStreams
|
||||||
{
|
{
|
||||||
public class FileTextSource : ITextSource
|
public class FileTextSource : ITextSource
|
||||||
{
|
{
|
||||||
protected FileStream? file;
|
protected FileStream? file;
|
||||||
protected StreamReader? reader;
|
protected StreamReader? reader;
|
||||||
protected string filename;
|
protected string filename;
|
||||||
|
|
||||||
public FileTextSource(string filename)
|
public FileTextSource(string filename)
|
||||||
{
|
{
|
||||||
this.filename = filename;
|
this.filename = filename;
|
||||||
file = null;
|
file = null;
|
||||||
reader = null;
|
reader = null;
|
||||||
}
|
}
|
||||||
|
|
||||||
public virtual void Open()
|
public virtual void Open()
|
||||||
{
|
{
|
||||||
file = new FileStream(filename, FileMode.Open, FileAccess.Read, FileShare.Read);
|
file = new FileStream(filename, FileMode.Open, FileAccess.Read, FileShare.Read);
|
||||||
reader = new StreamReader(file);
|
reader = new StreamReader(file);
|
||||||
}
|
}
|
||||||
|
|
||||||
public virtual IEnumerable<string> ReadAll()
|
public virtual IEnumerable<string> ReadAll()
|
||||||
{
|
{
|
||||||
string? line = reader?.ReadLine();
|
string? line = reader?.ReadLine();
|
||||||
while (line != null)
|
while (line != null)
|
||||||
{
|
{
|
||||||
yield return line;
|
yield return line;
|
||||||
line = reader?.ReadLine();
|
line = reader?.ReadLine();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public virtual string Read()
|
public virtual string Read()
|
||||||
{
|
{
|
||||||
if (!Eof())
|
if (!Eof())
|
||||||
return reader?.ReadLine() ?? string.Empty;
|
return reader?.ReadLine() ?? string.Empty;
|
||||||
else
|
else
|
||||||
return string.Empty;
|
return string.Empty;
|
||||||
}
|
}
|
||||||
|
|
||||||
public virtual bool Eof()
|
public virtual bool Eof()
|
||||||
{
|
{
|
||||||
return reader?.EndOfStream ?? true;
|
return reader?.EndOfStream ?? true;
|
||||||
}
|
}
|
||||||
|
|
||||||
public virtual void Close()
|
public virtual void Close()
|
||||||
{
|
{
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
file?.Flush();
|
file?.Flush();
|
||||||
}
|
}
|
||||||
finally
|
finally
|
||||||
{
|
{
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
file?.Close();
|
file?.Close();
|
||||||
}
|
}
|
||||||
finally
|
finally
|
||||||
{
|
{
|
||||||
file?.Dispose();
|
file?.Dispose();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -1,15 +1,15 @@
|
|||||||
using System;
|
using System;
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
using System.Linq;
|
using System.Linq;
|
||||||
using System.Text;
|
using System.Text;
|
||||||
using System.Threading.Tasks;
|
using System.Threading.Tasks;
|
||||||
|
|
||||||
namespace DotnetStreams
|
namespace DotnetStandardStreams
|
||||||
{
|
{
|
||||||
public interface IOutputTarget
|
public interface IOutputTarget
|
||||||
{
|
{
|
||||||
void Open();
|
void Open();
|
||||||
void Output(string line);
|
void Output(string line);
|
||||||
void Close();
|
void Close();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -1,17 +1,17 @@
|
|||||||
using System;
|
using System;
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
using System.Linq;
|
using System.Linq;
|
||||||
using System.Text;
|
using System.Text;
|
||||||
using System.Threading.Tasks;
|
using System.Threading.Tasks;
|
||||||
|
|
||||||
namespace DotnetStreams
|
namespace DotnetStandardStreams
|
||||||
{
|
{
|
||||||
public interface ITextSource
|
public interface ITextSource
|
||||||
{
|
{
|
||||||
void Open();
|
void Open();
|
||||||
IEnumerable<string> ReadAll();
|
IEnumerable<string> ReadAll();
|
||||||
string Read();
|
string Read();
|
||||||
bool Eof();
|
bool Eof();
|
||||||
void Close();
|
void Close();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -1,30 +1,30 @@
|
|||||||
using System;
|
using System;
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
using System.Linq;
|
using System.Linq;
|
||||||
using System.Text;
|
using System.Text;
|
||||||
using System.Threading.Tasks;
|
using System.Threading.Tasks;
|
||||||
|
|
||||||
namespace DotnetStreams
|
namespace DotnetStandardStreams
|
||||||
{
|
{
|
||||||
public class ListOutputTarget : IOutputTarget
|
public class ListOutputTarget : IOutputTarget
|
||||||
{
|
{
|
||||||
public IList<string> OutputList { get; }
|
public IList<string> OutputList { get; }
|
||||||
public ListOutputTarget(IList<string> outputList)
|
public ListOutputTarget(IList<string> outputList)
|
||||||
{
|
{
|
||||||
this.OutputList = outputList;
|
this.OutputList = outputList;
|
||||||
}
|
}
|
||||||
|
|
||||||
public virtual void Open()
|
public virtual void Open()
|
||||||
{
|
{
|
||||||
}
|
}
|
||||||
|
|
||||||
public virtual void Output(string text)
|
public virtual void Output(string text)
|
||||||
{
|
{
|
||||||
OutputList.Add(text);
|
OutputList.Add(text);
|
||||||
}
|
}
|
||||||
|
|
||||||
public virtual void Close()
|
public virtual void Close()
|
||||||
{
|
{
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -1,71 +1,71 @@
|
|||||||
using System;
|
using System;
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
using System.Linq;
|
using System.Linq;
|
||||||
using System.Text;
|
using System.Text;
|
||||||
using System.Threading.Tasks;
|
using System.Threading.Tasks;
|
||||||
|
|
||||||
namespace DotnetStreams
|
namespace DotnetStandardStreams
|
||||||
{
|
{
|
||||||
public class ListTextSource : ITextSource
|
public class ListTextSource : ITextSource
|
||||||
{
|
{
|
||||||
private bool isEof;
|
private bool isEof;
|
||||||
private readonly IEnumerable<string> source;
|
private readonly IEnumerable<string> source;
|
||||||
private IEnumerator<string>? enumerator;
|
private IEnumerator<string>? enumerator;
|
||||||
//private string lastValue;
|
//private string lastValue;
|
||||||
private bool firstIsRead;
|
private bool firstIsRead;
|
||||||
private string nextLine;
|
private string nextLine;
|
||||||
public ListTextSource(IEnumerable<string> source)
|
public ListTextSource(IEnumerable<string> source)
|
||||||
{
|
{
|
||||||
this.source = source;
|
this.source = source;
|
||||||
enumerator = null;
|
enumerator = null;
|
||||||
firstIsRead = false;
|
firstIsRead = false;
|
||||||
nextLine = string.Empty;
|
nextLine = string.Empty;
|
||||||
}
|
}
|
||||||
|
|
||||||
public virtual void Open()
|
public virtual void Open()
|
||||||
{
|
{
|
||||||
}
|
}
|
||||||
|
|
||||||
public virtual IEnumerable<string> ReadAll()
|
public virtual IEnumerable<string> ReadAll()
|
||||||
{
|
{
|
||||||
return source.AsEnumerable();
|
return source.AsEnumerable();
|
||||||
}
|
}
|
||||||
|
|
||||||
public virtual string Read()
|
public virtual string Read()
|
||||||
{
|
{
|
||||||
if (enumerator == null)
|
if (enumerator == null)
|
||||||
enumerator = source.GetEnumerator();
|
enumerator = source.GetEnumerator();
|
||||||
|
|
||||||
string thisLine;
|
string thisLine;
|
||||||
if (!firstIsRead)
|
if (!firstIsRead)
|
||||||
{
|
{
|
||||||
// Read the first, put it in the "last" buffer.
|
// Read the first, put it in the "last" buffer.
|
||||||
isEof = !enumerator.MoveNext();
|
isEof = !enumerator.MoveNext();
|
||||||
nextLine = enumerator.Current;
|
nextLine = enumerator.Current;
|
||||||
firstIsRead = true;
|
firstIsRead = true;
|
||||||
}
|
}
|
||||||
|
|
||||||
thisLine = nextLine;
|
thisLine = nextLine;
|
||||||
|
|
||||||
if (!isEof)
|
if (!isEof)
|
||||||
{
|
{
|
||||||
isEof = !enumerator.MoveNext();
|
isEof = !enumerator.MoveNext();
|
||||||
if (!isEof)
|
if (!isEof)
|
||||||
nextLine = enumerator.Current;
|
nextLine = enumerator.Current;
|
||||||
else
|
else
|
||||||
nextLine = string.Empty;
|
nextLine = string.Empty;
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return thisLine;
|
return thisLine;
|
||||||
}
|
}
|
||||||
|
|
||||||
public virtual bool Eof() => isEof;
|
public virtual bool Eof() => isEof;
|
||||||
|
|
||||||
public virtual void Close()
|
public virtual void Close()
|
||||||
{
|
{
|
||||||
enumerator = null;
|
enumerator = null;
|
||||||
isEof = false;
|
isEof = false;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -1,26 +1,26 @@
|
|||||||
using System;
|
using System;
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
using System.Linq;
|
using System.Linq;
|
||||||
using System.Text;
|
using System.Text;
|
||||||
using System.Threading.Tasks;
|
using System.Threading.Tasks;
|
||||||
|
|
||||||
namespace DotnetStreams
|
namespace DotnetStandardStreams
|
||||||
{
|
{
|
||||||
public class ProcessedConsoleOutputTarget : ConsoleOutputTarget
|
public class ProcessedConsoleOutputTarget : ConsoleOutputTarget
|
||||||
{
|
{
|
||||||
protected Func<string, string> processorFunc;
|
protected Func<string, string> processorFunc;
|
||||||
public ProcessedConsoleOutputTarget(Func<string, string> processorFunc) : base()
|
public ProcessedConsoleOutputTarget(Func<string, string> processorFunc) : base()
|
||||||
{
|
{
|
||||||
if (processorFunc != null)
|
if (processorFunc != null)
|
||||||
this.processorFunc = processorFunc;
|
this.processorFunc = processorFunc;
|
||||||
else
|
else
|
||||||
this.processorFunc = line => line;
|
this.processorFunc = line => line;
|
||||||
}
|
}
|
||||||
|
|
||||||
public override void Output(string line)
|
public override void Output(string line)
|
||||||
{
|
{
|
||||||
string output = processorFunc?.Invoke(line) ?? string.Empty;
|
string output = processorFunc?.Invoke(line) ?? string.Empty;
|
||||||
Console.WriteLine(output);
|
Console.WriteLine(output);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -1,47 +1,47 @@
|
|||||||
using System;
|
using System;
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
using System.Linq;
|
using System.Linq;
|
||||||
using System.Text;
|
using System.Text;
|
||||||
using System.Threading.Tasks;
|
using System.Threading.Tasks;
|
||||||
|
|
||||||
namespace DotnetStreams
|
namespace DotnetStandardStreams
|
||||||
{
|
{
|
||||||
public class StdInTextSource : ITextSource
|
public class StdInTextSource : ITextSource
|
||||||
{
|
{
|
||||||
protected bool isEof;
|
protected bool isEof;
|
||||||
|
|
||||||
public virtual void Open()
|
public virtual void Open()
|
||||||
{
|
{
|
||||||
isEof = false;
|
isEof = false;
|
||||||
}
|
}
|
||||||
|
|
||||||
public virtual IEnumerable<string> ReadAll()
|
public virtual IEnumerable<string> ReadAll()
|
||||||
{
|
{
|
||||||
string? s = Console.ReadLine();
|
string? s = Console.ReadLine();
|
||||||
while (s != null)
|
while (s != null)
|
||||||
{
|
{
|
||||||
yield return s;
|
yield return s;
|
||||||
s = Console.ReadLine();
|
s = Console.ReadLine();
|
||||||
}
|
}
|
||||||
isEof = true;
|
isEof = true;
|
||||||
}
|
}
|
||||||
|
|
||||||
public virtual string Read()
|
public virtual string Read()
|
||||||
{
|
{
|
||||||
string? s = Console.ReadLine();
|
string? s = Console.ReadLine();
|
||||||
if (s == null)
|
if (s == null)
|
||||||
{
|
{
|
||||||
isEof = true;
|
isEof = true;
|
||||||
return string.Empty;
|
return string.Empty;
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
return s;
|
return s;
|
||||||
}
|
}
|
||||||
|
|
||||||
public virtual bool Eof() => isEof;
|
public virtual bool Eof() => isEof;
|
||||||
|
|
||||||
public virtual void Close()
|
public virtual void Close()
|
||||||
{
|
{
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -1,14 +1,14 @@
|
|||||||
<Project Sdk="Microsoft.NET.Sdk">
|
<Project Sdk="Microsoft.NET.Sdk">
|
||||||
|
|
||||||
<PropertyGroup>
|
<PropertyGroup>
|
||||||
<OutputType>Exe</OutputType>
|
<OutputType>Exe</OutputType>
|
||||||
<TargetFramework>net5.0</TargetFramework>
|
<TargetFramework>net8.0</TargetFramework>
|
||||||
<Nullable>enable</Nullable>
|
<Nullable>enable</Nullable>
|
||||||
<WarningsAsErrors>nullable</WarningsAsErrors>
|
<WarningsAsErrors>nullable</WarningsAsErrors>
|
||||||
</PropertyGroup>
|
</PropertyGroup>
|
||||||
|
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
<ProjectReference Include="..\DotnetStreams\DotnetStreams.csproj" />
|
<ProjectReference Include="..\DotnetStandardStreams\DotnetStandardStreams.csproj" />
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
|
|
||||||
</Project>
|
</Project>
|
||||||
@ -1,297 +1,297 @@
|
|||||||
using System;
|
using System;
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
using System.IO;
|
using System.IO;
|
||||||
using System.Linq;
|
using System.Linq;
|
||||||
using System.Text;
|
using System.Text;
|
||||||
using System.Threading.Tasks;
|
using System.Threading.Tasks;
|
||||||
|
|
||||||
namespace DotnetStreams
|
namespace DotnetStandardStreams
|
||||||
{
|
{
|
||||||
//public interface ITextSource
|
//public interface ITextSource
|
||||||
//{
|
//{
|
||||||
// void Open();
|
// void Open();
|
||||||
// IEnumerable<string> ReadAll();
|
// IEnumerable<string> ReadAll();
|
||||||
// string Read();
|
// string Read();
|
||||||
// bool Eof();
|
// bool Eof();
|
||||||
// void Close();
|
// void Close();
|
||||||
//}
|
//}
|
||||||
|
|
||||||
//public class StdInTextSource : ITextSource
|
//public class StdInTextSource : ITextSource
|
||||||
//{
|
//{
|
||||||
// protected bool isEof;
|
// protected bool isEof;
|
||||||
|
|
||||||
// public virtual void Open()
|
// public virtual void Open()
|
||||||
// {
|
// {
|
||||||
// isEof = false;
|
// isEof = false;
|
||||||
// }
|
// }
|
||||||
|
|
||||||
// public virtual IEnumerable<string> ReadAll()
|
// public virtual IEnumerable<string> ReadAll()
|
||||||
// {
|
// {
|
||||||
// string? s = Console.ReadLine();
|
// string? s = Console.ReadLine();
|
||||||
// while (s != null)
|
// while (s != null)
|
||||||
// {
|
// {
|
||||||
// yield return s;
|
// yield return s;
|
||||||
// s = Console.ReadLine();
|
// s = Console.ReadLine();
|
||||||
// }
|
// }
|
||||||
// isEof = true;
|
// isEof = true;
|
||||||
// }
|
// }
|
||||||
|
|
||||||
// public virtual string Read()
|
// public virtual string Read()
|
||||||
// {
|
// {
|
||||||
// string? s = Console.ReadLine();
|
// string? s = Console.ReadLine();
|
||||||
// if (s == null)
|
// if (s == null)
|
||||||
// {
|
// {
|
||||||
// isEof = true;
|
// isEof = true;
|
||||||
// return string.Empty;
|
// return string.Empty;
|
||||||
// }
|
// }
|
||||||
// else
|
// else
|
||||||
// return s;
|
// return s;
|
||||||
// }
|
// }
|
||||||
|
|
||||||
// public virtual bool Eof() => isEof;
|
// public virtual bool Eof() => isEof;
|
||||||
|
|
||||||
// public virtual void Close()
|
// public virtual void Close()
|
||||||
// {
|
// {
|
||||||
// }
|
// }
|
||||||
//}
|
//}
|
||||||
|
|
||||||
//public class FileTextSource : ITextSource
|
//public class FileTextSource : ITextSource
|
||||||
//{
|
//{
|
||||||
// protected FileStream? file;
|
// protected FileStream? file;
|
||||||
// protected StreamReader? reader;
|
// protected StreamReader? reader;
|
||||||
// protected string filename;
|
// protected string filename;
|
||||||
|
|
||||||
// public FileTextSource(string filename)
|
// public FileTextSource(string filename)
|
||||||
// {
|
// {
|
||||||
// this.filename = filename;
|
// this.filename = filename;
|
||||||
// file = null;
|
// file = null;
|
||||||
// reader = null;
|
// reader = null;
|
||||||
// }
|
// }
|
||||||
|
|
||||||
// public virtual void Open()
|
// public virtual void Open()
|
||||||
// {
|
// {
|
||||||
// file = new FileStream(filename, FileMode.Open, FileAccess.Read, FileShare.Read);
|
// file = new FileStream(filename, FileMode.Open, FileAccess.Read, FileShare.Read);
|
||||||
// reader = new StreamReader(file);
|
// reader = new StreamReader(file);
|
||||||
// }
|
// }
|
||||||
|
|
||||||
// public virtual IEnumerable<string> ReadAll()
|
// public virtual IEnumerable<string> ReadAll()
|
||||||
// {
|
// {
|
||||||
// string? line = reader?.ReadLine();
|
// string? line = reader?.ReadLine();
|
||||||
// while (line != null)
|
// while (line != null)
|
||||||
// {
|
// {
|
||||||
// yield return line;
|
// yield return line;
|
||||||
// line = reader?.ReadLine();
|
// line = reader?.ReadLine();
|
||||||
// }
|
// }
|
||||||
// }
|
// }
|
||||||
|
|
||||||
// public virtual string Read()
|
// public virtual string Read()
|
||||||
// {
|
// {
|
||||||
// if (!Eof())
|
// if (!Eof())
|
||||||
// return reader?.ReadLine() ?? string.Empty;
|
// return reader?.ReadLine() ?? string.Empty;
|
||||||
// else
|
// else
|
||||||
// return string.Empty;
|
// return string.Empty;
|
||||||
// }
|
// }
|
||||||
|
|
||||||
// public virtual bool Eof()
|
// public virtual bool Eof()
|
||||||
// {
|
// {
|
||||||
// return reader?.EndOfStream ?? true;
|
// return reader?.EndOfStream ?? true;
|
||||||
// }
|
// }
|
||||||
|
|
||||||
// public virtual void Close()
|
// public virtual void Close()
|
||||||
// {
|
// {
|
||||||
// try
|
// try
|
||||||
// {
|
// {
|
||||||
// file?.Flush();
|
// file?.Flush();
|
||||||
// }
|
// }
|
||||||
// finally
|
// finally
|
||||||
// {
|
// {
|
||||||
// try
|
// try
|
||||||
// {
|
// {
|
||||||
// file?.Close();
|
// file?.Close();
|
||||||
// }
|
// }
|
||||||
// finally
|
// finally
|
||||||
// {
|
// {
|
||||||
// file?.Dispose();
|
// file?.Dispose();
|
||||||
// }
|
// }
|
||||||
// }
|
// }
|
||||||
// }
|
// }
|
||||||
//}
|
//}
|
||||||
|
|
||||||
//public class ListTextSource : ITextSource
|
//public class ListTextSource : ITextSource
|
||||||
//{
|
//{
|
||||||
// private bool isEof;
|
// private bool isEof;
|
||||||
// private readonly IEnumerable<string> source;
|
// private readonly IEnumerable<string> source;
|
||||||
// private IEnumerator<string>? enumerator;
|
// private IEnumerator<string>? enumerator;
|
||||||
// //private string lastValue;
|
// //private string lastValue;
|
||||||
// private bool firstIsRead;
|
// private bool firstIsRead;
|
||||||
// private string nextLine;
|
// private string nextLine;
|
||||||
// public ListTextSource(IEnumerable<string> source)
|
// public ListTextSource(IEnumerable<string> source)
|
||||||
// {
|
// {
|
||||||
// this.source = source;
|
// this.source = source;
|
||||||
// enumerator = null;
|
// enumerator = null;
|
||||||
// firstIsRead = false;
|
// firstIsRead = false;
|
||||||
// nextLine = string.Empty;
|
// nextLine = string.Empty;
|
||||||
// }
|
// }
|
||||||
|
|
||||||
// public virtual void Open()
|
// public virtual void Open()
|
||||||
// {
|
// {
|
||||||
// }
|
// }
|
||||||
|
|
||||||
// public virtual IEnumerable<string> ReadAll()
|
// public virtual IEnumerable<string> ReadAll()
|
||||||
// {
|
// {
|
||||||
// return source.AsEnumerable();
|
// return source.AsEnumerable();
|
||||||
// }
|
// }
|
||||||
|
|
||||||
// public virtual string Read()
|
// public virtual string Read()
|
||||||
// {
|
// {
|
||||||
// if (enumerator == null)
|
// if (enumerator == null)
|
||||||
// enumerator = source.GetEnumerator();
|
// enumerator = source.GetEnumerator();
|
||||||
|
|
||||||
// string thisLine;
|
// string thisLine;
|
||||||
// if (!firstIsRead)
|
// if (!firstIsRead)
|
||||||
// {
|
// {
|
||||||
// // Read the first, put it in the "last" buffer.
|
// // Read the first, put it in the "last" buffer.
|
||||||
// isEof = !enumerator.MoveNext();
|
// isEof = !enumerator.MoveNext();
|
||||||
// nextLine = enumerator.Current;
|
// nextLine = enumerator.Current;
|
||||||
// firstIsRead = true;
|
// firstIsRead = true;
|
||||||
// }
|
// }
|
||||||
|
|
||||||
// thisLine = nextLine;
|
// thisLine = nextLine;
|
||||||
|
|
||||||
// if (!isEof)
|
// if (!isEof)
|
||||||
// {
|
// {
|
||||||
// isEof = !enumerator.MoveNext();
|
// isEof = !enumerator.MoveNext();
|
||||||
// if (!isEof)
|
// if (!isEof)
|
||||||
// nextLine = enumerator.Current;
|
// nextLine = enumerator.Current;
|
||||||
// else
|
// else
|
||||||
// nextLine = string.Empty;
|
// nextLine = string.Empty;
|
||||||
|
|
||||||
// }
|
// }
|
||||||
|
|
||||||
// return thisLine;
|
// return thisLine;
|
||||||
// }
|
// }
|
||||||
|
|
||||||
// public virtual bool Eof() => isEof;
|
// public virtual bool Eof() => isEof;
|
||||||
|
|
||||||
// public virtual void Close()
|
// public virtual void Close()
|
||||||
// {
|
// {
|
||||||
// enumerator = null;
|
// enumerator = null;
|
||||||
// isEof = false;
|
// isEof = false;
|
||||||
// }
|
// }
|
||||||
//}
|
//}
|
||||||
|
|
||||||
//public interface IOutputTarget
|
//public interface IOutputTarget
|
||||||
//{
|
//{
|
||||||
// void Open();
|
// void Open();
|
||||||
// void Output(string line);
|
// void Output(string line);
|
||||||
// void Close();
|
// void Close();
|
||||||
//}
|
//}
|
||||||
|
|
||||||
//public class ConsoleOutputTarget : IOutputTarget
|
//public class ConsoleOutputTarget : IOutputTarget
|
||||||
//{
|
//{
|
||||||
// public virtual void Open()
|
// public virtual void Open()
|
||||||
// {
|
// {
|
||||||
// }
|
// }
|
||||||
// public virtual void Output(string line)
|
// public virtual void Output(string line)
|
||||||
// {
|
// {
|
||||||
// Console.WriteLine(line);
|
// Console.WriteLine(line);
|
||||||
// }
|
// }
|
||||||
// public virtual void Close()
|
// public virtual void Close()
|
||||||
// {
|
// {
|
||||||
// }
|
// }
|
||||||
//}
|
//}
|
||||||
|
|
||||||
//public class ProcessedConsoleOutputTarget : ConsoleOutputTarget
|
//public class ProcessedConsoleOutputTarget : ConsoleOutputTarget
|
||||||
//{
|
//{
|
||||||
// protected Func<string, string> processorFunc;
|
// protected Func<string, string> processorFunc;
|
||||||
// public ProcessedConsoleOutputTarget(Func<string, string> processorFunc) : base()
|
// public ProcessedConsoleOutputTarget(Func<string, string> processorFunc) : base()
|
||||||
// {
|
// {
|
||||||
// if (processorFunc != null)
|
// if (processorFunc != null)
|
||||||
// this.processorFunc = processorFunc;
|
// this.processorFunc = processorFunc;
|
||||||
// else
|
// else
|
||||||
// this.processorFunc = line => line;
|
// this.processorFunc = line => line;
|
||||||
// }
|
// }
|
||||||
|
|
||||||
// public override void Output(string line)
|
// public override void Output(string line)
|
||||||
// {
|
// {
|
||||||
// string output = processorFunc?.Invoke(line) ?? string.Empty;
|
// string output = processorFunc?.Invoke(line) ?? string.Empty;
|
||||||
// Console.WriteLine(output);
|
// Console.WriteLine(output);
|
||||||
// }
|
// }
|
||||||
//}
|
//}
|
||||||
|
|
||||||
//public class ListOutputTarget : IOutputTarget
|
//public class ListOutputTarget : IOutputTarget
|
||||||
//{
|
//{
|
||||||
// public IList<string> OutputList { get; }
|
// public IList<string> OutputList { get; }
|
||||||
// public ListOutputTarget(IList<string> outputList)
|
// public ListOutputTarget(IList<string> outputList)
|
||||||
// {
|
// {
|
||||||
// this.OutputList = outputList;
|
// this.OutputList = outputList;
|
||||||
// }
|
// }
|
||||||
|
|
||||||
// public virtual void Open()
|
// public virtual void Open()
|
||||||
// {
|
// {
|
||||||
// }
|
// }
|
||||||
|
|
||||||
// public virtual void Output(string text)
|
// public virtual void Output(string text)
|
||||||
// {
|
// {
|
||||||
// OutputList.Add(text);
|
// OutputList.Add(text);
|
||||||
// }
|
// }
|
||||||
|
|
||||||
// public virtual void Close()
|
// public virtual void Close()
|
||||||
// {
|
// {
|
||||||
// }
|
// }
|
||||||
//}
|
//}
|
||||||
|
|
||||||
//public class BytesReadEventArgs
|
//public class BytesReadEventArgs
|
||||||
//{
|
//{
|
||||||
// public byte[] Data { get; }
|
// public byte[] Data { get; }
|
||||||
// public int Size { get; }
|
// public int Size { get; }
|
||||||
// public BytesReadEventArgs(byte[] data, int size)
|
// public BytesReadEventArgs(byte[] data, int size)
|
||||||
// {
|
// {
|
||||||
// this.Data = data;
|
// this.Data = data;
|
||||||
// this.Size = size;
|
// this.Size = size;
|
||||||
// }
|
// }
|
||||||
//}
|
//}
|
||||||
|
|
||||||
//public delegate void BytesReadEventHandler(object sender, BytesReadEventArgs e);
|
//public delegate void BytesReadEventHandler(object sender, BytesReadEventArgs e);
|
||||||
|
|
||||||
//public class BinaryStdinReader
|
//public class BinaryStdinReader
|
||||||
//{
|
//{
|
||||||
// protected readonly Action<byte[], int> dataReceiverProc;
|
// protected readonly Action<byte[], int> dataReceiverProc;
|
||||||
// protected readonly Action<int> doneProc;
|
// protected readonly Action<int> doneProc;
|
||||||
// public event BytesReadEventHandler OnBytesRead;
|
// public event BytesReadEventHandler OnBytesRead;
|
||||||
// public event EventHandler OnDone;
|
// public event EventHandler OnDone;
|
||||||
// public BinaryStdinReader(Action<byte[], int> dataReceiverProc, Action<int> doneProc)
|
// public BinaryStdinReader(Action<byte[], int> dataReceiverProc, Action<int> doneProc)
|
||||||
// {
|
// {
|
||||||
// this.dataReceiverProc = dataReceiverProc;
|
// this.dataReceiverProc = dataReceiverProc;
|
||||||
// this.doneProc = doneProc;
|
// this.doneProc = doneProc;
|
||||||
// }
|
// }
|
||||||
|
|
||||||
// public BinaryStdinReader()
|
// public BinaryStdinReader()
|
||||||
// {
|
// {
|
||||||
// }
|
// }
|
||||||
|
|
||||||
// public void ReadBytes(Action<byte[], int> receiverProc, Action<int> doneProc)
|
// public void ReadBytes(Action<byte[], int> receiverProc, Action<int> doneProc)
|
||||||
// {
|
// {
|
||||||
// using System.IO.Stream stdinStream = System.Console.OpenStandardInput();
|
// using System.IO.Stream stdinStream = System.Console.OpenStandardInput();
|
||||||
|
|
||||||
// int totalBytesRead = 0;
|
// int totalBytesRead = 0;
|
||||||
// int bufferSize = 2048;
|
// int bufferSize = 2048;
|
||||||
// byte[] buffer = new byte[bufferSize];
|
// byte[] buffer = new byte[bufferSize];
|
||||||
// int bytesRead = stdinStream.Read(buffer, 0, bufferSize);
|
// int bytesRead = stdinStream.Read(buffer, 0, bufferSize);
|
||||||
// while (bytesRead > 0)
|
// while (bytesRead > 0)
|
||||||
// {
|
// {
|
||||||
// OnBytesRead?.Invoke(this, new BytesReadEventArgs(buffer, bytesRead));
|
// OnBytesRead?.Invoke(this, new BytesReadEventArgs(buffer, bytesRead));
|
||||||
// receiverProc?.Invoke(buffer, bytesRead);
|
// receiverProc?.Invoke(buffer, bytesRead);
|
||||||
|
|
||||||
// if (bytesRead < bufferSize)
|
// if (bytesRead < bufferSize)
|
||||||
// break;
|
// break;
|
||||||
|
|
||||||
// bytesRead = stdinStream.Read(buffer, 0, bufferSize);
|
// bytesRead = stdinStream.Read(buffer, 0, bufferSize);
|
||||||
// totalBytesRead += bytesRead;
|
// totalBytesRead += bytesRead;
|
||||||
// }
|
// }
|
||||||
|
|
||||||
// OnDone?.Invoke(this, new EventArgs());
|
// OnDone?.Invoke(this, new EventArgs());
|
||||||
// doneProc?.Invoke(totalBytesRead);
|
// doneProc?.Invoke(totalBytesRead);
|
||||||
// }
|
// }
|
||||||
//}
|
//}
|
||||||
}
|
}
|
||||||
@ -1,87 +1,87 @@
|
|||||||
using System;
|
using System;
|
||||||
using System.IO;
|
using System.IO;
|
||||||
using System.Linq;
|
using System.Linq;
|
||||||
using DotnetStreams;
|
using DotnetStandardStreams;
|
||||||
|
|
||||||
namespace StreamsTest
|
namespace StreamsTest
|
||||||
{
|
{
|
||||||
class Program
|
class Program
|
||||||
{
|
{
|
||||||
|
|
||||||
static void Main(string[] args)
|
static void Main(string[] args)
|
||||||
{
|
{
|
||||||
//Program p = new();
|
//Program p = new();
|
||||||
|
|
||||||
ITextSource textSource;
|
ITextSource textSource;
|
||||||
IOutputTarget textTarget = new ConsoleOutputTarget();
|
IOutputTarget textTarget = new ConsoleOutputTarget();
|
||||||
|
|
||||||
if (args.Length > 0)
|
if (args.Length > 0)
|
||||||
textSource = new FileTextSource(args[0]);
|
textSource = new FileTextSource(args[0]);
|
||||||
else
|
else
|
||||||
textSource = new StdInTextSource();
|
textSource = new StdInTextSource();
|
||||||
|
|
||||||
|
|
||||||
Execute(
|
Execute(
|
||||||
textSource,
|
textSource,
|
||||||
textTarget);
|
textTarget);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Works, but needs to be more compact.
|
// Works, but needs to be more compact.
|
||||||
//static void Main(string[] args)
|
//static void Main(string[] args)
|
||||||
//{
|
//{
|
||||||
// Program p = new Program();
|
// Program p = new Program();
|
||||||
|
|
||||||
// ITextSource textSource = null;
|
// ITextSource textSource = null;
|
||||||
|
|
||||||
// if (args.Length > 0)
|
// if (args.Length > 0)
|
||||||
// {
|
// {
|
||||||
// var filename = args[0];
|
// var filename = args[0];
|
||||||
// if (!File.Exists(filename))
|
// if (!File.Exists(filename))
|
||||||
// {
|
// {
|
||||||
// Console.WriteLine($"File not found ({filename}).");
|
// Console.WriteLine($"File not found ({filename}).");
|
||||||
// Environment.Exit(1);
|
// Environment.Exit(1);
|
||||||
// }
|
// }
|
||||||
// else
|
// else
|
||||||
// textSource = new FileTextSource(filename);
|
// textSource = new FileTextSource(filename);
|
||||||
// }
|
// }
|
||||||
// else
|
// else
|
||||||
// textSource = new StdInTextSource();
|
// textSource = new StdInTextSource();
|
||||||
|
|
||||||
// p.Execute(
|
// p.Execute(
|
||||||
// textSource,
|
// textSource,
|
||||||
// new ConsoleOutputTarget());
|
// new ConsoleOutputTarget());
|
||||||
//}
|
//}
|
||||||
|
|
||||||
|
|
||||||
//public void ExecuteReadLine(string[] args)
|
//public void ExecuteReadLine(string[] args)
|
||||||
//{
|
//{
|
||||||
// string s = Console.ReadLine();
|
// string s = Console.ReadLine();
|
||||||
// // This terminates on blank lines, no surprise
|
// // This terminates on blank lines, no surprise
|
||||||
// //while (!string.IsNullOrEmpty(s))
|
// //while (!string.IsNullOrEmpty(s))
|
||||||
// while (s != null)
|
// while (s != null)
|
||||||
// {
|
// {
|
||||||
// s = s.Replace("\r", "{CR}")
|
// s = s.Replace("\r", "{CR}")
|
||||||
// .Replace("\n", "{LF}")
|
// .Replace("\n", "{LF}")
|
||||||
// .Replace("\t", "{TAB}");
|
// .Replace("\t", "{TAB}");
|
||||||
// if (s == string.Empty)
|
// if (s == string.Empty)
|
||||||
// s = "{EMPTYSTRING}";
|
// s = "{EMPTYSTRING}";
|
||||||
// Console.WriteLine($"{s}");
|
// Console.WriteLine($"{s}");
|
||||||
// s = Console.ReadLine();
|
// s = Console.ReadLine();
|
||||||
// }
|
// }
|
||||||
// if (s == null)
|
// if (s == null)
|
||||||
// Console.WriteLine("/s/ is null");
|
// Console.WriteLine("/s/ is null");
|
||||||
//}
|
//}
|
||||||
|
|
||||||
public static void Execute(ITextSource source, IOutputTarget target)
|
public static void Execute(ITextSource source, IOutputTarget target)
|
||||||
{
|
{
|
||||||
source.Open();
|
source.Open();
|
||||||
target.Open();
|
target.Open();
|
||||||
|
|
||||||
foreach (string line in source.ReadAll())
|
foreach (string line in source.ReadAll())
|
||||||
target.Output(line);
|
target.Output(line);
|
||||||
|
|
||||||
target.Close();
|
target.Close();
|
||||||
source.Close();
|
source.Close();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -1,9 +1,9 @@
|
|||||||
{
|
{
|
||||||
"profiles": {
|
"profiles": {
|
||||||
"StreamsTest": {
|
"StreamsTest": {
|
||||||
"commandName": "Project",
|
"commandName": "Project",
|
||||||
"commandLineArgs": "C:\\Users\\Phil\\source\\repos\\DotnetStreams\\DotnetStreamsApp\\delme.txt",
|
"commandLineArgs": "C:\\Users\\Phil\\source\\repos\\DotnetStandardStreams\\DotnetStandardStreamsApp\\delme.txt",
|
||||||
"sqlDebugging": true
|
"sqlDebugging": true
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -1,4 +1,4 @@
|
|||||||
Line 1
|
Line 1
|
||||||
Line 2
|
Line 2
|
||||||
|
|
||||||
Line 3
|
Line 3
|
||||||
@ -1,38 +1,38 @@
|
|||||||
using System;
|
using System;
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
using System.Linq;
|
using System.Linq;
|
||||||
using System.Text;
|
using System.Text;
|
||||||
using System.Threading.Tasks;
|
using System.Threading.Tasks;
|
||||||
using Xunit;
|
using Xunit;
|
||||||
using Shouldly;
|
using Shouldly;
|
||||||
using DotnetStreams;
|
using DotnetStandardStreams;
|
||||||
using System.IO;
|
using System.IO;
|
||||||
using DotnetStreamsTests.Testables;
|
using DotnetStandardStreamsTests.Testables;
|
||||||
|
|
||||||
namespace DotnetStreamsTests
|
namespace DotnetStandardStreamsTests
|
||||||
{
|
{
|
||||||
public class ConsoleOutputTargetTests
|
public class ConsoleOutputTargetTests
|
||||||
{
|
{
|
||||||
private IOutputTarget outputTarget;
|
private IOutputTarget outputTarget;
|
||||||
|
|
||||||
[Fact]
|
[Fact]
|
||||||
public void WritesToOutputStream()
|
public void WritesToOutputStream()
|
||||||
{
|
{
|
||||||
ListWriter writer = new();
|
ListWriter writer = new();
|
||||||
outputTarget = new TestableConsoleOutputTarget(writer);
|
outputTarget = new TestableConsoleOutputTarget(writer);
|
||||||
|
|
||||||
outputTarget.Open();
|
outputTarget.Open();
|
||||||
outputTarget.Output("1");
|
outputTarget.Output("1");
|
||||||
outputTarget.Output("2");
|
outputTarget.Output("2");
|
||||||
outputTarget.Output("");
|
outputTarget.Output("");
|
||||||
outputTarget.Output("3");
|
outputTarget.Output("3");
|
||||||
outputTarget.Close();
|
outputTarget.Close();
|
||||||
|
|
||||||
writer.Lines.Count.ShouldBe(4);
|
writer.Lines.Count.ShouldBe(4);
|
||||||
writer.Lines[0].ShouldBe("1");
|
writer.Lines[0].ShouldBe("1");
|
||||||
writer.Lines[1].ShouldBe("2");
|
writer.Lines[1].ShouldBe("2");
|
||||||
writer.Lines[2].ShouldBe(string.Empty);
|
writer.Lines[2].ShouldBe(string.Empty);
|
||||||
writer.Lines[3].ShouldBe("3");
|
writer.Lines[3].ShouldBe("3");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -1,27 +1,27 @@
|
|||||||
<Project Sdk="Microsoft.NET.Sdk">
|
<Project Sdk="Microsoft.NET.Sdk">
|
||||||
|
|
||||||
<PropertyGroup>
|
<PropertyGroup>
|
||||||
<TargetFramework>net5.0</TargetFramework>
|
<TargetFramework>net8.0</TargetFramework>
|
||||||
|
|
||||||
<IsPackable>false</IsPackable>
|
<IsPackable>false</IsPackable>
|
||||||
</PropertyGroup>
|
</PropertyGroup>
|
||||||
|
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.9.4" />
|
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.9.4" />
|
||||||
<PackageReference Include="Shouldly" Version="4.0.3" />
|
<PackageReference Include="Shouldly" Version="4.0.3" />
|
||||||
<PackageReference Include="xunit" Version="2.4.1" />
|
<PackageReference Include="xunit" Version="2.4.1" />
|
||||||
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.3">
|
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.3">
|
||||||
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
|
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
|
||||||
<PrivateAssets>all</PrivateAssets>
|
<PrivateAssets>all</PrivateAssets>
|
||||||
</PackageReference>
|
</PackageReference>
|
||||||
<PackageReference Include="coverlet.collector" Version="3.0.2">
|
<PackageReference Include="coverlet.collector" Version="3.0.2">
|
||||||
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
|
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
|
||||||
<PrivateAssets>all</PrivateAssets>
|
<PrivateAssets>all</PrivateAssets>
|
||||||
</PackageReference>
|
</PackageReference>
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
|
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
<ProjectReference Include="..\DotnetStreams\DotnetStreams.csproj" />
|
<ProjectReference Include="..\DotnetStandardStreams\DotnetStandardStreams.csproj" />
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
|
|
||||||
</Project>
|
</Project>
|
||||||
@ -1,27 +1,27 @@
|
|||||||
using DotnetStreams;
|
using DotnetStandardStreams;
|
||||||
using System;
|
using System;
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
using Xunit;
|
using Xunit;
|
||||||
using Shouldly;
|
using Shouldly;
|
||||||
|
|
||||||
namespace DotnetStreamsTests
|
namespace DotnetStandardStreamsTests
|
||||||
{
|
{
|
||||||
public class ListOutputTargetTests
|
public class ListOutputTargetTests
|
||||||
{
|
{
|
||||||
[Fact]
|
[Fact]
|
||||||
public void WritesToList()
|
public void WritesToList()
|
||||||
{
|
{
|
||||||
ListOutputTarget target = new(new List<string>());
|
ListOutputTarget target = new(new List<string>());
|
||||||
target.Output("1");
|
target.Output("1");
|
||||||
target.Output("2");
|
target.Output("2");
|
||||||
target.Output("");
|
target.Output("");
|
||||||
target.Output("3");
|
target.Output("3");
|
||||||
|
|
||||||
target.OutputList.Count.ShouldBe(4);
|
target.OutputList.Count.ShouldBe(4);
|
||||||
target.OutputList[0].ShouldBe("1");
|
target.OutputList[0].ShouldBe("1");
|
||||||
target.OutputList[1].ShouldBe("2");
|
target.OutputList[1].ShouldBe("2");
|
||||||
target.OutputList[2].ShouldBe(string.Empty);
|
target.OutputList[2].ShouldBe(string.Empty);
|
||||||
target.OutputList[3].ShouldBe("3");
|
target.OutputList[3].ShouldBe("3");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -1,39 +1,39 @@
|
|||||||
using System;
|
using System;
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
using System.Linq;
|
using System.Linq;
|
||||||
using System.Text;
|
using System.Text;
|
||||||
using System.Threading.Tasks;
|
using System.Threading.Tasks;
|
||||||
using Xunit;
|
using Xunit;
|
||||||
using Shouldly;
|
using Shouldly;
|
||||||
using DotnetStreams;
|
using DotnetStandardStreams;
|
||||||
|
|
||||||
namespace DotnetStreamsTests
|
namespace DotnetStandardStreamsTests
|
||||||
{
|
{
|
||||||
public class ListTextSourceTests
|
public class ListTextSourceTests
|
||||||
{
|
{
|
||||||
[Fact]
|
[Fact]
|
||||||
public void ReadsFromList()
|
public void ReadsFromList()
|
||||||
{
|
{
|
||||||
ITextSource reader = new ListTextSource(new[] { "1", "2", "", "3" });
|
ITextSource reader = new ListTextSource(new[] { "1", "2", "", "3" });
|
||||||
string s0 = reader.Read();
|
string s0 = reader.Read();
|
||||||
s0.ShouldNotBeNull();
|
s0.ShouldNotBeNull();
|
||||||
s0.ShouldBe("1");
|
s0.ShouldBe("1");
|
||||||
reader.Eof().ShouldBe(false);
|
reader.Eof().ShouldBe(false);
|
||||||
|
|
||||||
string s1 = reader.Read();
|
string s1 = reader.Read();
|
||||||
s1.ShouldNotBeNull();
|
s1.ShouldNotBeNull();
|
||||||
s1.ShouldBe("2");
|
s1.ShouldBe("2");
|
||||||
reader.Eof().ShouldBe(false);
|
reader.Eof().ShouldBe(false);
|
||||||
|
|
||||||
string s2 = reader.Read();
|
string s2 = reader.Read();
|
||||||
s2.ShouldNotBeNull();
|
s2.ShouldNotBeNull();
|
||||||
s2.ShouldBe(string.Empty);
|
s2.ShouldBe(string.Empty);
|
||||||
reader.Eof().ShouldBe(false);
|
reader.Eof().ShouldBe(false);
|
||||||
|
|
||||||
string s3 = reader.Read();
|
string s3 = reader.Read();
|
||||||
s3.ShouldNotBeNull();
|
s3.ShouldNotBeNull();
|
||||||
s3.ShouldBe("3");
|
s3.ShouldBe("3");
|
||||||
reader.Eof().ShouldBe(true);
|
reader.Eof().ShouldBe(true);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -1,210 +1,210 @@
|
|||||||
using DotnetStreamsTests.Testables;
|
using DotnetStandardStreamsTests.Testables;
|
||||||
using System;
|
using System;
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
using System.IO;
|
using System.IO;
|
||||||
using System.Linq;
|
using System.Linq;
|
||||||
using System.Text;
|
using System.Text;
|
||||||
using System.Threading.Tasks;
|
using System.Threading.Tasks;
|
||||||
using Xunit;
|
using Xunit;
|
||||||
using Shouldly;
|
using Shouldly;
|
||||||
using DotnetStreams;
|
using DotnetStandardStreams;
|
||||||
|
|
||||||
namespace DotnetStreamsTests
|
namespace DotnetStandardStreamsTests
|
||||||
{
|
{
|
||||||
public class StdInTextSourceTests
|
public class StdInTextSourceTests
|
||||||
{
|
{
|
||||||
private static TextReader CreateStdIn()
|
private static TextReader CreateStdIn()
|
||||||
{
|
{
|
||||||
return new ListTextReader(new[] { "1", "2", "", "3" });
|
return new ListTextReader(new[] { "1", "2", "", "3" });
|
||||||
}
|
}
|
||||||
|
|
||||||
private static void WrapStdInTest(Action<ITextSource> testCode)
|
private static void WrapStdInTest(Action<ITextSource> testCode)
|
||||||
{
|
{
|
||||||
var oldInputReader = System.Console.In;
|
var oldInputReader = System.Console.In;
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
TextReader reader = CreateStdIn();
|
TextReader reader = CreateStdIn();
|
||||||
System.Console.SetIn(reader);
|
System.Console.SetIn(reader);
|
||||||
|
|
||||||
ITextSource stdin = new StdInTextSource();
|
ITextSource stdin = new StdInTextSource();
|
||||||
stdin.Open();
|
stdin.Open();
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
testCode?.Invoke(stdin);
|
testCode?.Invoke(stdin);
|
||||||
}
|
}
|
||||||
finally
|
finally
|
||||||
{
|
{
|
||||||
stdin.Close();
|
stdin.Close();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
finally
|
finally
|
||||||
{
|
{
|
||||||
System.Console.SetIn(oldInputReader);
|
System.Console.SetIn(oldInputReader);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
[Fact]
|
[Fact]
|
||||||
public void CanReadAllFromStandardIn()
|
public void CanReadAllFromStandardIn()
|
||||||
{
|
{
|
||||||
WrapStdInTest(stdin =>
|
WrapStdInTest(stdin =>
|
||||||
{
|
{
|
||||||
var actual = stdin.ReadAll().ToList();
|
var actual = stdin.ReadAll().ToList();
|
||||||
actual.Count.ShouldBe(4);
|
actual.Count.ShouldBe(4);
|
||||||
actual[0].ShouldBe("1");
|
actual[0].ShouldBe("1");
|
||||||
actual[1].ShouldBe("2");
|
actual[1].ShouldBe("2");
|
||||||
actual[2].ShouldBe(string.Empty);
|
actual[2].ShouldBe(string.Empty);
|
||||||
actual[3].ShouldBe("3");
|
actual[3].ShouldBe("3");
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
//{
|
//{
|
||||||
|
|
||||||
// // Fake the StdIn
|
// // Fake the StdIn
|
||||||
// var oldInputReader = System.Console.In;
|
// var oldInputReader = System.Console.In;
|
||||||
// try
|
// try
|
||||||
// {
|
// {
|
||||||
// TextReader reader = CreateStdIn();
|
// TextReader reader = CreateStdIn();
|
||||||
// System.Console.SetIn(reader);
|
// System.Console.SetIn(reader);
|
||||||
|
|
||||||
// ITextSource stdin = new StdInTextSource();
|
// ITextSource stdin = new StdInTextSource();
|
||||||
// stdin.Open();
|
// stdin.Open();
|
||||||
// try
|
// try
|
||||||
// {
|
// {
|
||||||
// var actual = stdin.ReadAll().ToList();
|
// var actual = stdin.ReadAll().ToList();
|
||||||
// actual.Count.ShouldBe(4);
|
// actual.Count.ShouldBe(4);
|
||||||
// actual[0].ShouldBe("1");
|
// actual[0].ShouldBe("1");
|
||||||
// actual[1].ShouldBe("2");
|
// actual[1].ShouldBe("2");
|
||||||
// actual[2].ShouldBe(string.Empty);
|
// actual[2].ShouldBe(string.Empty);
|
||||||
// actual[3].ShouldBe("3");
|
// actual[3].ShouldBe("3");
|
||||||
// }
|
// }
|
||||||
// finally
|
// finally
|
||||||
// {
|
// {
|
||||||
// stdin.Close();
|
// stdin.Close();
|
||||||
// }
|
// }
|
||||||
// }
|
// }
|
||||||
// finally
|
// finally
|
||||||
// {
|
// {
|
||||||
// System.Console.SetIn(oldInputReader);
|
// System.Console.SetIn(oldInputReader);
|
||||||
// }
|
// }
|
||||||
//}
|
//}
|
||||||
|
|
||||||
[Fact]
|
[Fact]
|
||||||
public void CanReadIndividualLinesFromStandardIn()
|
public void CanReadIndividualLinesFromStandardIn()
|
||||||
{
|
{
|
||||||
WrapStdInTest(stdin =>
|
WrapStdInTest(stdin =>
|
||||||
{
|
{
|
||||||
string s;
|
string s;
|
||||||
s = stdin.Read();
|
s = stdin.Read();
|
||||||
stdin.Eof().ShouldBe(false);
|
stdin.Eof().ShouldBe(false);
|
||||||
s.ShouldBe("1");
|
s.ShouldBe("1");
|
||||||
|
|
||||||
s = stdin.Read();
|
s = stdin.Read();
|
||||||
stdin.Eof().ShouldBe(false);
|
stdin.Eof().ShouldBe(false);
|
||||||
s.ShouldBe("2");
|
s.ShouldBe("2");
|
||||||
|
|
||||||
s = stdin.Read();
|
s = stdin.Read();
|
||||||
stdin.Eof().ShouldBe(false);
|
stdin.Eof().ShouldBe(false);
|
||||||
s.ShouldBe(string.Empty);
|
s.ShouldBe(string.Empty);
|
||||||
|
|
||||||
s = stdin.Read();
|
s = stdin.Read();
|
||||||
stdin.Eof().ShouldBe(false);
|
stdin.Eof().ShouldBe(false);
|
||||||
s.ShouldBe("3");
|
s.ShouldBe("3");
|
||||||
|
|
||||||
s = stdin.Read();
|
s = stdin.Read();
|
||||||
stdin.Eof().ShouldBe(true);
|
stdin.Eof().ShouldBe(true);
|
||||||
});
|
});
|
||||||
|
|
||||||
//// Fake the StdIn
|
//// Fake the StdIn
|
||||||
//var oldInputReader = System.Console.In;
|
//var oldInputReader = System.Console.In;
|
||||||
//try
|
//try
|
||||||
//{
|
//{
|
||||||
// TextReader reader = CreateStdIn();
|
// TextReader reader = CreateStdIn();
|
||||||
// System.Console.SetIn(reader);
|
// System.Console.SetIn(reader);
|
||||||
|
|
||||||
// ITextSource stdin = new StdInTextSource();
|
// ITextSource stdin = new StdInTextSource();
|
||||||
// stdin.Open();
|
// stdin.Open();
|
||||||
// try
|
// try
|
||||||
// {
|
// {
|
||||||
// string s;
|
// string s;
|
||||||
// s = stdin.Read();
|
// s = stdin.Read();
|
||||||
// stdin.Eof().ShouldBe(false);
|
// stdin.Eof().ShouldBe(false);
|
||||||
// s.ShouldBe("1");
|
// s.ShouldBe("1");
|
||||||
|
|
||||||
// s = stdin.Read();
|
// s = stdin.Read();
|
||||||
// stdin.Eof().ShouldBe(false);
|
// stdin.Eof().ShouldBe(false);
|
||||||
// s.ShouldBe("2");
|
// s.ShouldBe("2");
|
||||||
|
|
||||||
// s = stdin.Read();
|
// s = stdin.Read();
|
||||||
// stdin.Eof().ShouldBe(false);
|
// stdin.Eof().ShouldBe(false);
|
||||||
// s.ShouldBe(string.Empty);
|
// s.ShouldBe(string.Empty);
|
||||||
|
|
||||||
// s = stdin.Read();
|
// s = stdin.Read();
|
||||||
// stdin.Eof().ShouldBe(false);
|
// stdin.Eof().ShouldBe(false);
|
||||||
// s.ShouldBe("3");
|
// s.ShouldBe("3");
|
||||||
|
|
||||||
// s = stdin.Read();
|
// s = stdin.Read();
|
||||||
// stdin.Eof().ShouldBe(true);
|
// stdin.Eof().ShouldBe(true);
|
||||||
// }
|
// }
|
||||||
// finally
|
// finally
|
||||||
// {
|
// {
|
||||||
// stdin.Close();
|
// stdin.Close();
|
||||||
// }
|
// }
|
||||||
//}
|
//}
|
||||||
//finally
|
//finally
|
||||||
//{
|
//{
|
||||||
// System.Console.SetIn(oldInputReader);
|
// System.Console.SetIn(oldInputReader);
|
||||||
//}
|
//}
|
||||||
}
|
}
|
||||||
|
|
||||||
[Fact]
|
[Fact]
|
||||||
public void CanWhileLoopThroughStdInProperly()
|
public void CanWhileLoopThroughStdInProperly()
|
||||||
{
|
{
|
||||||
WrapStdInTest(stdin =>
|
WrapStdInTest(stdin =>
|
||||||
{
|
{
|
||||||
int lineCount = 0;
|
int lineCount = 0;
|
||||||
var s = stdin.Read();
|
var s = stdin.Read();
|
||||||
while (!stdin.Eof())
|
while (!stdin.Eof())
|
||||||
{
|
{
|
||||||
lineCount++;
|
lineCount++;
|
||||||
s = stdin.Read();
|
s = stdin.Read();
|
||||||
}
|
}
|
||||||
|
|
||||||
lineCount.ShouldBe(4);
|
lineCount.ShouldBe(4);
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
//{
|
//{
|
||||||
// // Fake the StdIn
|
// // Fake the StdIn
|
||||||
// var oldInputReader = System.Console.In;
|
// var oldInputReader = System.Console.In;
|
||||||
// try
|
// try
|
||||||
// {
|
// {
|
||||||
// TextReader reader = CreateStdIn();
|
// TextReader reader = CreateStdIn();
|
||||||
// System.Console.SetIn(reader);
|
// System.Console.SetIn(reader);
|
||||||
|
|
||||||
// ITextSource stdin = new StdInTextSource();
|
// ITextSource stdin = new StdInTextSource();
|
||||||
// stdin.Open();
|
// stdin.Open();
|
||||||
// try
|
// try
|
||||||
// {
|
// {
|
||||||
// int lineCount = 0;
|
// int lineCount = 0;
|
||||||
// var s = stdin.Read();
|
// var s = stdin.Read();
|
||||||
// while (!stdin.Eof())
|
// while (!stdin.Eof())
|
||||||
// {
|
// {
|
||||||
// // Do stuff with s here normally.
|
// // Do stuff with s here normally.
|
||||||
// lineCount++;
|
// lineCount++;
|
||||||
// s = stdin.Read();
|
// s = stdin.Read();
|
||||||
// }
|
// }
|
||||||
|
|
||||||
// lineCount.ShouldBe(4);
|
// lineCount.ShouldBe(4);
|
||||||
// }
|
// }
|
||||||
// finally
|
// finally
|
||||||
// {
|
// {
|
||||||
// stdin.Close();
|
// stdin.Close();
|
||||||
// }
|
// }
|
||||||
// }
|
// }
|
||||||
// finally
|
// finally
|
||||||
// {
|
// {
|
||||||
// System.Console.SetIn(oldInputReader);
|
// System.Console.SetIn(oldInputReader);
|
||||||
// }
|
// }
|
||||||
//}
|
//}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -1,26 +1,26 @@
|
|||||||
using System;
|
using System;
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
using System.IO;
|
using System.IO;
|
||||||
using System.Linq;
|
using System.Linq;
|
||||||
using System.Text;
|
using System.Text;
|
||||||
using System.Threading.Tasks;
|
using System.Threading.Tasks;
|
||||||
|
|
||||||
namespace DotnetStreamsTests.Testables
|
namespace DotnetStandardStreamsTests.Testables
|
||||||
{
|
{
|
||||||
public class ListTextReader : TextReader
|
public class ListTextReader : TextReader
|
||||||
{
|
{
|
||||||
private readonly IEnumerator<string> enumerator;
|
private readonly IEnumerator<string> enumerator;
|
||||||
public ListTextReader(IEnumerable<string> data)
|
public ListTextReader(IEnumerable<string> data)
|
||||||
{
|
{
|
||||||
enumerator = data.GetEnumerator();
|
enumerator = data.GetEnumerator();
|
||||||
}
|
}
|
||||||
|
|
||||||
public override string ReadLine()
|
public override string ReadLine()
|
||||||
{
|
{
|
||||||
if (enumerator.MoveNext())
|
if (enumerator.MoveNext())
|
||||||
return enumerator.Current;
|
return enumerator.Current;
|
||||||
else
|
else
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -1,30 +1,30 @@
|
|||||||
using System;
|
using System;
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
using System.IO;
|
using System.IO;
|
||||||
using System.Linq;
|
using System.Linq;
|
||||||
using System.Text;
|
using System.Text;
|
||||||
using System.Threading.Tasks;
|
using System.Threading.Tasks;
|
||||||
|
|
||||||
namespace DotnetStreamsTests.Testables
|
namespace DotnetStandardStreamsTests.Testables
|
||||||
{
|
{
|
||||||
public class ListWriter : TextWriter
|
public class ListWriter : TextWriter
|
||||||
{
|
{
|
||||||
public List<string> Lines { get; }
|
public List<string> Lines { get; }
|
||||||
public override Encoding Encoding => Encoding.UTF8;
|
public override Encoding Encoding => Encoding.UTF8;
|
||||||
|
|
||||||
public ListWriter()
|
public ListWriter()
|
||||||
{
|
{
|
||||||
Lines = new();
|
Lines = new();
|
||||||
}
|
}
|
||||||
|
|
||||||
public override void Write(string value)
|
public override void Write(string value)
|
||||||
{
|
{
|
||||||
Lines.Add(value);
|
Lines.Add(value);
|
||||||
}
|
}
|
||||||
|
|
||||||
public override void WriteLine(string value)
|
public override void WriteLine(string value)
|
||||||
{
|
{
|
||||||
Lines.Add(value);
|
Lines.Add(value);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -1,24 +1,24 @@
|
|||||||
using System;
|
using System;
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
using System.IO;
|
using System.IO;
|
||||||
using System.Text;
|
using System.Text;
|
||||||
using DotnetStreams;
|
using DotnetStandardStreams;
|
||||||
|
|
||||||
namespace DotnetStreamsTests.Testables
|
namespace DotnetStandardStreamsTests.Testables
|
||||||
{
|
{
|
||||||
public class TestableConsoleOutputTarget : ConsoleOutputTarget, IDisposable
|
public class TestableConsoleOutputTarget : ConsoleOutputTarget, IDisposable
|
||||||
{
|
{
|
||||||
private readonly TextWriter previousOutWriter;
|
private readonly TextWriter previousOutWriter;
|
||||||
public TestableConsoleOutputTarget(TextWriter outWriter) : base()
|
public TestableConsoleOutputTarget(TextWriter outWriter) : base()
|
||||||
{
|
{
|
||||||
previousOutWriter = System.Console.Out;
|
previousOutWriter = System.Console.Out;
|
||||||
System.Console.SetOut(outWriter);
|
System.Console.SetOut(outWriter);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void Dispose()
|
public void Dispose()
|
||||||
{
|
{
|
||||||
|
|
||||||
System.Console.SetOut(previousOutWriter);
|
System.Console.SetOut(previousOutWriter);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Loading…
Reference in New Issue
Block a user