C语言代码统计工具的源程序

来源:互联网 发布:程序员的量化交易之路 编辑:程序博客网 时间:2024/05/08 21:35

在控制台中运行,输入文件或文件夹的路径,可以统计当前文件夹下.C以及.H的文件数目,并计算文件中C代码的总行数,有效行数,空行数,注释行数。

并可以将统计后的结果输入到指定文件中或控制台上。代码有些BUG还没有完全解决,比如无法统计多级文件夹下的具体文件,同时对于被统计代码中的注释行,

要求不能有引号否则可能会统计出错,并且异常退出时动态分配的内存也有些没彻底释放。但毕竟这是第一次和同事一起合作完成的练习项目,还是蛮开心的。


本项目的设计思想是: 先将遍历完成的文件路径信息统计存放在单链表中,然后再遍历单链表依次将文件打开并统计代码行数,并将统计结果存放在此链表中

最后将此链表选择输出(控制台或文件)。


头文件:C_count.h文件

#ifndef _C_COUNT_
#define _C_COUNT_
#include<stdlib.h>
#include<string>
#include<sys/stat.h>
#include<io.h>
#include<process.h>
#include<malloc.h>
#include <windows.h>
#include "time.h"

#define D_NG 0                         /*处理失败*/
#define D_OK 1                         /*处理成功*/
#define D_DEBUG_PROJECT 0              /*开关*/

#define IS_SPACE(c)  ((c) == ' ' || (c) == '\t')


typedef unsigned int UINT32;
typedef char CHAR32;

typedef struct ltbl
{
    struct ltbl* pNxt;                  /* 指向链表中后一个元素的指针 */
    UINT32 Note;                         /* 注释行数 */
    UINT32 Empty;                        /* 空行数 */ 
    UINT32 Effect;                       /* 有效行数 */
    UINT32 Total;                        /* 总行数 */
    CHAR32 PATH[100];                   /*   地址 */
}LTBL;


typedef struct mng
{
    UINT32 nNodeCnt;         /* 链表的长度       */
    LTBL* pHead;           /* 指向链表头的指针 */
}MNG;

enum
{
    Folder = 1,              // 当输入的为文件夹或*和*.*时
    File_C,                  // 当输入的为 *.C文件时
    File_H,                  // 当输入的为 *.H文件时
    Specific_File,           // 当输入的为具体的文件时
  
};


UINT32 Final_order[9] ={0};           // 存储命令行信息
/* Final_order[9]数组中:第0位存放-all信息,以此类推分别为:-t,-p,-l,-c,-n,-a,-cp,-hp其中0表示不存在,1表示存在 */

extern UINT32 Free_Memory(CHAR32*&Command1);
extern UINT32 Check_Malloc(CHAR32**New_Mallo, UINT32 leD_NGth);
extern UINT32 Check_File(UINT32 argc, CHAR32* argv[]);
extern CHAR32* Storage_File_Path(UINT32 argc, CHAR32* argv[]);

extern UINT32 Command_Handle();

extern UINT32 Check_File(UINT32 argc, CHAR32* argv[]);

extern void Print(MNG *pMng, CHAR* File_Path);
extern CHAR * mytrim(CHAR *str);
extern void Process(MNG *pMng);
extern void TBLFree (MNG* pMng);
extern MNG* File_Find(MNG *pMng, CHAR *Path, CHAR *Temp_Path, UINT32 length);
extern void Print_time(clock_t start, clock_t finish, CHAR* File_Path);
extern UINT32 FileStorage_Specific(MNG *pMng, CHAR* Path);
extern UINT32 FileStorage_CH(MNG *pMng, CHAR* Path, UINT32 File_Type);
extern UINT32 FoaderStorage_H(MNG *pMng, CHAR* Path);
extern UINT32 FolderStorage_C(MNG *pMng, CHAR* Path);
extern UINT32  Folder_Check(MNG * pMng, MNG *pMng1, CHAR* Path);

#endif  //_C_COUNT_



源程序文件:project.cpp文件

// project.cpp : Defines the entry poUINT32 for the console application.
//
#include "stdafx.h"
#include "C_count.h"

/*******************************************************************************/
/*函数名称:unsigned int inline Free_Memory(CHAR_32* &Command1)                */
/*函数功能:对malloc申请的空间经行释放                                          */
/*参数:Command1:malloc申请的地址                                             */
/*返回值:value:表示malloc申请的地址是否成功                                   */
/********************************************************************************/
UINT32 Free_Memory(CHAR *&Command1)
{
    UINT32 value = D_OK;
    if(Command1 != NULL)                                                        /*内存释放成功*/
    {
        void* free(Command1);
        Command1 = NULL;
    }
    else
    {
        value = D_NG;
    }
    return value;
}

