zoukankan      html  css  js  c++  java
  • linecnt

    // testSystem.cpp : Defines the entry point for the console application.
    //

    #include "stdafx.h"
    //#include <process.h>
    #include <assert.h>
    #include <stdio.h>
    #include <windows.h>
    #include <map>
    #include <set>
    #include <string>

    char line[10000];
    FILE *fpW_Line = 0;        //count line
    std::map<float, std::string> file_dates;    //for srch file within some date
    long gnchar=0,gnline=0;    
    int hours_before = 0;
    std::set<std::string> exts;

    struct InitSystem
    {
        InitSystem()
        {
            char *exts_[] = {"h", "hpp","c","cpp","def","rc","vcproj","cfg"};
            exts.insert(exts_, exts_+sizeof(exts_)/sizeof(exts_[0]));
        }
    }g_InitSystem;

    FILETIME stNow;

    void count(const char *file, long &nchar, long &nline)
    {
        nchar = 0;
        nline = 0;
        FILE *fp = fopen(file, "r");
        if(fp)
        {
            for(char *p = fgets(line, 10000, fp); p; p = fgets(line, 10000, fp))
            {
                nline++;
                nchar += strlen(p);
            }
            
            fclose(fp);
        }
    }

    inline bool isExt(const char *file)
    {
        // get file ext
        const char *ext = strrchr(file, '.');
        if(!ext)
            return false;
        ext++;

        //to lower
        char ext_l[100]={0};
        for(int i=0, n=strlen(ext); i<=n; i++)
            ext_l[i] = (ext[i] >= 'A' && ext[i] <= 'Z')?(ext[i]-'A'+'a'):(ext[i]);
            
        // srch ext
        return exts.find(ext_l) != exts.end();
    }

    float seconds_between(const FILETIME & from, const FILETIME & to)
    {
        enum {
           TICKS_PER_DAY = 10000000,
        };
       return float((*(__int64 *) & to) - (*(__int64 *) & from)) / TICKS_PER_DAY;
    }

    void getFileList(const char *folder, long &fnchar, long &fnline)
    {
        WIN32_FIND_DATA stFindData;
        long nchar, nline;
        char sFull[MAX_PATH];
        
        sprintf(sFull, "%s\\*", folder);
        HANDLE Handle = FindFirstFile(sFull, &stFindData);
        
        if(Handle == INVALID_HANDLE_VALUE)return;
        FindNextFile(Handle, &stFindData);
        while (FindNextFile(Handle, &stFindData))
        {
            sprintf(sFull, "%s\\%s", folder, stFindData.cFileName);
                
            if(stFindData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)    //folder
            {
                printf("%s\n",sFull);
                long subfnchar=0, subfnline=0;
                getFileList(sFull, subfnchar, subfnline);
                fnchar += subfnchar;
                fnline += subfnline;
            }
            else    //file
            {
                if(isExt(stFindData.cFileName))
                {
                    if(fpW_Line)
                    {
                        count(sFull, nchar, nline);
                        fprintf(fpW_Line, "<tr><td>%s<td>%d<td>%d</tr>\n", sFull, nchar, nline);
                        gnchar += nchar;
                        gnline += nline;
                        fnchar += nchar;
                        fnline += nline;
                    }
                    
                    if(hours_before)
                    {
                        //FILETIME stNow;
                        //GetSystemTimeAsFileTime(&stNow);
                        float diff_second = seconds_between(stFindData.ftLastWriteTime,stNow);
                        assert(diff_second>0);
                        if(diff_second<hours_before*60*60)    //check date
                        {
                            file_dates[diff_second] = sFull;
                            //fprintf(fpW_Date, "%s\n", sFull);
                        }
                    }
                    
                }
            }
        }
        
        FindClose(Handle);
        
        if(fpW_Line)
            fprintf(fpW_Line, "<tr><td><font color=red>%s</font> <td><font color=red>%d</font> <td><font color=red>%d</font> </tr>\n", folder, fnchar, fnline);
    }

    void main(int argc, char *argv[])
    {
        bool succeed = false;
        char *src_folder = argv[argc-1];
        char *destFolder = src_folder;
        char destfile[300];

        if(argc>=2)
        {
            //parse arguments
            for(int i=1; i<argc; i++)
            {
                if(argv[i][0]=='-')
                {
                    if(argv[i][1]=='t')
                    {
                        sscanf(argv[i+1], "%d", &hours_before);
                    }
                    else if(argv[i][1]=='c')
                    {
                        strcpy(destFolder, argv[i+1]);
                    }
                    else if(argv[i][1]=='l')
                    {
                        sprintf(destfile, "%s\\lineCnt.htm", destFolder);
                        fpW_Line = fopen(destfile, "w");
                    }
                }
            }
            
            printf("parse result, hours_before %d, destFolder %s\n", hours_before, destFolder);
            
            long fnchar=0, fnline=0;
            
            GetSystemTimeAsFileTime(&stNow);

            if(fpW_Line)
                fprintf(fpW_Line, "<table>\n");
            
            getFileList(src_folder,fnchar,fnline);
            
            if(fpW_Line)
            {
                fprintf(fpW_Line, "<tr><td>%s<td>%d<td>%d</tr>\n", "Total", gnchar, gnline);
                fprintf(fpW_Line, "</table>\n");
            }
            
            if(fpW_Line)
                fclose(fpW_Line);

            if(hours_before)
            {
                sprintf(destfile, "%s\\DateCnt.txt", destFolder);
                FILE* fpW_Date = fopen(destfile, "w");

                sprintf(destfile, "%s\\DateCnt.htm", destFolder);
                FILE* fpW_Date_htm = fopen(destfile, "w");

                fprintf(fpW_Date_htm, "<table> \n");
                for(std::map<float, std::string>::iterator it = file_dates.begin(), end = file_dates.end();
                    it!=end; ++it)
                {
                    fprintf(fpW_Date, "%s\n", it->second.c_str());

                    fprintf(fpW_Date_htm, "<tr> <td>%f <td>%s\n", it->first/(60*60), it->second.c_str());
                }
                fprintf(fpW_Date_htm, "</table> \n");

                fclose(fpW_Date);
                fclose(fpW_Date_htm);
            }
        }
        
        if(!succeed)
        {
            printf("Usage:\n");
            printf("lineCnt [-t <hours before>] [-copyto <folder>] [-linecnt] <folder>\n");
        }
    }
  • 相关阅读:
    Spring4 MVC json问题(406 Not Acceptable)
    java 从网络Url中下载文件
    Java基础知识(一) 自增、自减运算符
    java文件读写操作
    java集合运算:求交集,并集,集合差
    httpclient+jsoup实现网页信息抓取
    java web使用gradle配置详情
    关于mysql登录异常处理方法
    windows server 2012 r2 远程桌面连接指南
    Java生成带小图标的二维码-google zxing 工具类
  • 原文地址:https://www.cnblogs.com/cutepig/p/1783642.html
Copyright © 2011-2022 走看看