让你的UNIX/LINUX又多条好使的命令 --> ccnt (code count)

                  让你的UNIX/LINUX又多条好使的命令 --> ccnt (code count)

    项目上需要一个可以统计JSP文件的有效行数的工具, 来评估项目的质量,苦于没有一个可以识别JSP的注释符号("<%--"   "--%>")的工具,于是我试着写了一个,大家都用了以后反映如何呢,现在我们就跟我们的记者去一线的使用者那里了解一下情况:

     记者来到开发室看到有位程序员,上班的时间竟然不工作,在看杂志,很奇怪,就走了过去.

    记者: 这位老友,你好,打扰一下哈,见你以前老是加班,现在上班时间在在这看 <<程序员>>耍起了,你最近为什 这么轻松呢?你好安逸哦!

    A使用者:哦,你还不晓得吧,是这样的,我以前老加班,是因为我每次写好代码,项目上都要求我们代码的有效行数啊,但是就是莫得可以统计JSP文件的工具,我每次都要加班一行行的数我的文件的行数,好郁闷哦,自从我用了公司有个家伙写得个ccnt统计工具以后呢,我只要打一个命令,瞬间就完成了,我以前加一个多小时的班才能做完的事情.好方便哦,开始我都不相信我怀疑这个工具行不行哦,统计错了怎么办呢,我就又自己数一遍,果然准确无误,我就放心了,所以就有时间在这看看<<程序员>>耍起了塞! ^_^

    记者:看起来我们的一线使用的反映还是有点好哦,哦,公司健身房里,有为帅哥在拼命的运动,我们也看看他使用了我们的ccnt代码有效行数工具以后是什么想法!

    记者:哎,老兄,你好,你在健身啊,你用了我们ufolab的ccnt代码有效行数工具了没,感觉如何啊?

    B使用者:工具?你说那个害人的ccnt鬼东西啊,我他妈的就是被他害惨喽,自从我用了它,每天不用加班是不错,因为用了他.我的工作太轻松了,胃口好,睡觉香,身体也发福了,我女友都说我胖了,没有以前帅了,害得我要在这减肥,你们别打扰我,该干嘛干嘛去哈!

    记者:看起来我们竟然一不小心害了这位仁兄啊,我们带着愧疚之情离开了现场,哦,那边还有个老兄在耍手机,我们也看看他使用了我们ufolab的ccnt代码有效行数工具以后是什么想法,能不能给我们提出我们好的意见啊.

    记者:老兄,你好,你在耍手机游戏吗,你用了我们ufolab的ccnt代码有效行数工具能不能给我们提出我们好的意见啊?

    C使用者:意见?莫得意见,我太满意了,我正要感谢开发者呢,(说着就感动的泪流满面哦,我们的记者也很是奇怪,不至于这么夸张吧,心里想)是他挽救我和我女朋友的这段感情哦,我和我女朋友是大二就开始谈恋爱了,一直关系都很好的,自从毕业以后啊,因为工作上的这些烂事,要数代码的有效行数啊,所以我经常加班啊,开始她还很理解我,说:男人嘛,事业为重,有时加加班,不能陪我,没什么啊,但是因为我老是加班,有时下班回家她都睡了,我起床上班的时候他都没有还没有醒,我都不好意思打扰她睡觉,有时一连几天都看不到我人,再加上他们公司有几个他妈的帅哥老是追她,她渐渐的和我关系疏远了,那一天,好恐怖,她突然提出要和我分手,原因不是别的,就是因为我的工作老做不完,老加班,真当我后悔绝望的时候,ccnt出现了,它使我的工作效率提高了N倍,现在我每天不但不用加班,这不?上班的时候还有空给她发发短信,培养一下感情,她现在都想嫁给我了,我太高兴,太幸福了,希望你见到那个开发这个好工具的ufolab的朋友,我结婚的时候他一定要来喝我的喜酒哦!

    我们怎么也没有想到,我们的一个小小的开发辅助工具竟然能成全了他们大好因缘的功臣,好感动哦!

----------------------------------------------------------------------------------------------------------------

以下是我们的广告词:

    ccnt是我们ufolab一款非常优秀的代码有效行数的统计工具,它支持很多种文件的统计:jsp,c,cpp,pc,h,csh等等,只要是你想要的就是我们可以做到的!

