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.
146 lines
3.1 KiB
146 lines
3.1 KiB
/*==========================================================================
|
|
*
|
|
* Copyright (C) 2000 Microsoft Corporation. All Rights Reserved.
|
|
*
|
|
* File: strcache.cpp
|
|
* Content: Class for caching strings
|
|
*@@BEGIN_MSINTERNAL
|
|
* History:
|
|
* Date By Reason
|
|
* ==== == ======
|
|
* 02/04/2000 rmt Created
|
|
* 02/17/2000 rmt Parameter validation work
|
|
* 02/21/2000 rmt Updated to make core Unicode and remove ANSI calls
|
|
*@@END_MSINTERNAL
|
|
*
|
|
***************************************************************************/
|
|
|
|
#include "dnaddri.h"
|
|
|
|
|
|
// # of slots to grow the cache at each opportunity
|
|
#define STRINGCACHE_GROW_SLOTS 10
|
|
|
|
#undef DPF_MODNAME
|
|
#define DPF_MODNAME "CStringCache::Initialize"
|
|
|
|
void CStringCache::Initialize( void )
|
|
{
|
|
m_ppszStringCache = NULL;
|
|
m_dwNumElements = 0;
|
|
m_dwNumSlots = 0;
|
|
}
|
|
|
|
#undef DPF_MODNAME
|
|
#define DPF_MODNAME "CStringCache::Deinitialize"
|
|
|
|
void CStringCache::Deinitialize( void )
|
|
{
|
|
for( DWORD dwIndex = 0; dwIndex < m_dwNumElements; dwIndex++ )
|
|
{
|
|
DNFree(m_ppszStringCache[dwIndex]);
|
|
}
|
|
|
|
DNFree(m_ppszStringCache);
|
|
}
|
|
|
|
|
|
#undef DPF_MODNAME
|
|
#define DPF_MODNAME "CStringCache::AddString"
|
|
|
|
HRESULT CStringCache::AddString( const WCHAR *pszString, WCHAR * *ppszSlot )
|
|
{
|
|
HRESULT hr;
|
|
PWSTR pszSlot;
|
|
|
|
hr = GetString( pszString, &pszSlot );
|
|
|
|
if( hr != DPN_OK )
|
|
{
|
|
DPFX(DPFPREP, 0, "Internal Error hr=0x%x", hr );
|
|
return hr;
|
|
}
|
|
|
|
// Entry was found
|
|
if( pszSlot != NULL )
|
|
{
|
|
*ppszSlot = pszSlot;
|
|
return DPN_OK;
|
|
}
|
|
else
|
|
{
|
|
if( m_dwNumElements == m_dwNumSlots )
|
|
{
|
|
hr = GrowCache( m_dwNumSlots + STRINGCACHE_GROW_SLOTS );
|
|
|
|
if( FAILED( hr ) )
|
|
{
|
|
DPFX(DPFPREP, 0, "Failed to grow string cache hr=0x%x", hr );
|
|
return hr;
|
|
}
|
|
}
|
|
|
|
m_ppszStringCache[m_dwNumElements] = (WCHAR*) DNMalloc((wcslen(pszString)+1)*sizeof(WCHAR));
|
|
|
|
if( m_ppszStringCache[m_dwNumElements] == NULL )
|
|
{
|
|
DPFX(DPFPREP, 0, "Failed to alloc mem" );
|
|
return DPNERR_OUTOFMEMORY;
|
|
}
|
|
|
|
wcscpy( m_ppszStringCache[m_dwNumElements], pszString );
|
|
*ppszSlot = m_ppszStringCache[m_dwNumElements];
|
|
|
|
m_dwNumElements++;
|
|
|
|
return DPN_OK;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#undef DPF_MODNAME
|
|
#define DPF_MODNAME "CStringCache::GetString"
|
|
|
|
HRESULT CStringCache::GetString( const WCHAR *pszString, WCHAR * *ppszSlot )
|
|
{
|
|
*ppszSlot = NULL;
|
|
|
|
for( DWORD dwIndex = 0; dwIndex < m_dwNumElements; dwIndex++ )
|
|
{
|
|
if( wcscmp( m_ppszStringCache[dwIndex], pszString ) == 0 )
|
|
{
|
|
*ppszSlot = m_ppszStringCache[dwIndex];
|
|
return DPN_OK;
|
|
}
|
|
}
|
|
|
|
return DPN_OK;
|
|
}
|
|
|
|
#undef DPF_MODNAME
|
|
#define DPF_MODNAME "CStringCache::GrowCache"
|
|
|
|
HRESULT CStringCache::GrowCache( DWORD dwNewSize )
|
|
{
|
|
WCHAR **ppszNewCache;
|
|
|
|
ppszNewCache = (WCHAR**) DNMalloc(dwNewSize * sizeof(WCHAR*));
|
|
|
|
if( ppszNewCache == NULL )
|
|
{
|
|
DPFX(DPFPREP, 0, "Error allocating memory" );
|
|
return DPNERR_OUTOFMEMORY;
|
|
}
|
|
|
|
memcpy( ppszNewCache, m_ppszStringCache, sizeof( WCHAR * ) * m_dwNumElements );
|
|
m_dwNumSlots = dwNewSize;
|
|
|
|
if( m_ppszStringCache != NULL )
|
|
DNFree(m_ppszStringCache);
|
|
|
|
m_ppszStringCache = ppszNewCache;
|
|
|
|
return DPN_OK;
|
|
}
|
|
|