private void cmdOpeninCodeView_Click(object sender, EventArgs e) { StringBuilder sbTemp = new StringBuilder(); if (treeFiles.SelectedNode == null) { return; } sbTemp.Append(treeFiles.SelectedNode.Text); TreeNode treeNodeTmp = treeFiles.SelectedNode; while (true) { if (treeNodeTmp.Parent == null) { break; } treeNodeTmp = treeNodeTmp.Parent; sbTemp.Insert(0, treeNodeTmp.Text + "\\"); } if (File.Exists(sbTemp.ToString()) == true) { byte[] fileBytes = null; try { fileBytes = File.ReadAllBytes(sbTemp.ToString()); } catch (Exception) { } Encoding encodingForFile = null; if (IsTextTester.IsText(out encodingForFile, sbTemp.ToString(), 1000) == true && fileBytes != null) { // Now initalize a search form frmCodeViewNew frmSearch = new frmCodeViewNew(sbTemp.ToString(), 0, frmMaster); frmSearch.MdiParent = this.MdiParent; frmSearch.Visible = true; } else if (IsTextTester.IsText(out encodingForFile, sbTemp.ToString(), 100) == false && fileBytes != null) { this.scintilla.Text = sbTemp.ToString() + " is binary and can't be displayed"; } else if (fileBytes == null) { this.scintilla.Text = sbTemp.ToString() + " could not be opened"; } } }
private void treeFiles_AfterSelect(object sender, TreeViewEventArgs e) { StringBuilder sbTemp = new StringBuilder(); sbTemp.Append(treeFiles.SelectedNode.Text); TreeNode treeNodeTmp = treeFiles.SelectedNode; while (true) { if (treeNodeTmp.Parent == null) { break; } treeNodeTmp = treeNodeTmp.Parent; sbTemp.Insert(0, treeNodeTmp.Text + "\\"); } this.scintilla.IsReadOnly = false; if (File.Exists(sbTemp.ToString()) == true) { byte[] fileBytes = null; try { strCurrFile = sbTemp.ToString(); fileBytes = File.ReadAllBytes(sbTemp.ToString()); } catch (Exception) { } Encoding encodingForFile = null; if (IsTextTester.IsText(out encodingForFile, sbTemp.ToString(), 100) == true && fileBytes != null) { this.scintilla.Text = File.ReadAllText(sbTemp.ToString(), encodingForFile); try { Console.WriteLine(frmMain.AssemblyDirectory + "\\NCCCodeNavi.CodeHighlighting\\" + Path.GetExtension(sbTemp.ToString()).Substring(1) + ".xml"); if (File.Exists(frmMain.AssemblyDirectory + "\\NCCCodeNavi.CodeHighlighting\\" + Path.GetExtension(sbTemp.ToString()).Substring(1) + ".xml")) { this.scintilla.ConfigurationManager.IsBuiltInEnabled = false; this.scintilla.ConfigurationManager.CustomLocation = frmMain.AssemblyDirectory + "\\NCCCodeNavi.CodeHighlighting\\"; this.scintilla.ConfigurationManager.Language = "default"; scintilla.Lexing.LexerLanguageMap[Path.GetExtension(sbTemp.ToString()).Substring(1)] = "cpp"; // probably a bit too dirty in the long term this.scintilla.ConfigurationManager.Language = Path.GetExtension(sbTemp.ToString()).Substring(1); if (Path.GetExtension(sbTemp.ToString()).Substring(1).ToLower().Equals("cs")) { this.scintilla.Indentation.SmartIndentType = SmartIndent.CPP; } if (Path.GetExtension(sbTemp.ToString()).Substring(1).ToLower().Equals("c")) { this.scintilla.Indentation.SmartIndentType = SmartIndent.CPP; } } else { if (Path.GetExtension(sbTemp.ToString()).Substring(1).ToLower().Equals("cs")) { this.scintilla.Indentation.SmartIndentType = SmartIndent.CPP; } if (Path.GetExtension(sbTemp.ToString()).Substring(1).ToLower().Equals("c")) { this.scintilla.Indentation.SmartIndentType = SmartIndent.CPP; } this.scintilla.ConfigurationManager.IsBuiltInEnabled = true; this.scintilla.ConfigurationManager.Configure(); } } catch (Exception) { this.scintilla.ConfigurationManager.IsBuiltInEnabled = true; try { if (Path.GetExtension(sbTemp.ToString()).Substring(1).ToLower().Equals("cs")) { this.scintilla.Indentation.SmartIndentType = SmartIndent.CPP; } if (Path.GetExtension(sbTemp.ToString()).Substring(1).ToLower().Equals("c")) { this.scintilla.Indentation.SmartIndentType = SmartIndent.CPP; } this.scintilla.ConfigurationManager.Language = Path.GetExtension(sbTemp.ToString()).Substring(1); } catch (Exception) { } this.scintilla.ConfigurationManager.Configure(); } } else if (IsTextTester.IsText(out encodingForFile, sbTemp.ToString(), 100) == false && fileBytes != null) { this.scintilla.Text = sbTemp.ToString() + " is binary and can't be displayed"; } else if (fileBytes == null) { this.scintilla.Text = sbTemp.ToString() + " could not be opened"; } } else if (Directory.Exists(sbTemp.ToString())) { this.scintilla.Text = sbTemp.ToString() + " is a directory"; } else { this.scintilla.Text = sbTemp.ToString() + " is unknown"; } this.scintilla.IsReadOnly = true; }
/// <summary> /// Scan a particular file /// </summary> /// <param name="strFile">the file we wish to scan</param> /// <param name="strAPIs">an array of regular expressions we've loaded</param> /// <returns></returns> private bool ScanFile(string strFile) { if (engineLocal.bStopped == true || engineLocal.intFinds > 2000) { engineLocal.LowerQueueCount(); return(false); } try { byte[] fileBytes = File.ReadAllBytes(strFile); Encoding encodingForFile = null; if (IsTextTester.IsText(out encodingForFile, strFile.ToString(), 100) == false) { return(false); } string[] strLines = File.ReadAllLines(strFile, encodingForFile); FileInfo fInfo = new FileInfo(strFile); int intCount = 0; foreach (string strLine in strLines) { intCount++; if (engineLocal.bStopped == true) { engineLocal.LowerQueueCount(); return(false); } try { Match commentregexMatch = null; if (bComments == true) { try { foreach (string strComRegex in strCommentsRegex) { commentregexMatch = Regex.Match(strLine, strComRegex); if (commentregexMatch.Success == true) { break; } } } catch (Exception) { } } if (bComments != true || commentregexMatch == null || (commentregexMatch != null && commentregexMatch.Success == false)) { if (strAPIs != null) // We're doing a grepify scan { foreach (string strRegex in strAPIs) { if (engineLocal.bStopped == true) { engineLocal.LowerQueueCount(); return(false); } else { Match regexMatch = null; if (bCase == true) { regexMatch = Regex.Match(strLine, strRegex, RegexOptions.IgnoreCase); } else { regexMatch = Regex.Match(strLine, strRegex); } if (regexMatch != null && regexMatch.Success) { if (frmSearch.IsDisposed == false) { frmSearch.UpdateList(fInfo.DirectoryName, fInfo.Name, fInfo.Extension, intCount, strLine, strRegex); } lock (engineLocal.objCount) { engineLocal.intFinds++; if (engineLocal.intFinds == 2000) { MessageBox.Show("2000 instances found stopping scan", "Stopping scan", MessageBoxButtons.OK, MessageBoxIcon.Information); } } } } } } else if (bRegex == true && strAPIs == null) // Standard term search but with regex { Match regexMatch = null; if (bCase == true) { regexMatch = Regex.Match(strLine, strTerm, RegexOptions.IgnoreCase); } else { regexMatch = Regex.Match(strLine, strTerm); } if (regexMatch != null && regexMatch.Success) { // Update the GUI frmSearch.UpdateList(fInfo.DirectoryName, fInfo.Name, fInfo.Extension, intCount, strLine); lock (engineLocal.objCount) { engineLocal.intFinds++; if (engineLocal.intFinds == 2000) { MessageBox.Show("2000 instances found stopping scan", "Stopping scan", MessageBoxButtons.OK, MessageBoxIcon.Information); } } } } else if (strAPIs == null) // Standard term search without regex { if (strLine.Contains(strTerm) && frmSearch.IsDisposed == false) { frmSearch.UpdateList(fInfo.DirectoryName, fInfo.Name, fInfo.Extension, intCount, strLine); lock (engineLocal.objCount) { engineLocal.intFinds++; if (engineLocal.intFinds == 2000) { MessageBox.Show("2000 instances found stopping scan", "Stopping scan", MessageBoxButtons.OK, MessageBoxIcon.Information); } } } else if (frmSearch.IsDisposed == true) { } } } } catch (Exception) { } } fInfo = null; strLines = null; } catch (Exception) { } finally { engineLocal.LowerQueueCount(); } return(true); }
private void treeFiles_AfterSelect(object sender, TreeViewEventArgs e) { StringBuilder sbTemp = new StringBuilder(); sbTemp.Append(treeFiles.SelectedNode.Text); TreeNode treeNodeTmp = treeFiles.SelectedNode; while (true) { if (treeNodeTmp.Parent == null) { break; } treeNodeTmp = treeNodeTmp.Parent; sbTemp.Insert(0, treeNodeTmp.Text + "\\"); } this.scintilla.IsReadOnly = false; if (File.Exists(sbTemp.ToString()) == true) { byte[] fileBytes = null; try { strCurrFile = sbTemp.ToString(); fileBytes = File.ReadAllBytes(sbTemp.ToString()); } catch (Exception) { } Encoding encodingForFile = null; if (IsTextTester.IsText(out encodingForFile, sbTemp.ToString(), 100) == true && fileBytes != null) { this.scintilla.Text = File.ReadAllText(sbTemp.ToString(), encodingForFile); try { this.scintilla.ConfigurationManager.Language = Path.GetExtension(sbTemp.ToString()).Substring(1); if (Path.GetExtension(sbTemp.ToString()).Substring(1).ToLower().Equals("c")) { this.scintilla.ConfigurationManager.Language = "cpp"; this.scintilla.Indentation.SmartIndentType = SmartIndent.CPP; } if (Path.GetExtension(sbTemp.ToString()).Substring(1).ToLower().Equals("py")) { this.scintilla.ConfigurationManager.Language = "python"; } } catch (Exception) { } } else if (IsTextTester.IsText(out encodingForFile, sbTemp.ToString(), 100) == false && fileBytes != null) { this.scintilla.Text = sbTemp.ToString() + " is binary and can't be displayed"; } else if (fileBytes == null) { this.scintilla.Text = sbTemp.ToString() + " could not be opened"; } } else if (Directory.Exists(sbTemp.ToString())) { this.scintilla.Text = sbTemp.ToString() + " is a directory"; } else { this.scintilla.Text = sbTemp.ToString() + " is unknown"; } this.scintilla.IsReadOnly = true; }
/// <summary> /// Scan a particular file /// </summary> /// <param name="strFile">the file we wish to scan</param> /// <param name="strAPIs">an array of regular expressions we've loaded</param> /// <returns></returns> private bool ScanFile(string strFile) { if (engineLocal.bStopped == true || engineLocal.intFinds > Properties.Settings.Default.MaxResults) { engineLocal.LowerQueueCount(); return(false); } try { byte[] fileBytes = File.ReadAllBytes(strFile); Encoding encodingForFile = null; if (IsTextTester.IsText(out encodingForFile, strFile.ToString(), 100) == false) { return(false); } FileInfo fInfo = new FileInfo(strFile); int intCount = 0; foreach (string strLine in File.ReadLines(strFile, encodingForFile)) { intCount++; if (engineLocal.bStopped == true) { engineLocal.LowerQueueCount(); return(false); } try { Match commentregexMatch = null; if (bComments == true) { try { foreach (string strComRegex in strCommentsRegex) { commentregexMatch = Regex.Match(strLine, strComRegex); if (commentregexMatch.Success == true) { break; } } } catch (Exception) { } } if (bComments != true || commentregexMatch == null || (commentregexMatch != null && commentregexMatch.Success == false)) { if (strAPIs != null || lstChecks != null) // We're doing a grepify scan { // V1 if (strAPIs != null) { foreach (string strRegex in strAPIs) { if (engineLocal.bStopped == true) { engineLocal.LowerQueueCount(); return(false); } else { Match regexMatch = null; if (bCase == false) { regexMatch = Regex.Match(strLine, strRegex, RegexOptions.IgnoreCase); } else { regexMatch = Regex.Match(strLine, strRegex); } if (regexMatch != null && regexMatch.Success) { if (frmSearch.IsDisposed == false) { frmSearch.UpdateList(fInfo.DirectoryName, fInfo.Name, fInfo.Extension, intCount, strLine, strRegex, null); } lock (engineLocal.objCount) { engineLocal.intFinds++; if (engineLocal.intFinds == Properties.Settings.Default.MaxResults) { MessageBox.Show("Maximum results found stopping scan", "Stopping scan", MessageBoxButtons.OK, MessageBoxIcon.Information); } } } } } } // V2 if (lstChecks != null) { foreach (Grepifyv2Check gv2Check in lstChecks) { string[] strExts = gv2Check.strExts.Split(';'); bool bFound = false; foreach (string strExt in strExts) { try { if (Path.GetExtension(strFile).EndsWith(strExt.Split('.')[1]) == true) { bFound = true; } } catch (Exception) { } } if (engineLocal.bStopped == true) { engineLocal.LowerQueueCount(); return(false); } else if (bFound == true) { Match regexMatch = null; if (bCase == false) { regexMatch = Regex.Match(strLine, gv2Check.strRegex, RegexOptions.IgnoreCase); } else { regexMatch = Regex.Match(strLine, gv2Check.strRegex); } if (regexMatch != null && regexMatch.Success) { if (frmSearch.IsDisposed == false) { frmSearch.UpdateList(fInfo.DirectoryName, fInfo.Name, fInfo.Extension, intCount, strLine, null, gv2Check); } lock (engineLocal.objCount) { engineLocal.intFinds++; if (engineLocal.intFinds == Properties.Settings.Default.MaxResults) { MessageBox.Show("Maximum results found stopping scan", "Stopping scan", MessageBoxButtons.OK, MessageBoxIcon.Information); } } } } } } } else if (bRegex == true && strAPIs == null && lstChecks == null) // Standard term search but with regex { Match regexMatch = null; if (bCase == false) { regexMatch = Regex.Match(strLine, strTerm, RegexOptions.IgnoreCase); } else { regexMatch = Regex.Match(strLine, strTerm); } if (regexMatch != null && regexMatch.Success) { // Update the GUI frmSearch.UpdateList(fInfo.DirectoryName, fInfo.Name, fInfo.Extension, intCount, strLine); lock (engineLocal.objCount) { engineLocal.intFinds++; if (engineLocal.intFinds == Properties.Settings.Default.MaxResults) { MessageBox.Show("Maximum results found stopping scan", "Stopping scan", MessageBoxButtons.OK, MessageBoxIcon.Information); } } } } else if (strAPIs == null) // Standard term search without regex { if (bCase == true && strLine.Contains(strTerm) && frmSearch.IsDisposed == false) { frmSearch.UpdateList(fInfo.DirectoryName, fInfo.Name, fInfo.Extension, intCount, strLine); lock (engineLocal.objCount) { engineLocal.intFinds++; if (engineLocal.intFinds == Properties.Settings.Default.MaxResults) { MessageBox.Show("Maximum results found stopping scan", "Stopping scan", MessageBoxButtons.OK, MessageBoxIcon.Information); } } } else if (bCase == false && strLine.ToLower().Contains(strTerm.ToLower()) && frmSearch.IsDisposed == false) { frmSearch.UpdateList(fInfo.DirectoryName, fInfo.Name, fInfo.Extension, intCount, strLine); lock (engineLocal.objCount) { engineLocal.intFinds++; if (engineLocal.intFinds == Properties.Settings.Default.MaxResults) { MessageBox.Show("Maximum results found stopping scan", "Stopping scan", MessageBoxButtons.OK, MessageBoxIcon.Information); } } } else if (frmSearch.IsDisposed == true) { } } } } catch (Exception) { } } fInfo = null; } catch (Exception) { } finally { engineLocal.LowerQueueCount(); } return(true); }