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); // } //} }