/****************************************************************************************/
/*函数名称:UINT32 Check_Malloc(CHAR*&New_Mallo, UINT32 length)              */
/*函数功能:判断malloc申请的空间是否成功                                                 */
/*参数:New_Mallo:申请空间的地址                                                       */
/*      length:申请空间char类型的长度                                                */
/*返回值:value:表示malloc申请的地址是否成功                                           */
/****************************************************************************************/
UINT32 Check_Malloc(CHAR*&New_Mallo, UINT32 length)
{

    UINT32 value = D_OK;
    if(( New_Mallo = (CHAR*)malloc(length * sizeof(CHAR)) ) != NULL)       /*内存申请成功*/
    {
        //do nothiD_NG
    }
    else
    {

#if D_DEBUG_PROJECT
        printf("malloc failed");
#endif
        value = D_NG;
    }
    return value;
}

/**************************************************************************************/
/*函数名称:UINT32 Check_File(int argc, CHAR* argv[])                                 */
/*函数功能:将指向在DOS命令行中执行程序名后第一行字符串保存                           */
/*参数:argc:,用来统计你运行程序时送给main函数的命令行参数的个数                     */
/*      argv[]:指向在DOS命令行中执行程序名后字符串                                   */
/*返回值:File_Type             :表示输入路径的类型                                  */
/*                              路径错误: 0                                          */
/*                              文件夹:1                                             */
/*                              *.c: 2                                                */
/*                              *.h: 3                                                */
/*                              文件:4                                               */
/**************************************************************************************/
UINT32 Check_File(UINT32 argc, CHAR* argv[])
{
    UINT32 File_Type = 0;
    UINT32 length = strlen(argv[1]);
    CHAR* Path = NULL;
    if(Check_Malloc(Path, length) == D_OK)                                     /*判断空间是否申请成功*/
    {
        //do nothing
    }
    else
    {
#if D_DEBUG_PROJECT
        printf("malloc failed");
#endif
        exit(0);
    }
    strcpy(Path, argv[1]);
    CHAR* fileName = Path;
    CHAR* Star_Address = strstr(Path, "\\*");

    if(Star_Address != NULL)                                                   /*判断字符串中是否有**/
    {

        Path = Star_Address;
        if(strstr(Path, "\\*") && strlen(Path) == 2)                            /*文件末尾为*,File_Type为1*/
        {
            File_Type = Folder;
        }
        else if(strstr(Path, "*.*") && strlen(Path) == 4)                      /*文件末尾为*.*,File_Type为1*/
        {
            File_Type = Folder;
        }
        else if(strstr(Path, "*.c") && strlen(Path) == 4)                      /*文件末尾为*.c,File_Type为2*/
        {
            File_Type = File_C;
        }
        else if(strstr(Path, "*.h") && strlen(Path) == 4)                      /*文件末尾为*.h,File_Type为3*/
        {
            File_Type = File_H;
        }
        else
        {
            printf("The path is error!\n");
            Free_Memory(fileName);
            exit(0);
        }
        if(File_Type != 0)                                                     /*将*后年的路径去除*/
        {
            *Path = '\0';
            strcpy(argv[1], fileName);

            if(_access(argv[1], 0) == 0)                                       /*判断*前面的路径是否正确*/
            {
                //do nothing
            }
            else
            {
                File_Type = 0;
            }
        }
        else
        {
            //do nothing
        }
    }
    else
    {
        struct _stat buf;
        _stat( fileName, &buf);                                               /*将路径信息传递给buf*/
        if(_S_IFDIR & buf.st_mode)                                            /*判断是否为录*/
        {
            File_Type = Folder;
        }
        else if(_S_IFREG & buf.st_mode)                                       /*判断是否为不含*的文件路径*/
        {
            File_Type = Specific_File;
        }
        if(_access(argv[1], 0) == 0)                                            /*判断路径是否正确*/
        {
            //do nothing
        }
        else
        {
            File_Type = 0;
        }
    }
    Free_Memory(fileName);
    return File_Type;
}

