请选择 进入手机版 | 继续访问电脑版
查看: 441|回复: 0

[运维管理] C语言代码统计工具(升级版)

715

主题

715

帖子

2162

积分

猿er

Rank: 1

积分
2162
发表于 2016-8-14 18:57:31
输入指定的文件或文件夹路径,自动统计C程序中代码中的有效行数,注释行数,空行数和总行数,仅对 .C 或 .H文件有效!
  1. #ifndef _C_COUNT_
  2. #define _C_COUNT_
  3. #include<stdlib.h>
  4. #include<string>
  5. #include<sys/stat.h>
  6. #include<io.h>
  7. #include<process.h>
  8. #include<malloc.h>
  9. #include <windows.h>
  10. #include "time.h"
  11. #define D_NG 0 /*处理失败*/
  12. #define D_OK 1 /*处理成功*/
  13. #define D_DEBUG_PROJECT 0 /*开关*/
  14. #define IS_SPACE(c) ((c) == ' ' || (c) == '\t')
  15. typedef unsigned int UINT32;
  16. typedef char CHAR32;
  17. typedef struct ltbl
  18. {
  19. struct ltbl* pNxt; /* 指向链表中后一个元素的指针 */
  20. UINT32 Note; /* 注释行数 */
  21. UINT32 Empty; /* 空行数 */
  22. UINT32 Effect; /* 有效行数 */
  23. UINT32 Total; /* 总行数 */
  24. CHAR32 PATH[100]; /* 地址 */
  25. }LTBL;
  26. typedef struct mng
  27. {
  28. UINT32 nNodeCnt; /* 链表的长度 */
  29. LTBL* pHead; /* 指向链表头的指针 */
  30. }MNG;
  31. enum
  32. {
  33. Folder = 1, // 当输入的为文件夹或*和*.*时
  34. File_C, // 当输入的为 *.C文件时
  35. File_H, // 当输入的为 *.H文件时
  36. Specific_File, // 当输入的为具体的文件时
  37. };
  38. UINT32 Final_order[9] ={0}; // 存储命令行信息
  39. /* Final_order[9]数组中:第0位存放-all信息,以此类推分别为:-t,-p,-l,-c,-n,-a,-cp,-hp其中0表示不存在,1表示存在 */
  40. extern UINT32 Free_Memory(CHAR32*&Command1);
  41. extern UINT32 Check_Malloc(CHAR32**New_Mallo, UINT32 leD_NGth);
  42. extern UINT32 Check_File(UINT32 argc, CHAR32* argv[]);
  43. extern CHAR32* Storage_File_Path(UINT32 argc, CHAR32* argv[]);
  44. extern UINT32 Command_Handle();
  45. extern UINT32 Check_File(UINT32 argc, CHAR32* argv[]);
  46. extern void Print(MNG *pMng, CHAR* File_Path);
  47. extern CHAR * mytrim(CHAR *str);
  48. extern void Process(MNG *pMng);
  49. extern void TBLFree (MNG* pMng);
  50. extern MNG* File_Find(MNG *pMng, CHAR *Path, CHAR *Temp_Path, UINT32 length);
  51. extern void Print_time(clock_t start, clock_t finish, CHAR* File_Path);
  52. extern UINT32 FileStorage_Specific(MNG *pMng, CHAR* Path);
  53. extern UINT32 FileStorage_CH(MNG *pMng, CHAR* Path, UINT32 File_Type);
  54. extern UINT32 FoaderStorage_H(MNG *pMng, CHAR* Path);
  55. extern UINT32 FolderStorage_C(MNG *pMng, CHAR* Path);
  56. extern UINT32 Folder_Check(MNG * pMng, MNG *pMng1, CHAR* Path);
  57. #endif //_C_COUNT_
