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.

213 lines
4.4 KiB

  1. /*++
  2. Copyright (C) 2000-2001 Microsoft Corporation
  3. --*/
  4. #ifndef __A51_TOOLS__H_
  5. #define __A51_TOOLS__H_
  6. #include <sync.h>
  7. #include <newnew.h>
  8. #include <xmemory>
  9. #ifdef DBG
  10. #define _A51_INTERNAL_ASSERT
  11. #endif
  12. typedef LONGLONG TFileOffset;
  13. #define MAX_HASH_LEN 32
  14. void* TempAlloc(DWORD dwLen);
  15. void TempFree(void* p, DWORD dwLen = 0);
  16. void* TempAlloc(CTempMemoryManager& Manager, DWORD dwLen);
  17. void TempFree(CTempMemoryManager& Manager, void* p, DWORD dwLen = 0);
  18. HRESULT A51TranslateErrorCode(long lRes);
  19. #define TEMPFREE_ME
  20. class CTempFreeMe
  21. {
  22. protected:
  23. void* m_p;
  24. DWORD m_dwLen;
  25. public:
  26. CTempFreeMe(void* p, DWORD dwLen = 0) : m_p(p), m_dwLen(dwLen){}
  27. ~CTempFreeMe() {TempFree(m_p, m_dwLen);}
  28. };
  29. HANDLE A51GetNewEvent();
  30. void A51ReturnEvent(HANDLE hEvent);
  31. class CReturnMe
  32. {
  33. protected:
  34. HANDLE m_h;
  35. public:
  36. CReturnMe(HANDLE h) : m_h(h){}
  37. ~CReturnMe() {A51ReturnEvent(m_h);}
  38. };
  39. inline void wbem_wcsupr(WCHAR* pwcTo, const WCHAR* pwcFrom)
  40. {
  41. while(*pwcFrom)
  42. {
  43. if(*pwcFrom >= 'a' && *pwcFrom <= 'z')
  44. *pwcTo = *pwcFrom + ('A'-'a');
  45. else if(*pwcFrom < 128)
  46. *pwcTo = *pwcFrom;
  47. else
  48. *pwcTo = towupper(*pwcFrom);
  49. pwcTo++;
  50. pwcFrom++;
  51. }
  52. *pwcTo = 0;
  53. }
  54. class CFileName
  55. {
  56. private:
  57. wchar_t *m_wszFilename;
  58. public:
  59. DWORD Length() { return MAX_PATH + 1; }
  60. CFileName() { m_wszFilename = (wchar_t*)TempAlloc(sizeof(wchar_t) * Length()); }
  61. ~CFileName() { TempFree(m_wszFilename, sizeof(wchar_t) * Length()); }
  62. operator wchar_t *() { return m_wszFilename; }
  63. };
  64. long EnsureDirectory(LPCWSTR wszPath, LPSECURITY_ATTRIBUTES pSA = NULL);
  65. long EnsureDirectoryForFile(LPCWSTR wszPath, LPSECURITY_ATTRIBUTES pSA = NULL);
  66. bool A51Hash(LPCWSTR wszName, LPWSTR wszHash);
  67. long A51DeleteFile(LPCWSTR wszFullPath);
  68. long A51WriteFile(LPCWSTR wszFullPath, DWORD dwLen, BYTE* pBuffer);
  69. long A51RemoveDirectory(LPCWSTR wszFullPath, bool bAbortOnFiles = true);
  70. long A51WriteToFileAsync(HANDLE hFile, long lStartingOffset, BYTE* pBuffer,
  71. DWORD dwBufferLen, OVERLAPPED* pov);
  72. long A51WriteToFileSync(HANDLE hFile, long lStartingOffset, BYTE* pBuffer,
  73. DWORD dwBufferLen);
  74. long A51ReadFromFileAsync(HANDLE hFile, long lStartingOffset, BYTE* pBuffer,
  75. DWORD dwBufferLen, OVERLAPPED* pov);
  76. long A51ReadFromFileSync(HANDLE hFile, long lStartingOffset, BYTE* pBuffer,
  77. DWORD dwBufferLen);
  78. extern __int64 g_nCurrentTime;
  79. #undef _ASSERT
  80. #ifdef _A51_INTERNAL_ASSERT
  81. #define _ASSERT(X, MSG) {if(!(X)) {A51TraceFlush(); DebugBreak();}}
  82. #else
  83. #define _ASSERT(X, MSG)
  84. #endif
  85. extern FILE* g_fLog;
  86. #ifdef _A51_INTERNAL_DEBUG
  87. #define A51TRACE(X) A51Trace X
  88. #else
  89. #define A51TRACE(X)
  90. #endif
  91. void A51Trace(LPCSTR szFormat, ...);
  92. void A51TraceFlush();
  93. template<class T>
  94. class CTempAllocator
  95. {
  96. public:
  97. typedef size_t size_type;
  98. typedef ptrdiff_t difference_type;
  99. typedef T *pointer;
  100. typedef const T *const_pointer;
  101. typedef T& reference;
  102. typedef const T& const_reference;
  103. typedef T value_type;
  104. char* _Charalloc(size_t n)
  105. {
  106. return (char*)TempAlloc(n);
  107. }
  108. void deallocate(void* p, size_t n)
  109. {
  110. TempFree(p, 0);
  111. }
  112. void construct(pointer p, const T& val)
  113. {
  114. new ((void*)p) T(val);
  115. }
  116. void destroy(pointer p)
  117. {
  118. p->T::~T();
  119. }
  120. };
  121. template<class T>
  122. class CPrivateTempAllocator
  123. {
  124. public:
  125. typedef size_t size_type;
  126. typedef ptrdiff_t difference_type;
  127. typedef T *pointer;
  128. typedef const T *const_pointer;
  129. typedef T& reference;
  130. typedef const T& const_reference;
  131. typedef T value_type;
  132. CPrivateTempAllocator(CTempMemoryManager* pManager)
  133. : m_pManager(pManager)
  134. {}
  135. char* _Charalloc(size_t n)
  136. {
  137. return (char*)m_pManager->Allocate(n);
  138. }
  139. void deallocate(void* p, size_t n)
  140. {
  141. m_pManager->Free(p, 0);
  142. }
  143. void construct(pointer p, const T& val)
  144. {
  145. new ((void*)p) T(val);
  146. }
  147. void destroy(pointer p)
  148. {
  149. p->T::~T();
  150. }
  151. protected:
  152. CTempMemoryManager* m_pManager;
  153. };
  154. #ifdef A51_CHECK_HRESULTS
  155. class CHR
  156. {
  157. protected:
  158. HRESULT m_hres;
  159. bool m_bTested;
  160. public:
  161. CHR(HRESULT hres) : m_hres(hres), m_bTested(false){}
  162. ~CHR() {_ASSERT(m_bTested, L"");}
  163. operator HRESULT() {m_bTested = true; return m_hres;}
  164. };
  165. #else
  166. typedef HRESULT CHR;
  167. #endif
  168. #endif