好消息: 现在ccnt的中文版,English版以及日本語版的可执行程序和源代码面向全球同步发行!

你值得拥有!

谁用谁知道!

----------------------------------------------------------------------------------------------------------------

 废话少说了,现在我们的ccnt就要登场了,使用的方法我们在这给你举个统计PC文件的例子:

第一种用法:只打ccnt命令,统计当前目录下的所有文件.

[root@localhost ufolab01]# ls *.pc                          
xxxccnt1.pc  xxxccnt2.pc  xxxccnt3.pc  xxxccnt4.pc                  [root@localhost ufolab01]# ./ccnt                          
统计报告                      实行数     总行数     注释数      空行数              
------------------------------------------------------------------------------          xxxccnt1.pc                   476        610         96         38               
xxxccnt2.pc                   343        424         65         16               
xxxccnt3.pc                   460        572         75         37               
xxxccnt4.pc                   460        589         93         36               

 其他的用法就不细致说了,下面是有中文版注释的源代码,它是个C语言的文件,把它粘贴到记事本保存命名为ccnt.c,放到LINUX/UNIX的任意的目录下,然后在LINUX下用gcc -o ccnt ccnt.c,如果在UNIX下用cc -o ccnt ccnt.c命令编译以下,就可以正常使用了,如果你想使用更方便的话,在任何的目录下都可以用这个命令的话,你只要把刚才编译可执行文件,考到操作系统的环境PATH所指定的一个目录下就可以了,比如

[root@localhost ufolab01]# env | grep "PATH"
PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin:/usr/X11R6/bin:/root/bin
[root@localhost ufolab01]#

你输入上面的命令env | grep "PATH",然后你就可以把它拷贝到其中的一个目录下,比如


[root@localhost ufolab01]# cp ccnt /usr/local/sbin
[root@localhost ufolab01]#

现在你就可以使用该命令了,代码有兴趣的话,可以自己改进一下哈,记得告诉我你的好的想法哦!

/* 源代码有效行数的统计工具(第二版)              */
/* 现在支持的文件类型有:.jsp .c .cpp .pc .h .csh  */
/* 时    间:2006.12.06                           */
/* 开发组织:ufolab                               */
/* 参加人员:李祥光 等                             */
/******************************************************************************/
/* 正确的使用方法:                                                            */
/* 1.1. ccnt                                                                  */
/* 1.2. ccnt xxx1.jsp xxx2.pc xxx3.h  xxx4.csh                                */
/* 2.1. ccnt /home/Administrator/ufolab06/ /home/Administrator/ufolab05/      */
/* 2.2. ccnt /home/xxx1.jsp /home/xxx2.pc /home/ufolab05/xxx3.csh             */
/******************************************************************************/

#include<stdio.h>

int strbstr( char *s1, char * s2 );
int manager( char * pstr_path );
int fcount( FILE *pf_stream, char * str_bgn, char * str_end, char * pstr_path );
int fcount2( FILE *pf_stream, char * str_bgn );
int fkind ( FILE *pf_stream, char * str_bgn, char * str_end  ,
            char ch_flag, char *str_line, long *l_real_number,
            long *l_note_number, long *l_null_number );
int fkind2( FILE *pf_stream, char * str_bgn, char *str_line,
            long *l_real_number, long *l_note_number,
            long *l_null_number );
