/// <summary> /// Searches the current ByteProvider /// </summary> /// <param name="options">contains all find options</param> /// <returns>the SelectionStart property value if find was successful or /// -1 if there is no match /// -2 if Find was aborted.</returns> public long FindPrev(FindOptions options) { var startIndex = SelectionStart; int match = 0; byte[] buffer1 = null; byte[] buffer2 = null; if (options.Type == FindType.Text && options.MatchCase) { if (options.FindBuffer == null || options.FindBuffer.Length == 0) throw new ArgumentException("FindBuffer can not be null when Type: Text and MatchCase: false"); buffer1 = options.FindBuffer; } else if (options.Type == FindType.Text && !options.MatchCase) { if (options.FindBufferLowerCase == null || options.FindBufferLowerCase.Length == 0) throw new ArgumentException("FindBufferLowerCase can not be null when Type is Text and MatchCase is true"); if (options.FindBufferUpperCase == null || options.FindBufferUpperCase.Length == 0) throw new ArgumentException("FindBufferUpperCase can not be null when Type is Text and MatchCase is true"); if (options.FindBufferLowerCase.Length != options.FindBufferUpperCase.Length) throw new ArgumentException("FindBufferUpperCase and FindBufferUpperCase must have the same size when Type is Text and MatchCase is true"); buffer1 = options.FindBufferLowerCase; buffer2 = options.FindBufferUpperCase; } else if (options.Type == FindType.Hex) { if (options.Hex == null || options.Hex.Length == 0) { return -2; //throw new ArgumentException("Hex can not be null when Type is Hex"); } buffer1 = options.Hex; } int buffer1Length = buffer1.Length; _abortFind = false; for (long pos = startIndex; pos >= 0; pos--) { if (_abortFind) return -2; if (pos % 1000 == 0) // for performance reasons: DoEvents only 1 times per 1000 loops Application.DoEvents(); byte compareByte = _byteProvider.ReadByte(pos); bool buffer1Match = compareByte == buffer1[buffer1.Length - 1 - match]; bool hasBuffer2 = buffer2 != null; bool buffer2Match = hasBuffer2 ? compareByte == buffer2[buffer2.Length - 1 - match] : false; bool isMatch = buffer1Match || buffer2Match; if (!isMatch) { pos += match; match = 0; _findingPos = pos; continue; } match++; if (match == buffer1Length) { long bytePos = pos; Select(bytePos, buffer1Length); ScrollByteIntoView(_bytePos + _selectionLength); ScrollByteIntoView(_bytePos); return bytePos; } } return -1; }
void FindNext() { if (_findBuffer.Length == 0) { Find(); return; } // show cancel dialog _formFindCancel = new FormFindCancel(); _formFindCancel.SetHexBox(hexBox1); _formFindCancel.Closed += new EventHandler(FormFindCancel_Closed); _formFindCancel.Show(); // block activation of main form //Activated += new EventHandler(FocusToFormFindCancel); // start find process FindOptions fo = new FindOptions(); fo.Hex = _findBuffer; fo.Type = FindType.Hex; //long res = hexBox1.Find(_findBuffer, hexBox1.SelectionStart + hexBox1.SelectionLength); long res = hexBox1.Find(fo); _formFindCancel.Dispose(); // unblock activation of main form //Activated -= new EventHandler(FocusToFormFindCancel); if (res == -1) // -1 = no match { MessageBox.Show("Find reached end of file", "EDC15P Suite", MessageBoxButtons.OK, MessageBoxIcon.Information); } else if (res == -2) // -2 = find was aborted { return; } else // something was found { if (!hexBox1.Focused) hexBox1.Focus(); } ManageAbility(); }