dumpcont.cpp 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279
  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. #include "stdafx.h"
  11. #ifdef AFX_DBG1_SEG
  12. #pragma code_seg(AFX_DBG1_SEG)
  13. #endif
  14. #ifdef _DEBUG
  15. #undef THIS_FILE
  16. static char THIS_FILE[] = __FILE__;
  17. #endif
  18. /////////////////////////////////////////////////////////////////////////////
  19. // Diagnostic Stream output
  20. void CDumpContext::OutputString(LPCTSTR lpsz)
  21. {
  22. #ifdef _DEBUG
  23. // all CDumpContext output is controlled by afxTraceEnabled
  24. if (!afxTraceEnabled)
  25. return;
  26. #endif
  27. // use C-runtime/OutputDebugString when m_pFile is NULL
  28. if (m_pFile == NULL)
  29. {
  30. AfxOutputDebugString(lpsz);
  31. return;
  32. }
  33. // otherwise, write the string to the file
  34. m_pFile->Write(lpsz, lstrlen(lpsz)*sizeof(TCHAR));
  35. }
  36. CDumpContext::CDumpContext(CFile* pFile)
  37. {
  38. if (pFile)
  39. ASSERT_VALID(pFile);
  40. m_pFile = pFile;
  41. m_nDepth = 0;
  42. }
  43. void CDumpContext::Flush()
  44. {
  45. if (m_pFile)
  46. m_pFile->Flush();
  47. }
  48. CDumpContext& CDumpContext::operator<<(LPCTSTR lpsz)
  49. {
  50. if (lpsz == NULL)
  51. {
  52. OutputString(_T("(NULL)"));
  53. return *this;
  54. }
  55. #ifdef _DEBUG // all CDumpContext output is controlled by afxTraceEnabled
  56. if (!afxTraceEnabled)
  57. return *this;
  58. #endif //_DEBUG
  59. if (m_pFile == NULL)
  60. {
  61. TCHAR szBuffer[512];
  62. LPTSTR lpBuf = szBuffer;
  63. while (*lpsz != '\0')
  64. {
  65. if (lpBuf > szBuffer + _countof(szBuffer) - 3)
  66. {
  67. *lpBuf = '\0';
  68. OutputString(szBuffer);
  69. lpBuf = szBuffer;
  70. }
  71. if (*lpsz == '\n')
  72. *lpBuf++ = '\r';
  73. *lpBuf++ = *lpsz++;
  74. }
  75. *lpBuf = '\0';
  76. OutputString(szBuffer);
  77. return *this;
  78. }
  79. m_pFile->Write(lpsz, lstrlen(lpsz)*sizeof(TCHAR));
  80. return *this;
  81. }
  82. CDumpContext& CDumpContext::operator<<(BYTE by)
  83. {
  84. TCHAR szBuffer[32];
  85. wsprintf(szBuffer, _T("%d"), (int)by);
  86. OutputString(szBuffer);
  87. return *this;
  88. }
  89. CDumpContext& CDumpContext::operator<<(WORD w)
  90. {
  91. TCHAR szBuffer[32];
  92. wsprintf(szBuffer, _T("%u"), (UINT) w);
  93. OutputString(szBuffer);
  94. return *this;
  95. }
  96. CDumpContext& CDumpContext::operator<<(UINT u)
  97. {
  98. TCHAR szBuffer[32];
  99. wsprintf(szBuffer, _T("0x%X"), u);
  100. OutputString(szBuffer);
  101. return *this;
  102. }
  103. CDumpContext& CDumpContext::operator<<(LONG l)
  104. {
  105. TCHAR szBuffer[32];
  106. wsprintf(szBuffer, _T("%ld"), l);
  107. OutputString(szBuffer);
  108. return *this;
  109. }
  110. CDumpContext& CDumpContext::operator<<(DWORD dw)
  111. {
  112. TCHAR szBuffer[32];
  113. wsprintf(szBuffer, _T("%lu"), dw);
  114. OutputString(szBuffer);
  115. return *this;
  116. }
  117. CDumpContext& CDumpContext::operator<<(int n)
  118. {
  119. TCHAR szBuffer[32];
  120. wsprintf(szBuffer, _T("%d"), n);
  121. OutputString(szBuffer);
  122. return *this;
  123. }
  124. CDumpContext& CDumpContext::operator<<(const CObject* pOb)
  125. {
  126. #ifdef _DEBUG // all CDumpContext output is controlled by afxTraceEnabled
  127. if (!afxTraceEnabled)
  128. return *this;
  129. #endif //_DEBUG
  130. if (pOb == NULL)
  131. *this << _T("NULL");
  132. else
  133. #ifdef _AFXDLL
  134. pOb->Dump(*this);
  135. #else
  136. *this << _T("Unable to dump object in static release builds");
  137. #endif
  138. return *this;
  139. }
  140. CDumpContext& CDumpContext::operator<<(const CObject& ob)
  141. {
  142. return *this << &ob;
  143. }
  144. CDumpContext& CDumpContext::operator<<(const void* lp)
  145. {
  146. TCHAR szBuffer[32];
  147. // prefix a pointer with "$" and print in hex
  148. wsprintf(szBuffer, _T("$%lX"), (LONG)lp);
  149. OutputString(szBuffer);
  150. return *this;
  151. }
  152. /////////////////////////////////////////////////////////////////////////////
  153. // Formatted output
  154. void CDumpContext::HexDump(LPCTSTR lpszLine, BYTE* pby,
  155. int nBytes, int nWidth)
  156. // do a simple hex-dump (8 per line) to a CDumpContext
  157. // the "lpszLine" is a string to print at the start of each line
  158. // (%lx should be used to expand the current address)
  159. {
  160. ASSERT(nBytes > 0);
  161. ASSERT(nWidth > 0);
  162. ASSERT(AfxIsValidString(lpszLine));
  163. ASSERT(AfxIsValidAddress(pby, nBytes, FALSE));
  164. #ifdef _DEBUG // all CDumpContext output is controlled by afxTraceEnabled
  165. if (!afxTraceEnabled)
  166. return;
  167. #endif //_DEBUG
  168. int nRow = 0;
  169. TCHAR szBuffer[32];
  170. while (nBytes--)
  171. {
  172. if (nRow == 0)
  173. {
  174. wsprintf(szBuffer, lpszLine, pby);
  175. *this << szBuffer;
  176. }
  177. wsprintf(szBuffer, _T(" %02X"), *pby++);
  178. *this << szBuffer;
  179. if (++nRow >= nWidth)
  180. {
  181. *this << _T("\n");
  182. nRow = 0;
  183. }
  184. }
  185. if (nRow != 0)
  186. *this << _T("\n");
  187. }
  188. /////////////////////////////////////////////////////////////////////////////
  189. #ifdef _UNICODE
  190. // special version for ANSI characters
  191. CDumpContext& CDumpContext::operator<<(LPCSTR lpsz)
  192. {
  193. if (lpsz == NULL)
  194. {
  195. OutputString(L"(NULL)");
  196. return *this;
  197. }
  198. #ifdef _DEBUG // all CDumpContext output is controlled by afxTraceEnabled
  199. if (!afxTraceEnabled)
  200. return *this;
  201. #endif //_DEBUG
  202. // limited length
  203. TCHAR szBuffer[512];
  204. _mbstowcsz(szBuffer, lpsz, _countof(szBuffer));
  205. return *this << szBuffer;
  206. }
  207. #else //_UNICODE
  208. // special version for WIDE characters
  209. CDumpContext& CDumpContext::operator<<(LPCWSTR lpsz)
  210. {
  211. if (lpsz == NULL)
  212. {
  213. OutputString("(NULL)");
  214. return *this;
  215. }
  216. #ifdef _DEBUG // all CDumpContext output is controlled by afxTraceEnabled
  217. if (!afxTraceEnabled)
  218. return *this;
  219. #endif //_DEBUG
  220. // limited length
  221. char szBuffer[512];
  222. _wcstombsz(szBuffer, lpsz, _countof(szBuffer));
  223. return *this << szBuffer;
  224. }
  225. #endif //!_UNICODE
  226. /////////////////////////////////////////////////////////////////////////////