最近在做一個項目的移植工作,項目很大,光c檔案大約有1800多。由于某些需要,想要對某些代碼檔案引用的.h檔案進行分析。
網上找了好久,暫無發現類似的工具。
正好,今天放假,就做了這麼個工具。
好了,廢話不多說了,先上圖。

由于是自己做的個demo,是以隻是先注重大體功能上的實作,細節上還有很多不足。比如沒有使用多線程,去除代碼檔案中注釋的地方還有個漏洞(檔案讀取1M導緻的,不過幾乎沒影響),還有循環繪制node的地方(邏輯上稍微修改下更好)。
後面奉上代碼,大家可以自己根據需求繼續修改,也可以湊合這麼用。
言歸正傳。
樹形關系呈現上使用了DotNetBar中的TreeGX控件。下載下傳位址:
http://down2.cr173.com/soft1/DotNetBarSetup.zip
先setup,再patcher。
裝好後,先添加引用,然後在工具箱中添加treeGX。
沒錯,項目名Jonce,“窮死”。好名字。
界面布局。
其實整個流程很簡單,1擷取指定目錄下的所有代碼檔案;2分析出檔案中#include包含的檔案;3繪制node節點
CType.cs檔案内容:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Jonce
{
struct CType
{
public string FullPath;
public string FileName;
public List<string> IncludeList;
}
}
該類型用于描述每個代碼檔案。
CFileHelper.cs檔案内容:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
namespace Jonce
{
class CFileHelper
{
private List<string> fileList = new List<string>();
/// <summary>
/// 擷取及分析所有C代碼檔案
/// </summary>
/// <param name="path">C項目路徑</param>
/// <returns></returns>
public List<CType> GetAllCFile(string path)
{
List<CType> retList = new List<CType>();
getAllByPath(path);
//過濾出所有檔案中的代碼檔案
//分析引用,并存入List<CType>結構内
foreach (string item in fileList)
{
string extension = Path.GetExtension(item).ToLower();
if (extension == ".c" || extension == ".h" || extension == ".cpp")
{
CType cType = new CType();
cType.FullPath = item;
cType.FileName = Path.GetFileName(item);
//擷取C檔案中include引用的頭檔案
cType.IncludeList = SourceHelper.GetIncludeFile(SourceHelper.RemoveComments(item));
retList.Add(cType);
}
}
return retList;
}
//擷取指定目錄下的所有檔案
private void getAllByPath(string path)
{
if (path.EndsWith("\\"))
{
fileList.Add(path);
}
else
{
fileList.Add(path + "\\");
}
string[] dirs = Directory.GetDirectories(path);
fileList.AddRange(Directory.GetFiles(path));
foreach (string dir in dirs)
{
getAllByPath(dir.ToString());
}
}
}
}
SourceHelper.cs檔案内容:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Windows.Forms;
using System.Text.RegularExpressions;
namespace Jonce
{
class SourceHelper
{
/// <summary>
/// 去掉代碼檔案中的注釋
/// </summary>
/// <param name="filePath">檔案全路徑</param>
/// <returns>檔案前1M内容(去掉注釋)</returns>
public static string RemoveComments(string filePath)
{
string retStr = "";
//1M緩沖區
char[] buffer = new char[1024 * 1024];
using (FileStream fs = new FileStream(filePath, FileMode.Open, FileAccess.Read))
{
using (StreamReader sr = new StreamReader(fs, Encoding.Default))
{
try
{
//string fileStr = sr.ReadToEnd();
//讀取檔案。隻讀取<=1M内容
sr.Read(buffer, 0, buffer.Length);
//字元數組轉換為字元串,進行正則比對
string fileStr = new string(buffer);
//正規表達式,比對多行注釋和單行注釋
string regStr = @"/\*[\s\S]*?\*/|//.*";
//去掉多行注釋
retStr = Regex.Replace(fileStr, regStr, "");
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString(), "ERR");
}
}
}
return retStr;
}
/// <summary>
/// 擷取C檔案中include引用的頭檔案
/// </summary>
/// <param name="fileStr">檔案全路徑</param>
/// <returns>頭檔案List集合</returns>
public static List<string> GetIncludeFile(string fileStr)
{
List<string> headFileList = new List<string>();
//比對include語句
string regStr1 = @"#\s*include\s(""|<).*";
//比對頭檔案
string regStr2 = @"\w+\.(h|H)\b";
Match mc1 = Regex.Match(fileStr, regStr1);
while (mc1.Success)
{
Match mc2 = Regex.Match(mc1.ToString(), regStr2);
if (mc2.Success)
{
headFileList.Add(mc2.ToString());
}
mc1 = mc1.NextMatch();
}
return headFileList;
}
}
}
Form1.cs内容:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using DevComponents.Tree;
namespace Jonce
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
//選取目錄
FolderBrowserDialog fbd = new FolderBrowserDialog();
if (fbd.ShowDialog()==DialogResult.OK)
{
string path = fbd.SelectedPath;
CFileHelper fileHelper = new CFileHelper();
//擷取及分析所有C代碼檔案
List<CType> cTypeList = fileHelper.GetAllCFile(path);
//treeGX.Node節點用style
ElementStyle style = new ElementStyle();
//節點文字顔色設定
style.TextColor = Color.Blue;
foreach (CType item in cTypeList)
{
if (Path.GetExtension(item.FullPath).ToLower() == ".c")
{
Node cNode = new Node();
cNode.Name = item.FileName;
cNode.Text = item.FileName;
cNode.Style = style;
cNode.NodeDoubleClick += cNode_NodeDoubleClick;
this.node1.Nodes.Add(cNode);
loopDraw(cTypeList, item.FileName, ref cNode);
}
}
//this.node1.ExpandAll();
this.node1.Text = path;
//重新整理treeGX
this.treeGX1.Refresh();
}
}
void cNode_NodeDoubleClick(object sender, EventArgs e)
{
Node node = sender as Node;
node.Expand();
//node.ExpandAll();
//throw new NotImplementedException();
}
private void loopDraw(List<CType> cTypeList, string fileName,ref Node node)
{
foreach (CType item in cTypeList)
{
if (item.FileName==fileName)
{
foreach (string strItem in item.IncludeList)
{
Node incNode = new Node();
incNode.Name = strItem;
incNode.Text = strItem;
incNode.NodeDoubleClick += cNode_NodeDoubleClick;
node.Nodes.Add(incNode);
loopDraw(cTypeList, strItem, ref incNode);
}
}
}
}
private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
if (comboBox1.SelectedItem==null)
{
return;
}
//DevComponents.Tree.eNodeLayout layout = (DevComponents.Tree.eNodeLayout)Enum.Parse(typeof(DevComponents.Tree.eNodeLayout), comboBox1.SelectedItem.ToString());
DevComponents.Tree.eMapFlow mapFlow = (DevComponents.Tree.eMapFlow)Enum.Parse(typeof(DevComponents.Tree.eMapFlow), comboBox1.SelectedItem.ToString());
if (treeGX1.MapLayoutFlow != mapFlow)
{
treeGX1.MapLayoutFlow = mapFlow;
treeGX1.RecalcLayout();
treeGX1.Refresh();
}
}
private void button2_Click(object sender, EventArgs e)
{
this.node1.ExpandAll();
treeGX1.Refresh();
}
private void button3_Click(object sender, EventArgs e)
{
this.node1.Nodes.Clear();
treeGX1.Refresh();
}
}
}
以上就是所有代碼,大家可以自己重新搭建一個。
當然,如果誰csdn下載下傳積分多的,也可以下載下傳整個項目:http://download.csdn.net/detail/geeking/8030119
隻要兩個下載下傳積分。本來想免費發的,可是我的積分一個也沒有了,很多資源都沒法下。shit,積分制度就是shit。