C # easily draw the inclusion relationship diagram of c-Language header files,
Recently, I am working on a project porting job. The project is very large and there are more than 1800 c files. For some purposes, you want to analyze the. h file referenced by some code files.
I have been searching online for a long time and have not found any similar tools.
That's right. Today's holiday, we made such a tool.
Okay, you don't have to talk about it anymore.
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 ;}}
This type is used to describe each code file.
CFileHelper. cs file content:
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> /// obtain and analyze all C code files /// </summary> /// <param name = "path"> C Project path </param >/// <returns> </returns> public List <CType> GetAllCFile (string path) {List <CType> retList = new List <CType> (); getAllByPath (path); // filter out the code files in all files. // analyze references, and saved to the foreach (string item in fileList) {string extension = Path in the List <CType> structure. getExtension (item ). toLower (); if (extension = ". c "| extension = ". h "| extension = ". cpp ") {CType cType = new CType (); cType. fullPath = item; cType. fileName = Path. getFileName (item); // obtain the cType of the header file referenced by include in the C file. includeList = SourceHelper. getIncludeFile (SourceHelper. removeComments (item); retList. add (cType) ;}} return retList;} // obtain all 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 file content:
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> // remove the comments in the code file /// </summary> /// <param name = "filePath"> all files path </param> /// 1 MB before <returns> file (remove comments) </returns> public static string RemoveComments (string filePath) {string retStr = ""; // 1 M Buffer 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 (); // read the file. Read Only <= 1 MB content sr. read (buffer, 0, buffer. length); // convert the character array to a string and perform regular expression matching string fileStr = new string (buffer); // regular expression, match the multi-line comment and single-line comment string regStr = @ "/\ * [\ s \ S] *? \ */| //. * "; // Remove the multi-line comment retStr = Regex. replace (fileStr, regStr, "");} catch (Exception ex) {MessageBox. show (ex. toString (), "ERR") ;}} return retStr ;} /// <summary> /// obtain the header file referenced by include in the C file /// </summary> /// <param name = "fileStr"> full file path </ param> // <returns> header file List set </returns> public static List <string> GetIncludeFile (string fileStr) {List <string> headFileList = new List <string> (); // match the include statement string regStr1 = @ "# \ s * include \ s (" "| <). * "; // match the header 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 content:
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 button#click (object sender, EventArgs e) {// select the FolderBrowserDialog fbd = New FolderBrowserDialog (); if (fbd. showDialog () = DialogResult. OK) {string path = fbd. selectedPath; CFileHelper fileHelper = new CFileHelper (); // obtain and analyze all C code files List <CType> cTypeList = fileHelper. getAllCFile (path); // treeGX. node uses style ElementStyle style = new ElementStyle (); // the color of the Node text to set the 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; // refresh 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_NodeDoub LeClick; 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 mapFlo W = (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 ();}}}
The above is all the code. You can build one by yourself.
Of course, if you who download more points csdn, You can also download the entire project: http://download.csdn.net/detail/geeking/8030119
Only two download points are required. I wanted to send it for free, but I didn't have any points, and I couldn't afford a lot of resources. Shit: the credit system is shit.
In the C language, what is the symbol (->) and how to use it?
This is a symbol in the struct pointer. Write a program to explain it, for example:
# Include <stdio. h>
Struct STU // define a struct
{
Int num;
} Stu;
Int main ()
{
Struct STU * p; // defines a struct pointer.
P = stu; // p points to the struct variable stu.
Stu. num = 100; // attaches an initial value to the struct member num.
Printf ("% d", p-> num); // output the num value in stu
Return;
}
As you can see, the-> method is to reference the variable in the struct !!
Format: p-> struct member (such as p-> num)
The function is equivalent to stu. num or (* p). num.
I don't know. You don't understand, and don't understand call me. O (∩ _ ∩) O ~
Hope to adopt it.
In the C language, what is the symbol (->) and how to use it?
This is a symbol in the struct pointer. Write a program to explain it, for example:
# Include <stdio. h>
Struct STU // define a struct
{
Int num;
} Stu;
Int main ()
{
Struct STU * p; // defines a struct pointer.
P = stu; // p points to the struct variable stu.
Stu. num = 100; // attaches an initial value to the struct member num.
Printf ("% d", p-> num); // output the num value in stu
Return;
}
As you can see, the-> method is to reference the variable in the struct !!
Format: p-> struct member (such as p-> num)
The function is equivalent to stu. num or (* p). num.
I don't know. You don't understand, and don't understand call me. O (∩ _ ∩) O ~
Hope to adopt it.