⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 countingdlg.cpp

📁 一个完整的源代码统计器
💻 CPP
📖 第 1 页 / 共 4 页
字号:
		bufRead.TrimLeft();

		if(bufRead.GetLength()==0)
		{
			nBlankLines++;
			continue;
		}

		if(bufRead.Find("--")==0 && !bCommentSet)
		{
			nCommentLines++;
			continue;
		}

		int nStartComment = bufRead.Find("/*");
		if(nStartComment == 0  && !bCommentSet ) // "/*" is the first two chars
		{
			bCommentSet = TRUE;
		}
		else if(nStartComment != -1 &&!bCommentSet) // "/*" is not the first two chars though found. so it's a code line
		{
			bCommentSet = TRUE;
			nCommentLines --;  
		}

		bufRead.TrimRight();

		int nEndComment = bufRead.Find("*/");
		if(bufRead.GetLength()>=2 && (nEndComment == bufRead.GetLength()-2))
		{
			bCommentSet = FALSE;
			nCommentLines ++;  
		}
		else if(nEndComment != -1)
		{
			bCommentSet = FALSE;
			if(bufRead.Find("--")!=-1) //it is very strange!  such as  " */   -- "
			{
				CString sTemp = bufRead.Right(bufRead.GetLength()-nEndComment-3);
				sTemp.TrimLeft();
				if(sTemp.Find("--")==0)
					nCommentLines++;
			}
		}

		if(bCommentSet)
			nCommentLines++;
	}

	*pnCommentLines = nCommentLines;
	*pnBlankLines = nBlankLines;

	file.Close();

	return nLines;
}

int CCountingDlg::GetVBFileLines(LPCTSTR strFileName, int *pnLength, int *pnCommentLines, int *pnBlankLines)
{
	*pnLength = 0;
	*pnCommentLines = 0;
	*pnBlankLines = 0;

	CStdioFile file;
	if(file.Open(strFileName, CFile::modeRead)==FALSE)
		return 0;

	int nLines = 0;
	int nCommentLines = 0;
	int nBlankLines = 0;

	int nLength = file.GetLength();
	m_nSize += nLength;
	*pnLength = nLength;

	CString bufRead;

	while(file.ReadString(bufRead)!=FALSE)
	{
		nLines++;

		bufRead.TrimLeft();

		if(bufRead.GetLength()==0)
		{
			nBlankLines++;
			continue;
		}

		if(bufRead.Find("'")==0)
		{
			nCommentLines++;
			continue;
		}
	}

	*pnCommentLines = nCommentLines;
	*pnBlankLines = nBlankLines;

	file.Close();

	return nLines;
}

int CCountingDlg::GetTxtFileLines(LPCTSTR strFileName, int *pnLength, int *pnCommentLines, int *pnBlankLines)
{
	*pnLength = 0;
	*pnCommentLines = 0;
	*pnBlankLines = 0;

	CStdioFile file;
	if(file.Open(strFileName, CFile::modeRead)==FALSE)
		return 0;

	int nLines = 0;
	int nCommentLines = 0;
	int nBlankLines = 0;

	int nLength = file.GetLength();
	m_nSize += nLength;
	*pnLength = nLength;

	CString bufRead;

	while(file.ReadString(bufRead)!=FALSE)
	{
		nLines++;

		bufRead.TrimLeft();

		if(bufRead.GetLength()==0)
		{
			nBlankLines++;
			continue;
		}
	}

	*pnCommentLines = nCommentLines;
	*pnBlankLines = nBlankLines;

	file.Close();

	return nLines;
}


