C語言實現(xiàn)的統(tǒng)計php代碼行數(shù)功能源碼(支持文件夾、多目錄)
更新時間:2014年08月05日 10:55:33 投稿:junjie
這篇文章主要介紹了C語言實現(xiàn)的統(tǒng)計php代碼行數(shù)功能源碼,支持文件夾、多級目錄的統(tǒng)計,在一些環(huán)境中會用到這個功能,需要的朋友可以參考下
放假在家沒事,睡過懶覺,看過電影,就想起來寫個小程序。 統(tǒng)計php代碼的行數(shù),對于phper還是挺實用的。支持單個文件和目錄。下面是代碼和演示的例子!
/**
* @date 2012-12-1
* @author bright
* @todo 統(tǒng)計php代碼行數(shù)
*/
#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; //是否顯示每個文件的行數(shù)
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("您沒有對該文件的讀權(quán)限!請檢查!\n");
break;
case 3:
printf("該目錄不存在!請檢查!\n");
break;
case 4:
printf("文件格式格式錯誤,不是%s格式,請重試!\n",FTYPE);
break;
}
}
演示例子:

相關(guān)文章
關(guān)于C++內(nèi)存中字節(jié)對齊問題的詳細(xì)介紹
本篇文章是對C++內(nèi)存中字節(jié)對齊的問題進行了詳細(xì)的分析與總結(jié)。需要的朋友參考下2013-05-05
C語言錯誤信息報告strerror函數(shù)和perror函數(shù)詳解
這篇文章主要介紹了C語言錯誤信息報告strerror函數(shù)和perror函數(shù),本文給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2023-03-03
關(guān)于C++類的成員初始化列表的相關(guān)問題
下面小編就為大家?guī)硪黄P(guān)于C++類的成員初始化列表的相關(guān)問題。小編覺得挺2016-05-05
C++中構(gòu)造函數(shù)與析構(gòu)函數(shù)的詳解及其作用介紹
這篇文章主要介紹了C++中構(gòu)造函數(shù)與析構(gòu)函數(shù)的詳解及其作用介紹,本文給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2021-09-09

