/***************************************************************************/
/* 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. */
/***************************************************************************/
// PrjStats.cpp: implementation of the PrjStats class.
//
//////////////////////////////////////////////////////////////////////
#include "stdafx.h"
#include "linecount.h"
#include "PrjStats.h"
#include "FileParser.h"
#include "CPP_Parser.h"
#include "ProgressDlg.h"
#include "Config.h"
#include "Utils.h"
#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[]=__FILE__;
#define new DEBUG_NEW
#endif
CMap<CString, LPCTSTR, CFileInfo, CFileInfo&> s_mapStats;
class CParsers : public CFileParser
{
public:
CParsers()
{
m_pParsers[0] = new CCParser();
};
virtual ~CParsers()
{
for (int i = 0; i < countof(m_pParsers); ++i)
{
delete m_pParsers[i];
}
}
virtual bool CanParseFile(const char *pszFileName)
{
for (int i = 0; i < countof(m_pParsers); ++i)
{
if (m_pParsers[i]->CanParseFile(pszFileName))
{
return true;
}
}
return false;
}
virtual void ParseFile(CFileInfo& info)
{
for (int i = 0; i < countof(m_pParsers); ++i)
{
if (m_pParsers[i]->CanParseFile(info.m_sFullFileName))
{
m_pParsers[i]->ParseFile(info);
}
}
}
CFileParser *m_pParsers[1];
} s_Parser;
int CalculateMixedLines(int iLinesTotal, int iLinesCode, int iLinesComments,
int iLinesBlank)
{
return iLinesCode + iLinesComments - (iLinesTotal - iLinesBlank);
}
void ProcessMessages(CWnd &cwnd)
{
MSG msg;
// process all messages pending in the queue
while (::PeekMessage(&msg, cwnd.m_hWnd, 0, 0, PM_REMOVE))
{
::TranslateMessage(&msg);
::DispatchMessage(&msg);
}
}
void ResetStats()
{
s_mapStats.RemoveAll();
}
bool UpdateStats(WWhizInterface *pWWI, ProjectList& projects)
{
int cFiles = 0;
CProgressDlg dlgProgress;
dlgProgress.Create(dlgProgress.IDD);
// count files
POSITION p = projects.GetHeadPosition();
while (p)
{
WWhizProject& prj = *projects.GetNext(p);
const CString& prjName = prj.GetName();
cFiles += prj.GetFileList().GetCount();
}
bool bInitialShowDlg = false;
bool bComplete = true;
int cCounted = 0;
CString sTemplateTop, sFiles, cStr;
sTemplateTop.LoadString(IDS_MSG_PROGRESS_COUNTING);
sFiles.Format("%02d", cFiles);
char full_path[_MAX_PATH];
CStringList lstExtensionsToParse;
cStr = cfg_sExtList;
cStr.MakeLower();
String2StringList(cStr, LIST_DELIMITER, lstExtensionsToParse);
CStringList lstFileExclusions;
cStr = cfg_sFileIgnoreList;
cStr.MakeLower();
String2StringList(cStr, LIST_DELIMITER, lstFileExclusions);
// count files
if (cFiles)
{
dlgProgress.m_ProgressCtrl.SetRange(0, cFiles - 1);
dlgProgress.UpdateData(FALSE);
POSITION p = projects.GetHeadPosition();
while (p)
{
WWhizProject& prj = *projects.GetNext(p);
WWhizFileList& files = prj.GetFileList();
for (int f = 0; f < files.GetCount(); ++f)
{
WWhizFile& file = *files.Get(f);
if (bInitialShowDlg && !dlgProgress.IsWindowVisible())
{
bComplete = false;
break;
}
++cCounted;
_fullpath(full_path, file.GetFullName(),
countof(full_path));
CFileInfo& info = s_mapStats[full_path];
if (!info.m_bFull)
{
info.SetFileName(full_path);
bool bDoParse = false;
// see if the extension is in our list
cStr = info.m_sFileExt.Mid(1);
cStr.MakeLower();
POSITION posExt = lstExtensionsToParse.GetHeadPosition();
while (posExt)
{
CString& sExtFromList =
lstExtensionsToParse.GetNext(posExt);
if (wildcmp(sExtFromList, cStr))
{
bDoParse = true;
break;
}
}
// see if this file/path is in the global exclusion list
if (bDoParse)
{
cStr = info.m_sFullFileName;
cStr.MakeLower();
POSITION posFile = lstFileExclusions.GetHeadPosition();
CString s;
while (posFile)
{
CString& sFileFromList =
lstFileExclusions.GetNext(posFile);
s.Format("*%s", sFileFromList);
if (wildcmp(s, cStr))
{
bDoParse = false;
break;
}
}
}
if (bDoParse)
{
CString sCounted, sPerCent;
sCounted.Format("%02d", cCounted);
sPerCent.Format("%d", cCounted * 100 / cFiles);
dlgProgress.m_sTextTop = sTemplateTop;
dlgProgress.m_sTextTop.Replace("%1", sCounted);
dlgProgress.m_sTextTop.Replace("%2", sFiles);
dlgProgress.m_sTextTop.Replace("%3", sPerCent);
dlgProgress.m_sTextBottom = file.GetFullName();
dlgProgress.UpdateData(FALSE);
dlgProgress.m_ProgressCtrl.SetPos(cCounted);
if (!bInitialShowDlg)
{
bInitialShowDlg = true;
dlgProgress.ShowWindow(SW_SHOW);
dlgProgress.RedrawWindow();
}
ProcessMessages(dlgProgress);
s_Parser.ParseFile(info);
}
}
}
}
}
dlgProgress.DestroyWindow();
return bComplete;
}
CFileInfo *GetStats(LPCTSTR pszFullFileName)
{
char full_path[1024];
_fullpath(full_path, pszFullFileName, countof(full_path));
CFileInfo& info = s_mapStats[full_path];
if (info.m_bFull)
{
return &info;
}
return NULL;
}