/**************************************************************************************/
/*函数名称:CHAR* Storage_File_Path(UINT32 argc, CHAR* argv[])                       */
/*函数功能:如果命令行中有-p指令,将后面的路径的地址返回,并且把命令存入数组          */
/*参数:argc:,用来统计你运行程序时送给main函数的命令行参数的个数                      */
/*      argv[]:指向在DOS命令行中执行程序名后字符串                                   */
/*返回值: File_Path:将-p后路径的首地址返回                                          */
/**************************************************************************************/
CHAR* Storage_File_Path(UINT32 argc, CHAR* argv[])
{

    CHAR* File_Path = NULL;


    if(argc > 2)
    {

        for(UINT32 i = 2; i < argc; i++)
        {
            if(strstr(argv[i], "-all") != NULL && strlen(argv[i]) == 4)
            {
                if(++Final_order[0] > 1)
                {
#if D_DEBUG_PROJECT
                    printf("Input error");
#endif
                    exit(0);
                }
                else
                {
                    //do nothing
                }
            }
            else if(strstr(argv[i], "-t") != NULL && strlen(argv[i]) == 2)
            {
                if(++Final_order[1] > 1)
                {
#if D_DEBUG_PROJECT
                    printf("Input error");
#endif
                    exit(0);
                }
                else
                {
                    //do nothing
                }
            }
            else if(strstr(argv[i], "-p") != NULL && strlen(argv[i]) == 2)
            {
                if(++Final_order[2] > 1)
                {
#if D_DEBUG_PROJECT
                    printf("Input error");
#endif
                    exit(0);
                }
                else
                {
                    if(++i < argc)
                    {
                        FILE* fg = NULL;
                        if( (fg = fopen(argv[i], "a+") ) == NULL)                     /* 以E追C加A的I方u式R写E入u文÷件? */
                        {
#if D_DEBUG_PROJECT
                            printf("Input error");
#endif
                            exit(0);
                        }
                        else
                        {
                            fclose(fg);
                            File_Path = argv[i];
                        }
                    }
                    else
                    {
#if D_DEBUG_PROJECT
                        printf("Input error");
#endif
                        exit(0);
                    }
                }

            }
            else if(strstr(argv[i], "-l") != NULL && strlen(argv[i]) == 2)
            {
                if(++Final_order[3] > 1)
                {
#if D_DEBUG_PROJECT
                    printf("Input error");
#endif
                    exit(0);
                }
                else
                {
                    //do nothing
                }
            }
            else if(strstr(argv[i], "-c") != NULL && strlen(argv[i]) == 2)
            {
                if(++Final_order[4] > 1)
                {
#if D_DEBUG_PROJECT
                    printf("Input error");
#endif
                    exit(0);
                }
                else
                {
                    //do nothing
                }
            }
            else if(strstr(argv[i], "-n") != NULL && strlen(argv[i]) == 2)
            {
                if(++Final_order[5] > 1)
                {
#if D_DEBUG_PROJECT
                    printf("Input error");
#endif
                    exit(0);
                }
                else
                {
                    //do nothing
                }
            }
            else if(strstr(argv[i], "-a") != NULL && strlen(argv[i]) == 2)
            {
                if(++Final_order[6] > 1)
                {
#if D_DEBUG_PROJECT
                    printf("Input error");
#endif
                    exit(0);
                }
                else
                {
                    //do nothing
                }
            }
            else if(strstr(argv[i], "-cp") != NULL && strlen(argv[i]) == 3)
            {
                if(++Final_order[7] > 1)
                {
#if D_DEBUG_PROJECT
                    printf("Input error");
#endif
                    exit(0);
                }
                else
                {
                    //do nothing
                }
            }
            else if(strstr(argv[i], "-hp") != NULL && strlen(argv[i]) == 3)
            {
                if(++Final_order[8] > 1)
                {
#if D_DEBUG_PROJECT
                    printf("Input error");
#endif
                    exit(0);
                }
                else
                {
                    //do nothing
                }
            }
            else
            {
#if D_DEBUG_PROJECT
                printf("Input error");
#endif
                exit(0);
            }
        }
    }
    return File_Path;

}

/********************************
* function:Command_Handle
* return值  :D_OK(1)/D_NG(0)
*********************************/

UINT32 Command_Handle(UINT32 File_Type)
{
    UINT32 ret  = 0;
    Final_order[0] = 0;
   
    if(Folder != File_Type)                                    /* 路径不是文件夹时,不能-cp 或-hp */
    {
        if(Final_order[7] || Final_order[8])
        {
            printf("Error! Can't -cp, -hp\n");
            ret = D_NG;
        }
        else
        {
            ;/* do nothing */
        }
    }
    if (Final_order[0] && (Final_order[3] || Final_order[4] || Final_order[5] || Final_order[6] || Final_order[7] || Final_order[8]))
    {
        printf("Error! There is all, no -l, -c, -n, -a, -cp, -hp\n");
        ret = D_NG;
    }                                                                                        /* -all 和 -l, -c, -n, -a等命令冲突的情况 */

    if (Final_order[3] && Final_order[4] && Final_order[5] && Final_order[6] && Final_order[7] && Final_order[8])
    {
       
        for (UINT32 i = 3; i < 9; i++)
        {
            Final_order[i] = 0;
        }
        Final_order[0] = 1;
        ret = D_OK;                                                                 /* -l, -c, -n, -a -cp, -hp同时存在与-all相同 */
    }

    if (Final_order[3] && Final_order[4] && Final_order[5] && Final_order[6])
    {
        for (UINT32 i = 3; i < 7; i++)
        {
            Final_order[i] = 0;
        }
        Final_order[0] = 1;                                                        /* -l, -c, -n, -a 与 -all 相同 */
        ret = D_OK;
    }
    if( !(Final_order[0] || Final_order[3] || Final_order[4] || Final_order[5] || Final_order[6] || Final_order[7] || Final_order[8] ) )
    {
        Final_order[0] = 1;
        ret = D_OK;                                                               /* 只有-t 或 -p 时,加-all */
    }
    else
    {
        ret = D_OK;
    }
   
    return ret;
}


