C語言實(shí)現(xiàn)的統(tǒng)計(jì)php代碼行數(shù)功能源碼(支持文件夾、多目錄)
放假在家沒事,睡過懶覺,看過電影,就想起來寫個小程序。 統(tǒng)計(jì)php代碼的行數(shù),對于phper還是挺實(shí)用的。支持單個文件和目錄。下面是代碼和演示的例子!
/**
* @date 2012-12-1
* @author bright
* @todo 統(tǒng)計(jì)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("請?jiān)诿詈竺嫣砑幽夸浕蛭募?\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;
}
}
演示例子:
上一篇:C++虛函數(shù)的實(shí)現(xiàn)機(jī)制分析
欄 目:C語言
下一篇:VC實(shí)現(xiàn)讓關(guān)閉按鈕成灰色不可用的方法
本文標(biāo)題:C語言實(shí)現(xiàn)的統(tǒng)計(jì)php代碼行數(shù)功能源碼(支持文件夾、多目錄)
本文地址:http://www.jygsgssxh.com/a1/Cyuyan/3539.html
您可能感興趣的文章
- 04-02c語言函數(shù)調(diào)用后清空內(nèi)存 c語言調(diào)用函數(shù)刪除字符
- 04-02c語言的正則匹配函數(shù) c語言正則表達(dá)式函數(shù)庫
- 04-02func函數(shù)+在C語言 func函數(shù)在c語言中
- 04-02c語言中對數(shù)函數(shù)的表達(dá)式 c語言中對數(shù)怎么表達(dá)
- 04-02c語言用函數(shù)寫分段 用c語言表示分段函數(shù)
- 04-02c語言編寫函數(shù)冒泡排序 c語言冒泡排序法函數(shù)
- 04-02c語言沒有round函數(shù) round c語言
- 04-02c語言分段函數(shù)怎么求 用c語言求分段函數(shù)
- 04-02C語言中怎么打出三角函數(shù) c語言中怎么打出三角函數(shù)的值
- 04-02c語言調(diào)用函數(shù)求fibo C語言調(diào)用函數(shù)求階乘


閱讀排行
本欄相關(guān)
- 04-02c語言函數(shù)調(diào)用后清空內(nèi)存 c語言調(diào)用
- 04-02func函數(shù)+在C語言 func函數(shù)在c語言中
- 04-02c語言的正則匹配函數(shù) c語言正則表達(dá)
- 04-02c語言用函數(shù)寫分段 用c語言表示分段
- 04-02c語言中對數(shù)函數(shù)的表達(dá)式 c語言中對
- 04-02c語言編寫函數(shù)冒泡排序 c語言冒泡排
- 04-02c語言沒有round函數(shù) round c語言
- 04-02c語言分段函數(shù)怎么求 用c語言求分段
- 04-02C語言中怎么打出三角函數(shù) c語言中怎
- 04-02c語言調(diào)用函數(shù)求fibo C語言調(diào)用函數(shù)求
隨機(jī)閱讀
- 08-05織夢dedecms什么時候用欄目交叉功能?
- 08-05dedecms(織夢)副欄目數(shù)量限制代碼修改
- 01-10使用C語言求解撲克牌的順子及n個骰子
- 01-10delphi制作wav文件的方法
- 08-05DEDE織夢data目錄下的sessions文件夾有什
- 01-11ajax實(shí)現(xiàn)頁面的局部加載
- 01-10C#中split用法實(shí)例總結(jié)
- 01-11Mac OSX 打開原生自帶讀寫NTFS功能(圖文
- 04-02jquery與jsp,用jquery
- 01-10SublimeText編譯C開發(fā)環(huán)境設(shè)置


