Repository navigation
Expand file tree
/
Copy pathConsoleEngineBase.cs
More file actions
executable file
·95 lines (84 loc) · 2.67 KB
/
Copy pathConsoleEngineBase.cs
File metadata and controls
executable file
·95 lines (84 loc) · 2.67 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
using System;
using System.IO;
namespace Utils
{
public abstract class ConsoleEngineBase
{
private string[] m_args;
protected bool ReadInput = true;
protected System.IO.TextReader In = null;
protected System.IO.TextWriter Out = null;
protected System.IO.TextWriter Error = null;
public ConsoleEngineBase()
{
//by default, read from/write to standard streams
this.In = System.Console.In;
this.Out = System.Console.Out;
this.Error = System.Console.Error;
}
public void Main(string[] args)
{
this.m_args = args;
if (this.ValidateArguments())
{
this.PreProcess();
if (this.ReadInput)
{
string currentLine = this.In.ReadLine();
while (currentLine != null)
{
this.ProcessLine(currentLine.Trim());
if(this.In.Peek() == -1) break;
currentLine = this.In.ReadLine();
}
}
this.PostProcess();
}
else
this.Error.Write("Usage: " + this.Usage());
}
public void Main(string[] args,
System.IO.TextReader In,
System.IO.TextWriter Out,
System.IO.TextWriter Error)
{
//this version of Main allows alternate streams
this.In = In;
this.Out = Out;
this.Error = Error;
this.Main(args);
}
protected virtual bool ValidateArguments()
{
//override this to add custom argument checking
return true;
}
protected virtual string Usage()
{
//override this to add custom usage statement
return "";
}
protected virtual void PreProcess()
{
//override this to add custom logic that
//executes just before standard in is processed
return;
}
protected virtual void PostProcess()
{
//override this to add custom logic that
//executes just after standard in is processed
return;
}
protected virtual void ProcessLine(string line)
{
//override this to add custom processing
//on each line of input
return;
}
protected string[] Arguments
{
get { return this.m_args; }
}
}
}