PageRenderTime 158ms CodeModel.GetById 1ms RepoModel.GetById 0ms app.codeStats 0ms

/indra/llmessage/message_string_table.cpp

https://bitbucket.org/lindenlab/viewer-beta/
C++ | 91 lines | 54 code | 11 blank | 26 comment | 6 complexity | 4320a0022d4000141bdaba96871f0f69 MD5 | raw file
Possible License(s): LGPL-2.1
  1. /**
  2. * @file message_string_table.cpp
  3. * @brief static string table for message template
  4. *
  5. * $LicenseInfo:firstyear=2001&license=viewerlgpl$
  6. * Second Life Viewer Source Code
  7. * Copyright (C) 2010, Linden Research, Inc.
  8. *
  9. * This library is free software; you can redistribute it and/or
  10. * modify it under the terms of the GNU Lesser General Public
  11. * License as published by the Free Software Foundation;
  12. * version 2.1 of the License only.
  13. *
  14. * This library is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  17. * Lesser General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU Lesser General Public
  20. * License along with this library; if not, write to the Free Software
  21. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  22. *
  23. * Linden Research, Inc., 945 Battery Street, San Francisco, CA 94111 USA
  24. * $/LicenseInfo$
  25. */
  26. #include "linden_common.h"
  27. #include "llerror.h"
  28. #include "message.h"
  29. inline U32 message_hash_my_string(const char *str)
  30. {
  31. U32 retval = 0;
  32. while (*str++)
  33. {
  34. retval += *str;
  35. retval <<= 1;
  36. }
  37. return (retval % MESSAGE_NUMBER_OF_HASH_BUCKETS);
  38. }
  39. LLMessageStringTable::LLMessageStringTable()
  40. : mUsed(0)
  41. {
  42. for (U32 i = 0; i < MESSAGE_NUMBER_OF_HASH_BUCKETS; i++)
  43. {
  44. mEmpty[i] = TRUE;
  45. mString[i][0] = 0;
  46. }
  47. }
  48. LLMessageStringTable::~LLMessageStringTable()
  49. { }
  50. char* LLMessageStringTable::getString(const char *str)
  51. {
  52. U32 hash_value = message_hash_my_string(str);
  53. while (!mEmpty[hash_value])
  54. {
  55. if (!strncmp(str, mString[hash_value], MESSAGE_MAX_STRINGS_LENGTH))
  56. {
  57. return mString[hash_value];
  58. }
  59. else
  60. {
  61. hash_value++;
  62. hash_value %= MESSAGE_NUMBER_OF_HASH_BUCKETS;
  63. }
  64. }
  65. // not found, so add!
  66. strncpy(mString[hash_value], str, MESSAGE_MAX_STRINGS_LENGTH); /* Flawfinder: ignore */
  67. mString[hash_value][MESSAGE_MAX_STRINGS_LENGTH - 1] = 0;
  68. mEmpty[hash_value] = FALSE;
  69. mUsed++;
  70. if (mUsed >= MESSAGE_NUMBER_OF_HASH_BUCKETS - 1)
  71. {
  72. U32 i;
  73. llinfos << "Dumping string table before crashing on HashTable full!" << llendl;
  74. for (i = 0; i < MESSAGE_NUMBER_OF_HASH_BUCKETS; i++)
  75. {
  76. llinfos << "Entry #" << i << ": " << mString[i] << llendl;
  77. }
  78. }
  79. return mString[hash_value];
  80. }