/**************************************************************************************/
/*函数名称:void Print(MNG *pMng, CHAR* File_Path)                                  */
/*函数功能:输出统计内容到屏幕或文件上                                                */
/*参数:pMng:已存入统计信息的链表;                                                  */
/*      File_Path:输出到文件的路径                                                   */
/*返回值: 无                                                                         */
/**************************************************************************************/
void Print(MNG *pMng, CHAR* File_Path)
{
    UINT32 D_Note = 0;                   /* 总注释行数 */
    UINT32 D_Empty = 0;                  /* 总空行数 */
    UINT32 D_Total = 0;                  /* 总行数 */
    UINT32 D_Effect = 0;                 /* 总有效行数 */

    LTBL *P = pMng->pHead;
    if(1 == Final_order[2])
    {
       
        FILE *fg;
        if( (fopen_s(&fg, File_Path, "a+") ) != NULL)                     /* 以追加的方式写入文件 */
        {
            printf("open is error!\n");
            Free_Memory(File_Path);
            exit(0);
        }

        for (UINT32 i = 0; i < pMng->nNodeCnt; ++i)                        /* 遍历输出链表 */
        {
            D_Note += P->Note, D_Empty += P->Empty, D_Total += P->Total, D_Effect += P->Effect;

            fprintf_s(fg, "%s\t", P->PATH);

            if (1 == Final_order[0])
            {
                fprintf_s(fg,"Effect Line:%d\tNote Line:%d\tEmpty Line:%d\tTotal Line:%d\t", P->Effect, P->Note, P->Empty, P->Total);
            }
            else
            {
                //do nothing;
            }

            if (1 == Final_order[3])
            {
                 fprintf_s(fg, "Effect Line:%d\t", P->Effect);
            }
            else
            {
                //do nothing;
            }

            if (1 == Final_order[4])
            {
                  fprintf_s(fg, "Note Line:%d\t",  P->Note);
            }
            else
            {
                //do nothing;
            }

            if (1 == Final_order[5])
            {
                 fprintf_s(fg, "Empty Line:%d\t", P->Empty);
            }
            else
            {
                //do nothing;
            }

            if (1 == Final_order[6])
            {
                 fprintf_s(fg, "Total Line:%d\t", P->Total);
            }
            else
            {
                //do nothing;
            }

            fputs("\n", fg);
            P = P->pNxt;

        }
        if (1 == Final_order[0])
        {
            fprintf_s(fg, "The Total result:\n");
            fprintf_s(fg, "Effect Line:%d\tNote Line:%d\tEmpty Line:%d\tTotal Line:%d\t", D_Effect, D_Note, D_Empty, D_Total);
        }
        fclose(fg);
    }
    else
    {
        for (UINT32 i = 0; i < pMng->nNodeCnt; ++i)
        {
            D_Note += P->Note, D_Empty += P->Empty, D_Total += P->Total, D_Effect += P->Effect;

           printf("%s\t", P->PATH);

           if (1 == Final_order[0])
           {
               printf("Effect Line:%d\tNote Line:%d\tEmpty Line:%d\tTotal Line:%d\t", P->Effect, P->Note, P->Empty, P->Total);
           }
           else
           {
               //do nothing;
           }

           if (1 == Final_order[3])
           {
               printf("Effect Line:%d\t", P->Effect);
           }
           else
           {
               //do nothing;
           }

           if (1 == Final_order[4])
           {
               printf_s("Note Line:%d\t",  P->Note);
           }
           else
           {
               //do nothing;
           }

           if (1 == Final_order[5])
           {
               printf("Empty Line:%d\t", P->Empty);
           }
           else
           {
               //do nothing;
           }

           if (1 == Final_order[6])
           {
               printf("Total Line:%d\t", P->Total);
           }
           else
           {
               //do nothing;
           }

            putchar('\n');
            P = P->pNxt;
        }
        if (1 == Final_order[0])
        {
            printf("The Total result:\n");
            printf("Effect Line:%d\tNote Line:%d\tEmpty Line:%d\tTotal Line:%d\t", D_Effect, D_Note, D_Empty, D_Total);
        }
        putchar('\n');
    }

}

/**************************************************************************************/
/*函数名称:CHAR *Trim(CHAR *str)                                                     */
/*函数功能:除去字符串数组的前后空格                                                  */
/*参数:str:字符串指针;                                                             */
/*返回值: NULL/str                                                                   */
/**************************************************************************************/
CHAR *Trim(CHAR *str)
{
    if (str == NULL)
    {
        return NULL;
    }

    while(IS_SPACE(*str))
    {
        str ++;
    }

    UINT32 len = strlen(str);
    if (len == 0)
    {
        return str;
    }

    CHAR *end = str + len - 1;
    while( IS_SPACE(*end) )
    {
        end --;
    }
    *(++ end) = '\0';

    return str;
}

