Rewrote language backend for integrity checking and optimization

Replaced CRC32 system with real hash-map
This commit is contained in:
David Anderson
2005-11-21 01:35:37 +00:00
parent bb292d13ad
commit f13599177f
8 changed files with 1006 additions and 392 deletions

View File

@@ -32,6 +32,8 @@
#ifndef _INCLUDE_CLANG_H
#define _INCLUDE_CLANG_H
#include "sh_tinyhash.h"
#define LANG_SERVER 0
#define LANG_PLAYER -1
@@ -45,19 +47,35 @@ struct md5Pair
String val;
};
struct keyEntry
{
String key;
uint32_t hash;
};
struct sKeyDef
{
sKeyDef() { key = -1; def = 0; }
~sKeyDef() { if (def) delete def; }
String *definition;
int key;
String *def;
};
class defentry
{
public:
defentry() : definition(NULL)
{
};
~defentry()
{
if (definition)
{
delete definition;
definition = NULL;
}
}
String *definition;
};
struct keytbl_val
{
keytbl_val() : index(-1)
{
};
int index;
};
class CLangMngr
@@ -73,9 +91,9 @@ class CLangMngr
~CLang();
// Get the definition
const char *GetDef(const char *key, int &status);
const char *GetDef(int key, int &status);
// Add definitions to this language
void MergeDefinitions(CQueue <sKeyDef*> & vec);
void MergeDefinitions(CQueue <sKeyDef> & vec);
// Reset this language
void Clear();
@@ -95,49 +113,9 @@ class CLangMngr
void SetMngr(CLangMngr *l) { m_LMan = l; }
// Get number of entries
int Entries() { return m_LookUpTable.size(); }
// Make a hash from a string; convert to lowercase first if needed
static uint32_t MakeHash(const char *src, bool makeLower = false);
protected:
// An entry in the language
class LangEntry
{
// the definition hash
uint32_t m_DefHash;
// index into the lookup table?
int key;
// the definition
String m_pDef;
// is this from the cache or not?
bool m_isCache;
public:
// Set
void SetKey(int key);
void SetDef(const char *pDef);
void SetCache(bool c);
// Get
uint32_t GetDefHash();
int GetKey();
const char *GetDef();
int GetDefLength();
bool GetCache();
// Constructors / destructors
LangEntry();
LangEntry(int key);
LangEntry(int key, const char *pDef);
LangEntry(const LangEntry &other);
LangEntry(int pKey, uint32_t defHash, const char *pDef);
// Reset
void Clear();
};
// Get (construct if needed) an entry
LangEntry * GetEntry(int key);
typedef CVector<LangEntry*> LookUpVec;
typedef LookUpVec::iterator LookUpVecIter;
typedef THash<int, defentry> LookUpVec;
typedef LookUpVec::iterator LookUpVecIter;
char m_LanguageName[3];
@@ -145,11 +123,11 @@ class CLangMngr
LookUpVec m_LookUpTable;
CLangMngr *m_LMan;
public:
LangEntry *AddEntry(int pKey, uint32_t defHash, const char *def, bool cache);
void AddEntry(int key, const char *definition);
};
// Merge definitions into a language
void MergeDefinitions(const char *lang, CQueue <sKeyDef*> &tmpVec);
void MergeDefinitions(const char *lang, CQueue <sKeyDef> &tmpVec);
// strip lowercase; make lower if needed
static size_t strip(char *str, char *newstr, bool makelower = false);
@@ -159,7 +137,8 @@ class CLangMngr
LangVec m_Languages;
CVector<md5Pair *> FileList;
CVector<keyEntry*> KeyList;
CVector<String *> KeyList;
THash<String, keytbl_val> KeyTable;
// Get a lang object (construct if needed)
CLang * GetLang(const char *name);
@@ -185,16 +164,15 @@ public:
// Cache
bool LoadCache(const char *filename);
bool SaveCache(const char *filename);
void InvalidateCache();
// Get index
int GetKeyEntry(String &key);
int GetKeyEntry(const char *key);
int GetKeyHash(int key);
int GetKeyIndex(const char *key);
// Get key from index
const char *GetKey(int key);
// Add key
int AddKeyEntry(String &key);
// Make a hash from a string; convert to lowercase first if needed
uint32_t MakeHash(const char *src, bool makeLower);
// Get the number of languages
int GetLangsNum();