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

📄 countingdlg.cpp

📁 不说了
💻 CPP
📖 第 1 页 / 共 5 页
字号:
			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 *pnCodeLines, 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 nCodeLines = 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;
		}

		if(bufRead.Find("'")!=-1)
		{
			if(m_nStatMethod==0)
				nCodeLines++;
			else if(m_nStatMethod==1)
			{
				nCodeLines++;
				nCommentLines++;
			}
			else if(m_nStatMethod==2)
			{
				nCommentLines++;
			}
			continue;
		}
		else
			nCodeLines++;
	}

	*pnCodeLines = nCodeLines;
	*pnCommentLines = nCommentLines;
	*pnBlankLines = nBlankLines;

	file.Close();

	return nLines;
}

int CCountingDlg::GetPerlFileLines(LPCTSTR strFileName, int *pnLength, int *pnCodeLines, 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 nCodeLines = 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;
		}

		if(bufRead.Find("#")!=-1)
		{
			if(m_nStatMethod==0)
				nCodeLines++;
			else if(m_nStatMethod==1)
			{
				nCodeLines++;
				nCommentLines++;
			}
			else if(m_nStatMethod==2)
			{
				nCommentLines++;
			}
			continue;
		}
		else
			nCodeLines++;
	}

	*pnCodeLines = nCodeLines;
	*pnCommentLines = nCommentLines;
	*pnBlankLines = nBlankLines;

	file.Close();

	return nLines;
}

int CCountingDlg::GetTxtFileLines(LPCTSTR strFileName, int *pnLength, int *pnCodeLines, 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);
}

/*********************************************************************
*  Save as Excel file 参考徐景周代码

/////////////////////////////////////////////////////////////////////////////
//名称:OnWriteexcel
//功能:创建并写入数据到Excel文件中
//作者:徐景周(jingzhou_xu@163.net)
//组织:未来工作室(Future Studio)
//日期:2002.9.1
/////////////////////////////////////////////////////////////////////////////

*	input: filename: the save file name
*	output: void		
**********************************************************************/
void CCountingDlg::SaveAsExcelFile(LPCTSTR filename)
{
	CDatabase database;
	CString sDriver = "MICROSOFT EXCEL DRIVER (*.XLS)"; // Excel安装驱动
	CString sSql;
    
	//获取主程序所在路径,存在sPath中
//	GetModuleFileName(NULL,sPath.GetBufferSetLength (MAX_PATH+1),MAX_PATH);
//	sPath.ReleaseBuffer ();
	
	TRY
	{
		// 创建进行存取的字符串
		sSql.Format("DRIVER={%s};DSN='';FIRSTROWHASNAMES=1;READONLY=FALSE;CREATE_DB=\"%s\";DBQ=%s",
			sDriver, filename, filename);
		
		// 创建数据库 (既Excel表格文件)
		if( database.OpenEx(sSql, CDatabase::noOdbcDialog) )
		{
			// 创建表结构(姓名、年龄)
			sSql = "CREATE TABLE 统计结果 (文件名 TEXT, 所在文件夹 TEXT, 总行数 NUMBER, 代码行 NUMBER, 注释行 NUMBER, 

⌨️ 快捷键说明

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