/**************************************************************************************/
/*函数名称:void Process(MNG *pMng)                                                   */
/*函数功能:统计代码行数并将统计信息存入链表                                           */
/*参数:pMng:已存入路径信息的链表;                                                  */
/*返回值: 无                                                                         */
/**************************************************************************************/
void Process(MNG *pMng)
{
    LTBL *P = pMng->pHead;
    UINT32 Total = 0, Note = 0, Empty = 0;                             /* 一个文件的总行数,注释行数,空行数 */
    CHAR *Line = NULL;                                                /* 将每行字符输入Line中 */
    Line = (CHAR*)malloc(sizeof(CHAR) * 10000);

    if(NULL == Line)
    {
        printf("memory is error!\n");
        TBLFree(pMng);
        exit(0);
    }
 
    memset(Line, 0, 10000);
    CHAR *temp = Line;                                           /* 标记所申请的内存的头地址 */
    CHAR *p_Head = NULL;                                        /* 标记所申请的已去除空格的头地址 */
    CHAR *Pfind_char1 = NULL;                                  /* 判断查找下一个字符串位置的指针 */
    CHAR *Pfind_char2 = NULL;                                 /* 判断查找下一个字符串位置的指针 */
    CHAR *Pfind_char3 = NULL;                                /* 判断查找下一个字符串位置的指针 */
    CHAR *Pfind_char4 = NULL;                               /* 判断查找下一个字符串位置的指针 */
    FILE *fp = NULL;

    UINT32 Note_Mark = 0;                                /* 注释段的标记位 */
    UINT32 Quote_Mark = 0;                              /* 引号的标记位 */
    UINT32 Effect_Add = 0;                             /* 统计带有注释行的有效行 */
    UINT32 Note_Add = 0;                              /* 统计注释段中的行数 */

    CHAR ch = '\0';
    UINT32 j = 0;
    UINT32 len = 0;
    for (register UINT32 i = 0; i < pMng->nNodeCnt; ++i)
    {
        if( (fopen_s(&fp, P->PATH, "r") ) != NULL)
        {
            printf("open is error2!\n");
            exit(0);
        }

       
        while( ( ch = fgetc(fp) ) != EOF)
        {

            if(ch != '\n')
            {
                *(Line + (j ++) ) = ch;
                continue;
            }

            Line = Trim(Line);                     /* 去除本行前后的空格 */
            p_Head = Line;

            if(Note_Mark == 1)
            {
                Note_Add ++;
            }

            Total ++;
            len = strlen (Line);

            if ( 0 == len )
            {
                Empty ++;
            }

            if ( (Pfind_char1 = strstr(Line, "\"") ) != NULL )
            {
                if( (Pfind_char2 = strstr(Pfind_char1 + 1, "\"") ) != NULL )
                {
                    if( ( Pfind_char3 = strstr(Pfind_char2, "//") ) != NULL)
                    {
                        Note ++;
                        Effect_Add ++;
                    }
                    else if( ( Pfind_char3 = strstr(Pfind_char2, "/*") ) != NULL)
                    {
                        if( ( Pfind_char4 = strstr(Pfind_char3, "*/") ) != NULL )
                        {
                            Note ++;
                            Effect_Add ++;
                        }
                        else
                        {
                            Note_Mark = 1;
                            Effect_Add ++;
                        }
                    }
                   
                }
                Quote_Mark = 1;
            }

            if(Quote_Mark == 1)
            {
                if ('\\' == *(Line + len - 1))
                {
                    Total --;
                }

                memset(Line, 0, len);
                j = 0;

                Quote_Mark = 0;
                continue;
            }

            else if( (Pfind_char1 = strstr(Line, "//") ) != NULL && Pfind_char1 != p_Head)
            {
                Note ++;
                Effect_Add ++;
            }
            else if( (Pfind_char1 = strstr(Line, "//") ) != NULL && Pfind_char1 == p_Head)
            {
                Note ++;
            }

            else if( (Pfind_char1 = strstr(Line, "/*") ) != NULL && Pfind_char1 != p_Head)
            {
                if( ( Pfind_char2 = strstr(Pfind_char1, "*/") ) != NULL )
                {
                    Note ++;
                    Effect_Add ++;
                }
                else
                {
                    Note_Mark = 1;
                    Effect_Add ++;
                }
            }
            else if( (Pfind_char1 = strstr(Line, "/*") ) != NULL && Pfind_char1 == p_Head)
            {
                Note ++;
                if( ( Pfind_char2 = strstr(Pfind_char1, "*/") ) != NULL )
                {
                    //do nothing.
                }
                else
                {
                    Note_Mark = 1;
                }
            }

            if ('\\' == *(Line + len - 1) && Note_Mark == 0)
            {
                Total --;
            }
            else
            {
                //do Nothing;
            }

            if( (Pfind_char1 = strstr(Line, "*/") ) != NULL)
            {
                Note ++;
                Note += Note_Add;
                Note_Add = 0;
                Note_Mark = 0;
            }


           memset(Line, 0, len);
           Line = temp;
           j = 0;
        }
        fclose(fp);
        P->Note = Note;
        P->Empty = Empty;
        P->Effect = Total - Empty - Note + Effect_Add;
        P->Total = Total;
        P = P->pNxt;

        Total = 0, Note = 0, Empty = 0;
        Effect_Add = 0;
    }
    Free_Memory(Line);
}

/**************************************************************************************/
/*函数名称:void TBLFree (MNG* pMng)                                                  */
/*函数功能:释放链表内存                                                              */
/*参数:pMng:释放的链表;                                                            */
/*返回值: 无                                                                         */
/**************************************************************************************/
void TBLFree (MNG* pMng)
{
    LTBL *P = pMng->pHead;
    LTBL *temp = NULL;
    for(UINT32 i = 0; i < pMng->nNodeCnt; ++i)
    {
        temp = P->pNxt;
        void* free(P);
        P = NULL;
        P = temp;
    }
    pMng->pHead = NULL;
    pMng->nNodeCnt = 0;
}

