Click here to Skip to main content
15,886,058 members
Articles / Programming Languages / C++

Project Line Counter Add-In v2.10 for VS.NET and VC6

Rate me:
Please Sign up or sign in to vote.
4.92/5 (38 votes)
29 Jun 2003 447.9K   5.3K   142  
Get statistics about your source code with a click of a button
/***************************************************************************/
/* NOTE:                                                                   */
/* This document is copyright (c) by Oz Solomonovich.  All non-commercial  */
/* use is allowed, as long as this document is not altered in any way, and */
/* due credit is given.                                                    */
/***************************************************************************/

#include "stdafx.h"
#include "LineCount.h"
#include "Parser.h"
#include "export.h"
#include "Config.h"

static CString NumAndPerCent(int iNum, int iTotal)
{
    CString cStr1, cStr2;

    cStr1.Format("(%d%%)", iNum * 100 / iTotal);
    cStr2.Format("%d%7s", iNum, cStr1);

    return cStr2;
}

void ExportData(CString sFileName, proj_stats *pStats, WWhizInterface *pWWI)
{
    CString s, s2;
    int     i;

    TRY
    {
        CStdioFile cFile(sFileName, CFile::modeCreate | CFile::modeWrite);
        
        s.Format("Project Line Counter v%d.%02d - Project Stats Summary\n\n",
            g_iVerMaj, g_iVerMin);
        cFile.WriteString(s);
        
        cFile.WriteString("Generated:     " +
            CTime::GetCurrentTime().Format("%A, %B %d, %Y") + "\n");


        /** Project List **/

        if (cfg_bActiveProjOnly  ||  
            pWWI->GetProjectList().GetProjectCount() == 1)
        {
            cFile.WriteString("Project File:  " + 
                pWWI->GetCurrentProject()->GetName() + "\n");
        }
        else
        {
            WWhizProjectList& prjList = pWWI->GetProjectList();
            for (i = 0; i < prjList.GetProjectCount(); i++)
            {
                s.Format("%-15s%s\n", i? "" : "Project Files: ", 
                    prjList.GetProjectByIndex(i)->GetName());
                cFile.WriteString(s);
            }
        }
        cFile.WriteString("\n");


        /** File Line Summary **/

        cFile.WriteString("\n\n** Line Count Analysis By File **\n\n");
        s.Format("%-30s%-11s%-17s%-17s%-17s%-17s%s\n", 
            "-File Name--", 
            "-Lines----", 
            "-Code-----------", 
            "-Comments-------", 
            "-Mixed----------", 
            "-Blank----------", 
            "-File Path--");
        cFile.WriteString(s);

        file_info *p = pStats->pfi, *pTotal = &pStats->pfi[pStats->iCount];
        int total;
        while (!p->sName.IsEmpty())
        {
            total = p->lines + p->codelines + p->commentlines + 
                    p->mixedlines, p->blanklines;
            s.Format("%-30s%10d %16s %16s %16s %16s %s\n",
                p->sName, 
                p->lines,
                NumAndPerCent(p->codelines,    p->lines),
                NumAndPerCent(p->commentlines, p->lines),
                NumAndPerCent(p->mixedlines,   p->lines),
                NumAndPerCent(p->blanklines,   p->lines),
                p->sPath);
            cFile.WriteString(s);
            p++;
        }
        


        /** File Counts **/

        cFile.WriteString("\n\n** File Count By Extension **\n\n");
        s.Format("%-30s%s   %s\n", "Extension", "Count", "Percent");
        cFile.WriteString(s);

        POSITION pos;
        pos = pStats->mapExtCount.GetStartPosition();
        while (pos)
        {
            pStats->mapExtCount.GetNextAssoc(pos, s2, i);
            s.Format("%-30s%5d   %6d%%\n", s2, i, (i * 100) / pStats->iCount);
            cFile.WriteString(s);
        }
    }
    CATCH(CFileException, e)
    {
        TCHAR szCause[255];
        CString msg;
        
        e->GetErrorMessage(szCause, 255);
        AfxFormatString1(msg, IDS_WRITEERROR, szCause);
        
        AfxMessageBox(msg, MB_OK | MB_ICONEXCLAMATION);
    }
    AND_CATCH_ALL(e)
    {
        AfxMessageBox("Error writing to file!", 
            MB_OK | MB_ICONEXCLAMATION);
    }
    END_CATCH_ALL
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
Experion
Canada Canada
You may know Oz from his WndTabs days. Oz has long since left client side development to work on web technologies and to consult in the R&D management field.

Comments and Discussions