/*********************************************************************
*  Save as Text file, in which every column has its own width
*	input: filename: the save file name
*	output: void		
**********************************************************************/
void CCountingDlg::SaveAsTextFile(LPCTSTR filename)
{
	CFile file(filename, CFile::modeCreate|CFile::modeWrite);
	CString strText;

#define COLUMN1_WIDTH	20
#define COLUMN2_WIDTH	30
#define COLUMN3_WIDTH	15
#define COLUMN4_WIDTH	15
#define COLUMN5_WIDTH	15
#define COLUMN6_WIDTH	15
#define COLUMN7_WIDTH	30

	strText.LoadString(IDS_FILENAME);
	strText = TextAppendSpace(strText, COLUMN1_WIDTH, TRUE);
	file.Write(strText, strText.GetLength());//write file name
	strText.LoadString(IDS_PATHNAME);
	strText = TextAppendSpace(strText, COLUMN2_WIDTH, TRUE);
	file.Write(strText, strText.GetLength());//write path name
	strText.LoadString(IDS_TOTALLINE);
	strText = TextAppendSpace(strText, COLUMN3_WIDTH, FALSE);
	file.Write(strText, strText.GetLength());
	strText.LoadString(IDS_CODELINE);
	strText = TextAppendSpace(strText, COLUMN4_WIDTH, FALSE);
	file.Write(strText, strText.GetLength());
	strText.LoadString(IDS_COMMENTLINE);
	strText = TextAppendSpace(strText, COLUMN5_WIDTH, FALSE);
	file.Write(strText, strText.GetLength());
	strText.LoadString(IDS_BLANKLINE);
	strText = TextAppendSpace(strText, COLUMN6_WIDTH, FALSE);
	file.Write(strText, strText.GetLength());
	file.Write("  ", 2);
	strText.LoadString(IDS_FILETYPE);
	strText = TextAppendSpace(strText, COLUMN7_WIDTH, TRUE);
	file.Write(strText, strText.GetLength());
	file.Write("\r\n", 2);

	for(int i=0; i<m_ctlResult.GetItemCount(); i++)
	{
		strText = m_ctlResult.GetItemText(i, 0);
		strText = TextAppendSpace(strText, COLUMN1_WIDTH, TRUE);
		file.Write((LPCTSTR)strText, COLUMN1_WIDTH);//write file name

		strText = m_ctlResult.GetItemText(i, 1);
		strText = TextAppendSpace(strText, COLUMN2_WIDTH, TRUE);
		file.Write((LPCTSTR)strText, COLUMN2_WIDTH);

		strText = m_ctlResult.GetItemText(i, 2);
		strText = TextAppendSpace(strText, COLUMN3_WIDTH, FALSE);
		file.Write((LPCTSTR)strText, COLUMN3_WIDTH);

		strText = m_ctlResult.GetItemText(i, 3);
		strText = TextAppendSpace(strText, COLUMN4_WIDTH, FALSE);
		file.Write((LPCTSTR)strText, COLUMN4_WIDTH);

		strText = m_ctlResult.GetItemText(i, 4);
		strText = TextAppendSpace(strText, COLUMN5_WIDTH, FALSE);
		file.Write((LPCTSTR)strText, COLUMN5_WIDTH);

		strText = m_ctlResult.GetItemText(i, 5);
		strText = TextAppendSpace(strText, COLUMN6_WIDTH, FALSE);
		file.Write((LPCTSTR)strText, COLUMN6_WIDTH);

		file.Write("  ", 2);

		strText = m_ctlResult.GetItemText(i, 6);
		strText = TextAppendSpace(strText, COLUMN7_WIDTH, TRUE);
		file.Write((LPCTSTR)strText, COLUMN7_WIDTH);

		file.Write("\r\n", 2);
	}

	file.Write("\r\n", 2);
	strText.LoadString(IDS_TOTAL);
	file.Write(strText, strText.GetLength());
	file.Write("\r\n", 2);

	CString str1, str2;
	str1.LoadString(IDS_TOTALFILES);
	str2.Format("%s: %d", str1, m_nFiles);
	file.Write(str2, str2.GetLength());
	file.Write("\r\n", 2);

	//show the total size
	CString strUnit = "";
	float fSize = (float)m_nSize;
	if(fSize>1024)
	{
		fSize /= 1024.;
		strUnit = "KB";
		if(fSize>1024)
		{
			fSize /= 1024.;
			strUnit = "MB";
		}
	}

	str1.LoadString(IDS_TOTALSIZE);
	str2.Format("%s: %2.2f%s", str1, fSize, strUnit);
	file.Write(str2, str2.GetLength());
	file.Write("\r\n", 2);

	str1.LoadString(IDS_TOTALLINE);
	str2.Format("%s: %d (100%%)", str1, m_nTotalLines);
	file.Write(str2, str2.GetLength());
	file.Write("\r\n", 2);

	str1.LoadString(IDS_CODELINE);
	str2.Format("%s: %d (%.1f%%)", str1, m_nCodeLines, 100.*m_nCodeLines/m_nTotalLines);
	file.Write(str2, str2.GetLength());
	file.Write("\r\n", 2);

	str1.LoadString(IDS_COMMENTLINE);
	str2.Format("%s: %d (%.1f%%)", str1, m_nCommentLines, 100.*m_nCommentLines/m_nTotalLines);
	file.Write(str2, str2.GetLength());
	file.Write("\r\n", 2);

	str1.LoadString(IDS_BLANKLINE);
	str2.Format("%s: %d (%2.1f%%)", str1, m_nBlankLines, 100.*m_nBlankLines/m_nTotalLines);
	file.Write(str2, str2.GetLength());
	file.Write("\r\n", 2);

	str1.LoadString(IDS_SAVEOVER);
	GetDlgItem(IDC_PATH_COUNTING)->SetWindowText(str1);
}

