home *** CD-ROM | disk | FTP | other *** search
/ Tricks of the Windows Gam…ming Gurus (2nd Edition) / Disc2.iso / vc98 / mfc / src / dumpcont.cpp < prev    next >
C/C++ Source or Header  |  1998-06-16  |  6KB  |  280 lines

  1. // This is a part of the Microsoft Foundation Classes C++ library.
  2. // Copyright (C) 1992-1998 Microsoft Corporation
  3. // All rights reserved.
  4. //
  5. // This source code is only intended as a supplement to the
  6. // Microsoft Foundation Classes Reference and related
  7. // electronic documentation provided with the library.
  8. // See these sources for detailed information regarding the
  9. // Microsoft Foundation Classes product.
  10.  
  11. #include "stdafx.h"
  12.  
  13. #ifdef AFX_DBG1_SEG
  14. #pragma code_seg(AFX_DBG1_SEG)
  15. #endif
  16.  
  17. #ifdef _DEBUG
  18. #undef THIS_FILE
  19. static char THIS_FILE[] = __FILE__;
  20. #endif
  21.  
  22. /////////////////////////////////////////////////////////////////////////////
  23. // Diagnostic Stream output
  24.  
  25. void CDumpContext::OutputString(LPCTSTR lpsz)
  26. {
  27. #ifdef _DEBUG
  28.     // all CDumpContext output is controlled by afxTraceEnabled
  29.     if (!afxTraceEnabled)
  30.         return;
  31. #endif
  32.  
  33.     // use C-runtime/OutputDebugString when m_pFile is NULL
  34.     if (m_pFile == NULL)
  35.     {
  36.         AfxOutputDebugString(lpsz);
  37.         return;
  38.     }
  39.  
  40.     // otherwise, write the string to the file
  41.     m_pFile->Write(lpsz, lstrlen(lpsz)*sizeof(TCHAR));
  42. }
  43.  
  44. CDumpContext::CDumpContext(CFile* pFile)
  45. {
  46.     if (pFile)
  47.         ASSERT_VALID(pFile);
  48.  
  49.     m_pFile = pFile;
  50.     m_nDepth = 0;
  51. }
  52.  
  53. void CDumpContext::Flush()
  54. {
  55.     if (m_pFile)
  56.         m_pFile->Flush();
  57. }
  58.  
  59. CDumpContext& CDumpContext::operator<<(LPCTSTR lpsz)
  60. {
  61.     if (lpsz == NULL)
  62.     {
  63.         OutputString(_T("(NULL)"));
  64.         return *this;
  65.     }
  66.  
  67. #ifdef _DEBUG // all CDumpContext output is controlled by afxTraceEnabled
  68.     if (!afxTraceEnabled)
  69.         return *this;
  70. #endif //_DEBUG
  71.  
  72.     if (m_pFile == NULL)
  73.     {
  74.         TCHAR szBuffer[512];
  75.         LPTSTR lpBuf = szBuffer;
  76.         while (*lpsz != '\0')
  77.         {
  78.             if (lpBuf > szBuffer + _countof(szBuffer) - 3)
  79.             {
  80.                 *lpBuf = '\0';
  81.                 OutputString(szBuffer);
  82.                 lpBuf = szBuffer;
  83.             }
  84.             if (*lpsz == '\n')
  85.                 *lpBuf++ = '\r';
  86.             *lpBuf++ = *lpsz++;
  87.         }
  88.         *lpBuf = '\0';
  89.         OutputString(szBuffer);
  90.         return *this;
  91.     }
  92.  
  93.     m_pFile->Write(lpsz, lstrlen(lpsz)*sizeof(TCHAR));
  94.     return *this;
  95. }
  96.  
  97. CDumpContext& CDumpContext::operator<<(BYTE by)
  98. {
  99.     TCHAR szBuffer[32];
  100.  
  101.     wsprintf(szBuffer, _T("%d"), (int)by);
  102.     OutputString(szBuffer);
  103.  
  104.     return *this;
  105. }
  106.  
  107. CDumpContext& CDumpContext::operator<<(WORD w)
  108. {
  109.     TCHAR szBuffer[32];
  110.  
  111.     wsprintf(szBuffer, _T("%u"), (UINT) w);
  112.     OutputString(szBuffer);
  113.  
  114.     return *this;
  115. }
  116.  
  117. CDumpContext& CDumpContext::operator<<(UINT u)
  118. {
  119.     TCHAR szBuffer[32];
  120.  
  121.     wsprintf(szBuffer, _T("0x%X"), u);
  122.     OutputString(szBuffer);
  123.  
  124.     return *this;
  125. }
  126.  
  127. CDumpContext& CDumpContext::operator<<(LONG l)
  128. {
  129.     TCHAR szBuffer[32];
  130.  
  131.     wsprintf(szBuffer, _T("%ld"), l);
  132.     OutputString(szBuffer);
  133.  
  134.     return *this;
  135. }
  136.  
  137. CDumpContext& CDumpContext::operator<<(DWORD dw)
  138. {
  139.     TCHAR szBuffer[32];
  140.  
  141.     wsprintf(szBuffer, _T("%lu"), dw);
  142.     OutputString(szBuffer);
  143.  
  144.     return *this;
  145. }
  146.  
  147. CDumpContext& CDumpContext::operator<<(int n)
  148. {
  149.     TCHAR szBuffer[32];
  150.  
  151.     wsprintf(szBuffer, _T("%d"), n);
  152.     OutputString(szBuffer);
  153.  
  154.     return *this;
  155. }
  156.  
  157. CDumpContext& CDumpContext::operator<<(const CObject* pOb)
  158. {
  159. #ifdef _DEBUG // all CDumpContext output is controlled by afxTraceEnabled
  160.     if (!afxTraceEnabled)
  161.         return *this;
  162. #endif //_DEBUG
  163.  
  164.     if (pOb == NULL)
  165.         *this << _T("NULL");
  166.     else
  167. #ifdef _AFXDLL
  168.         pOb->Dump(*this);
  169. #else
  170.         *this << _T("Unable to dump object in static release builds");
  171. #endif
  172.  
  173.     return *this;
  174. }
  175.  
  176. CDumpContext& CDumpContext::operator<<(const CObject& ob)
  177. {
  178.     return *this << &ob;
  179. }
  180.  
  181. CDumpContext& CDumpContext::operator<<(const void* lp)
  182. {
  183.     TCHAR szBuffer[32];
  184.  
  185.     // prefix a pointer with "$" and print in hex
  186.     wsprintf(szBuffer, _T("$%lX"), (LONG)lp);
  187.     OutputString(szBuffer);
  188.  
  189.     return *this;
  190. }
  191.  
  192. /////////////////////////////////////////////////////////////////////////////
  193. // Formatted output
  194.  
  195. void CDumpContext::HexDump(LPCTSTR lpszLine, BYTE* pby,
  196.     int nBytes, int nWidth)
  197. // do a simple hex-dump (8 per line) to a CDumpContext
  198. //  the "lpszLine" is a string to print at the start of each line
  199. //    (%lx should be used to expand the current address)
  200. {
  201.     ASSERT(nBytes > 0);
  202.     ASSERT(nWidth > 0);
  203.     ASSERT(AfxIsValidString(lpszLine));
  204.     ASSERT(AfxIsValidAddress(pby, nBytes, FALSE));
  205.  
  206. #ifdef _DEBUG // all CDumpContext output is controlled by afxTraceEnabled
  207.     if (!afxTraceEnabled)
  208.         return;
  209. #endif //_DEBUG
  210.  
  211.     int nRow = 0;
  212.     TCHAR szBuffer[32];
  213.  
  214.     while (nBytes--)
  215.     {
  216.         if (nRow == 0)
  217.         {
  218.             wsprintf(szBuffer, lpszLine, pby);
  219.             *this << szBuffer;
  220.         }
  221.  
  222.         wsprintf(szBuffer, _T(" %02X"), *pby++);
  223.         *this << szBuffer;
  224.  
  225.         if (++nRow >= nWidth)
  226.         {
  227.             *this << _T("\n");
  228.             nRow = 0;
  229.         }
  230.     }
  231.     if (nRow != 0)
  232.         *this << _T("\n");
  233. }
  234.  
  235. /////////////////////////////////////////////////////////////////////////////
  236.  
  237. #ifdef _UNICODE
  238. // special version for ANSI characters
  239. CDumpContext& CDumpContext::operator<<(LPCSTR lpsz)
  240. {
  241.     if (lpsz == NULL)
  242.     {
  243.         OutputString(L"(NULL)");
  244.         return *this;
  245.     }
  246.  
  247. #ifdef _DEBUG // all CDumpContext output is controlled by afxTraceEnabled
  248.     if (!afxTraceEnabled)
  249.         return *this;
  250. #endif //_DEBUG
  251.  
  252.     // limited length
  253.     TCHAR szBuffer[512];
  254.     _mbstowcsz(szBuffer, lpsz, _countof(szBuffer));
  255.     return *this << szBuffer;
  256. }
  257. #else   //_UNICODE
  258. // special version for WIDE characters
  259. CDumpContext& CDumpContext::operator<<(LPCWSTR lpsz)
  260. {
  261.     if (lpsz == NULL)
  262.     {
  263.         OutputString("(NULL)");
  264.         return *this;
  265.     }
  266.  
  267. #ifdef _DEBUG // all CDumpContext output is controlled by afxTraceEnabled
  268.     if (!afxTraceEnabled)
  269.         return *this;
  270. #endif //_DEBUG
  271.  
  272.     // limited length
  273.     char szBuffer[512];
  274.     _wcstombsz(szBuffer, lpsz, _countof(szBuffer));
  275.     return *this << szBuffer;
  276. }
  277. #endif  //!_UNICODE
  278.  
  279. /////////////////////////////////////////////////////////////////////////////
  280.