C语言实现的统计php代码行数功能源码(支持文件夹、多目录)
2023-06-13 09:15:40 时间
放假在家没事,睡过懒觉,看过电影,就想起来写个小程序。统计php代码的行数,对于phper还是挺实用的。支持单个文件和目录。下面是代码和演示的例子!
/** *@date 2012-12-1 *@authorbright *@todo 统计php代码行数 */ #include<stdio.h> #include<stdlib.h> #include<string.h> #include<unistd.h> #include<dirent.h> #include<sys/stat.h> #include<ftw.h> #defineLINESIZE300 intfn(constchar*file,conststructstat*sb,intflag); intcheck_file_type(constchar*file_name); voidread_file(constchar*file); intis_file(); voidprint_error(); interror_id=0; char*path; constchar*FTYPE=".php"; constchar*flags[]={"<?","<?php"}; constchar*rflags="?>"; intline_sum=0; intfile_sum=0; intshow_one_file_line=0;//是否显示每个文件的行数 intmain(intargc,char*argv[]) { if(argc==1){ printf("请在命令后面添加目录或文件名!\n"); return0; } 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:%dLines\n",file_sum,line_sum); return0; } voidread_file(constchar*file_path) { chararr[LINESIZE]; intfull_code=0; intline_num=0; FILE*fp; fp=fopen(file_path,"r+"); while((fgets(arr,LINESIZE,fp))!=NULL){ inti=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\tLines:%d\t\n",file_path,line_num); } intfn(constchar*file,conststructstat*sb,intflag) { if(flag==FTW_F){ if(is_file()==0){ if(check_file_type(file)){ read_file(file); } } } return0; } //return0:文件;1:目录 intis_file() { inti=strlen(path); for(;i>=0;i--){ if(path[i]=="."){//文件 if(access(path,F_OK)!=0){ error_id=1; } elseif(access(path,R_OK)!=0){ error_id=2; } return1; } elseif(path[i]=="/"){//目录 if(access(path,F_OK)!=0){ error_id=3; } return0; } } return0; } //文件是否为指定格式 intcheck_file_type(constchar*file_name) { char*tmp=rindex(file_name,"."); if(tmp==NULL){ return0; } if(strcmp(tmp,FTYPE)!=0){ error_id=4; return0; } return1; } //打印错误信息 voidprint_error() { switch(error_id){ case1: printf("该文件不存在!请检查!\n"); break; case2: printf("您没有对该文件的读权限!请检查!\n"); break; case3: printf("该目录不存在!请检查!\n"); break; case4: printf("文件格式格式错误,不是%s格式,请重试!\n",FTYPE); break; } }
演示例子:
相关文章
- 大型仓库进销存管理系统php源代码,多仓版进销存管理系统PHP源码ERP仓库管理系统php网络版进销存源码…
- C语言内存讲解-详说内存分布和heap空间
- 小白学习C语言 第1天
- 如何运行PHP代码_运行php网站
- C语言冒泡排序升序_c语言快速排序和冒泡排序
- 【C语言进阶】“数组指针”和“指针数组”都是啥跟啥?
- 编程Linux C语言按键编程实战(linuxc按键)
- 使用PHP操作Redis的入门实践(php中使用redis)
- PHP连接MySQL数据库指南(php连接到mysql)
- PHP在Linux下的应用突破之路(php使用linux)
- 使用PHP和MySQL精心构建表单(php表单mysql)
- 使用PHP测试Redis的最佳实践(php测试redis)
- 提升 PHP 技能,一攻 MySQL 面试!(php面试Mysql)
- Linux和C语言编程开创者——N吗?(linuxcn)
- PHP连接Microsoft SQL Server数据库技巧(php 连 mssql)
- 据使用PHP和MSSQL技术记录丰富的数据信息(php mssql记录数)
- PHP与MSSQL的融合——开启新的编程前程(php mssql 空白)
- 解决C语言与MySQL之间的连接问题(c mysql连接问题)
- 用C语言封装MySQL语句,优化功能(c mysql 语句封装)
- 用C语言连接Oracle数据库源码实现(c 链接oracle源码)
- 使用C语言连接Oracle数据库一个实践案例(c语言对接oracle)
- 代码管理MySQLC语言快速操纵MySQL,一行代码搞定(c mysql一行)
- php和js交互一例-PHP教程,PHP应用
- PHP中防止直接访问或查看或下载config.php文件的方法