C语言实现的统计php代码行数功能源码(支持文件夹、多目录)

所属分类: 软件编程 / C 语言 阅读数: 95
收藏 0 赞 0 分享

放假在家没事,睡过懒觉,看过电影,就想起来写个小程序。 统计php代码的行数,对于phper还是挺实用的。支持单个文件和目录。下面是代码和演示的例子!

/**
 * @date     2012-12-1
 * @author bright
 * @todo     统计php代码行数
 */

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <dirent.h>  
#include <sys/stat.h>
#include <ftw.h>
#define LINESIZE 300

int fn(const char *file,const struct stat *sb,int flag);
int check_file_type(const char * file_name);
void read_file(const char *file);
int is_file();
void print_error();

int error_id=0;
char *path;
const char *FTYPE=".php";
const char *flags[]={"<?","<?php"};
const char *rflags="?>";
int line_sum=0;
int file_sum=0;
int show_one_file_line=0; //是否显示每个文件的行数

int main(int argc, char *argv[])
{
    if(argc==1){
        printf("请在命令后面添加目录或文件名!\n");
        return 0;
    }
    if(argc==3 && strcmp(argv[2],"-p")==0){
        show_one_file_line=1;
    }
        
    path=argv[1];
    
    if(is_file(path)){
        if(check_file_type(path)){
            read_file(path);
        }
    }
    else{
        ftw(path,fn,1000);
    }
    
    if(error_id<=3){
        print_error();
    }
    printf("files: %d \ttotal: %d Lines\n",file_sum,line_sum);
    return 0;
}

void read_file(const char *file_path)
{
    char arr[LINESIZE];
    int full_code=0;
    int line_num=0;
    
    FILE *fp;
    fp=fopen(file_path,"r+");
    
    while ((fgets(arr, LINESIZE, fp)) != NULL){
        int i=sizeof(flags)/4-1;
        
        if (full_code){
            if (strstr(arr,rflags)!=0){
                full_code=0;
            }
            else{
                line_num++;
            }
        }
        else{
            for (;i>=0;i--){
                if (strstr(arr,flags[i])!=0){
                    full_code=1;
                    break;
                }
            }
        }
    }

    line_sum+=line_num;
    file_sum++;
    if(show_one_file_line)
        printf("%s\t Lines:%d\t\n",file_path,line_num);
}

int fn(const char *file,const struct stat *sb,int flag)
{
    if(flag==FTW_F){
        if(is_file()==0){
            if(check_file_type(file)){
                read_file(file);
            }
        }
    }
    
    return 0;
}


//return 0: 文件; 1:目录
int is_file()
{
    int i=strlen(path);
    for (;i>=0;i--){
        if (path[i]=='.'){//文件
            if (access(path,F_OK)!=0){
                error_id=1;
            }
            else if (access(path,R_OK)!=0){
                error_id=2;
            }
        
            return 1;
        }
        else if (path[i]=='/'){//目录
            if (access(path,F_OK)!=0){
                error_id=3;
            }
            return 0;
        }
    }
    return 0;
}

//文件是否为指定格式
int check_file_type(const char * file_name)
{
    char *tmp=rindex(file_name,'.');
    if(tmp==NULL){
        return 0;
    }
    if(strcmp(tmp,FTYPE)!=0){
        error_id=4;
        return 0;
    }
    return 1;
}

//打印错误信息
void print_error()
{
    switch(error_id){
    case 1:
        printf("该文件不存在!请检查!\n");
    break;
    case 2:
        printf("您没有对该文件的读权限!请检查!\n");
    break;
    case 3:
        printf("该目录不存在!请检查!\n");
    break;
    case 4:
        printf("文件格式格式错误,不是%s格式,请重试!\n",FTYPE);
    break;
    }
}

演示例子:

更多精彩内容其他人还在看

C++中四种对象生存期和作用域以及static的用法总结分析

以下是对C++中四种对象生存期和作用域以及static的用法进行了详细的介绍,需要的朋友可以过来参考下
收藏 0 赞 0 分享

C++嵌套类与局部类详细解析

从作用域的角度看,嵌套类被隐藏在外围类之中,该类名只能在外围类中使用。如果在外围类之外的作用域使用该类名时,需要加名字限定
收藏 0 赞 0 分享

C++空类详解

以下是对C++中的空类进行了详细的介绍,需要的朋友可以过来参考下
收藏 0 赞 0 分享

C++之友元:友元函数和友元类详解

友元是一种允许非类成员函数访问类的非公有成员的一种机制。可以把一个函数指定为类的友元,也可以把整个类指定为另一个类的友元
收藏 0 赞 0 分享

C++中返回指向函数的指针示例

int (*ff(int)) (int *,int);表示:ff(int)是一个函数,带有一个int型的形参,该函数返回int (*) (int *,int),它是一个指向函数的指针,所指向的函数返回int型并带有两个分别是Int*和int型的形参
收藏 0 赞 0 分享

C数据结构之单链表详细示例分析

以下是对C语言中的单链表进行了详细的分析介绍,需要的朋友可以过来参考下
收藏 0 赞 0 分享

C数据结构之双链表详细示例分析

以下是对c语言中的双链表进行了详细的分析介绍,需要的朋友可以过来参考下
收藏 0 赞 0 分享

浅析如何在c语言中调用Linux脚本

如何在c语言中调用Linux脚本呢?下面小编就为大家详细的介绍一下吧!需要的朋友可以过来参考下
收藏 0 赞 0 分享

深入解析unsigned int 和 int

以下是对unsigned int和int进行了详细的分析介绍,需要的朋友可以过来参考下
收藏 0 赞 0 分享

浅谈C++中的string 类型占几个字节

本篇文章小编并不是为大家讲解string类型的用法,而是讲解我个人比较好奇的问题,就是string 类型占几个字节
收藏 0 赞 0 分享
查看更多