代码改变世界

windows下统计某个目录下的源代码的行数

2013-03-15 23:39  张汉生  阅读(1406)  评论(0编辑  收藏  举报

 

 1 #include <stdio.h>
 2 #include <windows.h>
 3 
 4 using namespace std;
 5 
 6 int countLines(char * src){
 7   int count = 0;
 8   WIN32_FIND_DATA findFileData;
 9   char * toFind = new char[200];
10   strcpy(toFind,src);
11   strcat(toFind,"\\*.*");
12   HANDLE hFind = FindFirstFile(toFind,&findFileData);
13   if (hFind == INVALID_HANDLE_VALUE)
14     return count;
15   while(TRUE)
16     {
17       if(findFileData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY){
18     if(findFileData.cFileName[0]!='.') { //目录,并且不是隐藏目录
19       char * tmpPath = new char[200];
20       strcpy(tmpPath,src);
21       strcat(tmpPath,"\\");
22       strcat(tmpPath,findFileData.cFileName);
23       count += countLines(tmpPath);
24       delete []tmpPath;
25     }
26       }
27       else if (strstr(findFileData.cFileName,".as")!=NULL ||strstr(findFileData.cFileName,".mxml")!=NULL){ //这是我筛选出源代码文件的条件
28     char * filePath = new char[200];
29     char buffer[1024];
30     strcpy(filePath,src);
31     strcat(filePath,"\\");
32     strcat(filePath,findFileData.cFileName);
33     FILE *fp = fopen(filePath,"r"); 
34     int line = 0;
35     while (fgets(buffer,1024,fp)){
36       line++;
37     }
38     fclose(fp);
39     count += line;
40     printf("%s: %d\n",findFileData.cFileName,line);
41       }
42       if(!FindNextFile(hFind,&findFileData))
43     break;
44     }
45   return count;
46 }
47 int main(int argc, char* argv[]){
48   int lineCount = 0,i;
49   for (i=1; i<argc; i++){
50     lineCount += countLines(argv[i]);
51   }
52   printf("Total Num of Lines: %d\n",lineCount);
53   return 0;
54 }