C語(yǔ)言實(shí)現(xiàn)的統(tǒng)計(jì)php代碼行數(shù)功能源碼(支持文件夾、多目錄)
放假在家沒(méi)事,睡過(guò)懶覺(jué),看過(guò)電影,就想起來(lái)寫(xiě)個(gè)小程序。 統(tǒng)計(jì)php代碼的行數(shù),對(duì)于phper還是挺實(shí)用的。支持單個(gè)文件和目錄。下面是代碼和演示的例子!
/** * @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; //是否顯示每個(gè)文件的行數(shù) int main(int argc, char *argv[]) { if(argc==1){ printf("請(qǐng)?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; } //打印錯(cuò)誤信息 void print_error() { switch(error_id){ case 1: printf("該文件不存在!請(qǐng)檢查!\n"); break; case 2: printf("您沒(méi)有對(duì)該文件的讀權(quán)限!請(qǐng)檢查!\n"); break; case 3: printf("該目錄不存在!請(qǐng)檢查!\n"); break; case 4: printf("文件格式格式錯(cuò)誤,不是%s格式,請(qǐng)重試!\n",FTYPE); break; } }
演示例子:
上一篇:C++虛函數(shù)的實(shí)現(xiàn)機(jī)制分析
欄 目:C語(yǔ)言
下一篇:VC實(shí)現(xiàn)讓關(guān)閉按鈕成灰色不可用的方法
本文標(biāo)題:C語(yǔ)言實(shí)現(xiàn)的統(tǒng)計(jì)php代碼行數(shù)功能源碼(支持文件夾、多目錄)
本文地址:http://mengdiqiu.com.cn/a1/Cyuyan/3539.html
您可能感興趣的文章
- 04-02c語(yǔ)言函數(shù)調(diào)用后清空內(nèi)存 c語(yǔ)言調(diào)用函數(shù)刪除字符
- 04-02c語(yǔ)言的正則匹配函數(shù) c語(yǔ)言正則表達(dá)式函數(shù)庫(kù)
- 04-02func函數(shù)+在C語(yǔ)言 func函數(shù)在c語(yǔ)言中
- 04-02c語(yǔ)言中對(duì)數(shù)函數(shù)的表達(dá)式 c語(yǔ)言中對(duì)數(shù)怎么表達(dá)
- 04-02c語(yǔ)言用函數(shù)寫(xiě)分段 用c語(yǔ)言表示分段函數(shù)
- 04-02c語(yǔ)言編寫(xiě)函數(shù)冒泡排序 c語(yǔ)言冒泡排序法函數(shù)
- 04-02c語(yǔ)言沒(méi)有round函數(shù) round c語(yǔ)言
- 04-02c語(yǔ)言分段函數(shù)怎么求 用c語(yǔ)言求分段函數(shù)
- 04-02C語(yǔ)言中怎么打出三角函數(shù) c語(yǔ)言中怎么打出三角函數(shù)的值
- 04-02c語(yǔ)言調(diào)用函數(shù)求fibo C語(yǔ)言調(diào)用函數(shù)求階乘


閱讀排行
- 1C語(yǔ)言 while語(yǔ)句的用法詳解
- 2java 實(shí)現(xiàn)簡(jiǎn)單圣誕樹(shù)的示例代碼(圣誕
- 3利用C語(yǔ)言實(shí)現(xiàn)“百馬百擔(dān)”問(wèn)題方法
- 4C語(yǔ)言中計(jì)算正弦的相關(guān)函數(shù)總結(jié)
- 5c語(yǔ)言計(jì)算三角形面積代碼
- 6什么是 WSH(腳本宿主)的詳細(xì)解釋
- 7C++ 中隨機(jī)函數(shù)random函數(shù)的使用方法
- 8正則表達(dá)式匹配各種特殊字符
- 9C語(yǔ)言十進(jìn)制轉(zhuǎn)二進(jìn)制代碼實(shí)例
- 10C語(yǔ)言查找數(shù)組里數(shù)字重復(fù)次數(shù)的方法
本欄相關(guān)
- 04-02c語(yǔ)言函數(shù)調(diào)用后清空內(nèi)存 c語(yǔ)言調(diào)用
- 04-02func函數(shù)+在C語(yǔ)言 func函數(shù)在c語(yǔ)言中
- 04-02c語(yǔ)言的正則匹配函數(shù) c語(yǔ)言正則表達(dá)
- 04-02c語(yǔ)言用函數(shù)寫(xiě)分段 用c語(yǔ)言表示分段
- 04-02c語(yǔ)言中對(duì)數(shù)函數(shù)的表達(dá)式 c語(yǔ)言中對(duì)
- 04-02c語(yǔ)言編寫(xiě)函數(shù)冒泡排序 c語(yǔ)言冒泡排
- 04-02c語(yǔ)言沒(méi)有round函數(shù) round c語(yǔ)言
- 04-02c語(yǔ)言分段函數(shù)怎么求 用c語(yǔ)言求分段
- 04-02C語(yǔ)言中怎么打出三角函數(shù) c語(yǔ)言中怎
- 04-02c語(yǔ)言調(diào)用函數(shù)求fibo C語(yǔ)言調(diào)用函數(shù)求
隨機(jī)閱讀
- 08-05織夢(mèng)dedecms什么時(shí)候用欄目交叉功能?
- 08-05dedecms(織夢(mèng))副欄目數(shù)量限制代碼修改
- 01-10使用C語(yǔ)言求解撲克牌的順子及n個(gè)骰子
- 01-10delphi制作wav文件的方法
- 08-05DEDE織夢(mèng)data目錄下的sessions文件夾有什
- 01-11ajax實(shí)現(xiàn)頁(yè)面的局部加載
- 01-10C#中split用法實(shí)例總結(jié)
- 01-11Mac OSX 打開(kāi)原生自帶讀寫(xiě)NTFS功能(圖文
- 04-02jquery與jsp,用jquery
- 01-10SublimeText編譯C開(kāi)發(fā)環(huán)境設(shè)置