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.

155 lines
6.6 KiB

  1. // AttrStr.h : Declaration of the CMLStrAttrStrCommon
  2. #ifndef __ATTRSTR_H_
  3. #define __ATTRSTR_H_
  4. #include "mlatl.h"
  5. #include "mlstrbuf.h"
  6. /////////////////////////////////////////////////////////////////////////////
  7. // CMLStrAttrStrCommon
  8. class CMLStrAttrStrCommon
  9. {
  10. typedef HRESULT (CMLStrAttrStrCommon::*PFNUNLOCKPROC)(void* pKey, const void* pszSrc, long cchSrc, long* pcchActual, long* plActualLen);
  11. public:
  12. CMLStrAttrStrCommon(void);
  13. protected:
  14. class CLockInfo
  15. {
  16. protected:
  17. class CLockInfoEntry
  18. {
  19. public:
  20. void* m_psz;
  21. PFNUNLOCKPROC m_pfnUnlockProc;
  22. long m_lFlags;
  23. UINT m_uCodePage;
  24. long m_lPos;
  25. long m_lLen;
  26. long m_cchPos;
  27. long m_cchLen;
  28. };
  29. public:
  30. CLockInfo(CMLStrAttrStrCommon* pCommon) : m_pCommon(pCommon)
  31. {
  32. m_nLockCount = 0;
  33. m_pLockArray = NULL;
  34. }
  35. ~CLockInfo(void)
  36. {
  37. UnlockAll();
  38. }
  39. HRESULT UnlockAll(void);
  40. HRESULT StartLock(BOOL fWrite)
  41. {
  42. if (fWrite && !m_nLockCount)
  43. m_nLockCount = -1; // Negative means write lock
  44. else if (!fWrite && m_nLockCount >= 0)
  45. m_nLockCount++;
  46. else
  47. return MLSTR_E_ACCESSDENIED;
  48. return S_OK;
  49. }
  50. HRESULT EndLock(BOOL fWrite)
  51. {
  52. ASSERT(m_nLockCount);
  53. if (fWrite)
  54. m_nLockCount = 0;
  55. else
  56. m_nLockCount--;
  57. return S_OK;
  58. }
  59. HRESULT Lock(PFNUNLOCKPROC pfnUnlockProc, long lFlags, UINT uCodePage, void* psz, long lPos, long lLen, long cchPos, long cchLen);
  60. HRESULT Find(const void* psz, long cch, void** ppKey);
  61. HRESULT Unlock(void* pKey, const void* psz, long cch, long* pcchActual, long* plActualLen);
  62. long GetFlags(void* pKey) {return ((CLockInfoEntry*)pKey)->m_lFlags;}
  63. UINT GetCodePage(void* pKey) {return ((CLockInfoEntry*)pKey)->m_uCodePage;}
  64. long GetPos(void* pKey) {return ((CLockInfoEntry*)pKey)->m_lPos;}
  65. long GetLen(void* pKey) {return ((CLockInfoEntry*)pKey)->m_lLen;}
  66. long GetCChPos(void* pKey) {return ((CLockInfoEntry*)pKey)->m_cchPos;}
  67. long GetCChLen(void* pKey) {return ((CLockInfoEntry*)pKey)->m_cchLen;}
  68. protected:
  69. CMLStrAttrStrCommon* const m_pCommon;
  70. int m_nLockCount;
  71. CLockInfoEntry* m_pLockArray;
  72. };
  73. class CMLStrBufStandardW : public CMLStrBufW
  74. {
  75. protected:
  76. LPVOID MemAlloc(ULONG cb) {return ::CoTaskMemAlloc(cb);}
  77. LPVOID MemRealloc(LPVOID pv, ULONG cb) {return ::CoTaskMemRealloc(pv, cb);}
  78. void MemFree(LPVOID pv) {::CoTaskMemFree(pv);}
  79. long RoundBufSize(long cchStr);
  80. };
  81. public:
  82. class CLock
  83. {
  84. public:
  85. CLock(BOOL fWrite, CMLStrAttrStrCommon* pCommon, HRESULT& hr) : m_fWrite(fWrite), m_pCommon(pCommon) {m_fLocked = (SUCCEEDED(hr) && SUCCEEDED(hr = m_pCommon->GetLockInfo()->StartLock(m_fWrite)));}
  86. ~CLock(void) {if (m_fLocked) m_pCommon->GetLockInfo()->EndLock(m_fWrite);}
  87. HRESULT FallThrough(void) {m_fLocked = FALSE; return S_OK;} // Don't call EndLock in destructor
  88. protected:
  89. const BOOL m_fWrite;
  90. CMLStrAttrStrCommon* const m_pCommon;
  91. BOOL m_fLocked;
  92. };
  93. HRESULT PrepareMLStrBuf(void);
  94. HRESULT SetStrBufCommon(void* pMLStrX, long lDestPos, long lDestLen, UINT uCodePage, IMLangStringBufW* pSrcBufW, IMLangStringBufA* pSrcBufA, long* pcchActual, long* plActualLen);
  95. HRESULT UnlockStrCommon(const void* pszSrc, long cchSrc, long* pcchActual, long* plActualLen);
  96. HRESULT CheckThread(void) {return (m_dwThreadID == ::GetCurrentThreadId()) ? S_OK : E_FAIL;}
  97. HRESULT RegularizePosLen(long* plPos, long* plLen);
  98. HRESULT GetLen(long cchOffset, long cchLen, long* plLen);
  99. HRESULT GetCCh(long cchOffset, long lLen, long* pcchLen);
  100. static HRESULT CalcLenW(const WCHAR*, long cchLen, long* plLen) {if (plLen) *plLen = cchLen; return S_OK;}
  101. static HRESULT CalcLenA(UINT uCodePage, const CHAR*, long cchLen, long* plLen);
  102. static HRESULT CalcCChW(const WCHAR*, long lLen, long* pcchLen) {if (pcchLen) *pcchLen = lLen; return S_OK;}
  103. static HRESULT CalcCChA(UINT uCodePage, const CHAR*, long lLen, long* pcchLen);
  104. static HRESULT CalcBufSizeW(long lLen, long* pcchSize) {if (pcchSize) *pcchSize = lLen; return S_OK;}
  105. static HRESULT CalcBufSizeA(long lLen, long* pcchSize) {if (pcchSize) *pcchSize = lLen * 2; return S_OK;}
  106. static HRESULT ConvAStrToWStr(UINT uCodePage, const CHAR* pszSrc, long cchSrc, WCHAR* pszDest, long cchDest, long* pcchActualA, long* pcchActualW, long* plActualLen);
  107. static HRESULT ConvWStrToAStr(BOOL fCanStopAtMiddle, UINT uCodePage, const WCHAR* pszSrc, long cchSrc, CHAR* pszDest, long cchDest, long* pcchActualA, long* pcchActualW, long* plActualLen);
  108. IMLangStringBufW* GetMLStrBufW(void) const {return m_pMLStrBufW;}
  109. void SetMLStrBufW(IMLangStringBufW* pBuf) {m_pMLStrBufW = pBuf;}
  110. IMLangStringBufA* GetMLStrBufA(void) const {return m_pMLStrBufA;}
  111. void SetMLStrBufA(IMLangStringBufA* pBuf) {m_pMLStrBufA = pBuf;}
  112. UINT GetCodePage(void) const {return m_uCodePage;}
  113. void SetCodePage(UINT uCodePage) {m_uCodePage = uCodePage;}
  114. long GetBufFlags(void) const {return m_lBufFlags;}
  115. void SetBufFlags(long lBufFlags) {m_lBufFlags = lBufFlags;}
  116. long GetBufCCh(void) const {return m_cchBuf;}
  117. void SetBufCCh(long cchBuf) {m_cchBuf = cchBuf;}
  118. LCID GetLocale(void) const {return m_locale;}
  119. void SetLocale(LCID locale) {m_locale = locale;}
  120. CLockInfo* GetLockInfo(void) {return &m_LockInfo;}
  121. HRESULT MemAlloc(ULONG cb, void** ppv) {void* pv = ::CoTaskMemAlloc(cb); if (ppv) *ppv = pv; return (pv) ? S_OK : E_OUTOFMEMORY;}
  122. HRESULT MemFree(void* pv) {::CoTaskMemFree(pv); return S_OK;}
  123. HRESULT UnlockWStrDirect(void* pKey, const void* pszSrc, long cchSrc, long* pcchActual, long* plActualLen);
  124. HRESULT UnlockWStrIndirect(void* pKey, const void* pszSrc, long cchSrc, long* pcchActual, long* plActualLen);
  125. HRESULT UnlockAStrDirect(void* pKey, const void* pszSrc, long cchSrc, long* pcchActual, long* plActualLen);
  126. HRESULT UnlockAStrIndirect(void* pKey, const void* pszSrc, long cchSrc, long* pcchActual, long* plActualLen);
  127. protected:
  128. ~CMLStrAttrStrCommon(void);
  129. virtual IMLStrAttr* GetMLStrAttr(void) = 0;
  130. DWORD m_dwThreadID;
  131. IMLangStringBufW* m_pMLStrBufW;
  132. IMLangStringBufA* m_pMLStrBufA;
  133. UINT m_uCodePage;
  134. long m_lBufFlags;
  135. long m_cchBuf;
  136. LCID m_locale;
  137. CLockInfo m_LockInfo;
  138. };
  139. #endif //__ATTRSTR_H_