/******************************************************************************
 *  函数名      :    main
 *  功  能      :    统计源代码的有效行数
 *  参  数      :     args         I       输入参数的个数
 *                     argv         I       输入参数的内容
 *
 *  返回值      :     0                    正常
 *                    -1                    系统错误
*******************************************************************************/
int main( int args, char ** argv ) {
    char str_path[1000];
    char str_path_file_list[1000];
    char str_path_pwd_tmp[1000];
    char str_path_pwd[1000];
    char str_path_name[1000];
    char str_name[1000];
    char system_cmd_file_list[1000];
    char system_cmd_rm_file_list[1000];
    FILE *pf_stream_list;
    FILE *pf_stream_pwd;

    pf_stream_list = NULL;
    pf_stream_pwd = NULL;
    memset( str_path,                0x00, sizeof( str_path                ) -1 );
    memset( str_name,                0x00, sizeof( str_name                ) -1 );
    memset( str_path_name,           0x00, sizeof( str_path_name           ) -1 );
    memset( str_path_pwd,            0x00, sizeof( str_path_pwd            ) -1 );
    memset( str_path_pwd_tmp,        0x00, sizeof( str_path_pwd_tmp        ) -1 );
    memset( str_path_file_list,      0x00, sizeof( str_path_file_list      ) -1 );
    memset( system_cmd_file_list,    0x00, sizeof( system_cmd_file_list    ) -1 );
    memset( system_cmd_rm_file_list, 0x00, sizeof( system_cmd_rm_file_list ) -1 );

    printf( "统计报告                      实行数     总行数     注释数      空行数 /n" );
    printf( "------------------------------------------------------------------------------/n" );
    /* 无输入参数的时候 */
    if ( NULL == argv[1] ) {
        /* 本路径取得 */
        system( "pwd>/tmp/___ccnt_pwd_path_.txt" );
        pf_stream_pwd = fopen( "/tmp/___ccnt_pwd_path_.txt", "r" );
        if ( NULL == pf_stream_pwd ) {
            printf( "命令的输入的本路径取得错误" );
            return 1;
        }
        fgets( str_path_pwd_tmp, 1000, pf_stream_pwd );
        strncpy( str_path_pwd, str_path_pwd_tmp, strlen( str_path_pwd_tmp ) - 1 );
        fclose( pf_stream_pwd );
        system( "rm -f /tmp/___ccnt_pwd_path_.txt" );

        /* jsp_file_lsit嶌惉 */
        sprintf( system_cmd_file_list, "%s%s%s%s%s%s%s", "ls ", str_path_pwd,
                 "/", "*.jsp >", str_path_pwd, "/", "_ccnt_file_list_lxg.txt 2> /dev/null" );
        system( system_cmd_file_list );

        memset( system_cmd_file_list,    0x00, sizeof( system_cmd_file_list    ) -1 );
        sprintf( system_cmd_file_list, "%s%s%s%s%s%s%s", "ls ", str_path_pwd,
                 "/", "*.h >>", str_path_pwd, "/", "_ccnt_file_list_lxg.txt 2> /dev/null" );
        system( system_cmd_file_list );

        memset( system_cmd_file_list,    0x00, sizeof( system_cmd_file_list    ) -1 );
        sprintf( system_cmd_file_list, "%s%s%s%s%s%s%s", "ls ", str_path_pwd,
                 "/", "*.pc >>", str_path_pwd, "/", "_ccnt_file_list_lxg.txt 2> /dev/null" );
        system( system_cmd_file_list );

        memset( system_cmd_file_list,    0x00, sizeof( system_cmd_file_list    ) -1 );
        sprintf( system_cmd_file_list, "%s%s%s%s%s%s%s", "ls ", str_path_pwd,
                 "/", "*.csh >>", str_path_pwd, "/", "_ccnt_file_list_lxg.txt 2> /dev/null" );
        system( system_cmd_file_list );
        sprintf( str_path_file_list, "%s%s%s", str_path_pwd, "/", "_ccnt_file_list_lxg.txt" );
        pf_stream_list = fopen( str_path_file_list, "r" );

        if ( NULL == pf_stream_list ) {
            printf( "读文件名列表出错/n" );
            return 1;
        }

        while( !feof( pf_stream_list ) ) {
            fgets( str_name, 1000, pf_stream_list );
            if( 0 != *str_name ) {
                memset( str_path_name,'/0', sizeof( str_path_name ) );
                strncpy( str_path_name, str_name, strlen( str_name ) - 1 );
                manager( str_path_name );
                memset( str_name, 0x00, sizeof( str_name ) -1 );
                memset( str_path_name, 0x00, sizeof( str_path_name ) -1 );
            }
        }
        fclose( pf_stream_list );
        sprintf( system_cmd_rm_file_list, "%s%s%s%s", "rm -f ", str_path_pwd,
                 "/", "_ccnt_file_list_lxg.txt");
        system( system_cmd_rm_file_list );
    }
    /* 有输入参数的时候 */
    args = 1;
    while ( NULL != argv[args] ) {
        strcpy( str_path, argv[args] );
        if( NULL != strrchr( str_path, '.' ) ) {
            if( NULL != strrchr( str_path, '/' ) ) {

                /* "/home/Administrator/ufolab06/ccnt.jsp" */

                strcpy( str_path_name, str_path );
                manager( str_path_name );

            } else {
                /* "ccnt.jsp" */
                system( "pwd>/tmp/___ccnt_pwd_path_.txt" );
                pf_stream_pwd = fopen( "/tmp/___ccnt_pwd_path_.txt", "r" );
                fgets( str_path_pwd_tmp, 1000, pf_stream_pwd );
                strncpy( str_path_pwd, str_path_pwd_tmp, strlen( str_path_pwd_tmp ) - 1 );
                fclose( pf_stream_pwd );
                system( "rm -f /tmp/___ccnt_pwd_path_.txt" );
                sprintf( str_path_name, "%s%s%s", str_path_pwd, "/", argv[args]  );
                manager( str_path_name );
                memset( str_path_name, 0x00, sizeof( str_path_name ) -1 );
            }
        } else {
             if( NULL != strrchr( str_path, '/' ) ) {

                /* 参数像这样的"/home/Administrator/ufolab06" 类型的时候 */
                /* 和参数像这样的"/home/Administrator/ufolab06/" 类型的时候 */

                if( '/' == str_path[ strlen( str_path ) - 1 ] ) {
                    /* "/home/Administrator/ufolab06/" 的时候*/
                    sprintf( system_cmd_file_list, "%s%s%s%s%s", "ls ", str_path,
                         "*.jsp >", str_path, "_ccnt_file_list_lxg.txt 2> /dev/null" );
                    system( system_cmd_file_list );

                    memset( system_cmd_file_list,  0x00, sizeof( system_cmd_file_list  ) -1 );
                    sprintf( system_cmd_file_list, "%s%s%s%s%s", "ls ", str_path,
                         "*.pc >>", str_path, "_ccnt_file_list_lxg.txt 2> /dev/null" );
                    system( system_cmd_file_list );

                    memset( system_cmd_file_list,  0x00, sizeof( system_cmd_file_list  ) -1 );
                    sprintf( system_cmd_file_list, "%s%s%s%s%s", "ls ", str_path,
                         "*.csh >>", str_path, "_ccnt_file_list_lxg.txt 2> /dev/null" );
                    system( system_cmd_file_list );

                    sprintf( str_path_file_list, "%s%s", str_path, "_ccnt_file_list_lxg.txt" );

                } else {
                    /* "/home/Administrator/ufolab06" 的时候*/
                    sprintf( system_cmd_file_list, "%s%s%s%s%s%s%s", "ls ", str_path,
                             "/", "*.jsp >", str_path, "/", "_ccnt_file_list_lxg.txt 2> /dev/null" );
                    printf( "system_cmd_file_list=[%s]",system_cmd_file_list );
                    system( system_cmd_file_list );

                    memset( system_cmd_file_list,    0x00, sizeof( system_cmd_file_list    ) -1 );
                    sprintf( system_cmd_file_list, "%s%s%s%s%s%s%s", "ls ", str_path,
                             "/", "*.h >>", str_path, "/", "_ccnt_file_list_lxg.txt 2> /dev/null" );
                    printf( "system_cmd_file_list=[%s]",system_cmd_file_list );
                    system( system_cmd_file_list );

                    memset( system_cmd_file_list,    0x00, sizeof( system_cmd_file_list    ) -1 );
                    sprintf( system_cmd_file_list, "%s%s%s%s%s%s%s", "ls ", str_path,
                             "/", "*.pc >>", str_path, "/", "_ccnt_file_list_lxg.txt 2> /dev/null" );
                    printf( "system_cmd_file_list=[%s]",system_cmd_file_list );
                    system( system_cmd_file_list );

                    memset( system_cmd_file_list,    0x00, sizeof( system_cmd_file_list    ) -1 );
                    sprintf( system_cmd_file_list, "%s%s%s%s%s%s%s", "ls ", str_path,
                             "/", "*.csh >>", str_path, "/", "_ccnt_file_list_lxg.txt 2> /dev/null" );
                    system( system_cmd_file_list );

                    sprintf( str_path_file_list, "%s%s%s", str_path, "/", "_ccnt_file_list_lxg.txt" );
                }
                pf_stream_list = fopen( str_path_file_list, "r" );
                while( !feof( pf_stream_list ) ) {
                    fgets( str_name, 1000, pf_stream_list );
                    if( 0 != *str_name ) {
                        memset( str_path_name,'/0', sizeof( str_path_name ) );
                        strncpy( str_path_name, str_name, strlen( str_name ) - 1 );
                        manager( str_path_name );
                        memset( str_name, 0x00, sizeof( str_name ) -1 );
                        memset( str_path_name, 0x00, sizeof( str_path_name ) -1 );
                    }
                }
                fclose( pf_stream_list );
                sprintf( system_cmd_rm_file_list, "%s%s%s%s", "rm -f ", str_path,
                         "/", "_ccnt_file_list_lxg.txt");
                system( system_cmd_rm_file_list );
            } else {
                printf("正确的使用方法:/n" );
                printf("****************************************************************************/n");
                printf("ccnt                                                                       */n");
                printf("ccnt xxx1.jsp xxx2.pc  xxx3.h  xxx4.csh                                    */n");
                printf("ccnt /home/ufolab06/ /home/ufolab05/ /home/ufolab04/                       */n");
                printf("ccnt /home/xxx1.jsp /home/xxx2.pc /home/xxx3.h /home/xxx4.csh              */n");
                printf("****************************************************************************/n");
            }
        }
        args ++;
    }
    return 0;
}

