using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using System.Threading;
using System.Text.RegularExpressions;
namespace AutoSearchFile
{
public struct SearchInfo
{
public int line;
public int index;
public string filepath;
public string contentText;
public string author;
}
class Program
{
static List<string> logInfo = null;
static void Main(string[] args)
{
//string dir = @"E:\IandVAutoCase\com.huawei.netlab.swtbot.ci.main\src";
//string suffix = "*.java";
//string keyword = @"\btest[a-z0-9A-Z]+\(\)";
//string encodingString = "UTF-8";
//string needAuthor = "nkf65624";
string dir = args[0];
string suffix = args[1];
string keyword = args[2];
string encodingString = args[3];
string needAuthor = args[4];
Console.WriteLine(args[2]);
logInfo = new List<string>();
string[] filepath = FindALLFile.getAllFile(dir, suffix);
Thread[] serachThread = new Thread[filepath.Length];
//启动日志线程
Thread writeLogThread = new Thread(new ThreadStart(writelogtoFile));
writeLogThread.Start();
for (int i = 0; i < filepath.Length; i++)
{
string[] inputInfo = new string[5];
inputInfo[0] = filepath[i];
inputInfo[1] = keyword;
inputInfo[2] = dir;
inputInfo[3] = encodingString;
inputInfo[4] = needAuthor;
serachThread[i] = new Thread(new ParameterizedThreadStart(serach));
serachThread[i].Start(inputInfo);
}
while (isalive(serachThread))
{
Thread.Sleep(1000);
}
//结束写入线程
writeLogThread.Abort();
}
/// <summary>
/// 查询启动的线程是否都结束
/// </summary>
/// <param name="serachThread">所有查询线程</param>
/// <returns></returns>
static bool isalive(Thread[] serachThread)
{
for (int i = 0; i < serachThread.Length; i++)
{
if (serachThread[i].IsAlive)
{
return true;
}
}
return false;
}
/// <summary>
/// 写日志
/// </summary>
private static void writelogtoFile()
{
while (true)
{
while (logInfo.Count != 0)
{
lock (logInfo)
{
string log = logInfo[0];
File.AppendAllText("log.txt", log);
logInfo.Remove(log);
}
}
Thread.Sleep(100);
}
}
/// <summary>
/// 开始搜索并获取记录
/// </summary>
/// <param name="inputInfo"></param>
public static void serach(Object inputInfo)
{
string[] serachInfo = (String[])inputInfo;
List<SearchInfo> getall = SearchFile.search(serachInfo[0], serachInfo[1], serachInfo[2], serachInfo[3]);
for (int i = 0; i < getall.Count; i++)
{
if (serachInfo[4].ToLower() == "all")
{
string thistext = "文件名:" + getall[i].filepath.Substring(serachInfo[2].Length) + "\r\n行:" +
getall[i].line + "\t列:" + getall[i].index + "\t责任人:" + getall[i].author + "\r\n" +
"内容:" + getall[i].contentText + "\r\n";
lock (logInfo)
{
logInfo.Add(thistext);
}
Console.WriteLine(thistext);
}
else if (getall[i].author.ToLower() == serachInfo[4].ToLower())
{
string thistext = "文件名:" + getall[i].filepath.Substring(serachInfo[2].Length) + "\r\n行:" +
getall[i].line + "\t列:" + getall[i].index + "\t责任人:" + getall[i].author + "\r\n" +
"内容:" + getall[i].contentText + "\r\n";
lock (logInfo)
{
logInfo.Add(thistext);
}
Console.WriteLine(thistext);
}
}
}
}
public class SearchFile
{
/// <summary>
/// 获取信息
/// </summary>
/// <param name="filePath"></param>
/// <param name="keyWord"></param>
/// <param name="dir"></param>
/// <param name="encodingString"></param>
/// <returns></returns>
public static List<SearchInfo> search(string filePath, string keyWord, string dir, string encodingString)
{
List<SearchInfo> allSearchInfo= new List<SearchInfo>();
SearchInfo getSearchInfo = new SearchInfo();
String str = File.ReadAllText(filePath, Encoding.GetEncoding(encodingString));
string author = SearchFile.getAuthor(str);
List<int> allIndexs = SearchFile.getKeyWordIndexs(str, keyWord);
List<int[]> allRowAndCol = SearchFile.getRowCol(str, allIndexs);
List<string[]> allBeforeLineAndNextLine = SearchFile.getBeforeLineAndNextLine(filePath, allRowAndCol);
for (int i = 0; i < allBeforeLineAndNextLine.Count; i++)
{
getSearchInfo.line = allRowAndCol[i][0];
getSearchInfo.index = allRowAndCol[i][1];
getSearchInfo.filepath = filePath;
getSearchInfo.author = author;
getSearchInfo.contentText = allBeforeLineAndNextLine[i][0] + "\r\n" + allBeforeLineAndNextLine[i][1] + "\r\n" + allBeforeLineAndNextLine[i][2];
allSearchInfo.Add(getSearchInfo);
}
return allSearchInfo;
}
/// <summary>
/// 得到用例的作者
/// </summary>
/// <param name="str">文件内容</param>
/// <returns></returns>
public static string getAuthor(String str)
{
string author = "";
string key1 = " * author date description";
try
{
int index1 = str.IndexOf(key1);
if (index1 != -1)
{
string onekey2 = " * ";
string str1 = str.Substring(index1 + 50 + key1.Length, 50);
int index2 = str1.IndexOf(onekey2);
if (index1 != -1)
{
str1 = str1.Substring(str1.IndexOf(onekey2) + onekey2.Length);
author = str1.Split(' ')[0];
}
else
{
author = "";
}
}
}
catch (Exception e) { author = ""; }
return author;
}
/// <summary>
/// 获取该类中所有匹配项的索引
/// </summary>
/// <param name="str"></param>
/// <param name="keyWord"></param>
/// <returns></returns>
public static List<int> getKeyWordIndexs(string str, string keyWord)
{
List<int> allIndexs = new List<int>();
try
{
Regex r = new Regex(keyWord);
Match m = r.Match(str);
while (m.Value != "")
{
allIndexs.Add(m.Index);
m = m.NextMatch();
}
}
catch (Exception e) { }
return allIndexs;
}
/// <summary>
/// 获取行列
/// </summary>
/// <param name="str">文本</param>
/// <param name="allIndexs">所有匹配的索引</param>
/// <returns></returns>
public static List<int[]> getRowCol(String str, List<int> allIndexs)
{
List<int[]> allRowCol = new List<int[]>();
for (int i = 0; i < allIndexs.Count; i++)
{
int[] a = new int[2];
string needRow = str.Substring(0, allIndexs[i]);
a[0] = needRow.Split('\n').Length;
a[1] = allIndexs[i] - needRow.LastIndexOf("\n") + 3;
allRowCol.Add(a);
}
return allRowCol;
}
/// <summary>
/// 获取匹配行的上一行下一行
/// </summary>
/// <param name="filepath">文件路径</param>
/// <param name="allList">已经匹配的所有行列</param>
/// <returns></returns>
public static List<String[]> getBeforeLineAndNextLine(String filepath, List<int[]> allRowAndCol)
{
List<String[]> allBeforeAndNextLine = new List<String[]>();
string[] allLine = File.ReadAllLines(filepath, Encoding.GetEncoding("UTF-8"));
for (int i = 0; i < allRowAndCol.Count; i++)
{
String[] BeforeAndNextLine = new String[3];
try
{
BeforeAndNextLine[0] = allLine[allRowAndCol[i][0] - 2];
}
catch (Exception e)
{
BeforeAndNextLine[0] = "";
}
try
{
BeforeAndNextLine[1] = allLine[allRowAndCol[i][0] - 1];
}
catch (Exception e)
{
BeforeAndNextLine[1] = "";
}
try
{
BeforeAndNextLine[2] = allLine[allRowAndCol[i][0] - 0];
}
catch
{
BeforeAndNextLine[2] = "";
}
allBeforeAndNextLine.Add(BeforeAndNextLine);
}
return allBeforeAndNextLine;
}
}
public class FindALLFile
{
/// <summary>
/// 返回指定目录下指定后缀名字的所有文件
/// </summary>
/// <param name="dir">目录</param>
/// <param name="suffix">后缀名字如:*.java</param>
/// <returns>数组</returns>
public static string[] getAllFile(string dir, string suffix)
{
return Directory.GetFiles(dir, suffix, SearchOption.AllDirectories);
}
}
/*
* public class SearchFile
{
public static List<SearchInfo> search(string filePath, string keyWord, string dir, string encodingString)
{
string[] StrRead = File.ReadAllLines(filePath, Encoding.GetEncoding(encodingString));
List<SearchInfo> allInfo = new List<SearchInfo>();
string author = getAuthor(filePath);
for (int i = 0; i < StrRead.Length; i++)
{
SearchInfo searchInfo = new SearchInfo();
int index = getKeyWordIndex(StrRead[i], keyWord);
if (index != -1)
{
searchInfo.line = i;
searchInfo.index = index;
searchInfo.filepath = filePath.Substring(dir.Length);
//前后一行及本行
if (i > 1)
{
searchInfo.contentText = StrRead[i - 1] + "\r\n" + StrRead[i];
}
else
{
searchInfo.contentText = StrRead[i];
}
if (i < StrRead.Length - 2)
{
searchInfo.contentText += "\r\n" + StrRead[i + 1];
}
searchInfo.author = author;
allInfo.Add(searchInfo);
判断content中的index>7
//if (ismorethan(keyWord, searchInfo.contentText, 6) != null)
//{
// searchInfo.author = author;
// allInfo.Add(searchInfo);
//}
}
}
return allInfo;
}
private static string ismorethan(string keyWord, string content, int defineNum)
{
int startIndex = content.IndexOf(keyWord) + keyWord.Length;
int endIndex = content.Substring(startIndex).IndexOf(";");
string lastNum = content.Substring(startIndex, endIndex).Split(',')[2].Trim();
int num = 100;
try
{
num = Convert.ToInt32(lastNum.Substring(0, lastNum.Length - 1));
}
catch (Exception e) { }
if (num > defineNum)
{
return content;
}
return null;
}
private static int getKeyWordIndex(string str, string keyWord)
{
int keyWordIndex = -1;
try
{
Regex r = new Regex(keyWord);
Match m = r.Match(str);
keyWordIndex = m.Index;
if (keyWordIndex == 0)
{
throw new Exception("cuowu");
}
// keyWordIndex = getIndex(str);
}
catch (Exception e)
{
keyWordIndex = -1;
}
return keyWordIndex;
}
private static int getIndex(string str)
{
int keyWordIndex = -1;
try
{
int kk = str.IndexOf("@Test");
if (kk != -1)
{
keyWordIndex = str.IndexOf("//");
}
}
catch (ArgumentNullException e)
{
keyWordIndex = -1;
}
return keyWordIndex;
}
private static string getAuthor(String filePath)
{
string str = File.ReadAllText(filePath, Encoding.GetEncoding("gb2312"));
string key1 = " * author date description";
int index1 = str.IndexOf(key1);
if (index1 != -1)
{
string key2 = " * ";
str = str.Substring(index1 + 50 + key1.Length, 50);
int index2 = str.IndexOf(key2);
if (index1 != -1)
{
str = str.Substring(str.IndexOf(key2) + key2.Length);
return str.Split(' ')[0];
}
else
{
return null;
}
}
else
{
return null;
}
}
}
**/
}