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;
}
}
演示例子:

相關(guān)文章
C++實(shí)現(xiàn)封裝的順序表的操作與實(shí)踐
在程序設(shè)計(jì)中,順序表是一種常見(jiàn)的線(xiàn)性數(shù)據(jù)結(jié)構(gòu),通常用于存儲(chǔ)具有固定順序的元素,與鏈表不同,順序表中的元素是連續(xù)存儲(chǔ)的,因此訪(fǎng)問(wèn)速度較快,但插入和刪除操作的效率可能較低,本文將詳細(xì)介紹如何用 C++ 語(yǔ)言實(shí)現(xiàn)一個(gè)封裝的順序表類(lèi),深入探討順序表的核心操作2025-02-02
關(guān)于C++內(nèi)存中字節(jié)對(duì)齊問(wèn)題的詳細(xì)介紹
本篇文章是對(duì)C++內(nèi)存中字節(jié)對(duì)齊的問(wèn)題進(jìn)行了詳細(xì)的分析與總結(jié)。需要的朋友參考下2013-05-05
C語(yǔ)言錯(cuò)誤信息報(bào)告strerror函數(shù)和perror函數(shù)詳解
這篇文章主要介紹了C語(yǔ)言錯(cuò)誤信息報(bào)告strerror函數(shù)和perror函數(shù),本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2023-03-03
關(guān)于C++類(lèi)的成員初始化列表的相關(guān)問(wèn)題
下面小編就為大家?guī)?lái)一篇關(guān)于C++類(lèi)的成員初始化列表的相關(guān)問(wèn)題。小編覺(jué)得挺2016-05-05
C++中構(gòu)造函數(shù)與析構(gòu)函數(shù)的詳解及其作用介紹
這篇文章主要介紹了C++中構(gòu)造函數(shù)與析構(gòu)函數(shù)的詳解及其作用介紹,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2021-09-09

