Counter Strike : Global Offensive Source Code
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.

126 lines
3.3 KiB

  1. //===== Copyright � 1996-2005, Valve Corporation, All rights reserved. ======//
  2. //
  3. // Purpose:
  4. //
  5. // $NoKeywords: $
  6. //===========================================================================//
  7. #include "convar.h"
  8. #include "tier0/dbg.h"
  9. #include "stringpool.h"
  10. #include "tier1/strtools.h"
  11. #include "generichash.h"
  12. // memdbgon must be the last include file in a .cpp file!!!
  13. #include "tier0/memdbgon.h"
  14. //-----------------------------------------------------------------------------
  15. // Purpose: Comparison function for string sorted associative data structures
  16. //-----------------------------------------------------------------------------
  17. bool StrLessInsensitive( const char * const &pszLeft, const char * const &pszRight )
  18. {
  19. return ( Q_stricmp( pszLeft, pszRight) < 0 );
  20. }
  21. bool StrLessSensitive( const char * const &pszLeft, const char * const &pszRight )
  22. {
  23. return ( Q_strcmp( pszLeft, pszRight) < 0 );
  24. }
  25. //-----------------------------------------------------------------------------
  26. //-----------------------------------------------------------------------------
  27. CStringPool::CStringPool( StringPoolCase_t caseSensitivity )
  28. : m_Strings( 32, 256, caseSensitivity == StringPoolCaseInsensitive ? StrLessInsensitive : StrLessSensitive )
  29. {
  30. }
  31. //-----------------------------------------------------------------------------
  32. //-----------------------------------------------------------------------------
  33. CStringPool::~CStringPool()
  34. {
  35. FreeAll();
  36. }
  37. //-----------------------------------------------------------------------------
  38. //-----------------------------------------------------------------------------
  39. unsigned int CStringPool::Count() const
  40. {
  41. return m_Strings.Count();
  42. }
  43. //-----------------------------------------------------------------------------
  44. //-----------------------------------------------------------------------------
  45. const char * CStringPool::Find( const char *pszValue )
  46. {
  47. unsigned short i = m_Strings.Find(pszValue);
  48. if ( m_Strings.IsValidIndex(i) )
  49. return m_Strings[i];
  50. return NULL;
  51. }
  52. const char * CStringPool::Allocate( const char *pszValue )
  53. {
  54. char *pszNew;
  55. unsigned short i = m_Strings.Find(pszValue);
  56. bool bNew = (i == m_Strings.InvalidIndex());
  57. if ( !bNew )
  58. return m_Strings[i];
  59. pszNew = strdup( pszValue );
  60. m_Strings.Insert( pszNew );
  61. return pszNew;
  62. }
  63. //-----------------------------------------------------------------------------
  64. //-----------------------------------------------------------------------------
  65. void CStringPool::FreeAll()
  66. {
  67. unsigned short i = m_Strings.FirstInorder();
  68. while ( i != m_Strings.InvalidIndex() )
  69. {
  70. free( (void *)m_Strings[i] );
  71. i = m_Strings.NextInorder(i);
  72. }
  73. m_Strings.RemoveAll();
  74. }
  75. //-----------------------------------------------------------------------------
  76. //-----------------------------------------------------------------------------
  77. #ifdef _DEBUG
  78. CON_COMMAND( test_stringpool, "Tests the class CStringPool" )
  79. {
  80. CStringPool pool;
  81. Assert(pool.Count() == 0);
  82. pool.Allocate("test");
  83. Assert(pool.Count() == 1);
  84. pool.Allocate("test");
  85. Assert(pool.Count() == 1);
  86. pool.Allocate("test2");
  87. Assert(pool.Count() == 2);
  88. Assert( pool.Find("test2") != NULL );
  89. Assert( pool.Find("TEST") != NULL );
  90. Assert( pool.Find("Test2") != NULL );
  91. Assert( pool.Find("test") != NULL );
  92. pool.FreeAll();
  93. Assert(pool.Count() == 0);
  94. Msg("Pass.");
  95. }
  96. #endif