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.

163 lines
4.5 KiB

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