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.

134 lines
3.6 KiB

  1. //-----------------------------------------------------------------------------
  2. //
  3. //
  4. // File: refstr.cpp
  5. //
  6. // Description: Implementation of CRefCountedString and helper functions.
  7. //
  8. // Author: Mike Swafford (MikeSwa)
  9. //
  10. // History:
  11. // 11/11/98 - MikeSwa Created
  12. //
  13. // Copyright (C) 1998 Microsoft Corporation
  14. //
  15. //-----------------------------------------------------------------------------
  16. #include "aqprecmp.h"
  17. #include "refstr.h"
  18. //---[ CRefCountedString ]-----------------------------------------------------
  19. //
  20. //
  21. // Description:
  22. // Initializes a ref-counted string to the given string.
  23. // Parameters:
  24. // szStr String to initialize to
  25. // cbStrlen Length of string to initialize to
  26. // Returns:
  27. // TRUE on success
  28. // FALSE if required memory could not be allocated.
  29. // History:
  30. // 11/11/98 - MikeSwa Created
  31. //
  32. //-----------------------------------------------------------------------------
  33. BOOL CRefCountedString::fInit(LPSTR szStr, DWORD cbStrlen)
  34. {
  35. _ASSERT(CREFSTR_SIG_VALID == m_dwSignature);
  36. //We allow init of an empty string
  37. if (!cbStrlen || !szStr)
  38. {
  39. m_cbStrlen = 0;
  40. m_szStr = NULL;
  41. return TRUE;
  42. }
  43. _ASSERT(szStr);
  44. _ASSERT(cbStrlen);
  45. m_cbStrlen = cbStrlen;
  46. m_szStr = (LPSTR) pvMalloc(sizeof(CHAR) * (cbStrlen+1));
  47. if (!m_szStr)
  48. return FALSE;
  49. memcpy(m_szStr, szStr, cbStrlen);
  50. m_szStr[cbStrlen] = '\0';
  51. return TRUE;
  52. }
  53. //---[ HrUpdateRefCountedString ]----------------------------------------------
  54. //
  55. //
  56. // Description:
  57. // Function to update a ref-counted string. Typically used to update
  58. // config strings.
  59. // Parameters:
  60. // pprstrCurrent Ptr to ptr to string. Will be replaced with
  61. // an updated version if neccessary.
  62. // szNew The new string.
  63. // Returns:
  64. // S_OK on success
  65. // E_OUTOFMEMORY if we could not allocate the memory required to handle
  66. // this.
  67. // History:
  68. // 11/9/98 - MikeSwa Created
  69. //
  70. //-----------------------------------------------------------------------------
  71. HRESULT HrUpdateRefCountedString(CRefCountedString **pprstrCurrent, LPSTR szNew)
  72. {
  73. _ASSERT(pprstrCurrent);
  74. HRESULT hr = S_OK;
  75. DWORD cbStrLen = 0;
  76. CRefCountedString *prstrNew = *pprstrCurrent;
  77. CRefCountedString *prstrCurrent = *pprstrCurrent;
  78. if (!szNew)
  79. prstrNew = NULL; //we don't want to do a strcmp here
  80. else
  81. cbStrLen = lstrlen(szNew);
  82. if (prstrNew)
  83. {
  84. //First free up old info... if different
  85. if (!prstrCurrent->szStr() ||
  86. lstrcmp(prstrCurrent->szStr(), szNew))
  87. {
  88. //strings are different... blow away old info
  89. prstrNew = NULL;
  90. }
  91. }
  92. //Check if either old string is different, or there was no old string
  93. if (!prstrNew)
  94. {
  95. //only update and allocate if changed
  96. prstrNew = new CRefCountedString();
  97. if (prstrNew)
  98. {
  99. if (!prstrNew->fInit(szNew, cbStrLen))
  100. {
  101. prstrNew->Release();
  102. prstrNew = NULL;
  103. }
  104. }
  105. if (!prstrNew)
  106. {
  107. //We ran into some failure
  108. hr = E_OUTOFMEMORY;
  109. }
  110. else //release old value & save New
  111. {
  112. if (prstrCurrent)
  113. {
  114. prstrCurrent->Release();
  115. prstrCurrent = NULL;
  116. }
  117. *pprstrCurrent = prstrNew;
  118. }
  119. }
  120. return hr;
  121. }