/*******************************************************************************
 *  函数名      :    manager
 *  功能        :    解析源代码的类型
 *  参数        :     pstr_path       I       源代码存放的完全路径
 *
 *  返回值      :     0                    正常
 *                    -1                    系统错误
*******************************************************************************/
int manager( char * pstr_path ) {
    int i_sub;
    int i_name_len;
    char str_name[1000];
    FILE  *pf_stream;

    i_sub = 0;
    i_name_len = 0;
    pf_stream = NULL;
    memset( str_name, 0x00, sizeof( str_name ) -1 );

    i_name_len = strlen( pstr_path ) - ( ( int )strrchr( pstr_path, '.' ) - ( int ) pstr_path ) - 1;
    strncpy( str_name, strrchr( pstr_path, '.' ) + 1, i_name_len );

    if ( 0 == strcmp( str_name, "jsp" ) ) {
        pf_stream = fopen( pstr_path, "r" );
        if ( NULL == pf_stream ) {
            printf( "代码文件[%s]不存在/n/a", pstr_path );
            return 1;
        }
        i_sub = fcount( pf_stream, "<%--", "--%>", pstr_path );
        fclose( pf_stream );
    } else if  ( ( 0 == strcmp( str_name, "pc" ) ) || ( 0 == strcmp( str_name, "h" ) ) ) {
        pf_stream = fopen( pstr_path, "r" );
        if ( NULL == pf_stream ) {
            printf( "代码文件[%s]不存在/n/a", pstr_path );
            return 1;
        }
        i_sub = fcount( pf_stream, "/*", "*/", pstr_path );
        fclose( pf_stream );
    } else if  ( 0 == strcmp( str_name, "csh" ) ) {
        pf_stream = fopen( pstr_path, "r" );
        if ( NULL == pf_stream ) {
            printf( "代码文件[%s]不存在/n/a", pstr_path );
            return 1;
        }
        i_sub = fcount2( pf_stream, "#" );
        fclose( pf_stream );
    } else {
        printf( "不好意思,暂时还不能统计%s类型的文件/n", str_name );
        printf( "给我们发EMAIL,好加上去啊:ufosoft2@gmail.com^_^/n/a" );
    }
}
/*******************************************************************************
 *  函数名      :    fcount2
 *  功  能      :    计算行数
 *  参  数      :     pf_stream       I       文件指针
 *                     str_bgn         I       注释符号
 *
 *  返回值      :     0                    正常
 *                    -1                    系统错误
*******************************************************************************/
int fcount2( FILE *pf_stream, char * str_bgn ) {
    int i_sub;
    long l_line_number;   /* 总行数 */
    long l_line_number1;  /* 总行数 */
    long l_null_number;   /* 空行的行数 */
    long l_real_number;   /* 有用行的行数 */
    long l_note_number;   /* 注释行的行数 */
    char ch_flag;
    char str_line[90000];

    i_sub   = 0;
    ch_flag = 'W';
    l_line_number = 0;
    l_null_number = 0;
    l_real_number = 0;
    l_note_number = 0;
    memset( str_line, 0x00, sizeof( str_line ) );
    while( !feof( pf_stream ) ) {
        fgets( str_line, 1000, pf_stream );
        if ( 0 != *str_line ) {
            l_line_number ++;
            i_sub = fkind2(  pf_stream, str_bgn, str_line,
                            &l_real_number, &l_note_number, &l_null_number
                         );

            memset( str_line, 0x00, sizeof( str_line ) );
        }
    }
    if ( 0 == i_sub ) {
        printf( "摑寁曬崘:憤峴悢[%d]", l_line_number );
        printf( "  幚峴悢[%d]", l_real_number );
        printf( "  拲峴悢[%d]", l_note_number );
        printf( "  嬻峴悢[%d]/n", l_null_number );
    }
    return 0;
}
/*******************************************************************************
 *  函数名      :   fcount
 *  功能        :   计算行数
 *  参数        :   pf_stream       I       文件指针
 *                   str_bgn         I       左注释符
 *                   str_end         I       右注释符
 *                   pstr_path       I       文件全路径
 *
 *  返回值      :     0                     正常
 *                    -1                    系统错误
*******************************************************************************/
int fcount( FILE *pf_stream, char * str_bgn, char * str_end, char * pstr_path ){
    int i_sub;
    int i_name_len;
    long l_line_number;   /* 总行数 */
    long l_line_number1;  /* 总行数 */
    long l_null_number;   /* 空行的行数 */
    long l_real_number;   /* 有用行的行数 */
    long l_note_number;   /* 注释行的行数 */
    char ch_flag;
    char str_line[90000];
    char str_name[100];

    i_sub   = 0;
    i_name_len = 0;
    ch_flag = 'W';
    l_line_number = 0;
    l_null_number = 0;
    l_real_number = 0;
    l_note_number = 0;
    memset( str_line, 0x00, sizeof( str_line ) );
    memset( str_name, 0x00, sizeof( str_name ) );
    while( !feof( pf_stream ) ) {
        fgets( str_line, 1000, pf_stream );
        if ( 0 != *str_line ) {
            l_line_number ++;

            if (   ( 0 != ( strbstr( str_line, str_bgn ) ) )
                && ( 0 == ( strbstr( str_line, str_end ) ) )
                ) {
                ch_flag = 'N';
            } else if ( ( 0 == ( strbstr( str_line, str_bgn ) ) )
                    && ( 0 != ( strbstr( str_line, str_end ) ) )
                    ) {
                ch_flag = 'W';
            } else if ( ( 0 != ( strbstr( str_line, str_bgn ) ) )
                    && ( 0 != ( strbstr( str_line, str_end ) ) )
                    ) {
                if ( ( strbstr( str_line, str_bgn ) ) >
                     ( strbstr( str_line, str_end ) )
                   ) {
                    ch_flag = 'N';

                } else {
                    ch_flag = 'W';
                }
            } else {
                /* 不变 */
            }
            i_sub = fkind(  pf_stream, str_bgn, str_end, ch_flag, str_line,
                            &l_real_number, &l_note_number, &l_null_number
                         );

            memset( str_line, 0x00, sizeof( str_line ) );
        }
    }
    if ( 0 == i_sub ) {
        i_name_len = strlen( pstr_path ) - ( ( int )strrchr( pstr_path, '/' ) - ( int ) pstr_path ) - 1;
        strncpy( str_name, strrchr( pstr_path, '/' ) + 1, i_name_len );
        printf( "%-30s%d%11d%11d%11d/n", str_name, l_real_number, l_line_number, l_note_number, l_null_number );
    }
    return 0;
}

