DotnetStandardStreams/source/DotnetStandardStreamsApp/Program.cs

88 lines
1.7 KiB
C#

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