PageRenderTime 43ms CodeModel.GetById 22ms app.highlight 16ms RepoModel.GetById 1ms 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
 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
27#include "linden_common.h"
28
29#include "llerror.h"
30#include "message.h"
31
32inline U32	message_hash_my_string(const char *str)
33{
34	U32 retval = 0;
35	while (*str++)
36	{
37		retval += *str;
38		retval <<= 1;
39	}
40	return (retval % MESSAGE_NUMBER_OF_HASH_BUCKETS);
41}
42
43
44
45LLMessageStringTable::LLMessageStringTable()
46:	mUsed(0)
47{
48	for (U32 i = 0; i < MESSAGE_NUMBER_OF_HASH_BUCKETS; i++)
49	{
50		mEmpty[i] = TRUE;
51		mString[i][0] = 0;
52	}
53}
54
55
56LLMessageStringTable::~LLMessageStringTable()
57{ }
58
59
60char* LLMessageStringTable::getString(const char *str)
61{
62	U32 hash_value = message_hash_my_string(str);
63	while (!mEmpty[hash_value])
64	{
65		if (!strncmp(str, mString[hash_value], MESSAGE_MAX_STRINGS_LENGTH))
66		{
67			return mString[hash_value];
68		}
69		else
70		{
71			hash_value++;
72			hash_value %= MESSAGE_NUMBER_OF_HASH_BUCKETS;
73		}
74	}
75	// not found, so add!
76	strncpy(mString[hash_value], str, MESSAGE_MAX_STRINGS_LENGTH);	/* Flawfinder: ignore */
77	mString[hash_value][MESSAGE_MAX_STRINGS_LENGTH - 1] = 0;
78	mEmpty[hash_value] = FALSE;
79	mUsed++;
80	if (mUsed >= MESSAGE_NUMBER_OF_HASH_BUCKETS - 1)
81	{
82		U32 i;
83		llinfos << "Dumping string table before crashing on HashTable full!" << llendl;
84		for (i = 0; i < MESSAGE_NUMBER_OF_HASH_BUCKETS; i++)
85		{
86			llinfos << "Entry #" << i << ": " << mString[i] << llendl;
87		}
88	}
89	return mString[hash_value];
90}
91