Source code of Windows XP (NT5)
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

359 lines
9.2 KiB

  1. //+-------------------------------------------------------------------------
  2. //
  3. // Microsoft Windows
  4. // Copyright (C) Microsoft Corporation, 1992 - 2000
  5. //
  6. // File: Util.h
  7. //
  8. // Contents: Generic utility functions and classes for dscmd
  9. //
  10. // History: 01-Oct-2000 JeffJon Created
  11. //
  12. //--------------------------------------------------------------------------
  13. #ifndef _UTIL_H_
  14. #define _UTIL_H_
  15. #ifdef DBG
  16. //+--------------------------------------------------------------------------
  17. //
  18. // Class: CDebugSpew
  19. //
  20. // Purpose: Signifies whether to spew debug output on checked builds or not
  21. //
  22. // History: 01-Oct-2000 JeffJon Created
  23. //
  24. //---------------------------------------------------------------------------
  25. class CDebugSpew
  26. {
  27. public:
  28. //
  29. // Constructor/Destructor
  30. //
  31. CDebugSpew()
  32. : m_nDebugLevel(0),
  33. m_nIndent(0)
  34. {}
  35. ~CDebugSpew() {}
  36. //
  37. // Public data accessors
  38. //
  39. void SetDebugLevel(UINT nDebugLevel) { m_nDebugLevel = nDebugLevel; }
  40. UINT GetDebugLevel() { return m_nDebugLevel; }
  41. bool IsDebugEnabled() const { return (m_nDebugLevel > 0); }
  42. void SpewHeader();
  43. void EnterFunction(UINT nLevel, PCWSTR pszFunction);
  44. void LeaveFunction(UINT nLevel, PCWSTR pszFunction);
  45. void LeaveFunctionHr(UINT nLevel, PCWSTR pszFunction, HRESULT hr);
  46. void Output(UINT nLevel, PCWSTR pszOutput, ...);
  47. private:
  48. //
  49. // Private data accessors
  50. //
  51. void Indent() { m_nIndent += TAB; }
  52. void Outdent() { (m_nIndent >= TAB) ? m_nIndent -= TAB : m_nIndent = 0; }
  53. UINT GetIndent() { return m_nIndent; }
  54. //
  55. // Private data
  56. //
  57. //
  58. // This should always be in the range of 0 - 10 where zero is no debug output
  59. // and 10 is complete output
  60. //
  61. UINT m_nDebugLevel;
  62. UINT m_nIndent;
  63. static const UINT TAB = 3;
  64. };
  65. //
  66. // Globals
  67. //
  68. extern CDebugSpew DebugSpew;
  69. //+--------------------------------------------------------------------------
  70. //
  71. // Class: CFunctionSpew
  72. //
  73. // Purpose: Object which outputs the "Enter function" debug spew on creation
  74. // and outputs the "Leave function" debug spew on destruction
  75. //
  76. // History: 07-Dec-2000 JeffJon Created
  77. //
  78. //---------------------------------------------------------------------------
  79. class CFunctionSpew
  80. {
  81. public:
  82. //
  83. // Constructor/Destructor
  84. //
  85. CFunctionSpew(UINT nDebugLevel,
  86. PCWSTR pszFunctionName)
  87. : m_nDebugLevel(nDebugLevel),
  88. m_pszFunctionName(pszFunctionName),
  89. m_bLeaveAlreadyWritten(false)
  90. {
  91. ASSERT(pszFunctionName);
  92. DebugSpew.EnterFunction(nDebugLevel, pszFunctionName);
  93. }
  94. virtual ~CFunctionSpew()
  95. {
  96. if (!IsLeaveAlreadyWritten())
  97. {
  98. DebugSpew.LeaveFunction(GetDebugLevel(), GetFunctionName());
  99. }
  100. }
  101. protected:
  102. PCWSTR GetFunctionName() { return m_pszFunctionName; }
  103. UINT GetDebugLevel() { return m_nDebugLevel; }
  104. bool IsLeaveAlreadyWritten() { return m_bLeaveAlreadyWritten; }
  105. void SetLeaveAlreadyWritten(){ m_bLeaveAlreadyWritten = true; }
  106. private:
  107. PCWSTR m_pszFunctionName;
  108. UINT m_nDebugLevel;
  109. bool m_bLeaveAlreadyWritten;
  110. };
  111. //+--------------------------------------------------------------------------
  112. //
  113. // Class: CFunctionSpewHR
  114. //
  115. // Purpose: Object which outputs the "Enter function" debug spew on creation
  116. // and outputs the "Leave function" with the HRESULT return value
  117. // on destruction
  118. //
  119. // History: 07-Dec-2000 JeffJon Created
  120. //
  121. //---------------------------------------------------------------------------
  122. class CFunctionSpewHR : public CFunctionSpew
  123. {
  124. public:
  125. //
  126. // Constructor/Destructor
  127. //
  128. CFunctionSpewHR(UINT nDebugLevel,
  129. PCWSTR pszFunctionName,
  130. HRESULT& refHR)
  131. : m_refHR(refHR),
  132. CFunctionSpew(nDebugLevel, pszFunctionName)
  133. {
  134. }
  135. virtual ~CFunctionSpewHR()
  136. {
  137. DebugSpew.LeaveFunctionHr(GetDebugLevel(), GetFunctionName(), m_refHR);
  138. SetLeaveAlreadyWritten();
  139. }
  140. private:
  141. HRESULT& m_refHR;
  142. };
  143. //
  144. // Helper macros for use with CDebugSpew
  145. //
  146. #define ENABLE_DEBUG_OUTPUT(level) DebugSpew.SetDebugLevel((level)); \
  147. DebugSpew.SpewHeader();
  148. #define DISABLE_DEBUG_OUTPUT() DebugSpew.SetDebugLevel(0);
  149. #define ENTER_FUNCTION(level, func) CFunctionSpew functionSpewObject((level), TEXT(#func));
  150. #define ENTER_FUNCTION_HR(level, func, hr) HRESULT (hr) = S_OK; \
  151. CFunctionSpewHR functionSpewObject((level), TEXT(#func), (hr));
  152. #define LEAVE_FUNCTION(level, func) DebugSpew.LeaveFunction((level), TEXT(#func));
  153. #define LEAVE_FUNCTION_HR(level, func, hr) DebugSpew.LeaveFunctionHr((level), TEXT(#func), (hr));
  154. #define DEBUG_OUTPUT DebugSpew.Output
  155. #else
  156. #define ENABLE_DEBUG_OUTPUT(level)
  157. #define DISABLE_DEBUG_OUTPUT()
  158. #define ENTER_FUNCTION(level, func)
  159. #define ENTER_FUNCTION_HR(level, func, hr) HRESULT (hr) = S_OK;
  160. #define LEAVE_FUNCTION(level, func)
  161. #define LEAVE_FUNCTION_HR(level, func, hr)
  162. #define DEBUG_OUTPUT
  163. #endif // DBG
  164. //
  165. // Debug log levels - NOTE these can be given more meaningful names as needed
  166. //
  167. enum
  168. {
  169. NO_DEBUG_LOGGING = 0,
  170. MINIMAL_LOGGING,
  171. LEVEL2_LOGGING,
  172. LEVEL3_LOGGING,
  173. LEVEL4_LOGGING,
  174. LEVEL5_LOGGING,
  175. LEVEL6_LOGGING,
  176. LEVEL7_LOGGING,
  177. LEVEL8_LOGGING,
  178. LEVEL9_LOGGING,
  179. FULL_LOGGING
  180. };
  181. //+--------------------------------------------------------------------------
  182. //
  183. // Function: _UnicodeToOemConvert
  184. //
  185. // Synopsis: takes the passed in string (pszUnicode) and converts it to
  186. // the OEM code page
  187. //
  188. // Arguments: [pszUnicode - IN] : the string to be converted
  189. // [sbstrOemUnicode - OUT] : the converted string
  190. //
  191. // Returns:
  192. //
  193. // History: 04-Oct-2000 JeffJon Created
  194. //
  195. //---------------------------------------------------------------------------
  196. void _UnicodeToOemConvert(PCWSTR pszUnicode, CComBSTR& sbstrOemUnicode);
  197. //+--------------------------------------------------------------------------
  198. //
  199. // Function: SpewAttrs(ADS_ATTR_INFO* pCreateAttrs, DWORD dwNumAttrs);
  200. //
  201. // Synopsis: Uses the DEBUG_OUTPUT macro to output the attributes and the
  202. // values specified
  203. //
  204. // Arguments: [pAttrs - IN] : The ADS_ATTR_INFO
  205. // [dwNumAttrs - IN] : The number of attributes in pAttrs
  206. //
  207. // Returns:
  208. //
  209. // History: 04-Oct-2000 JeffJon Created
  210. //
  211. //---------------------------------------------------------------------------
  212. #ifdef DBG
  213. void SpewAttrs(ADS_ATTR_INFO* pAttrs, DWORD dwNumAttrs);
  214. #endif // DBG
  215. //+--------------------------------------------------------------------------
  216. //
  217. // Function: litow
  218. //
  219. // Synopsis:
  220. //
  221. // Arguments: [li - IN] : reference to large integer to be converted to string
  222. // [sResult - OUT] : Gets the output string
  223. // Returns: void
  224. //
  225. // History: 25-Sep-2000 hiteshr Created
  226. // Copied from dsadmin code base, changed work with CComBSTR
  227. //---------------------------------------------------------------------------
  228. void litow(LARGE_INTEGER& li, CComBSTR& sResult);
  229. //+--------------------------------------------------------------------------
  230. //
  231. // Class: CManagedStringEntry
  232. //
  233. // Synopsis: My own string list entry since we are not using MFC
  234. //
  235. // History: 25-Oct-2000 JeffJon Created
  236. //
  237. //---------------------------------------------------------------------------
  238. class CManagedStringEntry
  239. {
  240. public:
  241. //
  242. // Constructor
  243. //
  244. CManagedStringEntry(PCWSTR pszValue) : pNext(NULL), sbstrValue(pszValue) {}
  245. CComBSTR sbstrValue;
  246. CManagedStringEntry* pNext;
  247. };
  248. //+--------------------------------------------------------------------------
  249. //
  250. // Class: CManagedStringList
  251. //
  252. // Synopsis: My own string list since we are not using MFC
  253. //
  254. // History: 25-Oct-2000 JeffJon Created
  255. //
  256. //---------------------------------------------------------------------------
  257. class CManagedStringList
  258. {
  259. public:
  260. //
  261. // Constructor
  262. //
  263. CManagedStringList() : m_pHead(NULL), m_pTail(NULL), m_nCount(0) {}
  264. //
  265. // Destructor
  266. //
  267. ~CManagedStringList()
  268. {
  269. DeleteAll();
  270. }
  271. void DeleteAll()
  272. {
  273. CManagedStringEntry* pEntry = m_pHead;
  274. while (pEntry != NULL)
  275. {
  276. CManagedStringEntry* pTempEntry = pEntry;
  277. pEntry = pEntry->pNext;
  278. delete pTempEntry;
  279. }
  280. m_nCount = 0;
  281. }
  282. void Add(PCWSTR pszValue)
  283. {
  284. if (!m_pHead)
  285. {
  286. m_pHead = new CManagedStringEntry(pszValue);
  287. m_pTail = m_pHead;
  288. m_nCount++;
  289. }
  290. else
  291. {
  292. ASSERT(m_pTail);
  293. m_pTail->pNext = new CManagedStringEntry(pszValue);
  294. if (m_pTail->pNext)
  295. {
  296. m_pTail = m_pTail->pNext;
  297. m_nCount++;
  298. }
  299. }
  300. }
  301. bool Contains(PCWSTR pszValue)
  302. {
  303. bool bRet = false;
  304. for (CManagedStringEntry* pEntry = m_pHead; pEntry; pEntry = pEntry->pNext)
  305. {
  306. if (_wcsicmp(pEntry->sbstrValue, pszValue) == 0)
  307. {
  308. bRet = true;
  309. break;
  310. }
  311. }
  312. return bRet;
  313. }
  314. private:
  315. CManagedStringEntry* m_pHead;
  316. CManagedStringEntry* m_pTail;
  317. UINT m_nCount;
  318. };
  319. #endif // _UTIL_H_