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.

200 lines
9.1 KiB

  1. /*++
  2. Copyright (c) 2000 Microsoft Corporation
  3. Module Name:
  4. stringclass.h
  5. Abstract:
  6. Fusion string class, layered on the Fusion string buffer class.
  7. Author:
  8. mgrier (Michael Grier) 6/27/2000
  9. Revision History:
  10. --*/
  11. #if !defined(_FUSION_INC_STRINGCLASS_H_INCLUDED_)
  12. #define _FUSION_INC_STRINGCLASS_H_INCLUDED_
  13. #pragma once
  14. #include "fusionbuffer.h"
  15. #if defined(_UNICODE) || defined(UNICODE)
  16. typedef CUnicodeHeapStringBuffer CStringValueBase;
  17. #else
  18. typedef CANSIHeapStringBuffer CStringValueBase;
  19. #endif // defined(_UNICODE) || defined(UNICODE)
  20. class CReadOnlyStringAccessor;
  21. class CString;
  22. class CStringValue : protected CStringValueBase
  23. {
  24. friend CReadOnlyStringAccessor;
  25. friend CString;
  26. protected:
  27. typedef CStringValueBase Base;
  28. typedef Base::THeap THeap;
  29. typedef Base::TChar TChar;
  30. typedef Base::TConstantString TConstantString;
  31. CStringValue(THeap hHeap) : Base(hHeap), m_cch(0), m_cRef(1) { }
  32. void AddRef() { ::InterlockedIncrement(&m_cRef); }
  33. void Release() { if (::InterlockedDecrement(&m_cRef) == 0) { CSxsPreserveLastError ple; delete this; ple.Restore(); } }
  34. BOOL Win32Initialize(TConstantString psz, SIZE_T cch) { if (this->Win32Assign(psz, cch)) { m_cch = Base::Cch(); return TRUE; } return FALSE; }
  35. HRESULT ComInitialize(TConstantString psz, SIZE_T cch);
  36. private:
  37. ULONG m_cch;
  38. LONG m_cRef;
  39. };
  40. class CString
  41. {
  42. public:
  43. typedef CStringValue::THeap THeap;
  44. typedef CStringValue::TChar TChar;
  45. typedef CStringValue::TConstantString TConstantString;
  46. BOOL Win32Assign(const CString &r) { if (Base::Win32Assign(r, r.m_cch)) { m_cch = r.m_cch; return TRUE; } return FALSE; }
  47. HRESULT ComAssign(const CString &r) { HRESULT hr = Base::Assign(r, r.m_cch); if (SUCCEEDED(hr)) { m_cch = r.m_cch; hr = NOERROR; } return hr; }
  48. BOOL Win32Assign(PCWSTR s, SIZE_T cchIn = -1) { SIZE_T cch; if (Base::Win32Assign(s, cchIn, &cch)) { m_cch = cch; return TRUE; } return FALSE; }
  49. HRESULT ComAssign(PCWSTR s, SIZE_T cchIn = -1) { SIZE_T cch; HRESULT hr; if (SUCCEEDED(hr = Base::Assign(s, cchIn, &cch))) { m_cch = cch; hr = NOERROR; } return hr; }
  50. BOOL Win32Assign(PCSTR s, SIZE_T cchIn = -1) { SIZE_T cch; if (Base::Win32Assign(s, cchIn, &cch)) { m_cch = cch; return TRUE; } return FALSE; }
  51. HRESULT ComAssign(PCSTR s, SIZE_T cchIn = -1) { SIZE_T cch; HRESULT hr; if (SUCCEEDED(hr = Base::Assign(s, cchIn, &cch))) { m_cch = cch; hr = NOERROR; } return hr; }
  52. BOOL Win32AssignW(SIZE_T cStrings, ...) { va_list ap; va_start(ap, cStrings); BOOL fSuccess = this->Win32AssignWVa(cStrings, ap); va_end(ap); return fSuccess; }
  53. HRESULT ComAssignW(SIZE_T cStrings, ...) { va_list ap; va_start(ap, cStrings); HRESULT hr = this->ComAssignWVa(cStrings, ap); va_end(ap); return hr; }
  54. BOOL Win32AssignWVa(SIZE_T cStrings, va_list ap) { SIZE_T cch; if (Base::Win32AssignWVa(cStrings, &cch, ap)) { m_cch = cch; return TRUE; } return FALSE; }
  55. HRESULT ComAssignWVa(SIZE_T cStrings, va_list ap) { SIZE_T cch; HRESULT hr; if (SUCCEEDED(hr = Base::AssignWVa(cStrings, &cch, ap))) { m_cch = cch; hr = NOERROR; } return hr; }
  56. BOOL Win32Append(const CString &r) { return this->Win32Append(r, r.m_cch); }
  57. HRESULT ComAppend(const CString &r) { return this->ComAppend(r, r.m_cch); }
  58. BOOL Win32Append(PCWSTR s, SIZE_T cchIn = -1) { SIZE_T cch; if (Base::Win32Append(s, cchIn, &cch)) { m_cch = cch; return TRUE; } return FALSE; }
  59. HRESULT ComAppend(PCWSTR s, SIZE_T cchIn = -1) { SIZE_T cch; HRESULT hr; if (SUCCEEDED(hr = Base::Append(s, cchIn, &cch))) { m_cch = cch; hr = NOERROR; } return hr; }
  60. BOOL Win32Append(PCSTR s, SIZE_T cchIn = -1) { SIZE_T cch; if (Base::Win32Append(s, cchIn, &cch)) { m_cch = cch; return TRUE; } return FALSE; }
  61. HRESULT ComAppend(PCSTR s, SIZE_T cchIn = -1) { SIZE_T cch; HRESULT hr; if (SUCCEEDED(hr = Base::Append(s, cchIn, &cch))) { m_cch = cch; hr = NOERROR; } return hr; }
  62. inline BOOL Win32FormatFileTime(const FILETIME &rft);
  63. inline HRESULT ComFormatFileTime(const FILETIME &rft);
  64. inline BOOL Win32FormatFileTime(const LARGE_INTEGER &rli) { return this->Win32FormatFileTime(reinterpret_cast<const FILETIME &>(rli)); }
  65. SIZE_T Cch() const { return m_cch; }
  66. SIZE_T Cb() const { return m_cch * sizeof(TChar); }
  67. //
  68. // This function is rather special purpose in that several design choices are not
  69. // implemented as parameters. In particular, the pcbBytesWritten is assumed to
  70. // accumulate a number (thus it's updated by adding the number of bytes written to
  71. // it rather than just setting it to the count of bytes written).
  72. //
  73. // It also writes 0 bytes into the buffer is the string is zero length; if the string
  74. // is not zero length, it writes the string including a trailing null.
  75. //
  76. inline BOOL Win32CopyIntoBuffer(
  77. PWSTR *ppszCursor,
  78. SIZE_T cbBuffer,
  79. SIZE_T *pcbBytesWritten,
  80. PVOID pvBase,
  81. ULONG *pulOffset,
  82. ULONG *pulLength
  83. );
  84. //
  85. // String-as-path-name helpers
  86. //
  87. SIZE_T CchWithoutLastPathElement() const { return Base::CchWithoutLastPathElement(); }
  88. VOID RemoveLastPathElement() { Base::RemoveLastPathElement(); }
  89. VOID RemoveTrailingPathSeparators() { Base::RemoveTrailingPathSeparators(); }
  90. BOOL Win32AppendPathElement(const CString &r) { return this->Win32AppendPathElement(r, r.m_cch); }
  91. HRESULT ComAppendPathElement(const CString &r) { return this->ComAppendPathElement(r, r.m_cch); }
  92. BOOL Win32AppendPathElement(PCWSTR s, SIZE_T cchIn = -1) { SIZE_T cch; if (Base::Win32AppendPathElement(s, cchIn, &cch)) { m_cch = cch; return TRUE; } return FALSE; }
  93. HRESULT ComAppendPathElement(PCWSTR s, SIZE_T cchIn = -1) { SIZE_T cch; HRESULT hr; if (SUCCEEDED(hr = Base::ComAppendPathElement(s, cchIn, &cch))) { m_cch = cch; hr = NOERROR; } return hr; }
  94. BOOL Win32AppendPathElement(PCSTR s, SIZE_T cchIn = -1) { SIZE_T cch; if (Base::Win32AppendPathElement(s, cchIn, &cch)) { m_cch = cch; return TRUE; } return FALSE; }
  95. HRESULT ComAppendPathElement(PCSTR s, SIZE_T cchIn = -1) { SIZE_T cch; HRESULT hr; if (SUCCEEDED(hr = Base::ComAppendPathElement(s, cchIn, &cch))) { m_cch = cch; hr = NOERROR; } return hr; }
  96. BOOL Win32ChangePathExtension(PCWSTR s, SIZE_T cchIn, EIfNoExtension e) { SIZE_T cch; if (Base::Win32ChangePathExtension(s, cchIn, &cch, e)) { m_cch = cch; return TRUE; } return FALSE; }
  97. // uncomment this after Base::ComChangePathExtension is merged with Base::Win32ChangePathExtension
  98. //HRESULT ComChangePathExtension(PCWSTR s, SIZE_T cchIn, EIfNoExtension e) { SIZE_T cch; HRESULT hr; if (SUCCEEDED(hr = Base::ComChangePathExtension(s, cchIn, &cch, e))) { m_cch = cch; hr = NOERROR; } return hr; }
  99. //
  100. // Win32 helpers
  101. //
  102. inline HINSTANCE Win32LoadLibrary() const { return ::LoadLibrary(*this); }
  103. inline HINSTANCE Win32LoadLibraryEx(HANDLE hFile, DWORD dwFlags) const { ASSERT(hFile == NULL); return ::LoadLibraryEx(*this, hFile, dwFlags); }
  104. inline BOOL Win32GetFileSize(ULARGE_INTEGER &ruli) const;
  105. inline BOOL Win32CopyFileTo(const CString &r, BOOL fFailIfDestinationExists) const { return ::CopyFile(*this, r, fFailIfDestinationExists); }
  106. inline BOOL Win32CopyFileFrom(const CString &r, BOOL fFailIfDestinationExists) const { return ::CopyFile(r, *this, fFailIfDestinationExists); }
  107. inline BOOL Win32CopyFileTo(PCWSTR s, BOOL fFailIfDestinationExists) const;
  108. inline BOOL Win32CopyFileFrom(PCWSTR s, BOOL fFailIfDestinationExists) const;
  109. inline BOOL Win32CopyFileTo(PCSTR s, BOOL fFailIfDestinationExists) const;
  110. inline BOOL Win32CopyFileFrom(PCSTR s, BOOL fFailIfDestinationExists) const;
  111. inline BOOL Win32MoveFileExTo(const CString &r, DWORD dwFlags) const { return ::MoveFileEx(*this, r, dwFlags); }
  112. inline BOOL Win32MoveFileExFrom(const CString &r, DWORD dwFlags) const { return ::MoveFileEx(r, *this, dwFlags); }
  113. inline BOOL Win32MoveFileExTo(PCWSTR s, DWORD dwFlags) const;
  114. inline BOOL Win32MoveFileExFrom(PCWSTR s, DWORD dwFlags) const;
  115. inline BOOL Win32MoveFileExTo(PCSTR s, DWORD dwFlags) const;
  116. inline BOOL Win32MoveFileExFrom(PCSTR s, DWORD dwFlags) const;
  117. inline BOOL Win32CreateDirectory(LPSECURITY_ATTRIBUTES lpSecurityAttributes) const { return ::CreateDirectory(*this, lpSecurityAttributes); }
  118. protected:
  119. SIZE_T m_cch; // this is the real live number of characters in the buffer prior to the trailing NULL.
  120. operator TConstantString() const { return Base::operator TConstantString(); }
  121. private:
  122. // Intentionally not implemented:
  123. CString(const CString &r);
  124. void operator =(const CString &r);
  125. };
  126. class CReadOnlyStringAccessor
  127. {
  128. public:
  129. typedef CString::TChar TChar;
  130. typedef CString::TConstantString TConstantString;
  131. CReadOnlyStringAccessor(const CString &rstr) : m_rstr(rstr) { }
  132. operator TConstantString() const { return static_cast<TConstantString>(m_rstr); }
  133. void GetParts(TConstantString &rpsz, SIZE_T &rcch) const { rpsz = static_cast<TConstantString>(m_rstr); rcch = m_rstr.m_cch; }
  134. SIZE_T Cch() const { return m_rstr.Cch(); }
  135. protected:
  136. const CString &m_rstr;
  137. };
  138. #include "stringclass.inl"
  139. #endif // defined(_FUSION_INC_STRINGCLASS_H_INCLUDED_)