放假在家没事,睡过懒觉,看过电影,就想起来写个小程序。 统计php代码的行数,对于phper还是挺实用的。支持单个文件和目录。下面是代码和演示的例子!
?
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
|
/**
* @date 2012-12-1
* @author bright
* @todo 统计php代码行数
*/
#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; //是否显示每个文件的行数
int main(int argc, char *argv[])
{
if(argc==1){
printf("请在命令后面添加目录或文件名!\\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("您没有对该文件的读权限!请检查!\\n");
break;
case 3:
printf("该目录不存在!请检查!\\n");
break;
case 4:
printf("文件格式格式错误,不是%s格式,请重试!\\n",FTYPE);
break;
}
}
|
演示例子:
相关文章
猜你喜欢
- 个人网站搭建:如何挑选具有弹性扩展能力的服务器? 2025-06-10
- 个人服务器网站搭建:如何选择适合自己的建站程序或框架? 2025-06-10
- 64M VPS建站:能否支持高流量网站运行? 2025-06-10
- 64M VPS建站:怎样选择合适的域名和SSL证书? 2025-06-10
- 64M VPS建站:怎样优化以提高网站加载速度? 2025-06-10
TA的动态
- 2025-07-10 怎样使用阿里云的安全工具进行服务器漏洞扫描和修复?
- 2025-07-10 怎样使用命令行工具优化Linux云服务器的Ping性能?
- 2025-07-10 怎样使用Xshell连接华为云服务器,实现高效远程管理?
- 2025-07-10 怎样利用云服务器D盘搭建稳定、高效的网站托管环境?
- 2025-07-10 怎样使用阿里云的安全组功能来增强服务器防火墙的安全性?
快网idc优惠网
QQ交流群
您的支持,是我们最大的动力!
热门文章
-
2025-06-04 83
-
2025-05-29 62
-
2025-05-25 37
-
2025-05-29 12
-
2025-05-27 40
热门评论


