您的位置:首页 > 编程语言

递归统计项目中的非空白代码行数

2013-01-26 17:32 495 查看
在准备阅读一个开源项目的代码前,可以大约看看整个项目共有多少代码,估计项目的规模。我就写了一个简单的程序来达到此目的,其中的一些代码参考了apue中的代码。

代码如下:

View Code

//程序功能:统计一个文件夹(一个项目)中所有文件的有效代码行数(除去空白行)。

#include <apue.h>
#include <dirent.h>
#include <limits.h>
#include <string.h>
#include <fcntl.h>
#include <unistd.h>
#define MAXBUF 5000

typedef int Myfunc(const char *,const struct stat *,int);

static Myfunc myfunc;
static int    myftw(char *,Myfunc *);
static int    dopath(Myfunc *);
static int    sum_lines=0;//the sum of no empty lines

static long    nreg, ndir, nblk, nchr, nfifo, nslink, nsock, ntot;

int
main(int argc, char *argv[])
{
int        ret;

if (argc != 2)
err_quit("usage:  ftw  <starting-pathname>");

ret = myftw(argv[1], myfunc);        /* does it all */

printf("\n\nSum lines: %d lines\n\n\n",sum_lines);
exit(ret);
}

/*
* Descend through the hierarchy, starting at "pathname".
* The caller's func() is called for every file.
*/
#define    FTW_F    1        /* file other than directory */
#define    FTW_D    2        /* directory */
#define    FTW_DNR    3        /* directory that can't be read */
#define    FTW_NS    4        /* file that we can't stat */

static char    *fullpath;        /* contains full pathname for every file */

static int                    /* we return whatever func() returns */
myftw(char *pathname, Myfunc *func)
{
int len;
fullpath = path_alloc(&len);    /* malloc's for PATH_MAX+1 bytes */
/* ({Prog pathalloc}) */
strncpy(fullpath, pathname, len);    /* protect against */
fullpath[len-1] = 0;                /* buffer overrun */

return(dopath(func));
}

static int dopath(Myfunc * func)
{
struct stat    statbuf;
struct dirent  *dirp;
DIR            *dp;
int            ret;
char           *ptr;

if(lstat(fullpath,&statbuf)<0)
return (func(fullpath,&statbuf,FTW_NS));
if(S_ISDIR(statbuf.st_mode)==0)
return (func(fullpath,&statbuf,FTW_F));

if( (ret=func(fullpath,&statbuf,FTW_D))!=0 )
return ret;

ptr=fullpath+strlen(fullpath);
*ptr++='/';
*ptr=0;

if((dp=opendir(fullpath))==NULL)
return (func(fullpath,&statbuf,FTW_DNR));
while((dirp=readdir(dp))!=NULL)
{
if(strcmp(dirp->d_name,".")==0 || strcmp(dirp->d_name,"..")==0)
continue;
strcpy(ptr,dirp->d_name);
if(ret=dopath(func)!=0)
return ret;
}
ptr[-1]=0;
if(closedir(dp)<0)
err_ret("can't close directory %s",fullpath);
return ret;
}

static int
myfunc(const char *pathname, const struct stat *statptr, int type)
{
switch (type) {
case FTW_F:
{
int fd=0;
int num=0;
char buf[MAXBUF];
int file_lines=0;//current file lines
if((fd=open(pathname,O_RDONLY))<0)
err_sys("error open for %s\n",pathname);
while((num=read(fd,buf,4096))!=0)
{
int index;
char previous_c=buf[0];
char current_c;
for(index=1;index<num;index++)
{
current_c=buf[index];
if(previous_c!='\n' && current_c=='\n')
sum_lines++,file_lines++;
previous_c=current_c;
}
}
if(close(fd)==-1)
err_sys("error close for %s",pathname);
printf("%s:%d lines\n",pathname,file_lines);
}
break;

case FTW_D:
ndir++;
break;

case FTW_DNR:
err_ret("can't read directory %s", pathname);
break;

case FTW_NS:
err_ret("stat error for %s", pathname);
break;

default:
err_dump("unknown type %d for pathname %s", type, pathname);
}

return(0);
}


编译方法:

gcc -g -o count_line count_line.c fig2_15.c error.c


其中fig2_15.c和error.c来自apue,分别定义了一些简单的函数和错误处理函数。

输入样例:在redis这个开源项目的文件夹之下,输入:

./count_line src


输出:

src/rio.h:85 lines
src/crc16.c:85 lines
src/zipmap.h:46 lines
src/intset.h:46 lines
src/crc64.c:186 lines
src/adlist.h:82 lines
.....省略

Sum lines: 36108 lines


参考资料:apue

如果你觉得我的文章对你有帮助,请推荐一下,非常感谢!
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: