[Common Code] Clean up some of the code

This commit is contained in:
zilmar 2015-11-06 22:37:21 +11:00
parent 655abeb473
commit a3172b30ab
7 changed files with 209 additions and 206 deletions

View File

@ -91,8 +91,7 @@ bool CFile::Open(const char * lpszFileName, uint32_t nOpenFlags)
dwCreateFlag = OPEN_EXISTING; dwCreateFlag = OPEN_EXISTING;
// attempt file creation // attempt file creation
HANDLE hFile = ::CreateFile(lpszFileName, dwAccess, dwShareMode, &sa, HANDLE hFile = ::CreateFile(lpszFileName, dwAccess, dwShareMode, &sa, dwCreateFlag, FILE_ATTRIBUTE_NORMAL, NULL);
dwCreateFlag, FILE_ATTRIBUTE_NORMAL, NULL);
if (hFile == INVALID_HANDLE_VALUE) if (hFile == INVALID_HANDLE_VALUE)
{ //#define ERROR_PATH_NOT_FOUND 3L { //#define ERROR_PATH_NOT_FOUND 3L
//ULONG err = GetLastError(); //ULONG err = GetLastError();
@ -169,12 +168,12 @@ uint32_t CFile::Read(void* lpBuf, uint32_t nCount)
return 0; // avoid Win32 "null-read" return 0; // avoid Win32 "null-read"
} }
ULONG dwRead = 0; DWORD dwRead = 0;
if (!::ReadFile(m_hFile, lpBuf, nCount, &dwRead, NULL)) if (!::ReadFile(m_hFile, lpBuf, nCount, &dwRead, NULL))
{ {
return 0; return 0;
} }
return (UINT)dwRead; return (uint32_t)dwRead;
} }
long CFile::Seek(long lOff, SeekPosition nFrom) long CFile::Seek(long lOff, SeekPosition nFrom)

View File

