home *** CD-ROM | disk | FTP | other *** search
/ Supercompiler 1997 / SUPERCOMPILER97.iso / MS_VC.50 / VC / MFC / SRC / DUMPCONT.CPP < prev    next >
Encoding:
C/C++ Source or Header  |  1996-10-30  |  5.6 KB  |  278 lines

  1. // This is a part of the Microsoft Foundation Classes C++ library.
  2. // Copyright (C) 1992-1997 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. #ifndef _MAC
  85.             if (*lpsz == '\n')
  86.                 *lpBuf++ = '\r';
  87. #endif
  88.             *lpBuf++ = *lpsz++;
  89.         }
  90.         *lpBuf = '\0';
  91.         OutputString(szBuffer);
  92.         return *this;
  93.     }
  94.  
  95.     m_pFile->Write(lpsz, lstrlen(lpsz)*sizeof(TCHAR));
  96.     return *this;
  97. }
  98.  
  99. CDumpContext& CDumpContext::operator<<(BYTE by)
  100. {
  101.     TCHAR szBuffer[32];
  102.  
  103.     wsprintf(szBuffer, _T("%d"), (int)by);
  104.     OutputString(szBuffer);
  105.  
  106.     return *this;
  107. }
  108.  
  109. CDumpContext& CDumpContext::operator<<(WORD w)
  110. {
  111.     TCHAR szBuffer[32];
  112.  
  113.     wsprintf(szBuffer, _T("%u"), (UINT) w);
  114.     OutputString(szBuffer);
  115.  
  116.     return *this;
  117. }
  118.  
  119. CDumpContext& CDumpContext::operator<<(UINT u)
  120. {
  121.     TCHAR szBuffer[32];
  122.  
  123.     wsprintf(szBuffer, _T("0x%X"), u);
  124.     OutputString(szBuffer);
  125.  
  126.     return *this;
  127. }
  128.  
  129. CDumpContext& CDumpContext::operator<<(LONG l)
  130. {
  131.     TCHAR szBuffer[32];
  132.  
  133.     wsprintf(szBuffer, _T("%ld"), l);
  134.     OutputString(szBuffer);
  135.  
  136.     return *this;
  137. }
  138.  
  139. CDumpContext& CDumpContext::operator<<(DWORD dw)
  140. {
  141.     TCHAR szBuffer[32];
  142.  
  143.     wsprintf(szBuffer, _T("%lu"), dw);
  144.     OutputString(szBuffer);
  145.  
  146.     return *this;
  147. }
  148.  
  149. CDumpContext& CDumpContext::operator<<(int n)
  150. {
  151.     TCHAR szBuffer[32];
  152.  
  153.     wsprintf(szBuffer, _T("%d"), n);
  154.     OutputString(szBuffer);
  155.  
  156.     return *this;
  157. }
  158.  
  159. CDumpContext& CDumpContext::operator<<(const CObject* pOb)
  160. {
  161. #ifdef _DEBUG // all CDumpContext output is controlled by afxTraceEnabled
  162.     if (!afxTraceEnabled)
  163.         return *this;
  164. #endif //_DEBUG
  165.  
  166.     if (pOb == NULL)
  167.         *this << _T("NULL");
  168.     else
  169.         pOb->Dump(*this);
  170.  
  171.     return *this;
  172. }
  173.  
  174. CDumpContext& CDumpContext::operator<<(const CObject& ob)
  175. {
  176.     return *this << &ob;
  177. }
  178.  
  179. CDumpContext& CDumpContext::operator<<(const void* lp)
  180. {
  181.     TCHAR szBuffer[32];
  182.  
  183.     // prefix a pointer with "$" and print in hex
  184.     wsprintf(szBuffer, _T("$%lX"), (LONG)lp);
  185.     OutputString(szBuffer);
  186.  
  187.     return *this;
  188. }
  189.  
  190. /////////////////////////////////////////////////////////////////////////////
  191. // Formatted output
  192.  
  193. void CDumpContext::HexDump(LPCTSTR lpszLine, BYTE* pby,
  194.     int nBytes, int nWidth)
  195. // do a simple hex-dump (8 per line) to a CDumpContext
  196. //  the "lpszLine" is a string to print at the start of each line
  197. //    (%lx should be used to expand the current address)
  198. {
  199.     ASSERT(nBytes > 0);
  200.     ASSERT(nWidth > 0);
  201.     ASSERT(AfxIsValidString(lpszLine));
  202.     ASSERT(AfxIsValidAddress(pby, nBytes, FALSE));
  203.  
  204. #ifdef _DEBUG // all CDumpContext output is controlled by afxTraceEnabled
  205.     if (!afxTraceEnabled)
  206.         return;
  207. #endif //_DEBUG
  208.  
  209.     int nRow = 0;
  210.     TCHAR szBuffer[32];
  211.  
  212.     while (nBytes--)
  213.     {
  214.         if (nRow == 0)
  215.         {
  216.             wsprintf(szBuffer, lpszLine, pby);
  217.             *this << szBuffer;
  218.         }
  219.  
  220.         wsprintf(szBuffer, _T(" %02X"), *pby++);
  221.         *this << szBuffer;
  222.  
  223.         if (++nRow >= nWidth)
  224.         {
  225.             *this << _T("\n");
  226.             nRow = 0;
  227.         }
  228.     }
  229.     if (nRow != 0)
  230.         *this << _T("\n");
  231. }
  232.  
  233. /////////////////////////////////////////////////////////////////////////////
  234.  
  235. #ifdef _UNICODE
  236. // special version for ANSI characters
  237. CDumpContext& CDumpContext::operator<<(LPCSTR lpsz)
  238. {
  239.     if (lpsz == NULL)
  240.     {
  241.         OutputString(L"(NULL)");
  242.         return *this;
  243.     }
  244.  
  245. #ifdef _DEBUG // all CDumpContext output is controlled by afxTraceEnabled
  246.     if (!afxTraceEnabled)
  247.         return *this;
  248. #endif //_DEBUG
  249.  
  250.     // limited length
  251.     TCHAR szBuffer[512];
  252.     _mbstowcsz(szBuffer, lpsz, _countof(szBuffer));
  253.     return *this << szBuffer;
  254. }
  255. #else   //_UNICODE
  256. // special version for WIDE characters
  257. CDumpContext& CDumpContext::operator<<(LPCWSTR lpsz)
  258. {
  259.     if (lpsz == NULL)
  260.     {
  261.         OutputString("(NULL)");
  262.         return *this;
  263.     }
  264.  
  265. #ifdef _DEBUG // all CDumpContext output is controlled by afxTraceEnabled
  266.     if (!afxTraceEnabled)
  267.         return *this;
  268. #endif //_DEBUG
  269.  
  270.     // limited length
  271.     char szBuffer[512];
  272.     _wcstombsz(szBuffer, lpsz, _countof(szBuffer));
  273.     return *this << szBuffer;
  274. }
  275. #endif  //!_UNICODE
  276.  
  277. /////////////////////////////////////////////////////////////////////////////
  278.