some fixes that caused crashes

svn path=/trunk/; revision=9027
This commit is contained in:
Thomas Bluemel 2004-04-08 12:25:00 +00:00
parent f051684bf8
commit b351485907

View file

@ -28,20 +28,13 @@ typedef struct _FILE_INFO
struct _FILE_INFO * Next; struct _FILE_INFO * Next;
struct _FILE_INFO * StatInfoListNext; struct _FILE_INFO * StatInfoListNext;
PEXTENSION_INFO ExtInfo; PEXTENSION_INFO ExtInfo;
TCHAR FileName[256]; TCHAR FileName[MAX_PATH];
DWORD LineCount; DWORD LineCount;
DWORD FunctionCount; DWORD FunctionCount;
} FILE_INFO, *PFILE_INFO; } FILE_INFO, *PFILE_INFO;
HANDLE FileHandle;
DWORD TotalLineCount; DWORD TotalLineCount;
PCHAR FileBuffer;
DWORD FileBufferSize;
CHAR Line[256];
DWORD CurrentOffset;
DWORD CurrentChar;
DWORD CurrentLine;
DWORD LineLength;
PEXTENSION_INFO ExtInfoList; PEXTENSION_INFO ExtInfoList;
PFILE_INFO StatInfoList; PFILE_INFO StatInfoList;
@ -50,11 +43,6 @@ VOID
Initialize() Initialize()
{ {
TotalLineCount = 0; TotalLineCount = 0;
FileBuffer = NULL;
FileBufferSize = 0;
CurrentOffset = 0;
CurrentLine = 0;
LineLength = 0;
ExtInfoList = NULL; ExtInfoList = NULL;
StatInfoList = NULL; StatInfoList = NULL;
} }
@ -171,19 +159,14 @@ AddFile(LPTSTR FileName,
VOID VOID
CleanupAfterFile() CleanupAfterFile()
{ {
if (FileBuffer) if(FileHandle != INVALID_HANDLE_VALUE)
{ CloseHandle (FileHandle);
HeapFree (GetProcessHeap(), 0, FileBuffer);
FileBuffer = NULL;
}
} }
BOOL BOOL
LoadFile(LPTSTR FileName) LoadFile(LPTSTR FileName)
{ {
HANDLE FileHandle;
DWORD BytesRead;
LONG FileSize; LONG FileSize;
FileHandle = CreateFile (FileName, // Create this file FileHandle = CreateFile (FileName, // Create this file
@ -197,84 +180,53 @@ LoadFile(LPTSTR FileName)
return FALSE; return FALSE;
FileSize = GetFileSize (FileHandle, NULL); FileSize = GetFileSize (FileHandle, NULL);
if (FileSize < 0) if (FileSize <= 0)
{ {
CloseHandle (FileHandle); CloseHandle (FileHandle);
return FALSE; return FALSE;
} }
FileBufferSize = (DWORD) FileSize;
FileBuffer = (PCHAR) HeapAlloc (GetProcessHeap(), 0, FileBufferSize);
if (!FileBuffer)
{
CloseHandle (FileHandle);
return FALSE;
}
if (!ReadFile (FileHandle, FileBuffer, FileBufferSize, &BytesRead, NULL))
{
CloseHandle(FileHandle);
HeapFree (GetProcessHeap(), 0, FileBuffer);
FileBuffer = NULL;
return FALSE;
}
CloseHandle (FileHandle);
CurrentOffset = 0;
CurrentLine = 0;
CurrentChar = 0;
return TRUE; return TRUE;
} }
BOOL DWORD
ReadLine() ReadLines()
/*
* FUNCTION: Reads the next line into the line buffer
* RETURNS:
* TRUE if there is a new line, FALSE if not
*/
{ {
ULONG i, j; DWORD ReadBytes, LineLen, Lines = 0;
TCHAR ch; static TCHAR FileBuffer[1024];
TCHAR LastChar = _T('\0');
TCHAR *Current;
if (CurrentOffset >= FileBufferSize) LineLen = 0;
return FALSE; while(ReadFile (FileHandle, FileBuffer, sizeof(FileBuffer), &ReadBytes, NULL) && ReadBytes >= sizeof(TCHAR))
i = 0;
while ((((j = CurrentOffset + i) < FileBufferSize) && (i < sizeof (Line)) &&
((ch = FileBuffer[j]) != 0x0D && (ch = FileBuffer[j]) != 0x0A)))
{ {
Line[i] = ch; if(ReadBytes & 0x1)
i++; ReadBytes--;
for(Current = FileBuffer; ReadBytes > 0; ReadBytes -= sizeof(TCHAR), Current++)
{
if(*Current == 0x0A && LastChar == 0x0D)
{
LastChar = _T('\0');
if(LineLen > 0)
Lines++;
LineLen = 0;
}
LineLen++;
LastChar = *Current;
}
} }
Line[i] = '\0'; Lines += (LineLen > 0);
LineLength = i; return Lines;
if ((FileBuffer[CurrentOffset + i] == 0x0D) && (FileBuffer[CurrentOffset + i + 1] == 0x0A))
CurrentOffset++;
CurrentOffset += i + 1;
CurrentChar = 0;
CurrentLine++;
return TRUE;
} }
VOID VOID
DoStatisticsForFile(PFILE_INFO StatInfo) DoStatisticsForFile(PFILE_INFO StatInfo)
{ {
while (ReadLine()) StatInfo->LineCount = ReadLines();
{
}
StatInfo->LineCount = CurrentLine;
} }
@ -400,7 +352,7 @@ ProcessDirectories(LPTSTR Path)
HANDLE SearchHandle; HANDLE SearchHandle;
BOOL More; BOOL More;
_tprintf (_T("Processing directory %s\n"), Path); _tprintf (_T("Processing %s ...\n"), Path);
_tcscpy (SearchPath, Path); _tcscpy (SearchPath, Path);
_tcscat (SearchPath, _T("\\*.*")); _tcscat (SearchPath, _T("\\*.*"));