复制代码
  1. // project.cpp : Defines the entry poUINT32 for the console application.
  2. //
  3. #include "stdafx.h"
  4. #include "C_count.h"
  5. /*******************************************************************************/
  6. /*函数名称:unsigned int inline Free_Memory(CHAR_32* &Command1) */
  7. /*函数功能:对malloc申请的空间经行释放 */
  8. /*参数:Command1:malloc申请的地址 */
  9. /*返回值:value:表示malloc申请的地址是否成功 */
  10. /********************************************************************************/
  11. UINT32 Free_Memory(CHAR *&Command1)
  12. {
  13. UINT32 value = D_OK;
  14. if(Command1 != NULL) /*内存释放成功*/
  15. {
  16. void* free(Command1);
  17. Command1 = NULL;
  18. }
  19. else
  20. {
  21. value = D_NG;
  22. }
  23. return value;
  24. }
  25. /****************************************************************************************/
  26. /*函数名称:UINT32 Check_Malloc(CHAR*&New_Mallo, UINT32 length) */
  27. /*函数功能:判断malloc申请的空间是否成功 */
  28. /*参数:New_Mallo:申请空间的地址 */
  29. /* length:申请空间char类型的长度 */
  30. /*返回值:value:表示malloc申请的地址是否成功 */
  31. /****************************************************************************************/
  32. UINT32 Check_Malloc(CHAR*&New_Mallo, UINT32 length)
  33. {
  34. UINT32 value = D_OK;
  35. if(( New_Mallo = (CHAR*)malloc(length * sizeof(CHAR)) ) != NULL) /*内存申请成功*/
  36. {
  37. //do nothiD_NG
  38. }
  39. else
  40. {
  41. #if D_DEBUG_PROJECT
  42. printf("malloc failed");
  43. #endif
  44. value = D_NG;
  45. }
  46. return value;
  47. }
  48. /**************************************************************************************/
  49. /*函数名称:UINT32 Check_File(int argc, CHAR* argv[]) */
  50. /*函数功能:将指向在DOS命令行中执行程序名后第一行字符串保存 */
  51. /*参数:argc:,用来统计你运行程序时送给main函数的命令行参数的个数 */
  52. /* argv[]:指向在DOS命令行中执行程序名后字符串 */
  53. /*返回值:File_Type :表示输入路径的类型 */
  54. /* 路径错误: 0 */
  55. /* 文件夹:1 */
  56. /* *.c: 2 */
  57. /* *.h: 3 */
  58. /* 文件:4 */
  59. /**************************************************************************************/
  60. UINT32 Check_File(UINT32 argc, CHAR* argv[])
  61. {
  62. UINT32 File_Type = 0;
  63. UINT32 length = strlen(argv[1]);
  64. CHAR* Path = NULL;
  65. if(Check_Malloc(Path, length) == D_OK) /*判断空间是否申请成功*/
  66. {
  67. //do nothing
  68. }
  69. else
  70. {
  71. #if D_DEBUG_PROJECT
  72. printf("malloc failed");
  73. #endif
  74. exit(0);
  75. }
  76. strcpy(Path, argv[1]);
  77. CHAR* fileName = Path;
  78. CHAR* Star_Address = strstr(Path, "\\*");
  79. if(Star_Address != NULL) /*判断字符串中是否有**/
  80. {
  81. Path = Star_Address;
  82. if(strstr(Path, "\\*") && strlen(Path) == 2) /*文件末尾为*,File_Type为1*/
  83. {
  84. File_Type = Folder;
  85. }
  86. else if(strstr(Path, "*.*") && strlen(Path) == 4) /*文件末尾为*.*,File_Type为1*/
  87. {
  88. File_Type = Folder;
  89. }
  90. else if(strstr(Path, "*.c") && strlen(Path) == 4) /*文件末尾为*.c,File_Type为2*/
  91. {
  92. File_Type = File_C;
  93. }
  94. else if(strstr(Path, "*.h") && strlen(Path) == 4) /*文件末尾为*.h,File_Type为3*/
  95. {
  96. File_Type = File_H;
  97. }
  98. else
  99. {
  100. printf("The path is error!\n");
  101. Free_Memory(fileName);
  102. exit(0);
  103. }
  104. if(File_Type != 0) /*将*后年的路径去除*/
  105. {
  106. *Path = '\0';
  107. strcpy(argv[1], fileName);
  108. if(_access(argv[1], 0) == 0) /*判断*前面的路径是否正确*/
  109. {
  110. //do nothing
  111. }
  112. else
  113. {
  114. File_Type = 0;
  115. }
  116. }
  117. else
  118. {
  119. //do nothing
  120. }
  121. }
  122. else
  123. {
  124. struct _stat buf;
  125. _stat( fileName, &buf); /*将路径信息传递给buf*/
  126. if(_S_IFDIR & buf.st_mode) /*判断是否为录*/
  127. {
  128. File_Type = Folder;
  129. }
  130. else if(_S_IFREG & buf.st_mode) /*判断是否为不含*的文件路径*/
  131. {
  132. File_Type = Specific_File;
  133. }
  134. if(_access(argv[1], 0) == 0) /*判断路径是否正确*/
  135. {
  136. //do nothing
  137. }
  138. else
  139. {
  140. File_Type = 0;
  141. }
  142. }
  143. Free_Memory(fileName);
  144. return File_Type;
  145. }
  146. /**************************************************************************************/
  147. /*函数名称:CHAR* Storage_File_Path(UINT32 argc, CHAR* argv[]) */
  148. /*函数功能:如果命令行中有-p指令,将后面的路径的地址返回,并且把命令存入数组 */
  149. /*参数:argc:,用来统计你运行程序时送给main函数的命令行参数的个数 */
  150. /* argv[]:指向在DOS命令行中执行程序名后字符串 */
  151. /*返回值: File_Path:将-p后路径的首地址返回 */
  152. /**************************************************************************************/
  153. CHAR* Storage_File_Path(UINT32 argc, CHAR* argv[])
  154. {
  155. CHAR* File_Path = NULL;
  156. if(argc > 2)
  157. {
  158. for(UINT32 i = 2; i < argc; i++)
  159. {
  160. if(strstr(argv[i], "-all") != NULL && strlen(argv[i]) == 4)
  161. {
  162. if(++Final_order[0] > 1)
  163. {
  164. #if D_DEBUG_PROJECT
  165. printf("Input error");
  166. #endif
  167. exit(0);
  168. }
  169. else
  170. {
  171. //do nothing
  172. }
  173. }
  174. else if(strstr(argv[i], "-t") != NULL && strlen(argv[i]) == 2)
  175. {
  176. if(++Final_order[1] > 1)
  177. {
  178. #if D_DEBUG_PROJECT
  179. printf("Input error");
  180. #endif
  181. exit(0);
  182. }
  183. else
  184. {
  185. //do nothing
  186. }
  187. }
  188. else if(strstr(argv[i], "-p") != NULL && strlen(argv[i]) == 2)
  189. {
  190. if(++Final_order[2] > 1)
  191. {
  192. #if D_DEBUG_PROJECT
  193. printf("Input error");
  194. #endif
  195. exit(0);
  196. }
  197. else
  198. {
  199. if(++i < argc)
  200. {
  201. FILE* fg = NULL;
  202. if( (fg = fopen(argv[i], "a+") ) == NULL) /* 以E追C加A的I方u式R写E入u文÷件? */
  203. {
  204. #if D_DEBUG_PROJECT
  205. printf("Input error");
  206. #endif
  207. exit(0);
  208. }
  209. else
  210. {
  211. fclose(fg);
  212. File_Path = argv[i];
  213. }
  214. }
  215. else
  216. {
  217. #if D_DEBUG_PROJECT
  218. printf("Input error");
  219. #endif
  220. exit(0);
  221. }
  222. }
  223. }
  224. else if(strstr(argv[i], "-l") != NULL && strlen(argv[i]) == 2)
  225. {
  226. if(++Final_order[3] > 1)
  227. {
  228. #if D_DEBUG_PROJECT
  229. printf("Input error");
  230. #endif
  231. exit(0);
  232. }
  233. else
  234. {
  235. //do nothing
  236. }
  237. }
  238. else if(strstr(argv[i], "-c") != NULL && strlen(argv[i]) == 2)
  239. {
  240. if(++Final_order[4] > 1)
  241. {
  242. #if D_DEBUG_PROJECT
  243. printf("Input error");
  244. #endif
  245. exit(0);
  246. }
  247. else
  248. {
  249. //do nothing
  250. }
  251. }
  252. else if(strstr(argv[i], "-n") != NULL && strlen(argv[i]) == 2)
  253. {
  254. if(++Final_order[5] > 1)
  255. {
  256. #if D_DEBUG_PROJECT
  257. printf("Input error");
  258. #endif
  259. exit(0);
  260. }
  261. else
  262. {
  263. //do nothing
  264. }
  265. }
  266. else if(strstr(argv[i], "-a") != NULL && strlen(argv[i]) == 2)
  267. {
  268. if(++Final_order[6] > 1)
  269. {
  270. #if D_DEBUG_PROJECT
  271. printf("Input error");
  272. #endif
  273. exit(0);
  274. }
  275. else
  276. {
  277. //do nothing
  278. }
  279. }
  280. else if(strstr(argv[i], "-cp") != NULL && strlen(argv[i]) == 3)
  281. {
  282. if(++Final_order[7] > 1)
  283. {
  284. #if D_DEBUG_PROJECT
  285. printf("Input error");
  286. #endif
  287. exit(0);
  288. }
  289. else
  290. {
  291. //do nothing
  292. }
  293. }
  294. else if(strstr(argv[i], "-hp") != NULL && strlen(argv[i]) == 3)
  295. {
  296. if(++Final_order[8] > 1)
  297. {
  298. #if D_DEBUG_PROJECT
  299. printf("Input error");
  300. #endif
  301. exit(0);
  302. }
  303. else
  304. {
  305. //do nothing
  306. }
  307. }
  308. else
  309. {
  310. #if D_DEBUG_PROJECT
  311. printf("Input error");
  312. #endif
  313. exit(0);
  314. }
  315. }
  316. }
  317. return File_Path;
  318. }
  319. /********************************
  320. * function:Command_Handle
  321. * return值 :D_OK(1)/D_NG(0)
  322. *********************************/
  323. UINT32 Command_Handle(UINT32 File_Type)
  324. {
  325. UINT32 ret = 0;
  326. Final_order[0] = 0;
  327. if(Folder != File_Type) /* 路径不是文件夹时,不能-cp 或-hp */
  328. {
  329. if(Final_order[7] || Final_order[8])
  330. {
  331. printf("Error! Can't -cp, -hp\n");
  332. ret = D_NG;
  333. }
  334. else
  335. {
  336. ;/* do nothing */
  337. }
  338. }
  339. if (Final_order[0] && (Final_order[3] || Final_order[4] || Final_order[5] || Final_order[6] || Final_order[7] || Final_order[8]))
  340. {
  341. printf("Error! There is all, no -l, -c, -n, -a, -cp, -hp\n");
  342. ret = D_NG;
  343. } /* -all 和 -l, -c, -n, -a等命令冲突的情况 */
  344. if (Final_order[3] && Final_order[4] && Final_order[5] && Final_order[6] && Final_order[7] && Final_order[8])
  345. {
  346. for (UINT32 i = 3; i < 9; i++)
  347. {
  348. Final_order[i] = 0;
  349. }
  350. Final_order[0] = 1;
  351. ret = D_OK; /* -l, -c, -n, -a -cp, -hp同时存在与-all相同 */
  352. }
  353. if (Final_order[3] && Final_order[4] && Final_order[5] && Final_order[6])
  354. {
  355. for (UINT32 i = 3; i < 7; i++)
  356. {
  357. Final_order[i] = 0;
  358. }
  359. Final_order[0] = 1; /* -l, -c, -n, -a 与 -all 相同 */
  360. ret = D_OK;
  361. }
  362. if( !(Final_order[0] || Final_order[3] || Final_order[4] || Final_order[5] || Final_order[6] || Final_order[7] || Final_order[8] ) )
  363. {
  364. Final_order[0] = 1;
  365. ret = D_OK; /* 只有-t 或 -p 时,加-all */
  366. }
  367. else
  368. {
  369. ret = D_OK;
  370. }
  371. return ret;
  372. }
  373. /**************************************************************************************/
  374. /*函数名称:void Print(MNG *pMng, CHAR* File_Path) */
  375. /*函数功能:输出统计内容到屏幕或文件上 */
  376. /*参数:pMng:已存入统计信息的链表; */
  377. /* File_Path:输出到文件的路径 */
  378. /*返回值: 无 */
  379. /**************************************************************************************/
  380. void Print(MNG *pMng, CHAR* File_Path)
  381. {
  382. UINT32 D_Note = 0; /* 总注释行数 */
  383. UINT32 D_Empty = 0; /* 总空行数 */
  384. UINT32 D_Total = 0; /* 总行数 */
  385. UINT32 D_Effect = 0; /* 总有效行数 */
  386. LTBL *P = pMng->pHead;
  387. if(1 == Final_order[2])
  388. {
  389. FILE *fg;
  390. if( (fopen_s(&fg, File_Path, "a+") ) != NULL) /* 以追加的方式写入文件 */
  391. {
  392. printf("open is error!\n");
  393. Free_Memory(File_Path);
  394. exit(0);
  395. }
  396. for (UINT32 i = 0; i < pMng->nNodeCnt; ++i) /* 遍历输出链表 */
  397. {
  398. D_Note += P->Note, D_Empty += P->Empty, D_Total += P->Total, D_Effect += P->Effect;
  399. fprintf_s(fg, "%s\t", P->PATH);
  400. if (1 == Final_order[0])
  401. {
  402. fprintf_s(fg,"Effect Line:%d\tNote Line:%d\tEmpty Line:%d\tTotal Line:%d\t", P->Effect, P->Note, P->Empty, P->Total);
  403. }
  404. else
  405. {
  406. //do nothing;
  407. }
  408. if (1 == Final_order[3])
  409. {
  410. fprintf_s(fg, "Effect Line:%d\t", P->Effect);
  411. }
  412. else
  413. {
  414. //do nothing;
  415. }
  416. if (1 == Final_order[4])
  417. {
  418. fprintf_s(fg, "Note Line:%d\t", P->Note);
  419. }
  420. else
  421. {
  422. //do nothing;
  423. }
  424. if (1 == Final_order[5])
  425. {
  426. fprintf_s(fg, "Empty Line:%d\t", P->Empty);
  427. }
  428. else
  429. {
  430. //do nothing;
  431. }
  432. if (1 == Final_order[6])
  433. {
  434. fprintf_s(fg, "Total Line:%d\t", P->Total);
  435. }
  436. else
  437. {
  438. //do nothing;
  439. }
  440. fputs("\n", fg);
  441. P = P->pNxt;
  442. }
  443. if (1 == Final_order[0])
  444. {
  445. fprintf_s(fg, "The Total result:\n");
  446. fprintf_s(fg, "Effect Line:%d\tNote Line:%d\tEmpty Line:%d\tTotal Line:%d\t", D_Effect, D_Note, D_Empty, D_Total);
  447. }
  448. fclose(fg);
  449. }
  450. else
  451. {
  452. for (UINT32 i = 0; i < pMng->nNodeCnt; ++i)
  453. {
  454. D_Note += P->Note, D_Empty += P->Empty, D_Total += P->Total, D_Effect += P->Effect;
  455. printf("%s\t", P->PATH);
  456. if (1 == Final_order[0])
  457. {
  458. printf("Effect Line:%d\tNote Line:%d\tEmpty Line:%d\tTotal Line:%d\t", P->Effect, P->Note, P->Empty, P->Total);
  459. }
  460. else
  461. {
  462. //do nothing;
  463. }
  464. if (1 == Final_order[3])
  465. {
  466. printf("Effect Line:%d\t", P->Effect);
  467. }
  468. else
  469. {
  470. //do nothing;
  471. }
  472. if (1 == Final_order[4])
  473. {
  474. printf_s("Note Line:%d\t", P->Note);
  475. }
  476. else
  477. {
  478. //do nothing;
  479. }
  480. if (1 == Final_order[5])
  481. {
  482. printf("Empty Line:%d\t", P->Empty);
  483. }
  484. else
  485. {
  486. //do nothing;
  487. }
  488. if (1 == Final_order[6])
  489. {
  490. printf("Total Line:%d\t", P->Total);
  491. }
  492. else
  493. {
  494. //do nothing;
  495. }
  496. putchar('\n');
  497. P = P->pNxt;
  498. }
  499. if (1 == Final_order[0])
  500. {
  501. printf("The Total result:\n");
  502. printf("Effect Line:%d\tNote Line:%d\tEmpty Line:%d\tTotal Line:%d\t", D_Effect, D_Note, D_Empty, D_Total);
  503. }
  504. putchar('\n');
  505. }
  506. }
  507. /**************************************************************************************/
  508. /*函数名称:CHAR *Trim(CHAR *str) */
  509. /*函数功能:除去字符串数组的前后空格 */
  510. /*参数:str:字符串指针; */
  511. /*返回值: NULL/str */
  512. /**************************************************************************************/
  513. CHAR *Trim(CHAR *str)
  514. {
  515. if (str == NULL)
  516. {
  517. return NULL;
  518. }
  519. while(IS_SPACE(*str))
  520. {
  521. str ++;
  522. }
  523. UINT32 len = strlen(str);
  524. if (len == 0)
  525. {
  526. return str;
  527. }
  528. CHAR *end = str + len - 1;
  529. while( IS_SPACE(*end) )
  530. {
  531. end --;
  532. }
  533. *(++ end) = '\0';
  534. return str;
  535. }
  536. /**************************************************************************************/
  537. /*函数名称:void Process(MNG *pMng) */
  538. /*函数功能:统计代码行数并将统计信息存入链表 */
  539. /*参数:pMng:已存入路径信息的链表; */
  540. /*返回值: 无 */
  541. /**************************************************************************************/
  542. void Process(MNG *pMng)
  543. {
  544. LTBL *P = pMng->pHead;
  545. UINT32 Total = 0, Note = 0, Empty = 0; /* 一个文件的总行数,注释行数,空行数 */
  546. CHAR *Line = NULL; /* 将每行字符输入Line中 */
  547. Line = (CHAR*)malloc(sizeof(CHAR) * 10000);
  548. if(NULL == Line)
  549. {
  550. printf("memory is error!\n");
  551. TBLFree(pMng);
  552. exit(0);
  553. }
  554. memset(Line, 0, 10000);
  555. CHAR *temp = Line; /* 标记所申请的内存的头地址 */
  556. CHAR *p_Head = NULL; /* 标记所申请的已去除空格的头地址 */
  557. CHAR *Pfind_char1 = NULL; /* 判断查找下一个字符串位置的指针 */
  558. CHAR *Pfind_char2 = NULL; /* 判断查找下一个字符串位置的指针 */
  559. CHAR *Pfind_char3 = NULL; /* 判断查找下一个字符串位置的指针 */
  560. CHAR *Pfind_char4 = NULL; /* 判断查找下一个字符串位置的指针 */
  561. FILE *fp = NULL;
  562. UINT32 Note_Mark = 0; /* 注释段的标记位 */
  563. UINT32 Quote_Mark = 0; /* 引号的标记位 */
  564. UINT32 Effect_Add = 0; /* 统计带有注释行的有效行 */
  565. UINT32 Note_Add = 0; /* 统计注释段中的行数 */
  566. CHAR ch = '\0';
  567. UINT32 j = 0;
  568. UINT32 len = 0;
  569. for (register UINT32 i = 0; i < pMng->nNodeCnt; ++i)
  570. {
  571. if( (fopen_s(&fp, P->PATH, "r") ) != NULL)
  572. {
  573. printf("open is error2!\n");
  574. exit(0);
  575. }
  576. while( ( ch = fgetc(fp) ) != EOF)
  577. {
  578. if(ch != '\n')
  579. {
  580. *(Line + (j ++) ) = ch;
  581. continue;
  582. }
  583. Line = Trim(Line); /* 去除本行前后的空格 */
  584. p_Head = Line;
  585. if(Note_Mark == 1)
  586. {
  587. Note_Add ++;
  588. }
  589. Total ++;
  590. len = strlen (Line);
  591. if ( 0 == len )
  592. {
  593. Empty ++;
  594. }
  595. if ( (Pfind_char1 = strstr(Line, "\"") ) != NULL )
  596. {
  597. if( (Pfind_char2 = strstr(Pfind_char1 + 1, "\"") ) != NULL )
  598. {
  599. if( ( Pfind_char3 = strstr(Pfind_char2, "//") ) != NULL)
  600. {
  601. Note ++;
  602. Effect_Add ++;
  603. }
  604. else if( ( Pfind_char3 = strstr(Pfind_char2, "/*") ) != NULL)
  605. {
  606. if( ( Pfind_char4 = strstr(Pfind_char3, "*/") ) != NULL )
  607. {
  608. Note ++;
  609. Effect_Add ++;
  610. }
  611. else
  612. {
  613. Note_Mark = 1;
  614. Effect_Add ++;
  615. }
  616. }
  617. }
  618. Quote_Mark = 1;
  619. }
  620. if(Quote_Mark == 1)
  621. {
  622. if ('\\' == *(Line + len - 1))
  623. {
  624. Total --;
  625. }
  626. memset(Line, 0, len);
  627. j = 0;
  628. Quote_Mark = 0;
  629. continue;
  630. }
  631. else if( (Pfind_char1 = strstr(Line, "//") ) != NULL && Pfind_char1 != p_Head)
  632. {
  633. Note ++;
  634. Effect_Add ++;
  635. }
  636. else if( (Pfind_char1 = strstr(Line, "//") ) != NULL && Pfind_char1 == p_Head)
  637. {
  638. Note ++;
  639. }
  640. else if( (Pfind_char1 = strstr(Line, "/*") ) != NULL && Pfind_char1 != p_Head)
  641. {
  642. if( ( Pfind_char2 = strstr(Pfind_char1, "*/") ) != NULL )
  643. {
  644. Note ++;
  645. Effect_Add ++;
  646. }
  647. else
  648. {
  649. Note_Mark = 1;
  650. Effect_Add ++;
  651. }
  652. }
  653. else if( (Pfind_char1 = strstr(Line, "/*") ) != NULL && Pfind_char1 == p_Head)
  654. {
  655. Note ++;
  656. if( ( Pfind_char2 = strstr(Pfind_char1, "*/") ) != NULL )
  657. {
  658. //do nothing.
  659. }
  660. else
  661. {
  662. Note_Mark = 1;
  663. }
  664. }
  665. if ('\\' == *(Line + len - 1) && Note_Mark == 0)
  666. {
  667. Total --;
  668. }
  669. else
  670. {
  671. //do Nothing;
  672. }
  673. if( (Pfind_char1 = strstr(Line, "*/") ) != NULL)
  674. {
  675. Note ++;
  676. Note += Note_Add;
  677. Note_Add = 0;
  678. Note_Mark = 0;
  679. }
  680. memset(Line, 0, len);
  681. Line = temp;
  682. j = 0;
  683. }
  684. fclose(fp);
  685. P->Note = Note;
  686. P->Empty = Empty;
  687. P->Effect = Total - Empty - Note + Effect_Add;
  688. P->Total = Total;
  689. P = P->pNxt;
  690. Total = 0, Note = 0, Empty = 0;
  691. Effect_Add = 0;
  692. }
  693. Free_Memory(Line);
  694. }
  695. /**************************************************************************************/
  696. /*函数名称:void TBLFree (MNG* pMng) */
  697. /*函数功能:释放链表内存 */
  698. /*参数:pMng:释放的链表; */
  699. /*返回值: 无 */
  700. /**************************************************************************************/
  701. void TBLFree (MNG* pMng)
  702. {
  703. LTBL *P = pMng->pHead;
  704. LTBL *temp = NULL;
  705. for(UINT32 i = 0; i < pMng->nNodeCnt; ++i)
  706. {
  707. temp = P->pNxt;
  708. void* free(P);
  709. P = NULL;
  710. P = temp;
  711. }
  712. pMng->pHead = NULL;
  713. pMng->nNodeCnt = 0;
  714. }
  715. /**************************************************************************************/
  716. /*函数名称:UINT32 Folder_Check(MNG * pMng, MNG *pMng1, CHAR* Path) */
  717. /*函数功能:文件夹判断操作 */
  718. /*参数:pMng:*.C文件夹路径的链表 */
  719. /* Path:文件夹路径 */
  720. /*返回值: D_OK */
  721. /**************************************************************************************/
  722. UINT32 File_Search(UINT32 File_Type, MNG *pMng, CHAR *Path)
  723. {
  724. MNG *pMng1 = NULL;
  725. if ( ( pMng->pHead = (LTBL *)malloc( sizeof(LTBL) ) ) == NULL)
  726. {
  727. printf("Memory is error!");
  728. free(pMng);
  729. pMng = NULL;
  730. Free_Memory(Path);
  731. exit(0);
  732. }
  733. switch(File_Type)
  734. {
  735. case File_C:
  736. case File_H:
  737. FileStorage_CH(pMng, Path,File_Type);
  738. break;
  739. case Specific_File:
  740. FileStorage_Specific(pMng, Path);
  741. break;
  742. case Folder:
  743. pMng1 = (MNG*) malloc (sizeof(MNG));
  744. if (NULL == pMng1)
  745. {
  746. printf("Memory is error!");
  747. TBLFree(pMng);
  748. free(pMng);
  749. pMng = NULL;
  750. exit(0);
  751. }
  752. pMng1 ->nNodeCnt = 0;
  753. pMng1 ->pHead = NULL;
  754. if ( ( pMng1->pHead = (LTBL *)malloc( sizeof(LTBL) ) ) == NULL)
  755. {
  756. printf("Memory is error!");
  757. TBLFree(pMng);
  758. free(pMng);
  759. pMng = NULL;
  760. free(pMng1);
  761. pMng1 = NULL;
  762. exit(0);
  763. }
  764. Folder_Check(pMng, pMng1, Path);
  765. break;
  766. default:
  767. {
  768. printf("The File is error!");
  769. exit(0);
  770. }
  771. }
  772. return D_OK;
  773. }
  774. /**************************************************************************************/
  775. /*函数名称:UINT32 FileStorage_Specific(MNG *pMng, CHAR* Path) */
  776. /*函数功能:将文件路径存入链表 */
  777. /*参数:pMng:存入路径的链表; */
  778. /* Path:文件路径 */
  779. /*返回值: D_OK */
  780. /**************************************************************************************/
  781. UINT32 FileStorage_Specific(MNG *pMng, CHAR* Path)
  782. {
  783. LTBL *P = NULL;
  784. P = pMng->pHead ;
  785. strcpy_s(P->PATH, Path);
  786. pMng->nNodeCnt ++;
  787. P ->pNxt = NULL;
  788. return D_OK;
  789. }
  790. /**************************************************************************************/
  791. /*函数名称:UINT32 FileStorage_CH(MNG *pMng, CHAR* Path, UINT32 File_Type) */
  792. /*函数功能:将*.C或*.H文件路径存入链表 */
  793. /*参数:pMng:存入路径的链表; */
  794. /* Path:文件路径 */
  795. /* File_Type:文件类型 */
  796. /*返回值: D_OK */
  797. /**************************************************************************************/
  798. UINT32 FileStorage_CH(MNG *pMng, CHAR* Path, UINT32 File_Type)
  799. {
  800. UINT32 length = strlen(Path);
  801. CHAR *Temp_Path = NULL;
  802. Temp_Path = (CHAR*) malloc( (length + 4) * sizeof(CHAR) );
  803. if(NULL == Temp_Path)
  804. {
  805. printf("memory is error!\n");
  806. TBLFree(pMng);
  807. free(pMng);
  808. pMng = NULL;
  809. Free_Memory(Path);
  810. exit(0);
  811. }
  812. memset(Temp_Path, 0, length +4);
  813. memcpy(Temp_Path, Path, length);
  814. if (File_C == File_Type)
  815. {
  816. strcat(Temp_Path, "\\*.c");
  817. }
  818. else if (File_H == File_Type)
  819. {
  820. strcat(Temp_Path, "\\*.h");
  821. }
  822. CHAR *D_Path = (CHAR*) malloc( (length + 3) * sizeof(CHAR) );
  823. if(NULL == D_Path)
  824. {
  825. printf("memory is error!");
  826. exit(0);
  827. }
  828. memset(D_Path, 0, length);
  829. memcpy(D_Path, Path, length);
  830. *(D_Path + length) = '\0';
  831. strcat(D_Path, "\\");
  832. File_Find(pMng, D_Path, Temp_Path, length + 1);
  833. return D_OK;
  834. }
  835. /**************************************************************************************/
  836. /*函数名称:UINT32 FoaderStorage_H(MNG *pMng, CHAR* Path) */
  837. /*函数功能:将*.H文件路径存入链表 */
  838. /*参数:pMng:*.H文件路的链表; */
  839. /* Path:文件路径 */
  840. /*返回值: D_OK */
  841. /**************************************************************************************/
  842. UINT32 FoaderStorage_H(MNG *pMng, CHAR* Path)
  843. {
  844. CHAR *Temp_Path = NULL;
  845. UINT32 length =strlen(Path);
  846. Temp_Path = (CHAR*) malloc( (length + 6) * sizeof(CHAR) );
  847. if(NULL == Temp_Path)
  848. {
  849. printf("memory is error!");
  850. TBLFree(pMng);
  851. free(pMng);
  852. pMng = NULL;
  853. Free_Memory(Path);
  854. exit(0);
  855. }
  856. memset(Temp_Path, 0, length);
  857. memcpy(Temp_Path, Path, length);
  858. *(Temp_Path + length) = '\0';
  859. strcat(Temp_Path, "\\*.h");
  860. File_Find(pMng, Path, Temp_Path, length);
  861. return D_OK;
  862. }
  863. /**************************************************************************************/
  864. /*函数名称:UINT32 FolderStorage_C(MNG *pMng, CHAR* Path) */
  865. /*函数功能:将*.c文件夹路径存入链表 */
  866. /*参数:pMng:*.H文件夹路的链表; */
  867. /* Path:文件夹路径 */
  868. /*返回值: D_OK */
  869. /**************************************************************************************/
  870. UINT32 FolderStorage_C(MNG *pMng, CHAR* Path)
  871. {
  872. CHAR *Temp_Path = NULL;
  873. UINT32 length =strlen(Path);
  874. Temp_Path = (CHAR*) malloc( (length + 6) * sizeof(CHAR) );
  875. if(NULL == Temp_Path)
  876. {
  877. printf("memory is error!");
  878. TBLFree(pMng);
  879. free(pMng);
  880. pMng = NULL;
  881. Free_Memory(Path);
  882. exit(0);
  883. }
  884. memset(Temp_Path, 0, length);
  885. memcpy(Temp_Path, Path, length);
  886. *(Temp_Path + length) = '\0';
  887. strcat(Temp_Path, "\\*.c");
  888. pMng = File_Find(pMng, Path, Temp_Path, length);
  889. return D_OK;
  890. }
  891. /**************************************************************************************/
  892. /*函数名称:MNG* File_Find(MNG *pMng, CHAR *Path, CHAR *Temp_Path, UINT32 length */
  893. /*函数功能:遍历文件,并存入路径信息 */
  894. /*参数:pMng:文件路径的链表 */
  895. /* Path:文件路径 */
  896. /* Temp_Path:临时文件路径 */
  897. /* length:文件路径长度 */
  898. /*返回值: D_OK */
  899. /**************************************************************************************/
  900. MNG* File_Find(MNG *pMng, CHAR *Path, CHAR *Temp_Path, UINT32 length)
  901. {
  902. long Handle;
  903. struct _finddata_t FileInfo;
  904. if((Handle = _findfirst(Temp_Path, &FileInfo)) == -1L) /* 查找该类型的第一个文件 */
  905. {
  906. printf("NO find the file!\n");
  907. TBLFree(pMng);
  908. free(pMng);
  909. pMng = NULL;
  910. Free_Memory(Path);
  911. Free_Memory(Temp_Path);
  912. exit(0);
  913. }
  914. else
  915. {
  916. Free_Memory(Temp_Path);
  917. CHAR *D_FilePath = NULL; /* 文件路径指针 */
  918. UINT32 len = length + strlen(FileInfo.name) + 2; /* 文件路径的长度 */
  919. D_FilePath = (CHAR*) malloc( len * sizeof(CHAR) );
  920. if(NULL == D_FilePath)
  921. {
  922. printf("path is error!n");
  923. TBLFree(pMng);
  924. free(pMng);
  925. pMng = NULL;
  926. Free_Memory(Path);
  927. _findclose(Handle);
  928. exit(0);
  929. }
  930. memset(D_FilePath, 0, len);
  931. memcpy(D_FilePath, Path, length);
  932. strcat(D_FilePath, FileInfo.name);
  933. LTBL *Pf = NULL; /* 指向第一个链表的指针 */
  934. LTBL *Pn = NULL; /* 指向下一个链表的指针 */
  935. Pf = pMng->pHead ;
  936. strcpy_s(Pf->PATH, D_FilePath); /* 将路径复制到链表地址 */
  937. pMng->nNodeCnt ++;
  938. Pf ->pNxt = NULL;
  939. while(_findnext(Handle, &FileInfo) == 0) /* 继续查找此类型后面的文件 */
  940. {
  941. Free_Memory(D_FilePath);
  942. len = length + strlen(FileInfo.name) + 2;
  943. D_FilePath = (CHAR*) malloc( len * sizeof(CHAR) );
  944. if(NULL == D_FilePath)
  945. {
  946. printf("memory is error!n");
  947. TBLFree(pMng);
  948. free(pMng);
  949. pMng = NULL;
  950. Free_Memory(Path);
  951. _findclose(Handle);
  952. exit(0);
  953. }
  954. memset(D_FilePath, 0, len);
  955. memcpy(D_FilePath, Path, length);
  956. strcat(D_FilePath, FileInfo.name);
  957. if ( ( Pn = (LTBL *)malloc( sizeof(LTBL) ) ) != NULL)
  958. {
  959. strcpy_s(Pn->PATH, D_FilePath); /* 将路径复制到链表地址 */
  960. Pf->pNxt = Pn;
  961. Pf = Pn;
  962. pMng->nNodeCnt ++;
  963. Pf ->pNxt = NULL;
  964. }
  965. else
  966. {
  967. printf("memory is error!n");
  968. TBLFree (pMng);
  969. free (pMng);
  970. pMng = NULL;
  971. Free_Memory(Path);
  972. _findclose(Handle);
  973. exit(0);
  974. }
  975. }
  976. Free_Memory(D_FilePath);
  977. _findclose(Handle);
  978. }
  979. return pMng;
  980. }
  981. /**************************************************************************************/
  982. /*函数名称:UINT32 Folder_Check(MNG * pMng, MNG *pMng1, CHAR* Path) */
  983. /*函数功能:文件夹判断操作 */
  984. /*参数:pMng:*.C文件夹路径的链表 */
  985. /* pMng1:*.H文件夹路径的链表 */
  986. /* Path:文件夹路径 */
  987. /*返回值: D_OK */
  988. /**************************************************************************************/
  989. UINT32 Folder_Check(MNG * pMng, MNG *pMng1, CHAR* Path)
  990. {
  991. UINT32 len = strlen(Path);
  992. CHAR *D_Path = (CHAR*) malloc( (len + 3) * sizeof(CHAR) );
  993. if(NULL == D_Path)
  994. {
  995. printf("memory is error!");
  996. exit(0);
  997. }
  998. memset(D_Path, 0, len);
  999. memcpy(D_Path, Path, len);
  1000. *(D_Path + len) = '\0';
  1001. strcat(D_Path, "\\");
  1002. if( ( (1 == Final_order[7] && 1 == Final_order[8]) || ( 0 == Final_order[7] && 0 == Final_order[8]) ) )
  1003. {
  1004. FolderStorage_C(pMng, D_Path);
  1005. pMng1 = (MNG*) malloc (sizeof(MNG));
  1006. if (NULL == pMng1)
  1007. {
  1008. printf("Memory is error!");
  1009. TBLFree(pMng);
  1010. free(pMng);
  1011. pMng = NULL;
  1012. exit(0);
  1013. }
  1014. pMng1 ->nNodeCnt = 0;
  1015. pMng1 ->pHead = NULL;
  1016. if ( ( pMng1->pHead = (LTBL *)malloc( sizeof(LTBL) ) ) == NULL)
  1017. {
  1018. printf("Memory is error!");
  1019. TBLFree(pMng);
  1020. free(pMng);
  1021. pMng = NULL;
  1022. free(pMng1);
  1023. pMng1 = NULL;
  1024. exit(0);
  1025. }
  1026. FoaderStorage_H(pMng1, D_Path);
  1027. LTBL *P = pMng->pHead;
  1028. while (P->pNxt != NULL)
  1029. {
  1030. P = P->pNxt;
  1031. }
  1032. P->pNxt = pMng1 ->pHead;
  1033. pMng->nNodeCnt += pMng1->nNodeCnt;
  1034. }
  1035. else if(1 == Final_order[7] && 0 == Final_order[8] )
  1036. {
  1037. FolderStorage_C(pMng, D_Path);
  1038. }
  1039. else if(0 == Final_order[7] && 1 == Final_order[8] )
  1040. {
  1041. FoaderStorage_H(pMng, D_Path);
  1042. }
  1043. return D_OK;
  1044. }
  1045. /**************************************************************************************/
  1046. /*函数名称:void main(UINT32 argc, CHAR* argv[]) */
  1047. /*函数功能:主函数 */
  1048. /*参数:argc:输入参数的个数 */
  1049. /* argv[]:字符串数组 */
  1050. /*返回值: D_OK */
  1051. /**************************************************************************************/
  1052. void main(UINT32 argc, CHAR* argv[])
  1053. {
  1054. clock_t start,finish;
  1055. start = clock();
  1056. UINT32 File_Type = 0;
  1057. if(argc > 1)
  1058. {
  1059. File_Type = Check_File(argc, argv);
  1060. if (File_Type == 0)
  1061. {
  1062. exit(0);
  1063. }
  1064. else
  1065. {
  1066. //do nothing
  1067. }
  1068. }
  1069. else
  1070. {
  1071. exit(0);
  1072. }
  1073. CHAR *Path = argv[1];
  1074. CHAR *File_Path = NULL;
  1075. File_Path = Storage_File_Path(argc, argv) ;
  1076. if (D_NG == Command_Handle(File_Type) )
  1077. {
  1078. printf("The progress is error!\n");
  1079. Free_Memory(Path);
  1080. exit(0);
  1081. }
  1082. MNG *pMng = NULL;
  1083. pMng = (MNG*) malloc (sizeof(MNG));
  1084. if (NULL == pMng)
  1085. {
  1086. printf("Memory is error!");
  1087. Free_Memory(Path);
  1088. exit(0);
  1089. }
  1090. pMng ->nNodeCnt = 0;
  1091. pMng ->pHead = NULL;
  1092. File_Search(File_Type, pMng, Path);
  1093. Process(pMng);
  1094. finish = clock();
  1095. if(1 == Final_order[1] )
  1096. {
  1097. Print(pMng, File_Path);
  1098. Print_time(start, finish,File_Path);
  1099. TBLFree (pMng);
  1100. void*free(pMng);
  1101. pMng = NULL;
  1102. Free_Memory(File_Path);
  1103. Free_Memory(Path);
  1104. exit(0);
  1105. }
  1106. else
  1107. {
  1108. Print(pMng, File_Path);
  1109. TBLFree (pMng);
  1110. void* free(pMng);
  1111. pMng = NULL;
  1112. Free_Memory(File_Path);
  1113. Free_Memory(Path);
  1114. exit(0);
  1115. }
  1116. }
  1117. /**************************************************************************************/
  1118. /*函数名称:void Print_time(clock_t start, clock_t finish, CHAR* File_Path) */
  1119. /*函数功能:输出时间的函数 */
  1120. /*参数:start:开始时间 */
  1121. /* finish:结束时间 */
  1122. /* File_Path:写入文件的地址 */
  1123. /*返回值: D_OK */
  1124. /**************************************************************************************/
  1125. void Print_time(clock_t start, clock_t finish, CHAR* File_Path)
  1126. {
  1127. double duration;
  1128. duration = (double)(finish - start) / CLOCKS_PER_SEC;
  1129. if (NULL != File_Path)
  1130. {
  1131. FILE *fg;
  1132. if( (fopen_s(&fg, File_Path, "a+") ) != NULL)
  1133. {
  1134. printf("open is error3!\n");
  1135. Free_Memory(File_Path);
  1136. exit(0);
  1137. }
  1138. fprintf_s( fg,"\n%f seconds\n", duration );
  1139. fclose(fg);
  1140. }
  1141. else
  1142. {
  1143. printf("%f seconds\n", duration );
  1144. }
  1145. }
复制代码


回复

使用道具 举报