added paramter -e to skip and count empty lines

svn path=/trunk/; revision=9133
This commit is contained in:
Thomas Bluemel 2004-04-13 21:34:45 +00:00
parent b1a7c433b7
commit bbd9b3d4a8

View file

@ -21,6 +21,7 @@ typedef struct _EXTENSION_INFO
TCHAR Description[256]; TCHAR Description[256];
DWORD FileCount; DWORD FileCount;
DWORD LineCount; DWORD LineCount;
DWORD EmptyLines;
} EXTENSION_INFO, *PEXTENSION_INFO; } EXTENSION_INFO, *PEXTENSION_INFO;
typedef struct _FILE_INFO typedef struct _FILE_INFO
@ -30,26 +31,29 @@ typedef struct _FILE_INFO
PEXTENSION_INFO ExtInfo; PEXTENSION_INFO ExtInfo;
TCHAR FileName[MAX_PATH]; TCHAR FileName[MAX_PATH];
DWORD LineCount; DWORD LineCount;
DWORD EmptyLines;
DWORD FunctionCount; DWORD FunctionCount;
} FILE_INFO, *PFILE_INFO; } FILE_INFO, *PFILE_INFO;
HANDLE FileHandle; HANDLE FileHandle;
DWORD TotalLineCount;
PEXTENSION_INFO ExtInfoList; PEXTENSION_INFO ExtInfoList;
PFILE_INFO StatInfoList; PFILE_INFO StatInfoList;
BOOLEAN SkipEmptyLines;
#define MAX_OPTIONS 1
TCHAR *Options[MAX_OPTIONS];
VOID VOID
Initialize() Initialize(VOID)
{ {
TotalLineCount = 0;
ExtInfoList = NULL; ExtInfoList = NULL;
StatInfoList = NULL; StatInfoList = NULL;
} }
VOID VOID
Cleanup() Cleanup(VOID)
{ {
PEXTENSION_INFO ExtInfo; PEXTENSION_INFO ExtInfo;
PEXTENSION_INFO NextExtInfo; PEXTENSION_INFO NextExtInfo;
@ -157,7 +161,7 @@ AddFile(LPTSTR FileName,
VOID VOID
CleanupAfterFile() CleanupAfterFile(VOID)
{ {
if(FileHandle != INVALID_HANDLE_VALUE) if(FileHandle != INVALID_HANDLE_VALUE)
CloseHandle (FileHandle); CloseHandle (FileHandle);
@ -190,10 +194,10 @@ LoadFile(LPTSTR FileName)
} }
DWORD VOID
ReadLines() ReadLines(PFILE_INFO StatInfo)
{ {
DWORD ReadBytes, LineLen, Lines = 0; DWORD ReadBytes, LineLen;
static char FileBuffer[1024]; static char FileBuffer[1024];
char LastChar = '\0'; char LastChar = '\0';
char *Current; char *Current;
@ -201,51 +205,52 @@ ReadLines()
LineLen = 0; LineLen = 0;
while(ReadFile (FileHandle, FileBuffer, sizeof(FileBuffer), &ReadBytes, NULL) && ReadBytes >= sizeof(char)) while(ReadFile (FileHandle, FileBuffer, sizeof(FileBuffer), &ReadBytes, NULL) && ReadBytes >= sizeof(char))
{ {
if(ReadBytes & 0x1)
ReadBytes--;
for(Current = FileBuffer; ReadBytes > 0; ReadBytes -= sizeof(char), Current++) for(Current = FileBuffer; ReadBytes > 0; ReadBytes -= sizeof(char), Current++)
{ {
if(*Current == '\n' && LastChar == '\r') if(*Current == '\n' && LastChar == '\r')
{ {
LastChar = '\0'; LastChar = '\0';
if(LineLen > 0) if(!SkipEmptyLines || (LineLen > 0))
Lines++; StatInfo->LineCount++;
if(LineLen == 0)
StatInfo->EmptyLines++;
LineLen = 0; LineLen = 0;
continue;
} }
LineLen++;
LastChar = *Current; LastChar = *Current;
} if(SkipEmptyLines && (*Current == ' ' || *Current == '\t'))
}
Lines += (LineLen > 0);
return Lines;
}
VOID
DoStatisticsForFile(PFILE_INFO StatInfo)
{ {
StatInfo->LineCount = ReadLines(); continue;
}
if(*Current != '\r')
LineLen++;
}
}
StatInfo->LineCount += (LineLen > 0);
StatInfo->EmptyLines += ((LastChar != '\0') && (LineLen == 0));
} }
VOID VOID
PrintStatistics() PrintStatistics(VOID)
{ {
PEXTENSION_INFO Info; PEXTENSION_INFO Info;
DWORD TotalFileCount; DWORD TotalFileCount;
DWORD TotalLineCount; DWORD TotalLineCount;
DWORD TotalAvgLF; DWORD TotalAvgLF;
DWORD TotalEmptyLines;
TotalFileCount = 0; TotalFileCount = 0;
TotalLineCount = 0; TotalLineCount = 0;
TotalEmptyLines = 0;
Info = ExtInfoList; Info = ExtInfoList;
for (Info = ExtInfoList; Info != NULL; Info = Info->Next) for (Info = ExtInfoList; Info != NULL; Info = Info->Next)
{ {
TotalFileCount += Info->FileCount; TotalFileCount += Info->FileCount;
TotalLineCount += Info->LineCount; TotalLineCount += Info->LineCount;
TotalEmptyLines += Info->EmptyLines;
} }
TotalAvgLF = (TotalFileCount ? TotalLineCount / TotalFileCount : 0); TotalAvgLF = (TotalFileCount ? TotalLineCount / TotalFileCount : 0);
@ -268,6 +273,8 @@ PrintStatistics()
_tprintf (_T("File ext. description : %s\n"), Info->Description); _tprintf (_T("File ext. description : %s\n"), Info->Description);
_tprintf (_T("Number of files : %lu\n"), Info->FileCount); _tprintf (_T("Number of files : %lu\n"), Info->FileCount);
_tprintf (_T("Number of lines : %lu\n"), Info->LineCount); _tprintf (_T("Number of lines : %lu\n"), Info->LineCount);
if(SkipEmptyLines)
_tprintf (_T("Number of empty lines : %lu\n"), Info->EmptyLines);
_tprintf (_T("Proportion of lines : %.2f %%\n"), (float)(TotalLineCount ? (((float)Info->LineCount * 100) / (float)TotalLineCount) : 0)); _tprintf (_T("Proportion of lines : %.2f %%\n"), (float)(TotalLineCount ? (((float)Info->LineCount * 100) / (float)TotalLineCount) : 0));
_tprintf (_T("Average no. lines/file : %lu\n"), AvgLF); _tprintf (_T("Average no. lines/file : %lu\n"), AvgLF);
} }
@ -275,6 +282,8 @@ PrintStatistics()
_tprintf (_T("\n")); _tprintf (_T("\n"));
_tprintf (_T("Total number of files : %lu\n"), TotalFileCount); _tprintf (_T("Total number of files : %lu\n"), TotalFileCount);
_tprintf (_T("Total number of lines : %lu\n"), TotalLineCount); _tprintf (_T("Total number of lines : %lu\n"), TotalLineCount);
if(SkipEmptyLines)
_tprintf (_T("Total number of empty lines : %lu\n"), TotalEmptyLines);
_tprintf (_T("Average no. lines/file : %lu\n"), TotalAvgLF); _tprintf (_T("Average no. lines/file : %lu\n"), TotalAvgLF);
} }
@ -324,10 +333,11 @@ ProcessFiles(LPTSTR Path)
return FALSE; return FALSE;
} }
DoStatisticsForFile (StatInfo); ReadLines (StatInfo);
Info->FileCount++; Info->FileCount++;
Info->LineCount += StatInfo->LineCount; Info->LineCount += StatInfo->LineCount;
Info->EmptyLines += StatInfo->EmptyLines;
CleanupAfterFile(); CleanupAfterFile();
} }
@ -413,19 +423,36 @@ Execute(LPTSTR Path)
PrintStatistics(); PrintStatistics();
} }
BOOLEAN
IsOptionSet(TCHAR *Option)
{
int i;
for(i = 0; i < MAX_OPTIONS; i++)
{
if(!Options[i])
continue;
if(!_tcscmp(Options[i], Option))
return TRUE;
}
return FALSE;
}
int int
main (int argc, char * argv []) main (int argc, char * argv [])
{ {
int a;
#if UNICODE #if UNICODE
TCHAR Path[MAX_PATH + 1]; TCHAR Path[MAX_PATH + 1];
#endif #endif
_tprintf (_T("\nReactOS project statistics generator.\n\n")); _tprintf (_T("\nReactOS project statistics generator.\n\n"));
if (argc < 2) if (argc < 2 || argc > 2 + MAX_OPTIONS)
{ {
_tprintf(_T("Usage: stats directory")); _tprintf(_T("Usage: stats [-e] directory\n"));
_tprintf(_T(" -e: don't count empty lines"));
return 1; return 1;
} }
@ -433,12 +460,29 @@ main (int argc, char * argv [])
AddExtension (_T("c\0\0"), _T("Ansi C Source files")); AddExtension (_T("c\0\0"), _T("Ansi C Source files"));
AddExtension (_T("cpp\0cxx\0\0"), _T("C++ Source files")); AddExtension (_T("cpp\0cxx\0\0"), _T("C++ Source files"));
AddExtension (_T("h\0\0"), _T("Header files")); AddExtension (_T("h\0\0"), _T("Header files"));
for(a = 1; a < argc - 1; a++)
{
#if UNICODE
int len = lstrlenA(argv[a]);
TCHAR *str = (TCHAR*)HeapAlloc(GetProcessHeap(), 0, (len + 1) * sizeof(TCHAR));
if(MultiByteToWideChar(CP_ACP, 0, argv[a], -1, str, len + 1) > 0)
Options[a - 1] = str;
else
Options[a - 1] = NULL;
#else
Options[a - 1] = argv[a];
#endif
}
SkipEmptyLines = IsOptionSet(_T("-e"));
#if UNICODE #if UNICODE
ZeroMemory(Path, sizeof(Path)); ZeroMemory(Path, sizeof(Path));
if(MultiByteToWideChar(CP_ACP, 0, argv[1], -1, Path, MAX_PATH) > 0) if(MultiByteToWideChar(CP_ACP, 0, argv[argc - 1], -1, Path, MAX_PATH) > 0)
Execute (Path); Execute (Path);
#else #else
Execute (argv[1]); Execute (argv[argc - 1]);
#endif #endif
Cleanup(); Cleanup();