Example #1
0
        async Task <bool> BinarySearchFileAsync(string fileName, Searcher searcher, AnswerResult answer, IProgress <ProgressReport> progress, CancellationToken cancel)
        {
            try
            {
                if (answer.Answer == MessageOptions.Cancel)
                {
                    return(true);
                }

                if (Directory.Exists(fileName))
                {
                    return(false);
                }

                var findLen = searcher.MaxLen;
                if (findLen == 0)
                {
                    return(false);
                }
                var buffer = new byte[8192];
                var used   = 0;
                using (var stream = File.OpenRead(fileName))
                    while (!cancel.IsCancellationRequested)
                    {
                        var block = await stream.ReadAsync(buffer, used, buffer.Length - used);

                        if (block == 0)
                        {
                            break;
                        }
                        used += block;
                        progress.Report(new ProgressReport(stream.Position, stream.Length));

                        var result = searcher.Find(buffer, 0, used, true);
                        if (result.Any())
                        {
                            return(true);
                        }

                        var keep = Math.Min(used, findLen - 1);
                        Array.Copy(buffer, used - keep, buffer, 0, keep);
                        used = keep;
                    }

                return(false);
            }
            catch (Exception ex)
            {
                TabsParent.Dispatcher.Invoke(() =>
                {
                    if ((answer.Answer != MessageOptions.YesToAll) && (answer.Answer != MessageOptions.NoToAll))
                    {
                        answer.Answer = new Message(WindowParent)
                        {
                            Title         = "Confirm",
                            Text          = $"Unable to read {fileName}.\n\n{ex.Message}\n\nLeave selected?",
                            Options       = MessageOptions.YesNoYesAllNoAllCancel,
                            DefaultCancel = MessageOptions.Cancel,
                        }
                    }
                    .Show();
                });
Example #2
0
 public List <Tuple <int, int> > StringMatches(Searcher searcher, int offset, int length)
 {
     return(searcher.Find(Data, offset, length));
 }