@ -5,7 +5,8 @@
class CFileBase class CFileBase
{ {
public: public:
enum OpenFlags { enum OpenFlags
{
modeRead = 0x0000, modeRead = 0x0000,
modeWrite = 0x0001, modeWrite = 0x0001,
modeReadWrite = 0x0002, modeReadWrite = 0x0002,
@ -19,7 +20,8 @@ public:
modeNoTruncate = 0x2000, modeNoTruncate = 0x2000,
}; };
enum Attribute { enum Attribute
{
normal = 0x00, normal = 0x00,
readOnly = 0x01, readOnly = 0x01,
hidden = 0x02, hidden = 0x02,

View File

@ -2,7 +2,6 @@
#include <windows.h> #include <windows.h>
#include <stdio.h> #include <stdio.h>
#include <stdarg.h> #include <stdarg.h>
#include <TChar.h>
CLog::CLog (void ) : CLog::CLog (void ) :
m_FlushOnWrite(false), m_FlushOnWrite(false),
@ -16,7 +15,7 @@ CLog::~CLog (void)
{ {
} }
bool CLog::Open( LPCTSTR FileName, LOG_OPEN_MODE mode /* = Log_New */) bool CLog::Open( const char * FileName, LOG_OPEN_MODE mode /* = Log_New */)
{ {
if (FileName == NULL) if (FileName == NULL)
{ {
@ -34,14 +33,14 @@ bool CLog::Open( LPCTSTR FileName, LOG_OPEN_MODE mode /* = Log_New */)
m_hLogFile.Close(); m_hLogFile.Close();
} }
ULONG nOpenFlags = CFile::modeReadWrite | CFile::modeCreate; uint32_t nOpenFlags = CFile::modeReadWrite | CFile::modeCreate;
if (mode == Log_Append) { nOpenFlags |= CFile::modeNoTruncate; } if (mode == Log_Append) { nOpenFlags |= CFile::modeNoTruncate; }
if (!m_hLogFile.Open(File, nOpenFlags)) if (!m_hLogFile.Open(File, nOpenFlags))
{ {
return false; return false;
} }
m_FileName = (LPCTSTR)File; m_FileName = (const char *)File;
m_hLogFile.Seek(0,mode == Log_Append ? CFile::end : CFile::begin); m_hLogFile.Seek(0,mode == Log_Append ? CFile::end : CFile::begin);
#ifdef _UNICODE #ifdef _UNICODE
@ -64,7 +63,7 @@ void CLog::Close ( void )
} }
} }
void CLog::LogF(LPCTSTR Message, ...) void CLog::LogF(const char * Message, ...)
{ {
va_list ap; va_list ap;
va_start( ap, Message ); va_start( ap, Message );
@ -72,7 +71,7 @@ void CLog::LogF(LPCTSTR Message, ...)
va_end( ap ); va_end( ap );
} }
void CLog::LogArgs(LPCTSTR Message, va_list & args ) void CLog::LogArgs(const char * Message, va_list & args )
{ {
if (!m_hLogFile.IsOpen()) { return; } if (!m_hLogFile.IsOpen()) { return; }
@ -112,14 +111,16 @@ void CLog::LogArgs(LPCTSTR Message, va_list & args )
} }
if (buffer) if (buffer)
{
delete [] buffer; delete [] buffer;
}
#endif #endif
} }
void CLog::Log( LPCTSTR Message ) void CLog::Log( const char * Message )
{ {
if (!m_hLogFile.IsOpen()) { return; } if (!m_hLogFile.IsOpen()) { return; }
m_hLogFile.Write(Message,(ULONG)_tcslen(Message)*sizeof(TCHAR)); m_hLogFile.Write(Message,(uint32_t)strlen(Message)*sizeof(TCHAR));
if (m_FlushOnWrite) if (m_FlushOnWrite)
{ {
m_hLogFile.Flush(); m_hLogFile.Flush();
@ -128,7 +129,7 @@ void CLog::Log( LPCTSTR Message )
if (m_TruncateFileLog) if (m_TruncateFileLog)
{ {
// check file size // check file size
ULONG FileSize = m_hLogFile.GetLength(); uint32_t FileSize = m_hLogFile.GetLength();
// if larger then max size then // if larger then max size then
if (FileSize > m_MaxFileSize) if (FileSize > m_MaxFileSize)
{ {
@ -138,16 +139,17 @@ void CLog::Log( LPCTSTR Message )
FileSize = m_hLogFile.GetLength(); FileSize = m_hLogFile.GetLength();
} }
DWORD end = m_hLogFile.SeekToEnd(); uint32_t end = m_hLogFile.SeekToEnd();
// move to reduce size // move to reduce size
m_hLogFile.Seek((end - m_MaxFileSize) + m_FileChangeSize,CFile::begin); m_hLogFile.Seek((end - m_MaxFileSize) + m_FileChangeSize,CFile::begin);
// Find next end of line // Find next end of line
DWORD NextEnter = 0, dwRead = 0; uint32_t NextEnter = 0, dwRead = 0;
do { do
{
BYTE Data[300]; BYTE Data[300];
DWORD dwRead; uint32_t dwRead;
dwRead = m_hLogFile.Read(Data,sizeof(Data)); dwRead = m_hLogFile.Read(Data,sizeof(Data));
if (dwRead == 0) if (dwRead == 0)
@ -168,9 +170,10 @@ void CLog::Log( LPCTSTR Message )
} while(dwRead != 0); } while(dwRead != 0);
// copy content of log to the new file // copy content of log to the new file
DWORD ReadPos = (end - m_MaxFileSize) + m_FileChangeSize + NextEnter; uint32_t ReadPos = (end - m_MaxFileSize) + m_FileChangeSize + NextEnter;
DWORD SizeToRead, WritePos = 0; uint32_t SizeToRead, WritePos = 0;
do { do
{
enum { fIS_MvSize = 0x5000 }; enum { fIS_MvSize = 0x5000 };
unsigned char Data[fIS_MvSize + 1]; unsigned char Data[fIS_MvSize + 1];
@ -179,7 +182,7 @@ void CLog::Log( LPCTSTR Message )
m_hLogFile.Seek(ReadPos,CFile::begin); m_hLogFile.Seek(ReadPos,CFile::begin);
DWORD dwRead; uint32_t dwRead;
dwRead = m_hLogFile.Read(Data,SizeToRead); dwRead = m_hLogFile.Read(Data,SizeToRead);
m_hLogFile.Seek(WritePos,CFile::begin); m_hLogFile.Seek(WritePos,CFile::begin);

View File

@ -1,11 +1,13 @@
#ifndef __LOG_CLASS__H__ #pragma once
#define __LOG_CLASS__H__ #include "File Class.h"
enum LOG_OPEN_MODE { enum LOG_OPEN_MODE
{
Log_New, Log_Append Log_New, Log_Append
}; };
class CLog { class CLog
{
enum { MB = 1024 * 1024 }; enum { MB = 1024 * 1024 };
enum { MAX_FILE_SIZE = 10 * MB }; enum { MAX_FILE_SIZE = 10 * MB };
@ -13,24 +15,24 @@ class CLog {
bool m_FlushOnWrite; bool m_FlushOnWrite;
stdstr m_FileName; stdstr m_FileName;
bool m_TruncateFileLog; bool m_TruncateFileLog;
ULONG m_MaxFileSize; uint32_t m_MaxFileSize;
ULONG m_FileChangeSize; uint32_t m_FileChangeSize;
public: public:
CLog ( void ); CLog ( void );
~CLog ( void ); ~CLog ( void );
bool Open ( LPCTSTR FileName, LOG_OPEN_MODE mode = Log_New ); bool Open ( const char * FileName, LOG_OPEN_MODE mode = Log_New );
void Log ( LPCTSTR Message ); void Log ( const char * Message );
void LogF ( LPCTSTR Message, ... ); void LogF ( const char * Message, ... );
void LogArgs ( LPCTSTR Message, va_list & args ); void LogArgs ( const char * Message, va_list & args );
bool Empty ( void ); bool Empty ( void );
void Close ( void ); void Close ( void );
inline void SetMaxFileSize ( ULONG Size ) inline void SetMaxFileSize ( uint32_t Size )
{ {
m_MaxFileSize = Size; m_MaxFileSize = Size;
m_FileChangeSize = (ULONG)(Size * 0.1); m_FileChangeSize = (uint32_t)(Size * 0.1);
} }
inline void SetTruncateFile( bool Truncate ) { m_TruncateFileLog = Truncate; } inline void SetTruncateFile( bool Truncate ) { m_TruncateFileLog = Truncate; }
inline void SetFlush ( bool Always ) { m_FlushOnWrite = Always; } inline void SetFlush ( bool Always ) { m_FlushOnWrite = Always; }
@ -38,5 +40,3 @@ public:
inline bool Flush ( void ) { return m_hLogFile.Flush(); } inline bool Flush ( void ) { return m_hLogFile.Flush(); }
inline const stdstr & FileName ( void ) const { return m_FileName; } inline const stdstr & FileName ( void ) const { return m_FileName; }
}; };
#endif

View File

@ -12,20 +12,20 @@ public:
CTraceLog() CTraceLog()
{ {
} }
~CTraceLog() { CloseTrace (); } ~CTraceLog() { CloseTrace(); }
CTraceModule * AddTraceModule ( CTraceModule * TraceModule ); CTraceModule * AddTraceModule(CTraceModule * TraceModule);
CTraceModule * RemoveTraceModule ( CTraceModule * TraceModule ); CTraceModule * RemoveTraceModule(CTraceModule * TraceModule);
void CloseTrace ( void ); void CloseTrace(void);
void WriteTrace ( TraceType Type, LPCTSTR Message ); void WriteTrace(TraceType Type, LPCTSTR Message);
void WriteTraceF ( TraceType Type, LPCTSTR strFormat, va_list args); void WriteTraceF(TraceType Type, LPCTSTR strFormat, va_list args);
}; };
CTraceModule * CTraceLog::AddTraceModule ( CTraceModule * TraceModule ) CTraceModule * CTraceLog::AddTraceModule(CTraceModule * TraceModule)
{ {
CGuard Guard(m_CS); CGuard Guard(m_CS);
for (int i = 0; i < (int)m_Modules.size(); i++ ) for (int i = 0; i < (int)m_Modules.size(); i++)
{ {
if (m_Modules[i] == TraceModule) if (m_Modules[i] == TraceModule)
{ {
@ -36,7 +36,7 @@ CTraceModule * CTraceLog::AddTraceModule ( CTraceModule * TraceModule )
return TraceModule; return TraceModule;
} }
CTraceModule * CTraceLog::RemoveTraceModule ( CTraceModule * TraceModule ) CTraceModule * CTraceLog::RemoveTraceModule(CTraceModule * TraceModule)
{ {
CGuard Guard(m_CS); CGuard Guard(m_CS);
@ -51,38 +51,38 @@ CTraceModule * CTraceLog::RemoveTraceModule ( CTraceModule * TraceModule )
return NULL; return NULL;
} }
void CTraceLog::CloseTrace ( void) void CTraceLog::CloseTrace(void)
{ {
CGuard Guard(m_CS); CGuard Guard(m_CS);
for (int i = 0; i < (int)m_Modules.size(); i++ ) for (int i = 0; i < (int)m_Modules.size(); i++)
{ {
if(m_Modules[i]) if (m_Modules[i])
delete m_Modules[i]; delete m_Modules[i];
} }
m_Modules.clear(); m_Modules.clear();
} }
void CTraceLog::WriteTraceF ( TraceType Type, LPCTSTR strFormat, va_list args) void CTraceLog::WriteTraceF(TraceType Type, LPCTSTR strFormat, va_list args)
{ {
const int nMaxSize = 32*1024; const int nMaxSize = 32 * 1024;
TCHAR pBuffer[nMaxSize]; TCHAR pBuffer[nMaxSize];
_vsntprintf(pBuffer,nMaxSize,strFormat,args); _vsntprintf(pBuffer, nMaxSize, strFormat, args);
pBuffer[nMaxSize - 1] = 0; pBuffer[nMaxSize - 1] = 0;
WriteTrace(Type,pBuffer); WriteTrace(Type, pBuffer);
} }
void CTraceLog::WriteTrace ( TraceType Type, LPCTSTR Message) void CTraceLog::WriteTrace(TraceType Type, LPCTSTR Message)
{ {
CGuard Guard(m_CS); CGuard Guard(m_CS);
if (Type != TraceNone) if (Type != TraceNone)
{ {
bool WriteToLog = false; bool WriteToLog = false;
for (int i = 0; i < (int)m_Modules.size(); i++ ) for (int i = 0; i < (int)m_Modules.size(); i++)
{ {
if ((m_Modules[i]->GetTraceLevel() & Type) != 0) if ((m_Modules[i]->GetTraceLevel() & Type) != 0)
{ {
WriteToLog = true; WriteToLog = true;
break; break;
@ -99,58 +99,57 @@ void CTraceLog::WriteTrace ( TraceType Type, LPCTSTR Message)
SYSTEMTIME sysTime; SYSTEMTIME sysTime;
::GetLocalTime(&sysTime); ::GetLocalTime(&sysTime);
nPos = _stprintf( pBuffer, _T("%04d/%02d/%02d %02d:%02d:%02d.%03d %05d: "), sysTime.wYear, nPos = _stprintf(pBuffer, _T("%04d/%02d/%02d %02d:%02d:%02d.%03d %05d: "), sysTime.wYear,
sysTime.wMonth,sysTime.wDay,sysTime.wHour,sysTime.wMinute,sysTime.wSecond,sysTime.wMilliseconds, sysTime.wMonth, sysTime.wDay, sysTime.wHour, sysTime.wMinute, sysTime.wSecond, sysTime.wMilliseconds,
::GetCurrentThreadId() ::GetCurrentThreadId()
); );
// show the debug level // show the debug level
if (Type == TraceNone) { nPos += _stprintf(pBuffer+nPos,_T("%s"),_T("None : ")); } if (Type == TraceNone) { nPos += _stprintf(pBuffer + nPos, _T("%s"), _T("None : ")); }
else if ((Type & TraceError) != 0) { nPos += _stprintf(pBuffer+nPos,_T("%s"),_T("Error : ")); } else if ((Type & TraceError) != 0) { nPos += _stprintf(pBuffer + nPos, _T("%s"), _T("Error : ")); }
else if ((Type & TraceSettings) != 0) { nPos += _stprintf(pBuffer+nPos,_T("%s"),_T("Setting: ")); } else if ((Type & TraceSettings) != 0) { nPos += _stprintf(pBuffer + nPos, _T("%s"), _T("Setting: ")); }
else if ((Type & TraceGfxPlugin) != 0) { nPos += _stprintf(pBuffer+nPos,_T("%s"),_T("Gfx : ")); } else if ((Type & TraceGfxPlugin) != 0) { nPos += _stprintf(pBuffer + nPos, _T("%s"), _T("Gfx : ")); }
else if ((Type & TraceDebug) != 0) { nPos += _stprintf(pBuffer+nPos,_T("%s"),_T("Debug : ")); } else if ((Type & TraceDebug) != 0) { nPos += _stprintf(pBuffer + nPos, _T("%s"), _T("Debug : ")); }
else if ((Type & TraceRecompiler)!= 0) { nPos += _stprintf(pBuffer+nPos,_T("%s"),_T("Recomp : ")); } else if ((Type & TraceRecompiler) != 0) { nPos += _stprintf(pBuffer + nPos, _T("%s"), _T("Recomp : ")); }
else if ((Type & TraceRSP )!= 0) { nPos += _stprintf(pBuffer+nPos,_T("%s"),_T("RSP : ")); } else if ((Type & TraceRSP) != 0) { nPos += _stprintf(pBuffer + nPos, _T("%s"), _T("RSP : ")); }
else if ((Type & TraceTLB )!= 0) { nPos += _stprintf(pBuffer+nPos,_T("%s"),_T("TLB : ")); } else if ((Type & TraceTLB) != 0) { nPos += _stprintf(pBuffer + nPos, _T("%s"), _T("TLB : ")); }
else if ((Type & TraceValidate )!= 0) { nPos += _stprintf(pBuffer+nPos,_T("%s"),_T("Valid : ")); } else if ((Type & TraceValidate) != 0) { nPos += _stprintf(pBuffer + nPos, _T("%s"), _T("Valid : ")); }
else if ((Type & TraceAudio )!= 0) { nPos += _stprintf(pBuffer+nPos,_T("%s"),_T("Audio : ")); } else if ((Type & TraceAudio) != 0) { nPos += _stprintf(pBuffer + nPos, _T("%s"), _T("Audio : ")); }
else { nPos += _stprintf(pBuffer+nPos,_T("%s"),_T("Unknown: ")); } else { nPos += _stprintf(pBuffer + nPos, _T("%s"), _T("Unknown: ")); }
for (int i = 0; i < (int)m_Modules.size(); i++ ) for (int i = 0; i < (int)m_Modules.size(); i++)
{ {
if ((m_Modules[i]->GetTraceLevel() & Type) != 0) if ((m_Modules[i]->GetTraceLevel() & Type) != 0)
{ {
m_Modules[i]->Write(pBuffer, false); m_Modules[i]->Write(pBuffer, false);
} }
} }
} }
for (int i = 0; i < (int)m_Modules.size(); i++ ) for (int i = 0; i < (int)m_Modules.size(); i++)
{ {
if ((m_Modules[i]->GetTraceLevel() & Type) != 0) if ((m_Modules[i]->GetTraceLevel() & Type) != 0)
{ {
m_Modules[i]->Write(Message, true); m_Modules[i]->Write(Message, true);
} }
} }
} }
CTraceLog & GetTraceObjet(void)
CTraceLog & GetTraceObjet ( void )
{ {
static CTraceLog TraceLog; static CTraceLog TraceLog;
return TraceLog; return TraceLog;
} }
void WriteTrace (TraceType Type, LPCTSTR Message ) void WriteTrace(TraceType Type, LPCTSTR Message)
{ {
if (TraceClosed) if (TraceClosed)
{ {
return; return;
} }
GetTraceObjet().WriteTrace(Type,Message); GetTraceObjet().WriteTrace(Type, Message);
} }
void WriteTraceF ( TraceType Type, LPCTSTR strFormat, ... ) void WriteTraceF(TraceType Type, LPCTSTR strFormat, ...)
{ {
if (TraceClosed) if (TraceClosed)
{ {
@ -158,11 +157,11 @@ void WriteTraceF ( TraceType Type, LPCTSTR strFormat, ... )
} }
va_list args; va_list args;
va_start(args, strFormat); va_start(args, strFormat);
GetTraceObjet().WriteTraceF(Type,strFormat,args); GetTraceObjet().WriteTraceF(Type, strFormat, args);
va_end(args); va_end(args);
} }
CTraceModule * AddTraceModule ( CTraceModule * TraceModule ) CTraceModule * AddTraceModule(CTraceModule * TraceModule)
{ {
if (TraceClosed) if (TraceClosed)
{ {
@ -172,52 +171,55 @@ CTraceModule * AddTraceModule ( CTraceModule * TraceModule )
return TraceModule; return TraceModule;
} }
CTraceModule * RemoveTraceModule ( CTraceModule * TraceModule ) CTraceModule * RemoveTraceModule(CTraceModule * TraceModule)
{ {
return GetTraceObjet().RemoveTraceModule(TraceModule); return GetTraceObjet().RemoveTraceModule(TraceModule);
} }
void CloseTrace ( void ) void CloseTrace(void)
{ {
TraceClosed = true; TraceClosed = true;
GetTraceObjet().CloseTrace(); GetTraceObjet().CloseTrace();
} }
CTraceFileLog::CTraceFileLog(LPCTSTR FileName, bool FlushFile ) : CTraceFileLog::CTraceFileLog(LPCTSTR FileName, bool FlushFile) :
m_FlushFile(FlushFile) m_FlushFile(FlushFile)
{ {
m_hLogFile.SetFlush(false); m_hLogFile.SetFlush(false);
m_hLogFile.SetTruncateFile(true); m_hLogFile.SetTruncateFile(true);
m_hLogFile.SetMaxFileSize(5 * MB); m_hLogFile.SetMaxFileSize(5 * MB);
m_hLogFile.Open(FileName,Log_Append); m_hLogFile.Open(FileName, Log_Append);
} }
CTraceFileLog::CTraceFileLog (LPCTSTR FileName, bool FlushFile, LOG_OPEN_MODE eMode, DWORD dwMaxFileSize) : CTraceFileLog::CTraceFileLog(LPCTSTR FileName, bool FlushFile, LOG_OPEN_MODE eMode, uint32_t dwMaxFileSize) :
m_FlushFile(FlushFile) m_FlushFile(FlushFile)
{ {
enum { MB = 1024 * 1024 }; enum { MB = 1024 * 1024 };
m_hLogFile.SetFlush(false); m_hLogFile.SetFlush(false);
m_hLogFile.SetTruncateFile(true); m_hLogFile.SetTruncateFile(true);
if(dwMaxFileSize < 2048 && dwMaxFileSize > 2) if (dwMaxFileSize < 2048 && dwMaxFileSize > 2)
{
m_hLogFile.SetMaxFileSize(dwMaxFileSize * MB); m_hLogFile.SetMaxFileSize(dwMaxFileSize * MB);
}
else else
{
m_hLogFile.SetMaxFileSize(5 * MB); m_hLogFile.SetMaxFileSize(5 * MB);
}
m_hLogFile.Open(FileName,eMode); m_hLogFile.Open(FileName, eMode);
} }
CTraceFileLog::~CTraceFileLog() CTraceFileLog::~CTraceFileLog()
{ {
TraceClosed = true; TraceClosed = true;
} }
void CTraceFileLog::Write(LPCTSTR Message, bool EndOfLine ) void CTraceFileLog::Write(LPCTSTR Message, bool EndOfLine)
{ {
if (!m_hLogFile.IsOpen()) { return; } if (!m_hLogFile.IsOpen()) { return; }
CGuard Section(m_CriticalSection); CGuard Section(m_CriticalSection);
m_hLogFile.Log(Message); m_hLogFile.Log(Message);
if (EndOfLine) if (EndOfLine)
@ -230,7 +232,16 @@ void CTraceFileLog::Write(LPCTSTR Message, bool EndOfLine )
} }
} }
void CTraceFileLog::SetFlushFile( bool bFlushFile ) void CTraceFileLog::SetFlushFile(bool bFlushFile)
{ {
m_FlushFile = bFlushFile; m_FlushFile = bFlushFile;
} }
void CDebugTraceLog::Write(const char * Message, bool EndOfLine)
{
OutputDebugString(Message);
if (EndOfLine)
{
OutputDebugString("\n");
}
}

View File

@ -1,5 +1,8 @@
#pragma once #pragma once
#include "CriticalSection.h"
#include "Log Class.h"
class CTraceModule class CTraceModule
{ {
TraceLevel m_Type; TraceLevel m_Type;
@ -10,7 +13,7 @@ public:
inline void SetTraceLevel ( TraceLevel Type ) { m_Type = Type; } inline void SetTraceLevel ( TraceLevel Type ) { m_Type = Type; }
inline TraceLevel GetTraceLevel ( void ) const { return m_Type; } inline TraceLevel GetTraceLevel ( void ) const { return m_Type; }
virtual void Write ( LPCTSTR Message, bool EndOfLine ) = 0; virtual void Write ( const char * Message, bool EndOfLine ) = 0;
}; };
class CTraceFileLog : public CTraceModule class CTraceFileLog : public CTraceModule
@ -22,25 +25,18 @@ class CTraceFileLog : public CTraceModule
bool m_FlushFile; bool m_FlushFile;
public: public:
CTraceFileLog (LPCTSTR FileName, bool FlushFile = true); CTraceFileLog (const char * FileName, bool FlushFile = true);
CTraceFileLog (LPCTSTR FileName, bool FlushFile, LOG_OPEN_MODE eMode, DWORD dwMaxFileSize = 5); CTraceFileLog (const char * FileName, bool FlushFile, LOG_OPEN_MODE eMode, uint32_t dwMaxFileSize = 5);
virtual ~CTraceFileLog (); virtual ~CTraceFileLog ();
void Write ( LPCTSTR Message, bool EndOfLine ); void Write ( const char * Message, bool EndOfLine );
void SetFlushFile ( bool bFlushFile ); void SetFlushFile ( bool bFlushFile );
}; };
class CDebugTraceLog : public CTraceModule class CDebugTraceLog : public CTraceModule
{ {
public: public:
void Write ( LPCTSTR Message, bool EndOfLine ) void Write ( const char * Message, bool EndOfLine );
{
OutputDebugString(Message);
if (EndOfLine)
{
OutputDebugString("\n");
}
}
}; };
CTraceModule * AddTraceModule ( CTraceModule * TraceModule ); // Must be created with new CTraceModule * AddTraceModule ( CTraceModule * TraceModule ); // Must be created with new

View File

@ -1,22 +1,22 @@
// MD5.CC - source code for the C++/object oriented translation and // MD5.CC - source code for the C++/object oriented translation and
// modification of MD5. // modification of MD5.
// Translation and modification (c) 1995 by Mordechai T. Abzug // Translation and modification (c) 1995 by Mordechai T. Abzug
// This translation/ modification is provided "as is," without express or // This translation/ modification is provided "as is," without express or
// implied warranty of any kind. // implied warranty of any kind.
// The translator/ modifier does not claim (1) that MD5 will do what you think // The translator/ modifier does not claim (1) that MD5 will do what you think
// it does; (2) that this translation/ modification is accurate; or (3) that // it does; (2) that this translation/ modification is accurate; or (3) that
// this software is "merchantible." (Language for this disclaimer partially // this software is "merchantible." (Language for this disclaimer partially
// copied from the disclaimer below). // copied from the disclaimer below).
/* based on: /* based on:
MD5.H - header file for MD5C.C MD5.H - header file for MD5C.C
MDDRIVER.C - test driver for MD2, MD4 and MD5 MDDRIVER.C - test driver for MD2, MD4 and MD5
Copyright (C) 1991-2, RSA Data Security, Inc. Created 1991. All Copyright (C) 1991-2, RSA Data Security, Inc. Created 1991. All
rights reserved. rights reserved.
License to copy and use this software is granted provided that it License to copy and use this software is granted provided that it
@ -39,11 +39,7 @@ documentation and/or software.
*/ */
#if !defined(AFX_MD5_H__6DD6923B_E241_40CE_81A3_4C2C88C140E4__INCLUDED_) #pragma once
#define AFX_MD5_H__6DD6923B_E241_40CE_81A3_4C2C88C140E4__INCLUDED_
/* sprintf() */
#include <stdio.h>
#include <string> #include <string>
#include <functional> #include <functional>
@ -51,106 +47,102 @@ documentation and/or software.
struct MD5Digest struct MD5Digest
{ {
MD5Digest() { Reset(); } MD5Digest() { Reset(); }
unsigned char digest[16]; unsigned char digest[16];
void Reset() { ::memset(digest, 0, sizeof(digest)); } void Reset() { ::memset(digest, 0, sizeof(digest)); }
BOOL IsClear() bool IsClear()
{ {
int isClear = 0; int isClear = 0;
for (int i=0; i < 16; i++) for (int i=0; i < 16; i++)
isClear += digest[i]; {
return (isClear == 0); isClear += digest[i];
} }
std::string String ( void ) return (isClear == 0);
{ }
char s[33]; std::string String ( void )
{
char s[33];
::memset(s, 0, sizeof(s)); ::memset(s, 0, sizeof(s));
for (int i = 0; i < 16; i++) for (int i = 0; i < 16; i++)
{ {
sprintf(s+i*2, "%02X", digest[i]); sprintf(s+i*2, "%02X", digest[i]);
} }
s[32]='\0'; s[32]='\0';
return s; return s;
} }
}; };
struct MD5Digest_less : std::binary_function<MD5Digest, MD5Digest, bool> struct MD5Digest_less : std::binary_function<MD5Digest, MD5Digest, bool>
{ {
bool operator()(const MD5Digest& x, const MD5Digest& y) const bool operator()(const MD5Digest& x, const MD5Digest& y) const
{ {
return (memcmp(x.digest, y.digest, sizeof(x.digest)) < 0); return (memcmp(x.digest, y.digest, sizeof(x.digest)) < 0);
} }
}; };
class MD5 class MD5
{ {
public: public:
// methods for controlled operation: // methods for controlled operation:
MD5 (); // simple initializer MD5 (); // simple initializer
~MD5 (); ~MD5 ();
void update (const unsigned char *input, unsigned int input_length); void update (const unsigned char *input, unsigned int input_length);
void update (FILE *file); void update (FILE *file);
void finalize (); void finalize ();
// constructors for special circumstances. All these constructors finalize // constructors for special circumstances. All these constructors finalize
// the MD5 context. // the MD5 context.
MD5 (CPath File); // digest File, finalize MD5 (CPath File); // digest File, finalize
MD5 (const unsigned char *string); // digest string, finalize MD5 (const unsigned char *string); // digest string, finalize
MD5 (FILE *file); // digest file, close, finalize MD5 (FILE *file); // digest file, close, finalize
MD5 (const unsigned char *input, unsigned int input_length); MD5 (const unsigned char *input, unsigned int input_length);
MD5 (const stdstr & string); MD5 (const stdstr & string);
// methods to acquire finalized result // methods to acquire finalized result
void get_digest(MD5Digest& extdigest); //Digest into a digest structure void get_digest(MD5Digest& extdigest); //Digest into a digest structure
const unsigned char *raw_digest (); // digest as a 16-byte binary array const unsigned char *raw_digest (); // digest as a 16-byte binary array
const char * hex_digest (); // digest as a 33-byte ascii-hex string const char * hex_digest (); // digest as a 33-byte ascii-hex string
private: private:
// first, some types: // first, some types:
typedef unsigned int uint4; // assumes integer is 4 words long typedef unsigned int uint4; // assumes integer is 4 words long
typedef unsigned short int uint2; // assumes short integer is 2 words long typedef unsigned short int uint2; // assumes short integer is 2 words long
typedef unsigned char uint1; // assumes char is 1 word long typedef unsigned char uint1; // assumes char is 1 word long
// next, the private data: // next, the private data:
uint4 state[4]; uint4 state[4];
uint4 count[2]; // number of *bits*, mod 2^64 uint4 count[2]; // number of *bits*, mod 2^64
uint1 buffer[64]; // input buffer uint1 buffer[64]; // input buffer
uint1 digest[16]; uint1 digest[16];
uint1 finalized; uint1 finalized;
stdstr m_hex_digest; stdstr m_hex_digest;
// last, the private methods, mostly static: // last, the private methods, mostly static:
void init (); // called by all constructors void init (); // called by all constructors
void transform (uint1 *buffer); // does the real update work. Note void transform (uint1 *buffer); // does the real update work. Note
// that length is implied to be 64. // that length is implied to be 64.
static void encode (uint1 *dest, uint4 *src, uint4 length); static void encode (uint1 *dest, uint4 *src, uint4 length);
static void decode (uint4 *dest, uint1 *src, uint4 length); static void decode (uint4 *dest, uint1 *src, uint4 length);
static void memcpy (uint1 *dest, uint1 *src, uint4 length); static void memcpy (uint1 *dest, uint1 *src, uint4 length);
static void memset (uint1 *start, uint1 val, uint4 length); static void memset (uint1 *start, uint1 val, uint4 length);
static inline uint4 rotate_left (uint4 x, uint4 n);
static inline uint4 F (uint4 x, uint4 y, uint4 z);
static inline uint4 G (uint4 x, uint4 y, uint4 z);
static inline uint4 H (uint4 x, uint4 y, uint4 z);
static inline uint4 I (uint4 x, uint4 y, uint4 z);
static inline void FF (uint4& a, uint4 b, uint4 c, uint4 d, uint4 x,
uint4 s, uint4 ac);
static inline void GG (uint4& a, uint4 b, uint4 c, uint4 d, uint4 x,
uint4 s, uint4 ac);
static inline void HH (uint4& a, uint4 b, uint4 c, uint4 d, uint4 x,
uint4 s, uint4 ac);
static inline void II (uint4& a, uint4 b, uint4 c, uint4 d, uint4 x,
uint4 s, uint4 ac);
static inline uint4 rotate_left (uint4 x, uint4 n);
static inline uint4 F (uint4 x, uint4 y, uint4 z);
static inline uint4 G (uint4 x, uint4 y, uint4 z);
static inline uint4 H (uint4 x, uint4 y, uint4 z);
static inline uint4 I (uint4 x, uint4 y, uint4 z);
static inline void FF (uint4& a, uint4 b, uint4 c, uint4 d, uint4 x,
uint4 s, uint4 ac);
static inline void GG (uint4& a, uint4 b, uint4 c, uint4 d, uint4 x,
uint4 s, uint4 ac);
static inline void HH (uint4& a, uint4 b, uint4 c, uint4 d, uint4 x,
uint4 s, uint4 ac);
static inline void II (uint4& a, uint4 b, uint4 c, uint4 d, uint4 x,
uint4 s, uint4 ac);
}; };
#endif // !defined(AFX_MD5_H__6DD6923B_E241_40CE_81A3_4C2C88C140E4__INCLUDED_)