home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
s-gikan2.maizuru-ct.ac.jp
/
s-gikan2.maizuru-ct.ac.jp.zip
/
s-gikan2.maizuru-ct.ac.jp
/
pub
/
source
/
old
/
magdb140su.lzh
/
MagaDbgLib.cpp
< prev
next >
Wrap
C/C++ Source or Header
|
1999-07-16
|
10KB
|
407 lines
/*
MagaDebug32 âfâoâbâOâcü[âï Ver1.40
ébü{ü{û{æ╠
*/
#include <stdio.h>
#include <string.h>
#include <stdarg.h>
#include "MagaDbgMsg.h"
// Åπê╩4bits Mask
#define MAGADBGMAPNUMBERMASK 0x0fffffff
#define MAGADBGMAPLIBMTFLG 0x10000000
#define MAGADBGMAPLIBMDFLG 0x20000000
// --------------------------------
// èεû{âNâëâX
// --------------------------------
// ôαòöÅêù¥è╓Éö
// --------------------------------
void CMagaDbg::CMagaDbgInit(char* s, BOOL f, dbgColor c, DWORD n) {
m_fSep = f ? TRUE : FALSE;
m_iColor = (int)c;
m_nInterval = n;
m_nMapNumber = g_MagaDbgMapNumber;
InterlockedIncrement(&g_MagaDbgMapNumber);
m_nMapNumber &= MAGADBGMAPNUMBERMASK;
#if defined(_MT)
#if !defined(_DLL)
m_nMapNumber |= MAGADBGMAPLIBMTFLG;
#else
m_nMapNumber |= MAGADBGMAPLIBMDFLG;
#endif
#endif
char szTmp[32];
wsprintf(szTmp, "%s%08X", MAGADBG_COPYDATA_MAP, m_nMapNumber);
if ( (m_hDbgMap=CreateFileMapping((HANDLE)0xFFFFFFFF, NULL,
PAGE_READWRITE, 0, STRLEN, szTmp)) != NULL )
m_lpDbgMap = (LPSTR)MapViewOfFile(m_hDbgMap, FILE_MAP_WRITE, 0, 0, 0);
else
m_lpDbgMap = NULL;
SetHeader(s, this);
}
void CMagaDbg::SetHeader(char* s, CMagaDbg* p) {
m_lpszHead = NULL;
m_iHeadLen = 0;
if ( s!=NULL && lstrlen(s)!=0 ) {
char* tok;
char* szTmp = new char[lstrlen(s)+1];
lstrcpy(szTmp, s);
if ( szTmp[0] == '\n' ) {
if ( lstrlen(s) > 1) p->print(szTmp+1);
}
else {
tok = strtok(szTmp, "\n");
m_lpszHead = new char[lstrlen(tok)+2];
m_iHeadLen = lstrlen(lstrcat(lstrcpy(m_lpszHead, tok), ":"));
if ( (tok=strtok(NULL, "\n")) != NULL )
p->print(tok);
}
delete szTmp;
}
}
int CMagaDbg::AddHeader(char** szResult, char* szbuf) {
int len = 0;
if ( szbuf!=NULL ) len = lstrlen(szbuf);
if ( m_lpszHead != NULL ) {
len += m_iHeadLen;
*szResult = new char[len+1];
if ( len!=0 )
lstrcat(lstrcpy(*szResult, m_lpszHead), szbuf);
else
lstrcpy(*szResult, m_lpszHead);
}
else if ( len!=0 ) {
*szResult = new char[len+1];
lstrcpy(*szResult, szbuf);
}
else {
*szResult = NULL;
}
return len;
}
// âRâôâXâgâëâNâ^
// --------------------------------
CMagaDbg::CMagaDbg(void) {
CMagaDbgInit(NULL, FALSE, DBG_BLACK);
}
CMagaDbg::CMagaDbg(char* s, BOOL f, dbgColor c, DWORD n) {
CMagaDbgInit(s, f, c, n);
}
CMagaDbg::CMagaDbg(char* s, dbgColor c) {
CMagaDbgInit(s, FALSE, c);
}
CMagaDbg::CMagaDbg(char* s, BOOL f) {
CMagaDbgInit(s, f, DBG_BLACK);
}
CMagaDbg::CMagaDbg(BOOL f) {
CMagaDbgInit(NULL, f, DBG_BLACK);
}
CMagaDbg::CMagaDbg(dbgColor c) {
CMagaDbgInit(NULL, FALSE, c);
}
CMagaDbg::CMagaDbg(BOOL f, dbgColor c) {
CMagaDbgInit(NULL, f, c);
}
// âfâBâXâgâëâNâ^
// --------------------------------
CMagaDbg::~CMagaDbg() {
UnmapViewOfFile(m_lpDbgMap);
CloseHandle(m_hDbgMap);
InterlockedDecrement(&g_MagaDbgMapNumber);
if ( m_lpszHead!=NULL ) delete m_lpszHead;
if ( !m_fSep ) return;
HWND hwnd;
if ( (hwnd=MagaDbgFind()) == NULL ) return;
SendMessage(hwnd, WM_OBJ_MARK, (WPARAM)m_iColor, 0);
}
// ò╢ÄÜù±é╠ò\Ī
// --------------------------------
void CMagaDbg::print(char* s, BOOL f) {
HWND hwnd;
if ( (hwnd=MagaDbgFind())==NULL ) return;
char* tmp;
int len = AddHeader(&tmp, s);
if ( m_lpDbgMap == NULL ) {
m_cds.dwData = MAKELONG((f ? DBG_CR : 0), m_iColor);
m_cds.lpData = tmp;
m_cds.cbData = len;
SendMessage(hwnd, WM_COPYDATA, 0, (LPARAM)&m_cds);
}
else {
lstrcpyn(m_lpDbgMap, tmp, STRLEN);
SendMessage(hwnd, WM_DBGCOPYDATA, (WPARAM)m_nMapNumber, MAKELPARAM((f ? DBG_CR : 0), m_iColor));
}
if ( len!=0 ) delete tmp;
if ( m_nInterval!=0 ) Sleep(m_nInterval);
}
// öCê╙ò╢ÄÜù±é╠ò\Ī
// --------------------------------
void CMagaDbg::printf(char* s, ...) {
char msg[STRLEN];
char* pArg = (char *)&s + sizeof(s);
#if defined(_MSC_VER)
if ( _vsnprintf(msg, STRLEN, s, pArg) == -1 )
#else
if ( vsprintf(msg, s, pArg) == EOF )
#endif
msg[STRLEN-1] = '\0';
print(msg);
}
// éµé¡Ägéñì\æóæ╠âüâôâoé╠ò\Ī
// --------------------------------
void CMagaDbg::printStruct(LPRECT lpRect, char* lpStr) {
this->printf("%s left=%d top=%d right=%d bottom=%d", (lpStr==NULL ? "" : lpStr),
lpRect->left, lpRect->top, lpRect->right, lpRect->bottom);
}
void CMagaDbg::printStruct(LPSIZE lpSize, char* lpStr) {
this->printf("%s cx=%d cy=%d", (lpStr==NULL ? "" : lpStr),
lpSize->cx, lpSize->cy);
}
void CMagaDbg::printStruct(LPPOINT lpPoint, char* lpStr) {
this->printf("%s x=%d y=%d", (lpStr==NULL ? "" : lpStr),
lpPoint->x, lpPoint->y);
}
// öCê╙ò╢ÄÜù±ôⁿù═
// --------------------------------
BOOL CMagaDbg::scanf(char* szIndata, char* szTitle) {
if ( szIndata == NULL ) return -1;
HWND hwnd;
HANDLE hFileMap;
LPSTR lpView;
if ( (hwnd=MagaDbgFind())==NULL ) return -1;
hFileMap = CreateFileMapping((HANDLE)0xFFFFFFFF, NULL,
PAGE_READWRITE, 0, STRLEN, MAGADBG_SCANF_MAP);
if ( hFileMap!=NULL && GetLastError()==ERROR_ALREADY_EXISTS ) {
CloseHandle(hFileMap);
return -1;
}
else if ( hFileMap==NULL ) {
return -1;
}
if ( (lpView=(LPSTR)MapViewOfFile(hFileMap, FILE_MAP_WRITE, 0, 0, 0)) == NULL ) {
CloseHandle(hFileMap);
return -1;
}
if ( szTitle != NULL )
lstrcpyn(lpView, szTitle, STRLEN);
else
lstrcpy(lpView, "");
UnmapViewOfFile(lpView);
if ( SendMessage(hwnd, WM_SCANF, 0, 0) == IDCANCEL ) {
CloseHandle(hFileMap);
return -1;
}
if ( (lpView=(LPSTR)MapViewOfFile(hFileMap, FILE_MAP_READ, 0, 0, 0)) == NULL ) {
CloseHandle(hFileMap);
return -1;
}
lstrcpyn(szIndata, lpView, min(STRLEN, lstrlen(lpView)+1));
UnmapViewOfFile(lpView);
CloseHandle(hFileMap);
return 0;
}
// â_âôâvò\Ī
// --------------------------------
void CMagaDbg::dump(LPVOID lp, UINT byte, DWORD flg) {
char szbuf[STRLEN], szasc[32], sztmp[16];
UINT i, ii, factor;
factor = flg & DBG_OCTDUMP ? 8 : 16;
if ( !(flg & DBG_NOHEADERDUMP) ) {
if ( !(flg & DBG_NOADDRESSDUMP) )
lstrcpy(szbuf, " ");
else
szbuf[0] = '\0';
for (i=0; i<factor; i++) {
wsprintf(sztmp, " +%X", i);
lstrcat(szbuf, sztmp);
}
print(szbuf);
if ( !(flg & DBG_NOADDRESSDUMP) )
lstrcpy(szbuf, " ");
else
szbuf[0] = '\0';
for (i=0; i<factor; i++) lstrcat(szbuf, "---");
print(szbuf);
}
ii = 0;
while ( ii < byte ) {
if ( !(flg & DBG_NOADDRESSDUMP) ) {
wsprintf(sztmp, "%04X ", ii);
lstrcpy(szbuf, sztmp);
}
else {
szbuf[0] = '\0';
}
i = 0;
while ( ii+i<byte && i<factor ) {
wsprintf(sztmp, " %02X", *((unsigned char *)lp+ii+i));
lstrcat(szbuf, sztmp);
if ( !(flg & DBG_NOCHRDUMP) ) {
if ( *((unsigned char *)lp+ii+i)>=0x20 && *((unsigned char *)lp+ii+i)<=0x7e )
szasc[i] = *((unsigned char *)lp+ii+i);
else
szasc[i] = '.';
}
i++;
}
if ( !(flg & DBG_NOCHRDUMP) ) {
szasc[i] = '\0';
for ( ; i<factor; i++) lstrcat(szbuf, " ");
lstrcat(lstrcat(szbuf, " "), szasc);
}
print(szbuf);
ii += factor;
}
}
// --------------------------------
// öhÉ╢âNâëâX
// --------------------------------
// ôαòöÅêù¥è╓Éö
// --------------------------------
void CMagaDbgFile::ErrorMsg(char* s, DWORD code) {
char sztmp[MAX_PATH];
wsprintf(sztmp, "CMagaDbgFile %s = %lX", s, code);
Dbg_printf(sztmp);
MessageBox(NULL, sztmp, "CMagaDbgFile", MB_OK|MB_ICONERROR);
}
void CMagaDbgFile::CMagaDbgFileInit(LPMAGADBGOPT lpopt) {
m_hFile = INVALID_HANDLE_VALUE;
if ( lpopt->lpszFilename==NULL || lstrlen(lpopt->lpszFilename)==0 ) {
ErrorMsg("║▌╜─╫╕└┤╫░(╠º▓┘û╝û│î°)", 0);
return;
}
m_fDisplay = lpopt->fDisplay ? TRUE : FALSE;
DWORD dwOpenMode = lpopt->fOverwrite ? CREATE_ALWAYS : OPEN_ALWAYS;
if ( (m_hFile = CreateFile(lpopt->lpszFilename, GENERIC_WRITE, FILE_SHARE_WRITE, NULL,
dwOpenMode, FILE_ATTRIBUTE_ARCHIVE, NULL)) == INVALID_HANDLE_VALUE ) {
ErrorMsg("║▌╜─╫╕└┤╫░", GetLastError());
}
else {
if ( !lpopt->fOverwrite ) SetFilePointer(m_hFile, NULL, NULL, FILE_END);
}
SetHeader(lpopt->lpHead, this); // CMagaDbg
}
// âRâôâXâgâëâNâ^
// --------------------------------
// ɵé╔╠º▓┘é≡OPENé╖éΘòKùvé¬éáéΘé╠é┼üC
// èεû{╕╫╜é≡î─é╘æµéPê°Éöé╔ NULL
// --------------------------------
CMagaDbgFile::CMagaDbgFile
(char* lpszFilename, BOOL fOver, BOOL fDis, char* s, BOOL f, dbgColor c, DWORD n) :
CMagaDbg(NULL, f, c, n)
{
MAGADBGOPT opt;
opt.lpszFilename = lpszFilename;
opt.fOverwrite = fOver;
opt.fDisplay = fDis;
opt.lpHead = s;
opt.fSeparator = f;
opt.eColor = c;
opt.nInterval = n;
CMagaDbgFileInit(&opt);
}
CMagaDbgFile::CMagaDbgFile(LPMAGADBGOPT lpopt) :
CMagaDbg(NULL, lpopt->fSeparator, lpopt->eColor, lpopt->nInterval)
{
CMagaDbgFileInit(lpopt);
}
// âfâBâXâgâëâNâ^
// --------------------------------
CMagaDbgFile::~CMagaDbgFile() {
if ( m_hFile==INVALID_HANDLE_VALUE ) return;
if ( !GetSeparator() ) { // CMagaDbg
CloseHandle(m_hFile);
return;
}
HWND hwnd;
if ( (hwnd=MagaDbgFind())==NULL ) {
CloseHandle(m_hFile);
return;
}
// LogFileé╓╛╩▀┌░└Åoù═
HANDLE hFileMap = CreateFileMapping((HANDLE)0xFFFFFFFF, NULL,
PAGE_READWRITE, 0, STRLEN, MAGADBG_SEP_MAP);
if ( hFileMap != NULL ) {
int num;
if ( (num=(int)SendMessage(hwnd, WM_GETSEPARATOR, 0, 0)) > 0 ) {
LPSTR lpView;
if ( (lpView=(LPSTR)MapViewOfFile(hFileMap, FILE_MAP_READ, 0, 0, 0)) != NULL ) {
DWORD dwWritesize;
for ( int i=0; i<num; i++ ) {
WriteFile(m_hFile, lpView, lstrlen(lpView), &dwWritesize, NULL);
WriteFile(m_hFile, "\x0d\x0a", 2, &dwWritesize, NULL);
}
UnmapViewOfFile(lpView);
}
}
CloseHandle(hFileMap);
}
CloseHandle(m_hFile);
}
// ò╢ÄÜù±é╠ò\Ī
// --------------------------------
void CMagaDbgFile::print(char* s, BOOL fCR) {
if ( m_hFile != INVALID_HANDLE_VALUE ) {
BOOL fWrite;
DWORD dwWritesize;
char* tmp;
DWORD dwLen = (DWORD)AddHeader(&tmp, s); // CMagaDbg
fWrite = WriteFile(m_hFile, tmp, dwLen, &dwWritesize, NULL);
if ( !fWrite || dwLen!=dwWritesize )
ErrorMsg("file print error", GetLastError());
if ( fCR )
WriteFile(m_hFile, "\x0d\x0a", 2, &dwWritesize, NULL);
if ( dwLen!=0 ) delete tmp;
}
if ( m_fDisplay )
CMagaDbg::print(s, fCR);
else if ( GetInterval() != 0 )
Sleep(GetInterval());
}