/**************************************************************************************/
/*函数名称:UINT32  Folder_Check(MNG * pMng, MNG *pMng1, CHAR* Path)                  */
/*函数功能:文件夹判断操作                                                            */
/*参数:pMng:*.C文件夹路径的链表                                                     */
/*      Path:文件夹路径                                                              */
/*返回值: D_OK                                                                       */
/**************************************************************************************/
UINT32 File_Search(UINT32 File_Type, MNG *pMng, CHAR *Path)
{
    MNG *pMng1 = NULL;
    if ( ( pMng->pHead = (LTBL *)malloc( sizeof(LTBL) ) ) == NULL)
    {
        printf("Memory is error!");
        free(pMng);
        pMng = NULL;
        Free_Memory(Path);
        exit(0);
    }
    switch(File_Type)
    {
    case File_C:
    case File_H:
        FileStorage_CH(pMng, Path,File_Type);
        break;
    case Specific_File:
        FileStorage_Specific(pMng, Path);
        break;
    case Folder:
       
        pMng1 = (MNG*) malloc (sizeof(MNG));
        if (NULL == pMng1)
        {
            printf("Memory is error!");
            TBLFree(pMng);
            free(pMng);
            pMng = NULL;
            exit(0);
        }
        pMng1 ->nNodeCnt = 0;
        pMng1 ->pHead = NULL;
        if ( ( pMng1->pHead = (LTBL *)malloc( sizeof(LTBL) ) ) == NULL)
        {
            printf("Memory is error!");
            TBLFree(pMng);
            free(pMng);
            pMng = NULL;
            free(pMng1);
            pMng1 = NULL;
            exit(0);
        }

        Folder_Check(pMng, pMng1, Path);
        break;
    default:
        {
            printf("The File is error!");
            exit(0);
        }
    }

    return D_OK;
}

/**************************************************************************************/
/*函数名称:UINT32 FileStorage_Specific(MNG *pMng, CHAR* Path)                         */
/*函数功能:将文件路径存入链表                                                        */
/*参数:pMng:存入路径的链表;                                                        */
/*      Path:文件路径                                                                */
/*返回值: D_OK                                                                       */
/**************************************************************************************/
UINT32 FileStorage_Specific(MNG *pMng, CHAR* Path)
{
       LTBL *P = NULL;
       P = pMng->pHead ;
       strcpy_s(P->PATH, Path);
       pMng->nNodeCnt ++;
       P ->pNxt = NULL;
       return D_OK;
}

/**************************************************************************************/
/*函数名称:UINT32 FileStorage_CH(MNG *pMng, CHAR* Path, UINT32 File_Type)            */
/*函数功能:将*.C或*.H文件路径存入链表                                                */
/*参数:pMng:存入路径的链表;                                                        */
/*      Path:文件路径                                                                */
/*      File_Type:文件类型                                                           */
/*返回值: D_OK                                                                       */
/**************************************************************************************/
UINT32 FileStorage_CH(MNG *pMng, CHAR* Path, UINT32 File_Type)
{
    UINT32 length = strlen(Path);
    CHAR *Temp_Path = NULL;
    Temp_Path = (CHAR*) malloc( (length + 4) * sizeof(CHAR) );

    if(NULL == Temp_Path)
    {
        printf("memory is error!\n");
        TBLFree(pMng);
        free(pMng);
        pMng = NULL;
        Free_Memory(Path);
        exit(0);
    }
    memset(Temp_Path, 0, length +4);
    memcpy(Temp_Path, Path, length);

    if (File_C == File_Type)
    {
        strcat(Temp_Path, "\\*.c");
    }
    else if (File_H == File_Type)
    {
        strcat(Temp_Path, "\\*.h");
    }

    CHAR *D_Path = (CHAR*) malloc( (length + 3) * sizeof(CHAR) );
    if(NULL == D_Path)
    {
        printf("memory is error!");
        exit(0);
    }
    memset(D_Path, 0, length);
    memcpy(D_Path, Path, length);
    *(D_Path + length) = '\0';
    strcat(D_Path, "\\");
    File_Find(pMng, D_Path, Temp_Path, length + 1);
    return D_OK;
}


/**************************************************************************************/
/*函数名称:UINT32 FoaderStorage_H(MNG *pMng, CHAR* Path)                             */
/*函数功能:将*.H文件路径存入链表                                                     */
/*参数:pMng:*.H文件路的链表;                                                       */
/*      Path:文件路径                                                                */
/*返回值: D_OK                                                                       */
/**************************************************************************************/
UINT32 FoaderStorage_H(MNG *pMng, CHAR* Path)
{
    CHAR *Temp_Path = NULL;
    UINT32 length =strlen(Path);
    Temp_Path = (CHAR*) malloc( (length + 6) * sizeof(CHAR) );
    if(NULL == Temp_Path)
    {
        printf("memory is error!");
        TBLFree(pMng);
        free(pMng);
        pMng = NULL;
        Free_Memory(Path);
        exit(0);
    }
    memset(Temp_Path, 0, length);
    memcpy(Temp_Path, Path, length);
    *(Temp_Path + length) = '\0';
    strcat(Temp_Path, "\\*.h");
    File_Find(pMng, Path, Temp_Path, length);
    return D_OK;
}

