LinuxSir.cn,穿越时空的Linuxsir!

 找回密码
 注册
搜索
热搜: shell linux mysql
楼主: cozo

我想写个给程序统计行数的程序,大家有什么建议?

[复制链接]
发表于 2004-3-23 11:30:52 | 显示全部楼层
为什么
buf[0]==0x0A
不是应该buf[n]==0x0A吗?
ps 0x0A=\0?
 楼主| 发表于 2004-3-23 12:19:22 | 显示全部楼层
"目前只能统计总文件大小,总行数,空行数和注释行数,剩下的就是实际代码行数。不过功能似乎太简单了。我想加上函数个数,变量个数,php和html代码的行数,再增强一下对注释的处理。"

不是只有数行数这么简单的啊。
发表于 2004-3-23 13:29:39 | 显示全部楼层
0x0A就是\n
printf("0x%.2x\n", '\n');
发表于 2004-3-23 17:51:23 | 显示全部楼层
最初由 dancingpig 发表
为什么
buf[0]==0x0A
不是应该buf[n]==0x0A吗?
ps 0x0A=\0?

我也不懂,请教大家!!
发表于 2004-3-23 19:41:23 | 显示全部楼层
我觉得这种程序用perl写比较好,
大概写了一个,可以把PHP和HTML分开,以此为基础应该可以做很多事,
这个程序只是示意,只能处理<?php ... ?>标签
$isphp记录当前处于PHP还是HTML内,
@PHP和@HTML保存代码,这个程序最后只是打印了一下行数和内容,不过可以在这个基础上增加别的功能

  1. #! /usr/bin/perl -w

  2. $isphp = 0;
  3. while(<>){
  4.   chomp;
  5.   if($isphp){
  6.     if(/(.*)\?>(.*)/){
  7.       push(@php, $1) if $1 ne "";
  8.       push(@html, $2) if $2 ne "";
  9.       $isphp = 0;
  10.     }else{
  11.       push(@php, $_);
  12.     }
  13.   }else{
  14.     if(/(.*)<\?php(.*)/){
  15.       push(@html, $1) if $1 ne "";
  16.       push(@php, $2) if $2 ne "";
  17.       $isphp = 1;
  18.     }else{
  19.       push(@html, $_);
  20.     }
  21.   }
  22. }
  23. $php = join("\n", @php);
  24. $html = join("\n", @html);
  25. print "PHP:", @php+0, " lines\n", $php, "\n----------\n\n";
  26. print "HTML:", @html+0, " lines\n", $html, "\n";

复制代码

当然,程序还有很多问题,比如一行里出现两个或以上<?php标签就不行了,等等
只是抛砖引玉,呵呵
发表于 2004-3-23 23:36:58 | 显示全部楼层
最初由 devel 发表
我也不懂,请教大家!!

擦汗,你问的还没搞懂啊……
发表于 2004-3-24 10:31:18 | 显示全部楼层
最初由 dancingpig 发表
擦汗,你问的还没搞懂啊……


