Skip to content
Snippets Groups Projects
Program.cs 2 KiB
Newer Older
  • Learn to ignore specific revisions
  • Vojtěch Moravec's avatar
    Vojtěch Moravec committed
    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.");
            }
        }
    }