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.

159 lines
4.2 KiB

  1. //////////////////////////////////////////////////////////////////////////////
  2. //
  3. // Copyright (c) 1999-2000 Microsoft Corporation
  4. //
  5. // Module Name:
  6. // CStr.cpp
  7. //
  8. // Description:
  9. // Contains the definition of the CStr class.
  10. //
  11. // Documentation:
  12. // TODO: Add pointer to external documentation later.
  13. //
  14. // Maintained By:
  15. // Vij Vasu (Vvasu) 08-MAR-2000
  16. //
  17. //////////////////////////////////////////////////////////////////////////////
  18. //////////////////////////////////////////////////////////////////////////////
  19. // Include Files
  20. //////////////////////////////////////////////////////////////////////////////
  21. // The header file of this class
  22. #include "CStr.h"
  23. // For the exceptions thrown by CStr
  24. #include "CException.h"
  25. //////////////////////////////////////////////////////////////////////////////
  26. //++
  27. //
  28. // void
  29. // CStr::LoadString()
  30. //
  31. // Description:
  32. // Lookup a string in a string table using a string id.
  33. //
  34. // Arguments:
  35. // hInstIn
  36. // Instance handle of the module containing the string table resource.
  37. //
  38. // uiStringIdIn
  39. // Id of the string to look up
  40. //
  41. // Return Value:
  42. // None.
  43. //
  44. // Exceptions Thrown:
  45. // CException
  46. // If the lookup fails.
  47. //
  48. // Remarks:
  49. // This function cannot load a zero length string.
  50. //--
  51. //////////////////////////////////////////////////////////////////////////////
  52. void
  53. CStr::LoadString( HINSTANCE hInstIn, UINT nStringIdIn )
  54. {
  55. UINT uiCurrentSize = 0;
  56. TCHAR * pszCurrentString = NULL;
  57. UINT uiReturnedStringLen = 0;
  58. do
  59. {
  60. // Free the string allocated in the previous iteration.
  61. delete pszCurrentString;
  62. // Grow the current string by an arbitrary amount.
  63. uiCurrentSize += 256;
  64. pszCurrentString = new TCHAR[ uiCurrentSize ];
  65. if ( pszCurrentString == NULL )
  66. {
  67. THROW_EXCEPTION( E_OUTOFMEMORY );
  68. } // if: the memory allocation has failed
  69. uiReturnedStringLen = ::LoadString(
  70. hInstIn
  71. , nStringIdIn
  72. , pszCurrentString
  73. , uiCurrentSize
  74. );
  75. if ( uiReturnedStringLen == 0 )
  76. {
  77. HRESULT hrRetVal = TW32( GetLastError() );
  78. hrRetVal = HRESULT_FROM_WIN32( hrRetVal );
  79. delete pszCurrentString;
  80. THROW_EXCEPTION( hrRetVal );
  81. } // if: LoadString() had an error
  82. ++uiReturnedStringLen;
  83. }
  84. while( uiCurrentSize <= uiReturnedStringLen );
  85. // Free the existing string.
  86. Free();
  87. // Store details about the newly allocated string in the member variables.
  88. m_pszData = pszCurrentString;
  89. m_nLen = uiReturnedStringLen;
  90. m_cchBufferSize = uiCurrentSize;
  91. } //*** CStr::LoadString()
  92. //////////////////////////////////////////////////////////////////////////////
  93. //++
  94. //
  95. // void
  96. // CStr::AllocateBuffer( UINT cchBufferSizeIn )
  97. //
  98. // Description:
  99. // Allocate a buffer of cchBufferSizeIn characters. If the existing buffer is not
  100. // smaller than cchBufferSizeIn characters, nothing is done. Otherwise, a new
  101. // buffer is allocated and the old contents are filled into this buffer.
  102. //
  103. // Arguments:
  104. // cchBufferSizeIn
  105. // Required size of the new buffer in characters.
  106. //
  107. // Return Value:
  108. // None.
  109. //
  110. // Exceptions Thrown:
  111. // CException
  112. // If the memory allocation fails.
  113. //
  114. //--
  115. //////////////////////////////////////////////////////////////////////////////
  116. void
  117. CStr::AllocateBuffer( UINT cchBufferSizeIn )
  118. {
  119. // Check if the buffer is already big enough
  120. if ( m_cchBufferSize < cchBufferSizeIn )
  121. {
  122. TCHAR * psz = new TCHAR[ cchBufferSizeIn ];
  123. if ( psz == NULL )
  124. {
  125. THROW_EXCEPTION( E_OUTOFMEMORY );
  126. } // if: memory allocation failed
  127. // Copy the old data into the new buffer.
  128. _tcsncpy( psz, m_pszData, m_nLen + 1 );
  129. if ( m_pszData != &ms_chNull )
  130. {
  131. delete m_pszData;
  132. } // if: the pointer was dynamically allocated
  133. m_pszData = psz;
  134. m_cchBufferSize = cchBufferSizeIn;
  135. }
  136. } //*** AllocateBuffer()