/**************************************************************************************/
/*函数名称:UINT32 FolderStorage_C(MNG *pMng, CHAR* Path)                             */
/*函数功能:将*.c文件夹路径存入链表                                                   */
/*参数:pMng:*.H文件夹路的链表;                                                     */
/*      Path:文件夹路径                                                              */
/*返回值: D_OK                                                                       */
/**************************************************************************************/
UINT32 FolderStorage_C(MNG *pMng, CHAR* Path)
{
    CHAR *Temp_Path = NULL;
    UINT32 length =strlen(Path);
    Temp_Path = (CHAR*) malloc( (length + 6) * sizeof(CHAR) );
    if(NULL == Temp_Path)
    {
        printf("memory is error!");
        TBLFree(pMng);
        free(pMng);
        pMng = NULL;
        Free_Memory(Path);
        exit(0);
    }
    memset(Temp_Path, 0, length);
    memcpy(Temp_Path, Path, length);
    *(Temp_Path + length) = '\0';
    strcat(Temp_Path, "\\*.c");
    pMng = File_Find(pMng, Path, Temp_Path, length);
    return D_OK;
}

/**************************************************************************************/
/*函数名称:MNG* File_Find(MNG *pMng, CHAR *Path, CHAR *Temp_Path, UINT32 length      */
/*函数功能:遍历文件,并存入路径信息                                                  */
/*参数:pMng:文件路径的链表                                                          */
/*      Path:文件路径                                                                */
/*      Temp_Path:临时文件路径                                                       */
/*      length:文件路径长度                                                       */
/*返回值: D_OK                                                                       */
/**************************************************************************************/
MNG* File_Find(MNG *pMng, CHAR *Path, CHAR *Temp_Path, UINT32 length)
{
    long Handle;
    struct _finddata_t FileInfo;
    if((Handle = _findfirst(Temp_Path, &FileInfo)) == -1L)                  /* 查找该类型的第一个文件 */
    {
        printf("NO find the file!\n");
        TBLFree(pMng);
        free(pMng);
        pMng = NULL;
        Free_Memory(Path);
        Free_Memory(Temp_Path);
        exit(0);
    }

    else
    {

        Free_Memory(Temp_Path);
        CHAR *D_FilePath = NULL;                                  /* 文件路径指针 */
        UINT32 len = length + strlen(FileInfo.name) + 2;           /* 文件路径的长度 */
        D_FilePath = (CHAR*) malloc( len * sizeof(CHAR) );

        if(NULL == D_FilePath)
        {
            printf("path is error!n");
            TBLFree(pMng);
            free(pMng);
            pMng = NULL;
            Free_Memory(Path);
            _findclose(Handle);
            exit(0);
        }

        memset(D_FilePath, 0, len);
        memcpy(D_FilePath, Path, length);
        strcat(D_FilePath, FileInfo.name);

        LTBL *Pf = NULL;                                           /* 指向第一个链表的指针 */
        LTBL *Pn = NULL;                                          /* 指向下一个链表的指针 */
        Pf = pMng->pHead ;
        strcpy_s(Pf->PATH, D_FilePath);                         /* 将路径复制到链表地址 */
        pMng->nNodeCnt ++;
        Pf ->pNxt = NULL;

        while(_findnext(Handle, &FileInfo) == 0)             /* 继续查找此类型后面的文件 */
        {
            Free_Memory(D_FilePath);
            len = length + strlen(FileInfo.name) + 2;
            D_FilePath = (CHAR*) malloc( len * sizeof(CHAR) );

            if(NULL == D_FilePath)
            {
                printf("memory is error!n");
                TBLFree(pMng);
                free(pMng);
                pMng = NULL;
                Free_Memory(Path);
                _findclose(Handle);
                exit(0);
            }

            memset(D_FilePath, 0, len);
            memcpy(D_FilePath, Path, length);
            strcat(D_FilePath, FileInfo.name);

            if ( ( Pn = (LTBL *)malloc( sizeof(LTBL) ) ) != NULL)
            {
                strcpy_s(Pn->PATH, D_FilePath);                    /* 将路径复制到链表地址 */
                Pf->pNxt = Pn;
                Pf = Pn;
                pMng->nNodeCnt ++;
                Pf ->pNxt = NULL;
            }

            else
            {
                printf("memory is error!n");
                TBLFree (pMng);
                free (pMng);
                pMng = NULL;
                Free_Memory(Path);
                _findclose(Handle);
                exit(0);
            }

        }
        Free_Memory(D_FilePath);
        _findclose(Handle);
    }
    return pMng;
}

