Newer
Older
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
using System;
using System.Diagnostics;
using System.IO;
using System.Text;
using System.Text.RegularExpressions;
namespace czi_inspector
{
class Program
{
static void Main(string[] args)
{
string file = args[0];
string substring = args[1];
string encoding = args[2];
Regex regex = new Regex(substring, RegexOptions.IgnoreCase);
Encoding selectedEncoding = GetEncoding(encoding);
const int bufferSize = 1024;
byte[] buffer = new byte[bufferSize];
int count = 0;
Stopwatch stopwatch = new Stopwatch();
stopwatch.Start();
//var wholeFileBuffer = File.ReadAllBytes(file);
//string wholeFileString = selectedEncoding.GetString(wholeFileBuffer);
//count = regex.Matches(substring).Count;
using (FileStream reader = new FileStream(file, FileMode.Open, FileAccess.Read))
{
while (reader.Read(buffer, 0, bufferSize) > 0)
{
string bufferString = selectedEncoding.GetString(buffer);
var matches = regex.Matches(bufferString).Count;
count += matches;
if (matches > 0)
Console.WriteLine(bufferString);
}
}
stopwatch.Stop();
Console.WriteLine("Found: {0} occurencies of {1} in file {2} in {3} ms.", count, substring, file, stopwatch.Elapsed.TotalMilliseconds);
}
private static Encoding GetEncoding(string encoding)
{
encoding = encoding.ToUpper();
if (encoding == "ASCII") return Encoding.ASCII;
if (encoding == "UTF8") return Encoding.UTF8;
if (encoding == "UNICODE") return Encoding.Unicode;
if (encoding == "UTF32") return Encoding.UTF32;
throw new Exception("Try different encoding, currently supported: ASCII, UTF8, UTF32, UNICODE.");
}
}
}