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.

168 lines
4.0 KiB

  1. //+-------------------------------------------------------------------------
  2. //
  3. // Microsoft Windows
  4. //
  5. // Copyright (C) Microsoft Corporation, 1998 - 1999
  6. //
  7. // File: strings.cpp
  8. //
  9. //--------------------------------------------------------------------------
  10. /*----------------------------------------------------------------------------
  11. / Title;
  12. / strings.cpp
  13. /
  14. / Authors;
  15. / Rick Turner (ricktu)
  16. /
  17. / Notes;
  18. / Useful string manipulation functions.
  19. /----------------------------------------------------------------------------*/
  20. #include "precomp.hxx"
  21. #pragma hdrstop
  22. /*-----------------------------------------------------------------------------
  23. / LocalAllocString
  24. / ------------------
  25. / Allocate a string, and initialize it with the specified contents.
  26. /
  27. / In:
  28. / ppResult -> recieves pointer to the new string
  29. / pString -> string to initialize with
  30. /
  31. / Out:
  32. / HRESULT
  33. /----------------------------------------------------------------------------*/
  34. HRESULT LocalAllocString(LPTSTR* ppResult, LPCTSTR pString)
  35. {
  36. HRESULT hr;
  37. TraceEnter(TRACE_COMMON_STR, "LocalAllocString");
  38. TraceAssert(ppResult);
  39. TraceAssert(pString);
  40. if ( !ppResult || !pString )
  41. ExitGracefully(hr, E_INVALIDARG, "Bad arguments");
  42. *ppResult = (LPTSTR)LocalAlloc(LPTR, StringByteSize(pString) );
  43. if ( !*ppResult )
  44. ExitGracefully(hr, E_OUTOFMEMORY, "Failed to allocate buffer");
  45. lstrcpy(*ppResult, pString);
  46. hr = S_OK; // success
  47. exit_gracefully:
  48. TraceLeaveResult(hr);
  49. }
  50. /*----------------------------------------------------------------------------
  51. / LocalAllocStringLen
  52. / ---------------------
  53. / Given a length return a buffer of that size.
  54. /
  55. / In:
  56. / ppResult -> receives the pointer to the string
  57. / cLen = length in characters to allocate
  58. /
  59. / Out:
  60. / HRESULT
  61. /----------------------------------------------------------------------------*/
  62. HRESULT LocalAllocStringLen(LPTSTR* ppResult, UINT cLen)
  63. {
  64. HRESULT hr;
  65. TraceEnter(TRACE_COMMON_STR, "LocalAllocStringLen");
  66. TraceAssert(ppResult);
  67. if ( !ppResult || cLen == 0 )
  68. ExitGracefully(hr, E_INVALIDARG, "Bad arguments (length or buffer)");
  69. *ppResult = (LPTSTR)LocalAlloc(LPTR, (cLen+1) * SIZEOF(TCHAR));
  70. hr = *ppResult ? S_OK:E_OUTOFMEMORY;
  71. exit_gracefully:
  72. TraceLeaveResult(hr);
  73. }
  74. /*-----------------------------------------------------------------------------
  75. / LocalFreeString
  76. / -----------------
  77. / Release the string pointed to be *ppString (which can be null) and
  78. / then reset the pointer back to NULL.
  79. /
  80. / In:
  81. / ppString -> pointer to string pointer to be free'd
  82. /
  83. / Out:
  84. / -
  85. /----------------------------------------------------------------------------*/
  86. void LocalFreeString(LPTSTR* ppString)
  87. {
  88. TraceEnter(TRACE_COMMON_STR, "LocalFreeString");
  89. TraceAssert(ppString);
  90. if ( ppString )
  91. {
  92. if ( *ppString )
  93. LocalFree((HLOCAL)*ppString);
  94. *ppString = NULL;
  95. }
  96. TraceLeave();
  97. }
  98. /*-----------------------------------------------------------------------------
  99. / StrRetFromString
  100. / -----------------
  101. / Package a WIDE string into a LPSTRRET structure.
  102. /
  103. / In:
  104. / pStrRet -> receieves the newly allocate string
  105. / pString -> string to be copied.
  106. /
  107. / Out:
  108. / -
  109. /----------------------------------------------------------------------------*/
  110. HRESULT StrRetFromString(LPSTRRET lpStrRet, LPCWSTR pString)
  111. {
  112. HRESULT hr = S_OK;
  113. TraceEnter(TRACE_COMMON_STR, "StrRetFromString");
  114. Trace(TEXT("pStrRet %08x, lpszString -%ls-"), lpStrRet, pString);
  115. TraceAssert(lpStrRet);
  116. TraceAssert(pString);
  117. if (!lpStrRet || !pString)
  118. {
  119. hr = E_INVALIDARG;
  120. }
  121. else
  122. {
  123. lpStrRet->pOleStr = reinterpret_cast<LPWSTR>(SHAlloc((wcslen(pString)+1)*sizeof(WCHAR)));
  124. if ( !(lpStrRet->pOleStr) )
  125. {
  126. hr = E_OUTOFMEMORY;
  127. }
  128. else
  129. {
  130. lpStrRet->uType = STRRET_WSTR;
  131. wcscpy(lpStrRet->pOleStr, pString);
  132. }
  133. }
  134. TraceLeaveResult(hr);
  135. }