不懂哦,谁也没说清楚。。。。。:confused:
发表于 2004-3-25 21:11:41 | 显示全部楼层
刚才没什么事,就用C写了一个

  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. #include <sys/types.h>
  5. #include <sys/stat.h>
  6. #include <fcntl.h>
  7. #include <unistd.h>

  8. #define BUFSIZE 1024

  9. int codesize; /* 代码总大小 */
  10. int codebuf;  /* 代码缓冲区大小 */
  11. char *code;   /* 保存代码的缓冲区 */
  12. char *php;    /* 保存PHP代码的缓冲区 */
  13. char *html;   /* 保存HTML代码的缓冲区 */

  14. void getcode(int); /* 从文件获取代码 */
  15. void spcode(void); /* 把代码分开成PHP和HTML */
  16. int getLineNum(char *); /* 计算行数 */

  17. int
  18. main(int argc, char *argv[])
  19. {
  20.   int fd;
  21.   int i;
  22.   
  23.   /* 处理命令行参数,如果没有参数就读标准输入 */
  24.   if(argc == 1)
  25.     getcode(STDIN_FILENO);
  26.   else
  27.     for(i = 1; i < argc; ++i){
  28.       if((fd = open(argv[i], O_RDONLY)) < 0){
  29.         fprintf(stderr, "cannot open %s\n", argv[i]);
  30.         continue;
  31.       }
  32.       getcode(fd);
  33.       close(fd);
  34.     }
  35.   code[codesize] = '\0';
  36.   /* 为了简单,所以分配的空间有点浪费 */
  37.   php = (char *)malloc(codesize + 1);
  38.   html = (char *)malloc(codesize + 1);
  39.   spcode();

  40.   printf("Total %d lines\n", getLineNum(code));
  41.   printf("PHP: %d lines\n%s\n", getLineNum(php), php);
  42.   printf("HTML: %d lines\n%s\n", getLineNum(html), html);
  43.   
  44.   free(code);
  45.   free(php);
  46.   free(html);
  47.   exit(0);
  48. }

  49. void
  50. getcode(int fd)
  51. {
  52.   char buf[BUFSIZE];
  53.   int n;
  54.   
  55.   while((n = read(fd, buf, BUFSIZE)) > 0){
  56.     if(codesize + n > codebuf){
  57.       codebuf += BUFSIZE;
  58.       code = (char *)realloc(code, codebuf + 1);
  59.     }
  60.     memcpy(code + codesize, buf, n);
  61.     codesize += n;
  62.   }
  63.   if(n < 0){
  64.     fprintf(stderr, "read error");
  65.     exit(-1);
  66.   }
  67. }

  68. void
  69. spcode(void)
  70. {
  71.   /* h: HTML区指针; p: PHP区指针; c: code区指针; s: 搜索用指针 */
  72.   char *h, *p, *c, *s;
  73.   /* PHP代码起始和结束标签 */
  74.   const char *label;
  75.   const char * const begin = "<?php";
  76.   const char * const end = "?>";
  77.   int isphp = 0;

  78.   h = html;
  79.   p = php;
  80.   c = code;
  81.   label = begin;
  82.   while(s = strstr(c, label)){
  83.     if(isphp){
  84.       memcpy(p, c, s - c);
  85.       p += s - c;
  86.       c = s + strlen(label);
  87.       isphp = 0;
  88.       label = begin;
  89.     }else{
  90.       memcpy(h, c, s - c);
  91.       h += s - c;
  92.       c = s + strlen(label);
  93.       isphp = 1;
  94.       label = end;
  95.     }
  96.     /* 如果PHP标签独占一行,不计算这一行 */
  97.     if(*c == '\n')
  98.       c++;
  99.   }
  100.   *p = '\0';
  101.   strcpy(h, c);
  102.   *(h + strlen(c)) = '\0';
  103. }

  104. int
  105. getLineNum(char *s)
  106. {
  107.   int n = 0;
  108.   
  109.   /* 如果代码(C、P、H)为空,返回0,否则会返回1 */
  110.   if(*s == '\0')
  111.     return(0);
  112.   while(*s)
  113.     if(*s++ == '\n')
  114.       n++;
  115.   /* 如果代码最后不是\n,也应该算一行 */
  116.   if(*(s - 1) != '\n')
  117.     n++;
  118.   return(n);
  119. }
复制代码
发表于 2004-3-25 21:30:45 | 显示全部楼层
0x0a是什么意思啊。。。
为什么是从0开始不是n开始。。。。
发表于 2004-3-25 21:45:38 | 显示全部楼层
0x0a就是'\n'
buf[0]是因为只读入了一个字符

  1. while((n=read(fd,buf,1))>0)
  2.     {
  3.         if(buf[0]==0x0A)
复制代码
您需要登录后才可以回帖 登录 | 注册

本版积分规则

快速回复 返回顶部 返回列表