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

📄 checkdir.cpp

📁 用C实现的统计一个目录下的所有源程序共有多少行
💻 CPP
字号:
// CheckDir.cpp : implementation file
//

#include "stdafx.h"
#include "CheckDir.h"
#include <direct.h>
#include "ChkLine32.h"
#include "ChkLine32Dlg.h"

DWORD		g_dwFiles;
DWORD		g_dwLines;

char		g_rPath[_MAX_PATH];
char		g_SrcFile[_MAX_PATH];

int DoSearch(void* pVoid)
{
	CChkLine32Dlg* pDlgMain = (CChkLine32Dlg*)pVoid;
	lstrcpy(g_SrcFile, pDlgMain->m_strSrcFile);

	char szOldPath[_MAX_PATH];
	_getcwd(szOldPath, _MAX_PATH);
	_chdrive((*((const char*)(pDlgMain->m_strDir))) - 'A' + 1);
	_chdir(pDlgMain->m_strDir);

	strcpy(g_rPath, pDlgMain->m_strDir);
	if (*(strrchr(g_rPath, '\\') + 1) != '\0')
		strcat(g_rPath, "\\");		// C:\;

	g_dwFiles = 0;
	g_dwLines = 0;

	ChkDir("");

	_chdrive((*szOldPath) - 'A' + 1);
	_chdir(szOldPath);

	return 0;
}

void SearchInFile(const char* pszFileName)
{
	FILE *stream;
	char line[2048];
	if( (stream = fopen( pszFileName, "r" )) != NULL )
	{
		while ( fgets( line, 2048, stream ) != NULL)
			g_dwLines++;
		fclose( stream );
	}
}

void ChkFile(const char* pszFileName)
{
	if ((*pszFileName) == '\0') return;

	char szBuffer[_MAX_PATH];

	sprintf(szBuffer, "%s", g_rPath);
	sprintf(szBuffer + strlen(szBuffer), "%s", pszFileName);
	(((CChkLine32Dlg*)(AfxGetApp()->m_pMainWnd))->GetDlgItem(IDC_STATIC_INFO))->SetWindowText(szBuffer);

	g_dwFiles++;
	SearchInFile(pszFileName);
}

void ChkDir(const char* pszNextPath)
{
	WIN32_FIND_DATA  c_file;
	
	if ((*pszNextPath) != '\0') {
		_chdir(pszNextPath);
		strcat(g_rPath, pszNextPath);
		strcat(g_rPath, "\\");
	}
		
	HANDLE hFindFile;
	char szBuffer[_MAX_PATH];
	char szFindInFiles[_MAX_PATH];
	lstrcpy(szBuffer, g_SrcFile);
	char* token = strtok(szBuffer, " ;");
	while( token != NULL )
	{
		lstrcpy(szFindInFiles, token);
		if (*szFindInFiles == '.')
		{
			*szFindInFiles = '*';
			lstrcpy((szFindInFiles+1), token);
		}

		hFindFile = ::FindFirstFile(szFindInFiles, &c_file);
		if (hFindFile != INVALID_HANDLE_VALUE)
		{
			if ((c_file.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) == 0)
				ChkFile(c_file.cFileName);  // a file
		
			while(::FindNextFile(hFindFile, &c_file))
				if ((c_file.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) == 0)
					ChkFile(c_file.cFileName);  // a file
		
			::FindClose(hFindFile);
		}

		token = strtok( NULL, " ;");
	}

//	if (m_bChkSubDir) {
		hFindFile = ::FindFirstFile("*.*", &c_file); 
		if (hFindFile != INVALID_HANDLE_VALUE)
		{
			
		    if ((*c_file.cFileName) == '.')
			{
		    	FindNextFile(hFindFile, &c_file);   //filt ..
				FindNextFile(hFindFile, &c_file);
		    }
		
		    if ((*c_file.cFileName) != '.')
			{    //not an empty directory
				if ((c_file.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) != 0)
					ChkDir(c_file.cFileName); // a subdir
		
				while(::FindNextFile(hFindFile, &c_file))
					if ((c_file.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) != 0)
						ChkDir(c_file.cFileName); // a subdir
			}

			::FindClose(hFindFile);
		}
//	}
	
	*(strrchr(g_rPath, '\\')) = '\0';  // C:\DOS\;
	if (strrchr(g_rPath, '\\') != NULL)
		*(strrchr(g_rPath, '\\') + 1) = '\0';
	_chdir("..");
}

⌨️ 快捷键说明

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