Team Fortress 2 Source Code as on 22/4/2020
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.

145 lines
4.3 KiB

  1. //========= Copyright Valve Corporation, All rights reserved. ============//
  2. //
  3. // Purpose: A registry of strings and associated ints
  4. //
  5. // $Workfile: $
  6. // $Date: $
  7. //
  8. //-----------------------------------------------------------------------------
  9. // $Log: $
  10. //
  11. // $NoKeywords: $
  12. //=============================================================================//
  13. #include <stdio.h>
  14. #include <string.h>
  15. #include "stringregistry.h"
  16. #include "utldict.h"
  17. // memdbgon must be the last include file in a .cpp file!!!
  18. #include "tier0/memdbgon.h"
  19. #if !defined(_STATIC_LINKED) || defined(CLIENT_DLL)
  20. //-----------------------------------------------------------------------------
  21. // Purpose: This class wraps the containers that do the actual work
  22. //-----------------------------------------------------------------------------
  23. struct StringTable_t : public CUtlDict<int, unsigned short>
  24. {
  25. };
  26. //-----------------------------------------------------------------------------
  27. // Purpose: Add null terminated string to the string registry
  28. // Input :
  29. // Output :
  30. //-----------------------------------------------------------------------------
  31. unsigned short CStringRegistry::AddString(const char *stringText, int stringID)
  32. {
  33. return m_pStringList->Insert( stringText, stringID );
  34. }
  35. //-----------------------------------------------------------------------------
  36. // Purpose: Given string text get the string ID
  37. // Input : Text of string to find
  38. // Output : Return string id or -1 if no such string exists
  39. //-----------------------------------------------------------------------------
  40. int CStringRegistry::GetStringID( const char *stringText )
  41. {
  42. unsigned short index = m_pStringList->Find( stringText );
  43. if ( m_pStringList->IsValidIndex( index ) )
  44. {
  45. return (*m_pStringList)[index];
  46. }
  47. return -1;
  48. }
  49. //-----------------------------------------------------------------------------
  50. // Purpose: Given a string ID return the string text
  51. // Input : ID of string to find
  52. // Output : Return string text of NULL of no such ID exists
  53. //-----------------------------------------------------------------------------
  54. char const *CStringRegistry::GetStringText( int stringID )
  55. {
  56. for( unsigned short index = m_pStringList->First() ; index != m_pStringList->InvalidIndex(); index = m_pStringList->Next( index ) )
  57. {
  58. if ( (*m_pStringList)[index] == stringID )
  59. {
  60. return m_pStringList->GetElementName( index );
  61. }
  62. }
  63. return NULL;
  64. }
  65. //-----------------------------------------------------------------------------
  66. // Purpose: Given a key return the string text
  67. //-----------------------------------------------------------------------------
  68. char const *CStringRegistry::GetStringForKey( unsigned short key )
  69. {
  70. if ( !m_pStringList->IsValidIndex( key ) )
  71. return NULL;
  72. return m_pStringList->GetElementName( key );
  73. }
  74. //-----------------------------------------------------------------------------
  75. // Purpose: Given a key return the string text
  76. //-----------------------------------------------------------------------------
  77. int CStringRegistry::GetIDForKey( unsigned short key )
  78. {
  79. if ( !m_pStringList->IsValidIndex( key ) )
  80. return 0;
  81. return (*m_pStringList)[key];
  82. }
  83. //-----------------------------------------------------------------------------
  84. // Purpose: Clear all strings from the string registry
  85. //-----------------------------------------------------------------------------
  86. void CStringRegistry::ClearStrings(void)
  87. {
  88. m_pStringList->RemoveAll();
  89. }
  90. //-----------------------------------------------------------------------------
  91. // Purpose: Destructor - delete the list of strings and maps
  92. // Input :
  93. // Output :
  94. //-----------------------------------------------------------------------------
  95. CStringRegistry::~CStringRegistry(void)
  96. {
  97. delete m_pStringList;
  98. }
  99. //-----------------------------------------------------------------------------
  100. // Purpose: Constructor
  101. // Input :
  102. // Output :
  103. //-----------------------------------------------------------------------------
  104. CStringRegistry::CStringRegistry(void)
  105. {
  106. m_pStringList = new StringTable_t;
  107. }
  108. unsigned short CStringRegistry::First() const
  109. {
  110. return m_pStringList->First();
  111. }
  112. unsigned short CStringRegistry::Next( unsigned short key ) const
  113. {
  114. return m_pStringList->Next( key );
  115. }
  116. unsigned short CStringRegistry::InvalidIndex() const
  117. {
  118. return m_pStringList->InvalidIndex();
  119. }
  120. #endif // _STATIC_LINKED && CLIENT_DLL