/**************************************************************************************/
/*函数名称:UINT32  Folder_Check(MNG * pMng, MNG *pMng1, CHAR* Path)                  */
/*函数功能:文件夹判断操作                                                            */
/*参数:pMng:*.C文件夹路径的链表                                                     */
/*      pMng1:*.H文件夹路径的链表                                                    */
/*      Path:文件夹路径                                                              */
/*返回值: D_OK                                                                       */
/**************************************************************************************/
UINT32  Folder_Check(MNG * pMng, MNG *pMng1, CHAR* Path)
{
    UINT32 len = strlen(Path);
    CHAR *D_Path = (CHAR*) malloc( (len + 3) * sizeof(CHAR) );
    if(NULL == D_Path)
    {
        printf("memory is error!");
        exit(0);
    }
    memset(D_Path, 0, len);
    memcpy(D_Path, Path, len);
    *(D_Path + len) = '\0';
    strcat(D_Path, "\\");

    if( ( (1 == Final_order[7] && 1 == Final_order[8]) || ( 0 == Final_order[7] && 0 == Final_order[8]) ) )
    {
         FolderStorage_C(pMng, D_Path);
        
         pMng1 = (MNG*) malloc (sizeof(MNG));
         if (NULL == pMng1)
         {
             printf("Memory is error!");
             TBLFree(pMng);
             free(pMng);
             pMng = NULL;
             exit(0);
         }
         pMng1 ->nNodeCnt = 0;
         pMng1 ->pHead = NULL;
         if ( ( pMng1->pHead = (LTBL *)malloc( sizeof(LTBL) ) ) == NULL)
         {
             printf("Memory is error!");
             TBLFree(pMng);
             free(pMng);
             pMng = NULL;
             free(pMng1);
             pMng1 = NULL;
             exit(0);
         }

         FoaderStorage_H(pMng1, D_Path);
        
        LTBL *P = pMng->pHead;
        while (P->pNxt != NULL)
        {
            P = P->pNxt;
        }
        P->pNxt = pMng1 ->pHead;
        pMng->nNodeCnt += pMng1->nNodeCnt;

      
    }
    else if(1 == Final_order[7] && 0 == Final_order[8] )
    {
       FolderStorage_C(pMng, D_Path);
    }
    else if(0 == Final_order[7] && 1 == Final_order[8] )
    {
       FoaderStorage_H(pMng, D_Path);

    }
    return D_OK;
}

/**************************************************************************************/
/*函数名称:void main(UINT32 argc, CHAR* argv[])                                      */
/*函数功能:主函数                                                                    */
/*参数:argc:输入参数的个数                                                          */
/*      argv[]:字符串数组                                                            */
/*返回值: D_OK                                                                       */
/**************************************************************************************/
void main(UINT32 argc, CHAR* argv[])
{
    clock_t start,finish;
    start = clock();
    UINT32 File_Type = 0;
    if(argc > 1)
    {
        File_Type = Check_File(argc, argv);
        if (File_Type == 0)
        {
            exit(0);
        }
        else
        {
            //do nothing
        }
    }
    else
    {
        exit(0);
    }
    CHAR *Path = argv[1];
    CHAR *File_Path = NULL;
   
    File_Path = Storage_File_Path(argc, argv) ;

    if (D_NG == Command_Handle(File_Type) )
    {
        printf("The progress is error!\n");
        Free_Memory(Path);
        exit(0);
    }

    MNG *pMng = NULL;
    pMng = (MNG*) malloc (sizeof(MNG));
    if (NULL == pMng)
    {
        printf("Memory is error!");
        Free_Memory(Path);
        exit(0);
    }
    pMng ->nNodeCnt = 0;
    pMng ->pHead = NULL;

    File_Search(File_Type, pMng, Path);

    Process(pMng);

    finish = clock();
    if(1 == Final_order[1] )
    {
        Print(pMng, File_Path);
        Print_time(start, finish,File_Path);
        TBLFree (pMng);
        void*free(pMng);
        pMng = NULL;
        Free_Memory(File_Path);
        Free_Memory(Path);
        exit(0);
    }
    else
    {
        Print(pMng, File_Path);
        TBLFree (pMng);
        void* free(pMng);
        pMng = NULL;
        Free_Memory(File_Path);
        Free_Memory(Path);
        exit(0);
    }
}

/**************************************************************************************/
/*函数名称:void Print_time(clock_t start, clock_t finish, CHAR* File_Path)           */
/*函数功能:输出时间的函数                                                                    */
/*参数:start:开始时间                                                               */
/*      finish:结束时间                                                              */
/*      File_Path:写入文件的地址                                                     */
/*返回值: D_OK                                                                       */
/**************************************************************************************/
void Print_time(clock_t start, clock_t finish, CHAR* File_Path)
{
    double  duration;
    duration = (double)(finish - start) / CLOCKS_PER_SEC;
    if (NULL != File_Path)
    {
        FILE *fg;
        if( (fopen_s(&fg, File_Path, "a+") ) != NULL)
        {
            printf("open is error3!\n");
            Free_Memory(File_Path);
            exit(0);
        }
        fprintf_s( fg,"\n%f seconds\n", duration );
        fclose(fg);

    }
    else
    {
        printf("%f seconds\n", duration );

    }
}

1 0
原创粉丝点击