/*******************************************************************************
 *  函数名      :    fkind2
 *  功  能      :    行的种类的判定
 *  参  数      :     pf_stream       I       文件指针
 *                     str_bgn         I       注释符号
 *                     str_line        I       这行的内容
 *                     l_real_number   O       实际的行数
 *                     l_note_number   O       注释行行数
 *                     l_null_number   O       空行的行数
 *
 *  返回值     :     0                         正常
 *                    -1                        系统错误
*******************************************************************************/
int fkind2( FILE *pf_stream, char * str_bgn,
           char *str_line, long *l_real_number, long *l_note_number,
           long *l_null_number
         ) {
    long l_os_flag;
    long l_bgn_flag;
    long l_end_flag;
    l_os_flag  = 0;
    l_bgn_flag = 1;
    l_end_flag = 1;
    if ( 13 == str_line[strlen( str_line ) - 2 ] ) {
        //printf( " --------------------------------------Dos僞僀僾偱偡/n" );
        l_os_flag = 0; /* Dos */
    } else {
        //printf( " --------------------------------------Unix僞僀僾偱偡/n" );
        l_os_flag = 1; /* Unix */
    }
    if ( ( 13 == *str_line ) || ( 10 == *str_line ) ) {
        //printf( "偙偺峴偼 null line 偱偡/n" );
        ++ *l_null_number;
    } else if ( 0 != ( strstr( str_line, str_bgn ) ) ) {
        if ( ( strstr( str_line, str_bgn ) ) == ( int )str_line  ) {
            l_bgn_flag = 1;
        } else {
            while ( strstr( str_line, str_bgn ) > ( int )str_line
                    && ( 1 == l_bgn_flag ) ) {
                if ( ( 32 == *( str_line ) ) || ( 9 == *( str_line ) ) ) {
                    str_line ++;
                    l_bgn_flag = 1;
                } else {
                    l_bgn_flag = 0;
                }
            }
        }
        if ( 1 == l_bgn_flag ) {
            //printf( "偙偺峴偼 note line 偱偡/n" );
            ++ *l_note_number;
        } else {
            //printf( "偙偺峴偼 real line 偱偡/n" );
            ++ *l_real_number;
        }
    } else {
        ++ *l_real_number;
        //printf( "偙偺峴偼 real line 偱偡/n" );
        //printf( "偙偺庬椶峴偼傑偨夝愅丅丅丅/n/a" );
    }

    return 0;
}
/*******************************************************************************
 *  函数名      :    fkind
 *  功  能      :    行的种类的判定
 *  参  数      :     pf_stream       I       文件指针
 *                     str_bgn         I       左注释符号
 *                     str_end         I       右注释符号
 *                     str_line        I       这行的内容
 *                     l_real_number   O       实际的行数
 *                     l_note_number   O       注释行行数
 *                     l_null_number   O       空行的行数
 *
 *  返回值     :     0                         正常
 *                    -1                        系统错误
*******************************************************************************/
int fkind( FILE *pf_stream, char * str_bgn, char * str_end, char ch_flag,
           char *str_line, long *l_real_number, long *l_note_number,
           long *l_null_number
         ) {
    long l_os_flag;
    long l_bgn_flag;
    long l_end_flag;
    l_os_flag  = 0;
    l_bgn_flag = 1;
    l_end_flag = 1;
    if ( 13 == str_line[strlen( str_line ) - 2 ] ) {
        //printf( " --------------------------------------Dos文件格式的文件/n" );
        l_os_flag = 0; /* Dos */
    } else {
        //printf( " --------------------------------------Unix文件格式的文件/n" );
        l_os_flag = 1; /* Unix */
    }
    if ( 'N' == ch_flag ) {
        if ( ( 13 == *str_line ) || ( 10 == *str_line ) ) {
            ++ *l_null_number;
        } else if (  ( 0 == ( strstr( str_line, str_bgn ) ) )
                  && ( 0 == ( strstr( str_line, str_end ) ) )
                  ) {
            ++ *l_note_number;
        } else if (  ( 0 != ( strstr( str_line, str_bgn ) ) )
                  && ( 0 == ( strstr( str_line, str_end ) ) )
                  ) {
            if ( ( strstr( str_line, str_bgn ) ) == ( int )str_line  ) {
                l_bgn_flag = 1;
            } else {
                while ( strstr( str_line, str_bgn ) > ( int )str_line
                        && ( 1 == l_bgn_flag )
                      ) {
                    if ( ( 32 == *( str_line ) ) || ( 9 == *( str_line ) ) ) {
                        str_line ++;
                        l_bgn_flag = 1;
                    } else {
                        l_bgn_flag = 0;
                    }
                }
            }
            if ( 1 == l_bgn_flag ) {
                //printf( "这行是注释行/n" );
                ++ *l_note_number;
            } else {
                //printf( "这行是有效行/n" );
                ++ *l_real_number;
            }
        } else if (  ( 0 != ( strstr( str_line, str_bgn ) ) )
                  && ( 0 != ( strstr( str_line, str_end ) ) )
                  ) {
            while ( strstr( str_line, str_bgn ) > ( int )str_line
                    && ( 1 == l_bgn_flag )
                  ) {
                //printf("lxg1=[%d][%c]/n", *str_line, *str_line );
                if ( ( 32 == *( str_line ) ) || ( 9 == *( str_line ) ) ) {
                    str_line ++;
                    l_bgn_flag = 1;
                } else {
                    l_bgn_flag = 0;
                }
            }
            if ( 1 == l_bgn_flag ) {
                //printf( "这行是注释行/n" );
                ++ *l_note_number;
            } else {
                //printf( "这行是有效行/n" );
                ++ *l_real_number;
            }
        } else {
            printf( "偙偺庬椶峴偼傑偨夝愅丅丅丅/n/a" );
        }
    } else {
        if ( ( 13 == *str_line ) || ( 10 == *str_line )  ) {
            ++ *l_null_number;
        } else if (  ( 0 == ( strstr( str_line, str_bgn ) ) )
                  && ( 0 == ( strstr( str_line, str_end ) ) )
                  ) {
            ++ *l_real_number;
        } else if (  ( 0 != ( strstr( str_line, str_bgn ) ) )
                  && ( 0 != ( strstr( str_line, str_end ) ) )
                  ) {
            if( strstr( str_line, str_bgn ) == ( int )str_line ) {
                l_bgn_flag = 1;
            } else {
                while ( strstr( str_line, str_bgn ) > ( int )str_line
                        && ( 1 == l_bgn_flag )
                      ) {
                    //printf("lxg1=[%d][%c]/n", *str_line, *str_line );
                    if ( ( 32 == *( str_line ) ) || ( 9 == *( str_line ) ) ) {
                    //printf("lxg2=[%d][%c]/n", *str_line, *str_line );
                        str_line ++;
                        l_bgn_flag = 1;
                    } else {
                        l_bgn_flag = 0;
                    }
                }
            }
            if ( 1 == l_bgn_flag ) {
                ++ *l_note_number;
            } else {
                ++ *l_real_number;
            }
        } else if (  ( 0 == ( strstr( str_line, str_bgn ) ) )
                  && ( 0 != ( strstr( str_line, str_end ) ) )
                  ) {
            ++ *l_note_number;
        } else {
            printf( "这种类型的行不能解析啊,郁闷/n/a" );
        }
    }
    return 0;
}
/*******************************************************************************
 *  函数名      :    strbstr
 *  功  能      :    从一个字符串的后面查找另一个字符串第一次出现的位置
 *  参  数      :     s1       I       字符串
 *                     s2       I       另一个字符串
 *
 *  返回值      :     0                 没有查找找到
 *                    非0                另一个字符串第一次出现的位置
*******************************************************************************/
int strbstr( char *s1, char * s2 ) {
    long l_len1;
    long l_len2;
    long i1;
    long i2;

    l_len1 = 0;
    l_len2 = 0;
    i2 = 0;
    i1 = 0;

    l_len1 = strlen( s1 );
    l_len2 = strlen( s2 );

    for( i1 = l_len1; i1 >= l_len2; i1 -- ) {
        if( 0 != strncmp( s1 + l_len1 - l_len2, s2, l_len2 ) ) {
            s1 --;
        } else {
            return ( long )s1 + strlen( s1 ) - l_len2;
        }
    }
    return 0;
}

 

 

 

 

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值