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.

154 lines
3.6 KiB

  1. //////////////////////////////////////////////////////////////////////////////
  2. //
  3. // Copyright (c) 1999-2002 Microsoft Corporation
  4. //
  5. // Module Name:
  6. // CBString.cpp
  7. //
  8. // Description:
  9. // Contains the definition of the BString class.
  10. //
  11. // Maintained By:
  12. // John Franco (jfranco) 17-APR-2002
  13. //
  14. //////////////////////////////////////////////////////////////////////////////
  15. //////////////////////////////////////////////////////////////////////////////
  16. // Include Files
  17. //////////////////////////////////////////////////////////////////////////////
  18. #include "Pch.h"
  19. #include "CBString.h"
  20. // For the exceptions thrown by CBString
  21. #include "CException.h"
  22. //////////////////////////////////////////////////////////////////////////////
  23. //++
  24. //
  25. // CBString::AllocateBuffer
  26. //
  27. // Description:
  28. // Given a character count, make a BSTR sized to hold that many
  29. // characters (NOT including terminating null).
  30. // If the count is zero, return null.
  31. //
  32. // Arguments:
  33. // cchIn
  34. // Character count.
  35. //
  36. // Return Values:
  37. // Newly allocated BSTR, or null.
  38. //
  39. // Exceptions Thrown:
  40. // CException
  41. // If the memory allocation fails.
  42. //
  43. //--
  44. //////////////////////////////////////////////////////////////////////////////
  45. BSTR
  46. CBString::AllocateBuffer( UINT cchIn )
  47. {
  48. TraceFunc1( "cchIn == %d", cchIn );
  49. BSTR bstr = NULL;
  50. if ( cchIn > 0 )
  51. {
  52. bstr = TraceSysAllocStringLen( NULL, cchIn );
  53. if ( bstr == NULL )
  54. {
  55. THROW_EXCEPTION( E_OUTOFMEMORY );
  56. }
  57. } // if: non-zero size specified
  58. RETURN( bstr );
  59. } //*** CBString::AllocateBuffer
  60. //////////////////////////////////////////////////////////////////////////////
  61. //++
  62. //
  63. // CBString::CopyString
  64. //
  65. // Description:
  66. // Given a null-terminated unicode string, return a BSTR copy of it.
  67. // If the argument is null, return null.
  68. //
  69. // Arguments:
  70. // pcwszIn - original string.
  71. //
  72. // Return Value:
  73. // Newly allocated BSTR, or null.
  74. //
  75. // Exceptions Thrown:
  76. // CException
  77. // If the memory allocation fails.
  78. //
  79. //--
  80. //////////////////////////////////////////////////////////////////////////////
  81. BSTR
  82. CBString::CopyString( PCWSTR pcwszIn )
  83. {
  84. TraceFunc1( "pcwszIn = '%ws'", pcwszIn == NULL ? L"<NULL>" : pcwszIn );
  85. BSTR bstr = NULL;
  86. if ( pcwszIn != NULL )
  87. {
  88. bstr = TraceSysAllocString( pcwszIn );
  89. if ( bstr == NULL )
  90. {
  91. THROW_EXCEPTION( E_OUTOFMEMORY );
  92. }
  93. } // if: non-NULL string pointer specified
  94. RETURN( bstr );
  95. } //*** CBString::CopyString
  96. //////////////////////////////////////////////////////////////////////////////
  97. //++
  98. //
  99. // CBString::CopyBSTR
  100. //
  101. // Description:
  102. // Given a BSTR, return a BSTR copy of it.
  103. // If the argument is null, return null.
  104. //
  105. // Arguments:
  106. // bstrIn - original string.
  107. //
  108. // Return Value:
  109. // Newly allocated BSTR, or null.
  110. //
  111. // Exceptions Thrown:
  112. // CException
  113. // If the memory allocation fails.
  114. //
  115. //--
  116. //////////////////////////////////////////////////////////////////////////////
  117. BSTR
  118. CBString::CopyBSTR( BSTR bstrIn )
  119. {
  120. TraceFunc1( "bstrIn = '%ws'", bstrIn == NULL ? L"<NULL>" : bstrIn );
  121. BSTR bstr = NULL;
  122. if ( bstrIn != NULL )
  123. {
  124. bstr = TraceSysAllocString( bstrIn );
  125. if ( bstr == NULL )
  126. {
  127. THROW_EXCEPTION( E_OUTOFMEMORY );
  128. }
  129. } // if: non-NULL BSTR specified
  130. RETURN( bstr );
  131. } //*** CBString::CopyBSTR