/*********************************************************************
*  Save as csv file which can be analysed by Excel conveniently
*	input: filename: the save file name
*	output: void		
**********************************************************************/
void CCountingDlg::SaveAsCSVFile(LPCTSTR filename)
{
	CStdioFile file(filename, CFile::modeCreate|CFile::modeWrite);
	CString strText;

	CString str1, str2, str3, str4, str5, str6, str7;
	str1.LoadString(IDS_FILENAME);
	str2.LoadString(IDS_PATHNAME);
	str3.LoadString(IDS_TOTALLINE);
	str4.LoadString(IDS_CODELINE);
	str5.LoadString(IDS_COMMENTLINE);
	str6.LoadString(IDS_BLANKLINE);
	str7.LoadString(IDS_FILETYPE);
	strText.Format("%s,%s,%s,%s,%s,%s,%s\n", str1, str2, str3, str4, str5, str6, str7);
	file.WriteString(strText);

	for(int i=0; i<m_ctlResult.GetItemCount(); i++)
	{
		strText.Format("%s,%s,%s,%s,%s,%s,%s\n", 
			m_ctlResult.GetItemText(i, 0), m_ctlResult.GetItemText(i, 1),
			m_ctlResult.GetItemText(i, 2), m_ctlResult.GetItemText(i, 3),
			m_ctlResult.GetItemText(i, 4), m_ctlResult.GetItemText(i, 5),
			m_ctlResult.GetItemText(i, 6));

		file.WriteString(strText);
	}

	file.Write("\n", 1);
	str1.LoadString(IDS_TOTAL);
	file.Write(str1, str1.GetLength());
	file.Write("\n", 1);

	str1.LoadString(IDS_TOTALFILES);
	str2.Format("%s,%d", str1, m_nFiles);
	file.Write(str2, str2.GetLength());
	file.Write("\n", 1);

	//show the total size
	CString strUnit = "";
	float fSize = (float)m_nSize;
	if(fSize>1024)
	{
		fSize /= 1024.;
		strUnit = "KB";
		if(fSize>1024)
		{
			fSize /= 1024.;
			strUnit = "MB";
		}
	}

	str1.LoadString(IDS_TOTALSIZE);
	str2.Format("%s,%2.2f%s", str1, fSize, strUnit);
	file.Write(str2, str2.GetLength());
	file.Write("\n", 1);

	str1.LoadString(IDS_TOTALLINE);
	str2.Format("%s,%d (100%%)", str1, m_nTotalLines);
	file.Write(str2, str2.GetLength());
	file.Write("\n", 1);

	str1.LoadString(IDS_CODELINE);
	str2.Format("%s,%d (%2.1f%%)", str1, m_nCodeLines, 100.*m_nCodeLines/m_nTotalLines);
	file.Write(str2, str2.GetLength());
	file.Write("\n", 1);

	str1.LoadString(IDS_COMMENTLINE);
	str2.Format("%s,%d (%2.1f%%)", str1, m_nCommentLines, 100.*m_nCommentLines/m_nTotalLines);
	file.Write(str2, str2.GetLength());
	file.Write("\n", 1);

	str1.LoadString(IDS_BLANKLINE);
	str2.Format("%s,%d (%2.1f%%)", str1, m_nBlankLines, 100.*m_nBlankLines/m_nTotalLines);
	file.Write(str2, str2.GetLength());
	file.Write("\n", 1);

	str1.LoadString(IDS_SAVEOVER);
	GetDlgItem(IDC_PATH_COUNTING)->SetWindowText(str1);
}

/*********************************************************************
*  Show the About dialogbox
**********************************************************************/
void CCountingDlg::OnAbout() 
{
	// TODO: Add your control notification handler code here
	CAboutDlg dlgAbout;
	dlgAbout.DoModal();
}

/*********************************************************************
*  Update counting result frequently
*	the result including: total files, total size, 
*			total lines, code lines, comment lines, blank lines
**********************************************************************/
void CCountingDlg::UpdateResult()
{
	char sTemp[64]=" ";

	//show the number of files
	sprintf(sTemp, "%d", m_nFiles);
	GetDlgItem(IDC_FILES)->SetWindowText(sTemp);

	//show the total size
	CString strUnit = "";
	float fSize = (float)m_nSize;
	if(fSize>1024)
	{
		fSize /= 1024;
		strUnit = "KB";
		if(fSize>1024)
		{
			fSize /= 1024;
			strUnit = "MB";
		}
	}
	sprintf(sTemp, "%.2f%s", fSize, strUnit);
	GetDlgItem(IDC_SIZES)->SetWindowText(sTemp);

	//show total lines
	sprintf(sTemp, "%d", m_nTotalLines);
	GetDlgItem(IDC_TOTALLINES)->SetWindowText(sTemp);

	//show code lines
	if(m_nTotalLines!=0)
		sprintf(sTemp, "%d(%2.1f)", m_nCodeLines, m_nCodeLines*100./m_nTotalLines);
	else
		sprintf(sTemp, "0(0%)");
	GetDlgItem(IDC_CODELINES)->SetWindowText(sTemp);

	//show comment lines
	if(m_nTotalLines!=0)
		sprintf(sTemp, "%d(%2.1f)", m_nCommentLines, m_nCommentLines*100./m_nTotalLines);
	else
		sprintf(sTemp, "0(0%)");
	GetDlgItem(IDC_COMMENTLINES)->SetWindowText(sTemp);

	//show blank lines
	if(m_nTotalLines!=0)
		sprintf(sTemp, "%d(%2.1f)", m_nBlankLines, m_nBlankLines*100./m_nTotalLines);
	else
		sprintf(sTemp, "0(0%)");
	GetDlgItem(IDC_BLANKLINES)->SetWindowText(sTemp);
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -