private void DoSearch()
{
// Error checking
if (Directory.Exists(txtCodePath.Text) == false) // does the directory exist?
{
MessageBox.Show("You need to specify a valid path", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
return;
}
else if (txtSearch.Text.Length == 0) // if the search term > 0
{
MessageBox.Show("You need to specify a valid search term", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
return;
}
else if (txtCodePath.Text.ToLower().Contains("test") && optIgnoreTest.Checked == true)
{
MessageBox.Show("Your patch contains test and you have the ignore directories and files with test option selected." + Environment.NewLine + "No results will be found, cancelling search!", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
return;
}
string[] strExts = txtExt.Text.Split (';'); // is the defined extensions list the correct format?
if (!(strExts.Length == 1 && strExts[0] == "*"))
{
foreach (string strExt in strExts)
{
if (strExt.StartsWith("*.") == false)
{
MessageBox.Show(txtExt.Text + " is not in the correct format\nPlease use *.ext1;*.ext2\n", "Incorrect format", MessageBoxButtons.OK, MessageBoxIcon.Error);
return;
}
}
}
AddAndSaveCodePath();
txtSearch.Items.Insert(0, txtSearch.Text);
if (txtSearch.Items.Count > 20)
{
while (txtSearch.Items.Count > 20)
{
txtSearch.Items.RemoveAt(20);
}
}
// This saves the search strings so they are persistent over runs
StringCollection strSearchStringCollection = new StringCollection();
foreach (String strSearchString in txtSearch.Items)
{
strSearchStringCollection.Add(strSearchString);
}
Properties.Settings.Default.SearchStrings = strSearchStringCollection;
Properties.Settings.Default.Extensions = txtExt.Text;
Properties.Settings.Default.Save();
// ISSUE 1: https://github.com/nccgroup/ncccodenavi/issues/1
// Prompt the user to automatically escape
string strSearchText = null;
if (opRegexSearch.Checked == true)
{
//if (MessageBox.Show("You have regex search enabled. Do you want me to escape your search term automatically to result in a literal search?", "Regex escape?", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
//{
//if (opRegexSearch.Checked == true)
//{
strSearchText = Regex.Escape(txtSearch.Text);
//}
//}
//else
//{
strSearchText = txtSearch.Text;
//}
}
else
{
strSearchText = txtSearch.Text;
}
// Now initalize a search form
frmSearch frmSearch = new frmSearch(strSearchText + " in " + txtCodePath.Text + " (Regex:"+opRegexSearch.Checked+",Case:"+opCaseSearch.Checked+",Ignore Test:" + optIgnoreTest.Checked+",Ignore Comments:"+ optIgnoreComments.Checked+") - " + txtExt.Text, this);
frmSearch.MdiParent = this;
frmSearch.Visible = true;
// Now initialize the object and start a scan
Scanner scanYoink = new Scanner(frmSearch, txtCodePath.Text, strSearchText, optIgnoreComments.Checked, opRegexSearch.Checked, opCaseSearch.Checked, optIgnoreTest.Checked, txtExt.Text, richExclusions.Lines);
frmSearch.SetScanEngine(scanYoink);
scanYoink.Start(this, frmSearch);
}