-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathOdinSearch_OutputSimpleConsole.cs
238 lines (212 loc) · 8.39 KB
/
OdinSearch_OutputSimpleConsole.cs
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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace OdinSearchEngine.OdinSearch_OutputConsumerTools
{
/// <summary>
/// Output matches to a TextWriter or a Stream or a File. Default action is send positive match text to stdout and error text to stderr.
/// One can tell it to just send the matched location itself by setting custom arg OutputOnlyFileName to true.
/// Telling this to output to something other than the console likely is gonna need FlushAlways also set.
/// </summary>
/// <remarks>This class is intended to be an Example. There's not plans to modify this currently unless there's issues / bug fixes. Use <see cref="StreamWriterCommonBased.OdinSearch_OutputConsole"/> for additonal needs</remarks>
public class OdinSearch_OutputSimpleConsole : OdinSearch_OutputConsumerBase
{
/// <summary>
/// Set the argument to a string to place output to that file in unicode. If a stream or TestWriter, writes directly to that.
/// </summary>
public const string MatchStream = "STDOUT";
/// <summary>
/// Set the argument to a string to place output to that file in unicode. If a stream or TestWriter, writes directly to that.
/// </summary>
public const string BlockStream = "STDERR";
/// <summary>
/// Default action is stdout/stderr is assumed to always flush but streams are flushed on SearchDone(). Set this to flush after each call regardless
/// </summary>
public const string FlushAlways = "FLUSHALWAYS";
/// <summary>
/// Optional and default is false: If set to bool, we output *only* filename for matches rather than File Match *a @ this
/// </summary>
/// <example>
/// A Match is found at C:\\Windows\\notepad.exe.
///
/// JUSTTHENAME Set would then cause this to output "C:\\Windows\\notepad.exe".
/// JUSTTHENAME unspecified or clear would calse this to output ' filematch "notepad.exe" @ "C:\\Windows\\notepad.exe"'
/// </example>
public const string OutputOnlyFileName = "JUSTTHENAME";
#pragma warning disable IDE0052 // Remove unread private members
// Suppression due to the noise, these hold the streams that stdout and stderr deal with
Stream outstream, errstream;
TextWriter stdout, stderr;
bool OutputOnlyName = false;
bool FlushAlwaysFlag = false;
bool DisposeOutStream = false;
bool DisploseErrStream = false;
#pragma warning restore IDE0052
public OdinSearch_OutputSimpleConsole()
{
this[MatchStream] = Console.Out;
this[BlockStream] = Console.Error;
// yay defaults.
stdout = Console.Out;
stderr = Console.Error;
FlushAlwaysFlag = false;
}
public override void AllDone()
{
if (FlushAlwaysFlag)
{
errstream?.Flush();
outstream?.Flush();
}
base.AllDone();
}
/// <summary>
/// Begin the search, parse our custom arguments and set defaults.
/// </summary>
/// <param name="Start">This is the DateTime of now when the search starts</param>
/// <returns>returns same thing as <see cref="OdinSearch_OutputConsumerBase.SearchBegin(DateTime)"/><returns>
/// <exception cref="InvalidOperationException"></exception>
/// <remarks> see also <see cref="OdinSearch_OutputConsumerBase.SearchBegin(DateTime)"/></remarks>
public override bool SearchBegin(DateTime Start)
{
string[] Custom = this.GetCustomParameterNames();
if (Custom.Contains(MatchStream))
{
string test_string = this[MatchStream] as string;
if (test_string != null)
{
outstream = File.OpenWrite(test_string);
DisposeOutStream = true;
}
else
{
stdout = this[MatchStream] as TextWriter;
if (stdout == null)
{
outstream = this[MatchStream] as Stream;
if (outstream == null)
{
throw new InvalidOperationException("Invalid Argument for MatchStream. Expected a string for a file, a StreamWRiter or a textwriter for stream");
}
}
}
}
if (Custom.Contains(BlockStream))
{
string test_string = this[BlockStream] as string;
if (test_string != null)
{
errstream = File.OpenWrite(test_string);
DisploseErrStream = true;
}
stderr = this[BlockStream] as TextWriter;
if (stderr == null)
{
errstream = this[BlockStream] as Stream;
if (errstream == null)
{
throw new InvalidOperationException("Invalid Argument for BlockStream. Expected a string for a file, a Stream or a textwriter for stream");
}
}
}
if (Custom.Contains(OutputOnlyFileName))
{
bool result;
try
{
result = (bool)this[OutputOnlyFileName];
}
catch (Exception e)
{
throw new InvalidOperationException("Invalid argument for output control flag. Expected true or false value", e);
}
OutputOnlyName = result;
}
if (Custom.Contains(FlushAlways))
{
bool result;
try
{
result = (bool)this[FlushAlways];
}
catch (Exception e)
{
throw new InvalidOperationException("Invalid argument for Flush Always flag. Expected true or false value", e);
}
}
return base.SearchBegin(Start);
}
public override void WasNotMatched(FileSystemInfo info)
{
base.WasNotMatched(info);
}
public override void Match(FileSystemInfo info)
{
if (!OutputOnlyName)
{
if (stdout != null)
{
stdout.WriteLine("File Match: \"{0}\" @ \"{1}\"", info.Name, info.FullName);
}
else
{
byte[] b = Encoding.UTF8.GetBytes(string.Format("File Match: \"{0}\" @ \"{1}\"" + "\r\n", info.Name, info.FullName));
outstream.Write(b, 0, b.Length);
if (FlushAlwaysFlag)
{
outstream.Flush();
}
}
}
else
{
if (stdout != null)
{
stdout.WriteLine("{0}\r\n", info.FullName);
}
else
{
byte[] b = Encoding.UTF8.GetBytes(info.FullName + "\r\n");
outstream.Write(b, 0, b.Length);
if (FlushAlwaysFlag)
{
outstream.Flush();
}
}
}
base.Match(info);
}
public override void Blocked(string Blocked)
{
stderr.WriteLine(Blocked);
base.Blocked(Blocked);
}
public override void Messaging(string Message)
{
stdout.WriteLine(Message);
base.Messaging(Message);
}
protected override void Dispose(bool disposing)
{
if (disposing)
{
if ( DisposeOutStream)
{
outstream?.Dispose();
}
if (DisploseErrStream)
{
errstream?.Dispose();
}
}
base.Dispose(disposing);
}
~OdinSearch_OutputSimpleConsole()
{
Dispose(true);
}
}
}