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.

142 lines
2.9 KiB

  1. /*==========================================================================
  2. *
  3. * Copyright (C) 2000 Microsoft Corporation. All Rights Reserved.
  4. *
  5. * File: strcache.cpp
  6. * Content: Class for caching strings
  7. *@@BEGIN_MSINTERNAL
  8. * History:
  9. * Date By Reason
  10. * ==== == ======
  11. * 02/04/2000 rmt Created
  12. * 02/17/2000 rmt Parameter validation work
  13. * 02/21/2000 rmt Updated to make core Unicode and remove ANSI calls
  14. *@@END_MSINTERNAL
  15. *
  16. ***************************************************************************/
  17. #include "dnaddri.h"
  18. // # of slots to grow the cache at each opportunity
  19. #define STRINGCACHE_GROW_SLOTS 10
  20. #undef DPF_MODNAME
  21. #define DPF_MODNAME "CStringCache::CStringCache"
  22. CStringCache::CStringCache(): m_ppszStringCache(NULL), m_dwNumElements(0), m_dwNumSlots(0)
  23. {
  24. }
  25. #undef DPF_MODNAME
  26. #define DPF_MODNAME "CStringCache::~CStringCache"
  27. CStringCache::~CStringCache()
  28. {
  29. for( DWORD dwIndex = 0; dwIndex < m_dwNumElements; dwIndex++ )
  30. {
  31. delete [] m_ppszStringCache[dwIndex];
  32. }
  33. delete [] m_ppszStringCache;
  34. }
  35. #undef DPF_MODNAME
  36. #define DPF_MODNAME "CStringCache::AddString"
  37. HRESULT CStringCache::AddString( const WCHAR *pszString, WCHAR * *ppszSlot )
  38. {
  39. HRESULT hr;
  40. PWSTR pszSlot;
  41. hr = GetString( pszString, &pszSlot );
  42. if( hr != DPN_OK )
  43. {
  44. DPFX(DPFPREP, 0, "Internal Error hr=0x%x", hr );
  45. return hr;
  46. }
  47. // Entry was found
  48. if( pszSlot != NULL )
  49. {
  50. *ppszSlot = pszSlot;
  51. return DPN_OK;
  52. }
  53. else
  54. {
  55. if( m_dwNumElements == m_dwNumSlots )
  56. {
  57. hr = GrowCache( m_dwNumSlots + STRINGCACHE_GROW_SLOTS );
  58. if( FAILED( hr ) )
  59. {
  60. DPFX(DPFPREP, 0, "Failed to grow string cache hr=0x%x", hr );
  61. return hr;
  62. }
  63. }
  64. m_ppszStringCache[m_dwNumElements] = new WCHAR[wcslen(pszString)+1];
  65. if( m_ppszStringCache[m_dwNumElements] == NULL )
  66. {
  67. DPFX(DPFPREP, 0, "Failed to alloc mem" );
  68. return DPNERR_OUTOFMEMORY;
  69. }
  70. wcscpy( m_ppszStringCache[m_dwNumElements], pszString );
  71. *ppszSlot = m_ppszStringCache[m_dwNumElements];
  72. m_dwNumElements++;
  73. return DPN_OK;
  74. }
  75. }
  76. #undef DPF_MODNAME
  77. #define DPF_MODNAME "CStringCache::GetString"
  78. HRESULT CStringCache::GetString( const WCHAR *pszString, WCHAR * *ppszSlot )
  79. {
  80. *ppszSlot = NULL;
  81. for( DWORD dwIndex = 0; dwIndex < m_dwNumElements; dwIndex++ )
  82. {
  83. if( wcscmp( m_ppszStringCache[dwIndex], pszString ) == 0 )
  84. {
  85. *ppszSlot = m_ppszStringCache[dwIndex];
  86. return DPN_OK;
  87. }
  88. }
  89. return DPN_OK;
  90. }
  91. #undef DPF_MODNAME
  92. #define DPF_MODNAME "CStringCache::GrowCache"
  93. HRESULT CStringCache::GrowCache( DWORD dwNewSize )
  94. {
  95. WCHAR **ppszNewCache;
  96. ppszNewCache = new WCHAR *[dwNewSize];
  97. if( ppszNewCache == NULL )
  98. {
  99. DPFX(DPFPREP, 0, "Error allocating memory" );
  100. return DPNERR_OUTOFMEMORY;
  101. }
  102. memcpy( ppszNewCache, m_ppszStringCache, sizeof( WCHAR * ) * m_dwNumElements );
  103. m_dwNumSlots = dwNewSize;
  104. if( m_ppszStringCache != NULL )
  105. delete [] m_ppszStringCache;
  106. m_ppszStringCache = ppszNewCache;
  107. return DPN_OK;
  108. }