DotnetStandardStreams/source/DotnetStreamsApp/DotnetStreams.cs
2025-05-15 13:27:49 -06:00

298 lines
6.2 KiB
C#

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<string> ReadAll();
// string Read();
// bool Eof();
// void Close();
//}
//public class StdInTextSource : ITextSource
//{
// protected bool isEof;
// public virtual void Open()
// {
// isEof = false;
// }
// public virtual IEnumerable<string> 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<string> 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<string> source;
// private IEnumerator<string>? enumerator;
// //private string lastValue;
// private bool firstIsRead;
// private string nextLine;
// public ListTextSource(IEnumerable<string> source)
// {
// this.source = source;
// enumerator = null;
// firstIsRead = false;
// nextLine = string.Empty;
// }
// public virtual void Open()
// {
// }
// public virtual IEnumerable<string> 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<string, string> processorFunc;
// public ProcessedConsoleOutputTarget(Func<string, string> 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<string> OutputList { get; }
// public ListOutputTarget(IList<string> 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<byte[], int> dataReceiverProc;
// protected readonly Action<int> doneProc;
// public event BytesReadEventHandler OnBytesRead;
// public event EventHandler OnDone;
// public BinaryStdinReader(Action<byte[], int> dataReceiverProc, Action<int> doneProc)
// {
// this.dataReceiverProc = dataReceiverProc;
// this.doneProc = doneProc;
// }
// public BinaryStdinReader()
// {
// }
// public void ReadBytes(Action<byte[], int> receiverProc, Action<int> 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);
// }
//}
}