home *** CD-ROM | disk | FTP | other *** search
/ Liren Large Software Subsidy 7 / 07.iso / c / c480 / 19.ddi / MFC / SRC / DUMPCONT.CP_ / DUMPCONT.CP
Encoding:
Text File  |  1993-02-08  |  4.8 KB  |  258 lines

  1. // This is a part of the Microsoft Foundation Classes C++ library. 
  2. // Copyright (C) 1992 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 Microsoft 
  7. // QuickHelp and/or WinHelp 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 BASED_CODE THIS_FILE[] = __FILE__;
  20. #endif
  21.  
  22.  
  23. /////////////////////////////////////////////////////////////////////////////
  24. // Diagnostic Stream output
  25.  
  26. void 
  27. CDumpContext::OutputString(LPCSTR lpsz)
  28. {
  29. #ifdef _DEBUG // all CDumpContext output is controlled by afxTraceEnabled
  30.     if (!afxTraceEnabled)
  31.         return;
  32. #endif //_DEBUG
  33. #ifdef _WINDOWS
  34.     if (m_pFile == NULL)
  35.         ::OutputDebugString(lpsz);
  36.     else
  37. #endif
  38.         m_pFile->Write(lpsz, lstrlen(lpsz));
  39. }
  40.  
  41. CDumpContext::CDumpContext(CFile* pFile)
  42. {
  43.     if (m_pFile)
  44.         ASSERT_VALID(pFile);
  45.  
  46.     m_pFile = pFile;
  47.     m_nDepth = 0;
  48. }
  49.  
  50. void CDumpContext::Flush()
  51. {
  52.     if (m_pFile)
  53.         m_pFile->Flush();
  54. }
  55.  
  56. CDumpContext&
  57. CDumpContext::operator<<(LPCSTR lpsz)
  58. {
  59.     if (lpsz == NULL)
  60.         return *this;
  61.  
  62. #ifdef _DEBUG // all CDumpContext output is controlled by afxTraceEnabled
  63.     if (!afxTraceEnabled)
  64.         return *this;
  65. #endif //_DEBUG
  66.  
  67. #ifdef _WINDOWS
  68.     if (m_pFile == NULL)
  69.     {
  70. #ifndef _WINDLL
  71.         char szBuffer[512];
  72. #else
  73.         static char szBuffer[512];
  74. #endif
  75.         LPSTR lpBuf = szBuffer;
  76.  
  77.         while (*lpsz != '\0')
  78.         {
  79.             if (lpBuf > szBuffer + sizeof(szBuffer) - 3)
  80.             {
  81.                 *lpBuf = '\0';
  82.                 OutputString(szBuffer);
  83.                 lpBuf = szBuffer;
  84.             }
  85.             if (*lpsz == '\n')
  86.                 *lpBuf++ = '\r';
  87.             *lpBuf++ = *lpsz++;
  88.         }
  89.         *lpBuf = '\0';
  90.         OutputString(szBuffer);
  91.         return *this;
  92.     }
  93. #endif
  94.     m_pFile->Write(lpsz, lstrlen(lpsz));
  95.     return *this;
  96. }
  97.  
  98. CDumpContext&
  99. CDumpContext::operator<<(BYTE by)
  100. {
  101.     char szBuffer[32];
  102.  
  103.     sprintf(szBuffer, "%d", (int)by);
  104.     OutputString(szBuffer);
  105.  
  106.     return *this;
  107. }
  108.  
  109. CDumpContext&
  110. CDumpContext::operator<<(WORD w)
  111. {
  112.     char szBuffer[32];
  113.  
  114.     sprintf(szBuffer, "%u", (UINT) w);
  115.     OutputString(szBuffer);
  116.  
  117.     return *this;
  118. }
  119.  
  120. CDumpContext&
  121. CDumpContext::operator<<(UINT u)
  122. {
  123.     char szBuffer[32];
  124.  
  125.     sprintf(szBuffer, "0x%X", u);
  126.     OutputString(szBuffer);
  127.  
  128.     return *this;
  129. }
  130.  
  131. CDumpContext&
  132. CDumpContext::operator<<(LONG l)
  133. {
  134.     char szBuffer[32];
  135.  
  136.     sprintf(szBuffer, "%ld", l);
  137.     OutputString(szBuffer);
  138.  
  139.     return *this;
  140. }
  141.  
  142. CDumpContext&
  143. CDumpContext::operator<<(DWORD dw)
  144. {
  145.     char szBuffer[32];
  146.  
  147.     sprintf(szBuffer, "%lu", dw);
  148.     OutputString(szBuffer);
  149.  
  150.     return *this;
  151. }
  152.  
  153. CDumpContext&
  154. CDumpContext::operator<<(int n)
  155. {
  156.     char szBuffer[32];
  157.  
  158.     sprintf(szBuffer, "%d", n);
  159.     OutputString(szBuffer);
  160.  
  161.     return *this;
  162. }
  163.  
  164. CDumpContext&
  165. CDumpContext::operator<<(const CObject* pOb)
  166. {
  167. #ifdef _DEBUG // all CDumpContext output is controlled by afxTraceEnabled
  168.     if (!afxTraceEnabled)
  169.         return *this;
  170. #endif //_DEBUG
  171.  
  172.     if (pOb == NULL)
  173.         *this << "NULL";
  174.     else
  175.     {
  176.         ASSERT_VALID(pOb);
  177.         pOb->Dump(*this);
  178.     }
  179.     return *this;
  180. }
  181.  
  182. CDumpContext&
  183. CDumpContext::operator<<(const CObject& ob)
  184. {
  185.     return *this << &ob;
  186. }
  187.  
  188. #ifdef _NEARDATA
  189. CDumpContext&
  190. CDumpContext::operator<<(const void NEAR* np)
  191. {
  192.     char szBuffer[32];
  193.  
  194.     // prefix a pointer with "$" and print in hex
  195.     sprintf(szBuffer, "$%X", (WORD)np);
  196.     OutputString(szBuffer);
  197.  
  198.     return *this;
  199. }
  200. #endif //_NEARDATA
  201.  
  202. CDumpContext&
  203. CDumpContext::operator<<(const void FAR* lp)
  204. {
  205.     char szBuffer[32];
  206.  
  207.     // prefix a pointer with "$" and print in hex
  208.     sprintf(szBuffer, "$%lX", (LONG)lp);
  209.     OutputString(szBuffer);
  210.  
  211.     return *this;
  212. }
  213.  
  214. /////////////////////////////////////////////////////////////////////////////
  215. // Formatted output
  216.  
  217. void CDumpContext::HexDump(const char* pszLine, BYTE* pby,
  218.     int nBytes, int nWidth)
  219. // do a simple hex-dump (8 per line) to a CDumpContext
  220. //  the "pszLine" is a string to print at the start of each line
  221. //    (%lx should be used to expand the current address)
  222. {
  223.     ASSERT(nBytes > 0);
  224.     ASSERT(nWidth > 0);
  225.     ASSERT(AfxIsValidString(pszLine));
  226.     ASSERT(AfxIsValidAddress(pby, nBytes, FALSE));
  227.  
  228. #ifdef _DEBUG // all CDumpContext output is controlled by afxTraceEnabled
  229.     if (!afxTraceEnabled)
  230.         return;
  231. #endif //_DEBUG
  232.  
  233.     int nRow = 0;
  234.     char szBuffer[32];
  235.  
  236.     while (nBytes--)
  237.     {
  238.         if (nRow == 0)
  239.         {
  240.             sprintf(szBuffer, pszLine, pby);
  241.             *this << szBuffer;
  242.         }
  243.  
  244.         sprintf(szBuffer, " %02X", *pby++);
  245.         *this << szBuffer;
  246.  
  247.         if (++nRow >= nWidth)
  248.         {
  249.             *this << "\n";
  250.             nRow = 0;
  251.         }
  252.     }
  253.     if (nRow != 0)
  254.         *this << "\n";
  255. }
  256.  
  257. /////////////////////////////////////////////////////////////////////////////
  258.