Click here to Skip to main content
15,888,351 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 448.7K   5.3K   142  
Get statistics about your source code with a click of a button
/***************************************************************************/
/* NOTE:                                                                   */
/* This document is copyright (c) by Oz Solomonovich, and is bound by the  */
/* MIT open source license (www.opensource.org/licenses/mit-license.html). */
/* See License.txt for more information.                                   */
/***************************************************************************/

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

bool ExportAsCSV(CString sFileName, ProjectList& projects)
{
    CString s;
    bool bSuccess = false;

    TRY
    {
        CStdioFile cFile(sFileName, CFile::modeCreate | CFile::modeWrite);
        
        s.LoadString(IDS_CSV_COLUMNS);
        cFile.WriteString(s + "\n");

        POSITION p = projects.GetHeadPosition();
        while (p)
        {
            const IWorkspaceProject * const pPrj = projects.GetNext(p);
            const int cFiles = pPrj->GetFileCount();
            for (int f = 0; f < cFiles; ++f)
            {
                IProjectFile *pFile = pPrj->GetFile(f);
                const CFileInfo& fi = ::GetStats(pFile->GetPath());
                delete pFile;

                if (fi.m_stat == CFileInfo::filtered  &&  
                    !CHK_FLAG(cfg_ShowFiltered, SF_EXPORT))
                {
                    // not including filtered files
                    continue;
                }

                if (fi.m_stat != CFileInfo::full  &&  
                    !CHK_FLAG(cfg_ShowBadFiles, SF_EXPORT))
                {
                    // not including bad files
                    continue;
                }

                s.Format("%s,%s,%s,%s,%s,%s,%s,%s,%s\n",
                    pPrj->GetName(),
                    fi.m_sFilePath,
                    fi.m_sFileName,
                    fi.m_sFileExt,
                    fi.GetTotalLinesStr(false),
                    fi.GetLinesWithCodeStr(false),
                    fi.GetLinesWithCommentsStr(false),
                    fi.GetMixedLinesStr(false),
                    fi.GetBlankLinesStr(false));
                cFile.WriteString(s);
            }
        }
        bSuccess = true;
    }
    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

    return bSuccess;    
}


bool ExportAsXML(CString sFileName, ProjectList& projects, 
                 LPCTSTR pszWorkspaceName)
{
    CString s;
    bool bSuccess = false;

    TRY
    {
        CStdioFile cFile(sFileName, CFile::modeCreate | CFile::modeWrite);

        CString sXSLT;
        if (cfg_XSLTUsage != none)
        {
            CString sXSLFileName;
            if (cfg_XSLTUsage == fixed_name)
            {
                sXSLFileName = cfg_sXSLTTemplate;
            }
            else
            {
                if (sFileName.Find('.'))
                {
                    sXSLFileName = 
                        sFileName.Left(sFileName.ReverseFind('.'));
                    sXSLFileName += ".xsl";
                }
                else
                {
                    sXSLFileName = sFileName;
                }
            }
            sXSLT.Format(
                "<?xml-stylesheet type=\"text/xsl\" href=\"%s\"?>\n",
                sXSLFileName);
        }
        
        
        // the header
        s.Format(
            // header
            "<?xml version=\"1.0\"?>\n"

            // style sheet
            "%s"

            // root
            "<statistics workspace=\"%s\" date=\"%s\" plc_ver=\"%d.%02d\">\n",
            sXSLT, pszWorkspaceName,
            CTime::GetCurrentTime().Format("%A, %B %d, %Y"),
            g_iVerMaj, g_iVerMin);
        cFile.WriteString(s);

        POSITION p = projects.GetHeadPosition();
        while (p)
        {
            const IWorkspaceProject * const pPrj = projects.GetNext(p);
            const int cFiles = pPrj->GetFileCount();
           
            s.Format("\t<project path=\"%s\">\n", pPrj->GetName());
            cFile.WriteString(s);

            for (int f = 0; f < cFiles; ++f)
            {
                IProjectFile *pFile = pPrj->GetFile(f);
                const CFileInfo& fi = ::GetStats(pFile->GetPath());
                delete pFile;

                if (fi.m_stat == CFileInfo::filtered  &&  
                    !CHK_FLAG(cfg_ShowFiltered, SF_EXPORT))
                {
                    // not including filtered files
                    continue;
                }

                if (fi.m_stat != CFileInfo::full  &&  
                    !CHK_FLAG(cfg_ShowBadFiles, SF_EXPORT))
                {
                    // not including bad files
                    continue;
                }

                s.Format(
                    "\t\t<file id=\"%s\">\n"
                    "\t\t\t<path>%s</path>\n"
                    "\t\t\t<name>%s</name>\n"
                    "\t\t\t<ext>%s</ext>\n"
                    "\t\t\t<lines-total>%s</lines-total>\n"
                    "\t\t\t<lines-code>%s</lines-code>\n"
                    "\t\t\t<lines-comments>%s</lines-comments>\n"
                    "\t\t\t<lines-mixed>%s</lines-mixed>\n"
                    "\t\t\t<lines-blank>%s</lines-blank>\n"
                    "\t\t</file>\n",
                    fi.m_sFullFileName,
                    fi.m_sFilePath,
                    fi.m_sFileName,
                    fi.m_sFileExt,
                    fi.GetTotalLinesStr(false),
                    fi.GetLinesWithCodeStr(false),
                    fi.GetLinesWithCommentsStr(false),
                    fi.GetMixedLinesStr(false),
                    fi.GetBlankLinesStr(false));
                cFile.WriteString(s);
            }

            cFile.WriteString("\t</project>\n");
        }

        cFile.WriteString("</statistics>\n");

        bSuccess = true;
    }
    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

    return bSuccess;    
}

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