C語言實(shí)現(xiàn)的統(tǒng)計(jì)php代碼行數(shù)功能源碼(支持文件夾、多目錄)
更新時(shí)間:2014年08月05日 10:55:33 投稿:junjie
這篇文章主要介紹了C語言實(shí)現(xiàn)的統(tǒng)計(jì)php代碼行數(shù)功能源碼,支持文件夾、多級(jí)目錄的統(tǒng)計(jì),在一些環(huán)境中會(huì)用到這個(gè)功能,需要的朋友可以參考下
放假在家沒事,睡過懶覺,看過電影,就想起來寫個(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("您沒有對(duì)該文件的讀權(quán)限!請(qǐng)檢查!\n"); break; case 3: printf("該目錄不存在!請(qǐng)檢查!\n"); break; case 4: printf("文件格式格式錯(cuò)誤,不是%s格式,請(qǐng)重試!\n",FTYPE); break; } }
演示例子:
相關(guān)文章
C語言數(shù)據(jù)結(jié)構(gòu)之單鏈表存儲(chǔ)詳解
鏈表是一種物理存儲(chǔ)結(jié)構(gòu)上非連續(xù)、非順序的存儲(chǔ)結(jié)構(gòu),數(shù)據(jù)元素的邏輯順序是通過鏈表中的指針鏈接次序?qū)崿F(xiàn)的。本文將和大家一起聊聊C語言中單鏈表的存儲(chǔ),感興趣的可以學(xué)習(xí)一下2022-07-07C語言練習(xí)之?dāng)?shù)組中素?cái)?shù)交換
這篇文章主要為大家介紹了C語言數(shù)組中素?cái)?shù)交換,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下,希望能夠給你帶來幫助<BR>2021-12-12C語言深入探究sizeof與整型數(shù)據(jù)存儲(chǔ)及數(shù)據(jù)類型取值范圍
在main函數(shù)中,sizeof是可以正常工作的,但是在自定義函數(shù)中就不可以了。所以本文將為大家詳細(xì)講解一下關(guān)鍵字sizeof、整型數(shù)據(jù)存儲(chǔ)深入、數(shù)據(jù)類型取值范圍深入2022-07-07C語言進(jìn)階二叉樹的基礎(chǔ)與銷毀及層序遍歷詳解
朋友們好,這篇播客我們繼續(xù)C++的初階學(xué)習(xí),現(xiàn)在對(duì)我們對(duì)C++的二叉樹基礎(chǔ)oj與二叉樹銷毀和層序遍歷進(jìn)行練習(xí),讓我們相互學(xué)習(xí),共同進(jìn)步2022-06-06C++設(shè)計(jì)模式之策略模式(Strategy)
這篇文章主要為大家詳細(xì)介紹了C++設(shè)計(jì)模式之策略模式Strategy ,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-04-04