Leaked source code of windows server 2003
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.

192 lines
7.9 KiB

  1. #if !defined(_BCL_W32COMMON_H_INCLUDED_)
  2. #define _BCL_W32COMMON_H_INCLUDED_
  3. #pragma once
  4. #include <windows.h>
  5. #include <bcl_inlinestring.h>
  6. #include <bcl_unicodechartraits.h>
  7. namespace BCL
  8. {
  9. class CWin32CaseInsensitivityData
  10. {
  11. public:
  12. inline CWin32CaseInsensitivityData(LCID lcid = LOCALE_INVARIANT, DWORD dwCmpFlags = NORM_IGNORECASE) : m_lcid(lcid), m_dwCmpFlags(dwCmpFlags) { }
  13. inline CWin32CaseInsensitivityData(const CWin32CaseInsensitivityData &r) : m_lcid(r.m_lcid), m_dwCmpFlags(r.m_dwCmpFlags) { }
  14. inline CWin32CaseInsensitivityData &operator =(const CWin32CaseInsensitivityData &r) { m_lcid = r.m_lcid; m_dwCmpFlags = r.m_dwCmpFlags; return *this; }
  15. inline ~CWin32CaseInsensitivityData() { }
  16. static inline CWin32CaseInsensitivityData LocaleInvariant() { return CWin32CaseInsensitivityData(LOCALE_INVARIANT, NORM_IGNORECASE); }
  17. LCID m_lcid;
  18. DWORD m_dwCmpFlags;
  19. }; // class CWin32CaseInsensitivityData
  20. class CWin32StringComparisonResult
  21. {
  22. public:
  23. inline void SetLessThan() { m_iComparisonResult = CSTR_LESS_THAN; }
  24. inline bool IsLessThan() const { return m_iComparisonResult == CSTR_LESS_THAN; }
  25. inline static CWin32StringComparisonResult LessThan() { CWin32StringComparisonResult cr; cr.m_iComparisonResult = CSTR_LESS_THAN; return cr; }
  26. inline void SetEqualTo() { m_iComparisonResult = CSTR_EQUAL; }
  27. inline bool IsEqualTo() const { return m_iComparisonResult == CSTR_EQUAL; }
  28. inline static CWin32StringComparisonResult EqualTo() { CWin32StringComparisonResult cr; cr.m_iComparisonResult = CSTR_EQUAL; return cr; }
  29. inline void SetGreaterThan() { m_iComparisonResult = CSTR_GREATER_THAN; }
  30. inline bool IsGreaterThan() const { return m_iComparisonResult == CSTR_GREATER_THAN; }
  31. inline static CWin32StringComparisonResult GreaterThan() { CWin32StringComparisonResult cr; cr.m_iComparisonResult = CSTR_GREATER_THAN; return cr; }
  32. int m_iComparisonResult;
  33. };
  34. // For CWin32UnicodeToMBCSDataIn, think of the input parameters to WideCharToMultiByte
  35. class CWin32UnicodeToMBCSDataIn
  36. {
  37. public:
  38. UINT m_CodePage;
  39. DWORD m_dwFlags;
  40. };
  41. // For CWin32UnicodeToMBCSDataOut, think of the output parameters from WideCharToMultiByte
  42. class CWin32UnicodeToMBCSDataOut
  43. {
  44. public:
  45. PCSTR m_lpDefaultChar;
  46. LPBOOL m_lpUsedDefaultChar;
  47. };
  48. // For CWin32MBCSToUnicodeDataIn, think of the input parameters to MultiByteToWideChar
  49. class CWin32MBCSToUnicodeDataIn
  50. {
  51. public:
  52. UINT m_CodePage;
  53. DWORD m_dwFlags;
  54. };
  55. // For CWin32MBCSToUnicodeDataOut, think of the output parameters from MultiByteToWideChar
  56. class CWin32MBCSToUnicodeDataOut
  57. {
  58. public:
  59. // Nothing!
  60. };
  61. class CWin32StringComparisonResultOnExitHelper : public CWin32StringComparisonResult
  62. {
  63. public:
  64. inline CWin32StringComparisonResultOnExitHelper(int &riComparisonResult) : m_riComparisonResult(riComparisonResult) { }
  65. inline ~CWin32StringComparisonResultOnExitHelper() { m_riComparisonResult = m_iComparisonResult; }
  66. CWin32StringComparisonResultOnExitHelper& operator=(const CWin32StringComparisonResultOnExitHelper& o) { if (this != &o) { this->m_iComparisonResult = o.m_iComparisonResult; } return *this; }
  67. protected:
  68. int &m_riComparisonResult;
  69. };
  70. class CWin32CallDisposition
  71. {
  72. public:
  73. inline bool DidFail() const { return m_dwWin32Error != ERROR_SUCCESS; }
  74. inline bool DidSucceed() const { return m_dwWin32Error == ERROR_SUCCESS; }
  75. inline void SetSuccess() { m_dwWin32Error = ERROR_SUCCESS; }
  76. inline void SetArithmeticOverflow() { m_dwWin32Error = ERROR_ARITHMETIC_OVERFLOW; }
  77. inline void SetArithmeticUnderflow() { m_dwWin32Error = ERROR_ARITHMETIC_OVERFLOW; }
  78. inline void SetInternalError_ObjectLocked() { m_dwWin32Error = ERROR_INTERNAL_ERROR; }
  79. inline void SetInternalError_RuntimeCheck() { m_dwWin32Error = ERROR_INTERNAL_ERROR; }
  80. inline void SetInternalError_EpilogSkipped() { m_dwWin32Error = ERROR_INTERNAL_ERROR; }
  81. inline void SetBadParameter() { m_dwWin32Error = ERROR_INVALID_PARAMETER; }
  82. inline void SetBufferOverflow() { m_dwWin32Error = ERROR_BUFFER_OVERFLOW; }
  83. inline void SetOutOfMemory() { m_dwWin32Error = ERROR_OUTOFMEMORY; }
  84. inline static CWin32CallDisposition Success() { CWin32CallDisposition t; t.m_dwWin32Error = ERROR_SUCCESS; return t; }
  85. inline static CWin32CallDisposition ArithmeticOverflow() { CWin32CallDisposition t; t.m_dwWin32Error = ERROR_ARITHMETIC_OVERFLOW; return t; }
  86. inline static CWin32CallDisposition ArithmeticUnderflow() { CWin32CallDisposition t; t.m_dwWin32Error = ERROR_ARITHMETIC_OVERFLOW; return t; }
  87. inline static CWin32CallDisposition InternalError_ObjectLocked() { CWin32CallDisposition t; t.m_dwWin32Error = ERROR_INTERNAL_ERROR; return t; }
  88. inline static CWin32CallDisposition InternalError_RuntimeCheck() { CWin32CallDisposition t; t.m_dwWin32Error = ERROR_INTERNAL_ERROR; return t; }
  89. inline static CWin32CallDisposition InternalError_EpilogSkipped() { CWin32CallDisposition t; t.m_dwWin32Error = ERROR_INTERNAL_ERROR; return t; }
  90. inline static CWin32CallDisposition BadParameter() { CWin32CallDisposition t; t.m_dwWin32Error = ERROR_INVALID_PARAMETER; return t; }
  91. inline static CWin32CallDisposition BufferOverflow() { CWin32CallDisposition t; t.m_dwWin32Error = ERROR_BUFFER_OVERFLOW; return t; }
  92. inline static CWin32CallDisposition OutOfMemory() { CWin32CallDisposition t; t.m_dwWin32Error = ERROR_OUTOFMEMORY; return t; }
  93. inline static CWin32CallDisposition FromLastError() { CWin32CallDisposition t; t.m_dwWin32Error = ::GetLastError(); return t; }
  94. inline static CWin32CallDisposition FromWin32Error(DWORD dwWin32Error) { CWin32CallDisposition t; t.m_dwWin32Error = dwWin32Error; return t; }
  95. inline BOOL OnPublicReturn() const { if (m_dwWin32Error != ERROR_SUCCESS) ::SetLastError(m_dwWin32Error); return (m_dwWin32Error == ERROR_SUCCESS); }
  96. protected:
  97. DWORD m_dwWin32Error;
  98. };
  99. class CWin32PWSTRAllocationHelper
  100. {
  101. typedef CWin32CallDisposition TCallDisposition;
  102. public:
  103. CWin32PWSTRAllocationHelper() : m_pwstr(NULL) { }
  104. ~CWin32PWSTRAllocationHelper() { if (m_pwstr != NULL) { ::HeapFree(::GetProcessHeap(), 0, m_pwstr); m_pwstr = NULL; } }
  105. CWin32CallDisposition Allocate(SIZE_T cch)
  106. {
  107. BCL_MAYFAIL_PROLOG
  108. SIZE_T cb;
  109. if (m_pwstr != NULL)
  110. BCL_ORIGINATE_ERROR(CWin32CallDisposition::InternalError_RuntimeCheck());
  111. cb = cch * sizeof(WCHAR);
  112. if ((cb / sizeof(WCHAR)) != cch)
  113. BCL_ORIGINATE_ERROR(CWin32CallDisposition::ArithmeticOverflow());
  114. m_pwstr = reinterpret_cast<PWSTR>(::HeapAlloc(::GetProcessHeap(), 0, cb));
  115. if (m_pwstr == NULL)
  116. BCL_ORIGINATE_ERROR(CWin32CallDisposition::OutOfMemory());
  117. BCL_MAYFAIL_EPILOG_INTERNAL
  118. }
  119. PWSTR Detach() { PWSTR pszResult = m_pwstr; m_pwstr = NULL; return pszResult; }
  120. operator PWSTR() const { return m_pwstr; }
  121. private:
  122. PWSTR m_pwstr;
  123. };
  124. class CWin32PSTRAllocationHelper
  125. {
  126. typedef CWin32CallDisposition TCallDisposition;
  127. public:
  128. CWin32PSTRAllocationHelper() : m_pstr(NULL) { }
  129. ~CWin32PSTRAllocationHelper() { if (m_pstr != NULL) { ::HeapFree(::GetProcessHeap(), 0, m_pstr); m_pstr = NULL; } }
  130. CWin32CallDisposition Allocate(SIZE_T cch)
  131. {
  132. BCL_MAYFAIL_PROLOG
  133. if (m_pstr != NULL)
  134. BCL_ORIGINATE_ERROR(CWin32CallDisposition::InternalError_RuntimeCheck());
  135. m_pstr = reinterpret_cast<PSTR>(::HeapAlloc(::GetProcessHeap(), 0, cch));
  136. if (m_pstr == NULL)
  137. BCL_ORIGINATE_ERROR(CWin32CallDisposition::OutOfMemory());
  138. BCL_MAYFAIL_EPILOG_INTERNAL
  139. }
  140. PSTR Detach() { PSTR pszResult = m_pstr; m_pstr = NULL; return pszResult; }
  141. operator PSTR() const { return m_pstr; }
  142. private:
  143. PSTR m_pstr;
  144. };
  145. }; // namespace BCL
  146. #endif // !defined(_BCL_W32COMMON_H_INCLUDED_)