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.

165 lines
3.8 KiB

  1. /*++
  2. Copyright (c) 1999 Microsoft Corporation
  3. Module Name:
  4. vs_str.hxx
  5. Abstract:
  6. Various defines for general usage
  7. Author:
  8. Adi Oltean [aoltean] 07/09/1999
  9. Revision History:
  10. Name Date Comments
  11. aoltean 07/09/1999 Created
  12. aoltean 08/11/1999 Adding throw specification
  13. aoltean 09/03/1999 Adding DuplicateXXX functions
  14. aoltean 09/09/1999 dss -> vss
  15. aoltean 09/20/1999 Adding ThrowIf, Err, Msg, MsgNoCR, etc.
  16. --*/
  17. #ifndef __VSS_STR_HXX__
  18. #define __VSS_STR_HXX__
  19. #if _MSC_VER > 1000
  20. #pragma once
  21. #endif
  22. ////////////////////////////////////////////////////////////////////////
  23. // Standard foo for file name aliasing. This code block must be after
  24. // all includes of VSS header files.
  25. //
  26. #ifdef VSS_FILE_ALIAS
  27. #undef VSS_FILE_ALIAS
  28. #endif
  29. #define VSS_FILE_ALIAS "INCSTRH"
  30. //
  31. ////////////////////////////////////////////////////////////////////////
  32. /////////////////////////////////////////////////////////////////////////////
  33. // Misc string routines
  34. inline void VssDuplicateStr(
  35. OUT LPWSTR& pwszDestination,
  36. IN LPCWSTR pwszSource
  37. )
  38. {
  39. BS_ASSERT(pwszDestination == NULL);
  40. if (pwszSource == NULL)
  41. pwszDestination = NULL;
  42. else
  43. {
  44. pwszDestination = (LPWSTR)::CoTaskMemAlloc(sizeof(WCHAR)*(1 + ::wcslen(pwszSource)));
  45. if (pwszDestination != NULL)
  46. ::wcscpy(pwszDestination, pwszSource);
  47. }
  48. }
  49. inline void VssSafeDuplicateStr(
  50. IN CVssFunctionTracer& ft,
  51. OUT LPWSTR& pwszDestination,
  52. IN LPCWSTR pwszSource
  53. ) throw(HRESULT)
  54. {
  55. BS_ASSERT(pwszDestination == NULL);
  56. if (pwszSource == NULL)
  57. pwszDestination = NULL;
  58. else
  59. {
  60. pwszDestination = (LPWSTR)::CoTaskMemAlloc(sizeof(WCHAR)*(1 + ::wcslen(pwszSource)));
  61. if (pwszDestination == NULL)
  62. ft.Throw( VSSDBG_GEN, E_OUTOFMEMORY, L"Memory allocation error");
  63. ::wcscpy(pwszDestination, pwszSource);
  64. }
  65. }
  66. inline LPWSTR VssAllocString(
  67. IN CVssFunctionTracer& ft,
  68. IN size_t nStringLength
  69. )
  70. {
  71. BS_ASSERT(nStringLength > 0);
  72. LPWSTR pwszStr = reinterpret_cast<LPWSTR>(::CoTaskMemAlloc((nStringLength + 1) * sizeof(WCHAR)));
  73. if (pwszStr == NULL)
  74. ft.Throw( VSSDBG_GEN, E_OUTOFMEMORY, L"Memory allocation error");
  75. return pwszStr;
  76. }
  77. inline void VssFreeString(
  78. IN OUT LPCWSTR& pwszString
  79. )
  80. {
  81. ::CoTaskMemFree(const_cast<LPWSTR>(pwszString));
  82. pwszString = NULL;
  83. }
  84. inline LPWSTR VssReallocString(
  85. IN CVssFunctionTracer& ft,
  86. IN LPWSTR pwszString,
  87. IN LONG lNewStrLen // Without the zero character.
  88. )
  89. {
  90. LPWSTR pwszNewString = (LPWSTR)::CoTaskMemRealloc( (PVOID)(pwszString),
  91. sizeof(WCHAR) * (lNewStrLen + 1));
  92. if (pwszNewString == NULL)
  93. ft.Throw( VSSDBG_GEN, E_OUTOFMEMORY, L"Memory allocation error");
  94. return pwszNewString;
  95. }
  96. inline bool VssIsStrEqual(
  97. IN LPCWSTR wsz1,
  98. IN LPCWSTR wsz2
  99. )
  100. {
  101. if ((wsz1 == NULL) && (wsz2 == NULL))
  102. return true;
  103. if (wsz1 && wsz2 && (::wcscmp(wsz1, wsz2) == 0) )
  104. return true;
  105. return false;
  106. }
  107. inline void VssConcatenate(
  108. IN CVssFunctionTracer& ft,
  109. IN OUT LPWSTR pwszDestination,
  110. IN size_t nDestinationLength,
  111. IN LPCWSTR wszSource1,
  112. IN LPCWSTR wszSource2
  113. )
  114. {
  115. BS_ASSERT(pwszDestination);
  116. BS_ASSERT(wszSource1);
  117. BS_ASSERT(wszSource2);
  118. BS_ASSERT(nDestinationLength > 0);
  119. // Check if the buffer is passed
  120. if (::wcslen(wszSource1) + ::wcslen(wszSource2) > nDestinationLength ) {
  121. BS_ASSERT(false);
  122. ft.Throw( VSSDBG_SWPRV, E_UNEXPECTED,
  123. L"Buffer not large enough. ( %d + %d > %d )",
  124. ::wcslen(wszSource1), ::wcslen(wszSource2), nDestinationLength);
  125. }
  126. ::wcscpy(pwszDestination, wszSource1);
  127. ::wcscat(pwszDestination, wszSource2);
  128. }
  129. #endif // __VSS_STR_HXX__