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 >
C/C++ Source or Header  |  1999-07-16  |  10KB  |  407 lines

  1. /*
  2.     MagaDebug32 âfâoâbâOâcü[âï Ver1.40
  3.     ébü{ü{û{æ╠
  4. */
  5. #include <stdio.h>
  6. #include <string.h>
  7. #include <stdarg.h>
  8. #include "MagaDbgMsg.h"
  9.  
  10. // Åπê╩4bits Mask
  11. #define    MAGADBGMAPNUMBERMASK    0x0fffffff
  12. #define    MAGADBGMAPLIBMTFLG        0x10000000
  13. #define    MAGADBGMAPLIBMDFLG        0x20000000
  14.  
  15. // --------------------------------
  16. // èεû{âNâëâX
  17. // --------------------------------
  18.  
  19. // ôαòöÅêù¥è╓Éö
  20. // --------------------------------
  21. void CMagaDbg::CMagaDbgInit(char* s, BOOL f, dbgColor c, DWORD n) {
  22.     m_fSep      = f ? TRUE : FALSE;
  23.     m_iColor    = (int)c;
  24.     m_nInterval = n;
  25.  
  26.     m_nMapNumber = g_MagaDbgMapNumber;
  27.     InterlockedIncrement(&g_MagaDbgMapNumber);
  28.     m_nMapNumber &= MAGADBGMAPNUMBERMASK;
  29. #if defined(_MT)
  30. #if !defined(_DLL)
  31.     m_nMapNumber |= MAGADBGMAPLIBMTFLG;
  32. #else
  33.     m_nMapNumber |= MAGADBGMAPLIBMDFLG;
  34. #endif
  35. #endif
  36.  
  37.     char    szTmp[32];
  38.     wsprintf(szTmp, "%s%08X", MAGADBG_COPYDATA_MAP, m_nMapNumber);
  39.     if ( (m_hDbgMap=CreateFileMapping((HANDLE)0xFFFFFFFF, NULL,
  40.                         PAGE_READWRITE, 0, STRLEN, szTmp)) != NULL )
  41.         m_lpDbgMap = (LPSTR)MapViewOfFile(m_hDbgMap, FILE_MAP_WRITE, 0, 0, 0);
  42.     else
  43.         m_lpDbgMap = NULL;
  44.  
  45.     SetHeader(s, this);
  46. }
  47.  
  48. void CMagaDbg::SetHeader(char* s, CMagaDbg* p) {
  49.     m_lpszHead = NULL;
  50.     m_iHeadLen = 0;
  51.     if ( s!=NULL && lstrlen(s)!=0 ) {
  52.         char*    tok;
  53.         char*    szTmp = new char[lstrlen(s)+1];
  54.         lstrcpy(szTmp, s);
  55.         if ( szTmp[0] == '\n' ) {
  56.             if ( lstrlen(s) > 1) p->print(szTmp+1);
  57.         }
  58.         else {
  59.             tok = strtok(szTmp, "\n");
  60.             m_lpszHead = new char[lstrlen(tok)+2];
  61.             m_iHeadLen = lstrlen(lstrcat(lstrcpy(m_lpszHead, tok), ":"));
  62.             if ( (tok=strtok(NULL, "\n")) != NULL )
  63.                 p->print(tok);
  64.         }
  65.         delete szTmp;
  66.     }
  67. }
  68.  
  69. int CMagaDbg::AddHeader(char** szResult, char* szbuf) {
  70.     int        len = 0;
  71.  
  72.     if ( szbuf!=NULL ) len = lstrlen(szbuf);
  73.     if ( m_lpszHead != NULL ) {
  74.         len += m_iHeadLen;
  75.         *szResult = new char[len+1];
  76.         if ( len!=0 )
  77.             lstrcat(lstrcpy(*szResult, m_lpszHead), szbuf);
  78.         else
  79.             lstrcpy(*szResult, m_lpszHead);
  80.     }
  81.     else if ( len!=0 ) {
  82.         *szResult = new char[len+1];
  83.         lstrcpy(*szResult, szbuf);
  84.     }
  85.     else {
  86.         *szResult = NULL;
  87.     }
  88.     return len;
  89. }
  90.  
  91. // âRâôâXâgâëâNâ^
  92. // --------------------------------
  93. CMagaDbg::CMagaDbg(void) {
  94.     CMagaDbgInit(NULL, FALSE, DBG_BLACK);
  95. }
  96.  
  97. CMagaDbg::CMagaDbg(char* s, BOOL f, dbgColor c, DWORD n) {
  98.     CMagaDbgInit(s, f, c, n);
  99. }
  100.  
  101. CMagaDbg::CMagaDbg(char* s, dbgColor c) {
  102.     CMagaDbgInit(s, FALSE, c);
  103. }
  104.  
  105. CMagaDbg::CMagaDbg(char* s, BOOL f) {
  106.     CMagaDbgInit(s, f, DBG_BLACK);
  107. }
  108.  
  109. CMagaDbg::CMagaDbg(BOOL f) {
  110.     CMagaDbgInit(NULL, f, DBG_BLACK);
  111. }
  112.  
  113. CMagaDbg::CMagaDbg(dbgColor c) {
  114.     CMagaDbgInit(NULL, FALSE, c);
  115. }
  116.  
  117. CMagaDbg::CMagaDbg(BOOL f, dbgColor c) {
  118.     CMagaDbgInit(NULL, f, c);
  119. }
  120.  
  121. // âfâBâXâgâëâNâ^
  122. // --------------------------------
  123. CMagaDbg::~CMagaDbg() {
  124.     UnmapViewOfFile(m_lpDbgMap);
  125.     CloseHandle(m_hDbgMap);
  126.     InterlockedDecrement(&g_MagaDbgMapNumber);
  127.     
  128.     if ( m_lpszHead!=NULL ) delete m_lpszHead;
  129.     if ( !m_fSep ) return;
  130.  
  131.     HWND    hwnd;
  132.     if ( (hwnd=MagaDbgFind()) == NULL ) return;
  133.     SendMessage(hwnd, WM_OBJ_MARK, (WPARAM)m_iColor, 0);
  134. }
  135.  
  136. // ò╢ÄÜù±é╠ò\Ī
  137. // --------------------------------
  138. void CMagaDbg::print(char* s, BOOL f) {
  139.     HWND    hwnd;
  140.     if ( (hwnd=MagaDbgFind())==NULL ) return;
  141.  
  142.     char*    tmp;
  143.     int len = AddHeader(&tmp, s);
  144.     if ( m_lpDbgMap == NULL ) {
  145.         m_cds.dwData = MAKELONG((f ? DBG_CR : 0), m_iColor);
  146.         m_cds.lpData = tmp;
  147.         m_cds.cbData = len;
  148.         SendMessage(hwnd, WM_COPYDATA, 0, (LPARAM)&m_cds);
  149.     }
  150.     else {
  151.         lstrcpyn(m_lpDbgMap, tmp, STRLEN);
  152.         SendMessage(hwnd, WM_DBGCOPYDATA, (WPARAM)m_nMapNumber, MAKELPARAM((f ? DBG_CR : 0), m_iColor));
  153.     }
  154.     if ( len!=0 ) delete tmp;
  155.     if ( m_nInterval!=0 ) Sleep(m_nInterval);
  156. }
  157.  
  158. // öCê╙ò╢ÄÜù±é╠ò\Ī
  159. // --------------------------------
  160. void CMagaDbg::printf(char* s, ...) {
  161.     char    msg[STRLEN];
  162.     char*    pArg = (char *)&s + sizeof(s);
  163. #if defined(_MSC_VER)
  164.     if ( _vsnprintf(msg, STRLEN, s, pArg) == -1 )
  165. #else
  166.     if ( vsprintf(msg, s, pArg) == EOF )
  167. #endif
  168.         msg[STRLEN-1] = '\0';
  169.     print(msg);
  170. }
  171.  
  172. // éµé¡Ägéñì\æóæ╠âüâôâoé╠ò\Ī
  173. // --------------------------------
  174. void CMagaDbg::printStruct(LPRECT lpRect, char* lpStr) {
  175.     this->printf("%s left=%d top=%d right=%d bottom=%d", (lpStr==NULL ? "" : lpStr),
  176.                     lpRect->left, lpRect->top, lpRect->right, lpRect->bottom);
  177. }
  178.  
  179. void CMagaDbg::printStruct(LPSIZE lpSize, char* lpStr) {
  180.     this->printf("%s cx=%d cy=%d", (lpStr==NULL ? "" : lpStr),
  181.                     lpSize->cx, lpSize->cy);
  182. }
  183.  
  184. void CMagaDbg::printStruct(LPPOINT lpPoint, char* lpStr) {
  185.     this->printf("%s x=%d y=%d", (lpStr==NULL ? "" : lpStr),
  186.                     lpPoint->x, lpPoint->y);
  187. }
  188.  
  189. // öCê╙ò╢ÄÜù±ôⁿù═
  190. // --------------------------------
  191. BOOL CMagaDbg::scanf(char* szIndata, char* szTitle) {
  192.  
  193.     if ( szIndata == NULL ) return -1;
  194.  
  195.     HWND    hwnd;
  196.     HANDLE    hFileMap;
  197.     LPSTR    lpView;
  198.     if ( (hwnd=MagaDbgFind())==NULL ) return -1;
  199.  
  200.     hFileMap = CreateFileMapping((HANDLE)0xFFFFFFFF, NULL,
  201.         PAGE_READWRITE, 0, STRLEN, MAGADBG_SCANF_MAP);
  202.     if ( hFileMap!=NULL && GetLastError()==ERROR_ALREADY_EXISTS ) {
  203.         CloseHandle(hFileMap);
  204.         return -1;
  205.     }
  206.     else if ( hFileMap==NULL ) {
  207.         return -1;
  208.     }
  209.     if ( (lpView=(LPSTR)MapViewOfFile(hFileMap, FILE_MAP_WRITE, 0, 0, 0)) == NULL ) {
  210.         CloseHandle(hFileMap);
  211.         return -1;
  212.     }
  213.     if ( szTitle != NULL )
  214.         lstrcpyn(lpView, szTitle, STRLEN);
  215.     else
  216.         lstrcpy(lpView, "");
  217.     UnmapViewOfFile(lpView);
  218.     if ( SendMessage(hwnd, WM_SCANF, 0, 0) == IDCANCEL ) {
  219.         CloseHandle(hFileMap);
  220.         return -1;
  221.     }
  222.     if ( (lpView=(LPSTR)MapViewOfFile(hFileMap, FILE_MAP_READ, 0, 0, 0)) == NULL ) {
  223.         CloseHandle(hFileMap);
  224.         return -1;
  225.     }
  226.     lstrcpyn(szIndata, lpView, min(STRLEN, lstrlen(lpView)+1));
  227.     UnmapViewOfFile(lpView);
  228.     CloseHandle(hFileMap);
  229.     return 0;
  230. }
  231.  
  232. // â_âôâvò\Ī
  233. // --------------------------------
  234. void CMagaDbg::dump(LPVOID lp, UINT byte, DWORD flg) {
  235.  
  236.     char    szbuf[STRLEN], szasc[32], sztmp[16];
  237.     UINT    i, ii, factor;
  238.  
  239.     factor = flg & DBG_OCTDUMP ? 8 : 16;
  240.     if ( !(flg & DBG_NOHEADERDUMP) ) {
  241.         if ( !(flg & DBG_NOADDRESSDUMP) )
  242.             lstrcpy(szbuf, "     ");
  243.         else
  244.             szbuf[0] = '\0';
  245.         for (i=0; i<factor; i++) {
  246.             wsprintf(sztmp, " +%X", i);
  247.             lstrcat(szbuf, sztmp);
  248.         }
  249.         print(szbuf);
  250.         if ( !(flg & DBG_NOADDRESSDUMP) )
  251.             lstrcpy(szbuf, "     ");
  252.         else
  253.             szbuf[0] = '\0';
  254.         for (i=0; i<factor; i++) lstrcat(szbuf, "---");
  255.         print(szbuf);
  256.     }
  257.  
  258.     ii = 0;
  259.     while ( ii < byte ) {
  260.         if ( !(flg & DBG_NOADDRESSDUMP) ) {
  261.             wsprintf(sztmp, "%04X ", ii);
  262.             lstrcpy(szbuf, sztmp);
  263.         }
  264.         else {
  265.             szbuf[0] = '\0';
  266.         }
  267.         i = 0;
  268.         while ( ii+i<byte && i<factor ) {
  269.             wsprintf(sztmp, " %02X", *((unsigned char *)lp+ii+i));
  270.             lstrcat(szbuf, sztmp);
  271.             if ( !(flg & DBG_NOCHRDUMP) ) {
  272.                 if ( *((unsigned char *)lp+ii+i)>=0x20 && *((unsigned char *)lp+ii+i)<=0x7e )
  273.                     szasc[i] = *((unsigned char *)lp+ii+i);
  274.                 else
  275.                     szasc[i] = '.';
  276.             }
  277.             i++;
  278.         }
  279.         if ( !(flg & DBG_NOCHRDUMP) ) {
  280.             szasc[i] = '\0';
  281.             for ( ; i<factor; i++) lstrcat(szbuf, "   ");
  282.             lstrcat(lstrcat(szbuf, "  "), szasc);
  283.         }
  284.         print(szbuf);
  285.         ii += factor;
  286.     }
  287. }
  288.  
  289.  
  290. // --------------------------------
  291. // öhÉ╢âNâëâX
  292. // --------------------------------
  293.  
  294. // ôαòöÅêù¥è╓Éö
  295. // --------------------------------
  296. void CMagaDbgFile::ErrorMsg(char* s, DWORD code) {
  297.     char    sztmp[MAX_PATH];
  298.     wsprintf(sztmp, "CMagaDbgFile %s = %lX", s, code);
  299.     Dbg_printf(sztmp);
  300.     MessageBox(NULL, sztmp, "CMagaDbgFile", MB_OK|MB_ICONERROR);
  301. }
  302.  
  303. void CMagaDbgFile::CMagaDbgFileInit(LPMAGADBGOPT lpopt) {
  304.  
  305.     m_hFile = INVALID_HANDLE_VALUE;
  306.     if ( lpopt->lpszFilename==NULL || lstrlen(lpopt->lpszFilename)==0 ) {
  307.         ErrorMsg("║▌╜─╫╕└┤╫░(╠º▓┘û╝û│î°)", 0);
  308.         return;
  309.     }
  310.     m_fDisplay = lpopt->fDisplay ? TRUE : FALSE;
  311.     DWORD dwOpenMode = lpopt->fOverwrite ? CREATE_ALWAYS : OPEN_ALWAYS;
  312.     if ( (m_hFile = CreateFile(lpopt->lpszFilename, GENERIC_WRITE, FILE_SHARE_WRITE, NULL,
  313.             dwOpenMode, FILE_ATTRIBUTE_ARCHIVE, NULL)) == INVALID_HANDLE_VALUE ) {
  314.         ErrorMsg("║▌╜─╫╕└┤╫░", GetLastError());
  315.     }
  316.     else {
  317.         if ( !lpopt->fOverwrite ) SetFilePointer(m_hFile, NULL, NULL, FILE_END);
  318.     }
  319.  
  320.     SetHeader(lpopt->lpHead, this);        // CMagaDbg
  321. }
  322.  
  323. // âRâôâXâgâëâNâ^
  324. // --------------------------------
  325. // Éµé╔╠º▓┘é≡OPENé╖éΘòKùvé¬éáéΘé╠é┼üC
  326. // èεû{╕╫╜é≡î─é╘æµéPê°Éöé╔ NULL
  327. // --------------------------------
  328. CMagaDbgFile::CMagaDbgFile
  329.     (char* lpszFilename, BOOL fOver, BOOL fDis, char* s, BOOL f, dbgColor c, DWORD n) :
  330.     CMagaDbg(NULL, f, c, n)
  331. {
  332.     MAGADBGOPT    opt;
  333.     opt.lpszFilename    = lpszFilename;
  334.     opt.fOverwrite        = fOver;
  335.     opt.fDisplay        = fDis;
  336.     opt.lpHead            = s;
  337.     opt.fSeparator        = f;
  338.     opt.eColor            = c;
  339.     opt.nInterval        = n;
  340.     CMagaDbgFileInit(&opt);
  341. }
  342.  
  343. CMagaDbgFile::CMagaDbgFile(LPMAGADBGOPT lpopt) :
  344.     CMagaDbg(NULL, lpopt->fSeparator, lpopt->eColor, lpopt->nInterval)
  345. {
  346.     CMagaDbgFileInit(lpopt);
  347. }
  348.  
  349. // âfâBâXâgâëâNâ^
  350. // --------------------------------
  351. CMagaDbgFile::~CMagaDbgFile() {
  352.  
  353.     if ( m_hFile==INVALID_HANDLE_VALUE ) return;
  354.     if ( !GetSeparator() ) {        // CMagaDbg
  355.         CloseHandle(m_hFile);
  356.         return;
  357.     }
  358.     HWND    hwnd;
  359.     if ( (hwnd=MagaDbgFind())==NULL ) {
  360.         CloseHandle(m_hFile);
  361.         return;
  362.     }
  363.     // LogFileé╓╛╩▀┌░└Åoù═
  364.     HANDLE    hFileMap = CreateFileMapping((HANDLE)0xFFFFFFFF, NULL,
  365.                             PAGE_READWRITE, 0, STRLEN, MAGADBG_SEP_MAP);
  366.     if ( hFileMap != NULL ) {
  367.         int        num;
  368.         if ( (num=(int)SendMessage(hwnd, WM_GETSEPARATOR, 0, 0)) > 0 ) {
  369.             LPSTR    lpView;
  370.             if ( (lpView=(LPSTR)MapViewOfFile(hFileMap, FILE_MAP_READ, 0, 0, 0)) != NULL ) {
  371.                 DWORD    dwWritesize;
  372.                 for ( int i=0; i<num; i++ ) {
  373.                     WriteFile(m_hFile, lpView, lstrlen(lpView), &dwWritesize, NULL);
  374.                     WriteFile(m_hFile, "\x0d\x0a", 2, &dwWritesize, NULL);
  375.                 }
  376.                 UnmapViewOfFile(lpView);
  377.             }
  378.         }
  379.         CloseHandle(hFileMap);
  380.     }
  381.  
  382.     CloseHandle(m_hFile);
  383. }
  384.  
  385. // ò╢ÄÜù±é╠ò\Ī
  386. // --------------------------------
  387. void CMagaDbgFile::print(char* s, BOOL fCR) {
  388.  
  389.     if ( m_hFile != INVALID_HANDLE_VALUE ) {
  390.         BOOL    fWrite; 
  391.         DWORD    dwWritesize;
  392.         char*    tmp;
  393.         DWORD    dwLen = (DWORD)AddHeader(&tmp, s);        // CMagaDbg
  394.         fWrite = WriteFile(m_hFile, tmp, dwLen, &dwWritesize, NULL);
  395.         if ( !fWrite || dwLen!=dwWritesize )
  396.             ErrorMsg("file print error", GetLastError());
  397.         if ( fCR )
  398.             WriteFile(m_hFile, "\x0d\x0a", 2, &dwWritesize, NULL);
  399.         if ( dwLen!=0 ) delete tmp;
  400.     }
  401.  
  402.     if ( m_fDisplay )
  403.         CMagaDbg::print(s, fCR);
  404.     else if ( GetInterval() != 0 )
  405.         Sleep(GetInterval());
  406. }
  407.