Fixed almost all CLang bugs: (mem leaks?, parsing, key system, etc)

Warning - caching is still broken!
Fixed bug in ucfirst()
This commit is contained in:
David Anderson 2004-08-15 13:49:59 +00:00
parent 3bbfca6d08
commit 46adfaedd3
3 changed files with 75 additions and 30 deletions

View File

@ -201,7 +201,7 @@ const char *CLangMngr::CLang::LangEntry::GetDef()
int CLangMngr::CLang::LangEntry::GetKey() int CLangMngr::CLang::LangEntry::GetKey()
{ {
return 0; return key;
} }
CLangMngr::CLang::LangEntry::LangEntry() CLangMngr::CLang::LangEntry::LangEntry()
@ -267,31 +267,30 @@ CLangMngr::CLang::~CLang()
void CLangMngr::CLang::Clear() void CLangMngr::CLang::Clear()
{ {
/*for (int i=0; i<m_LookUpTable.size(); i++) for (unsigned int i=0; i<m_LookUpTable.size(); i++)
{ {
if (m_LookUpTable[i]) if (m_LookUpTable[i])
delete m_LookUpTable[i]; delete m_LookUpTable[i];
}*/ }
m_LookUpTable.clear(); m_LookUpTable.clear();
} }
CLangMngr::CLang::LangEntry * CLangMngr::CLang::GetEntry(int key) CLangMngr::CLang::LangEntry * CLangMngr::CLang::GetEntry(int pkey)
{ {
LangEntry *e = 0;
unsigned int i = 0; unsigned int i = 0;
uint32_t hash = lman->GetKeyHash(key);
for (i=0; i<m_LookUpTable.size(); i++) for (i=0; i<m_LookUpTable.size(); i++)
{ {
e = m_LookUpTable.at(i); if (m_LookUpTable[i]->GetKey() == pkey)
if (lman->GetKeyHash(e->GetKey()) == hash) {
break; return m_LookUpTable[i];
}
} }
if (i < m_LookUpTable.size())
return m_LookUpTable.at(i); LangEntry *e = new LangEntry(pkey);
e->SetKey(pkey);
e = new LangEntry(key);
m_LookUpTable.push_back(e); m_LookUpTable.push_back(e);
return m_LookUpTable.back(); return e;
} }
void CLangMngr::CLang::MergeDefinitions(CQueue<sKeyDef*> &vec) void CLangMngr::CLang::MergeDefinitions(CQueue<sKeyDef*> &vec)
@ -306,6 +305,7 @@ void CLangMngr::CLang::MergeDefinitions(CQueue<sKeyDef*> &vec)
if (entry->GetDefHash() != MakeHash(def)) if (entry->GetDefHash() != MakeHash(def))
{ {
entry->SetDef(def); entry->SetDef(def);
entry->SetKey(key);
} }
delete vec.front(); delete vec.front();
vec.pop(); vec.pop();
@ -320,8 +320,6 @@ const char * CLangMngr::CLang::GetDef(const char *key)
return nfind; return nfind;
for (unsigned int i = 0; i<m_LookUpTable.size(); i++) for (unsigned int i = 0; i<m_LookUpTable.size(); i++)
{ {
qdbg("Key: %d\tLookup: %d\tString: %s\n",
ikey, m_LookUpTable[i]->GetKey(), m_LookUpTable[i]->GetDef());
if (m_LookUpTable[i]->GetKey() == ikey) if (m_LookUpTable[i]->GetKey() == ikey)
return m_LookUpTable[i]->GetDef(); return m_LookUpTable[i]->GetDef();
} }
@ -376,6 +374,11 @@ int CLangMngr::GetKeyEntry(const char *key)
uint32_t cmpKey = 0; uint32_t cmpKey = 0;
unsigned int i = 0; unsigned int i = 0;
if (hKey == 0)
{
return -1;
}
for (i = 0; i<KeyList.size(); i++) for (i = 0; i<KeyList.size(); i++)
{ {
cmpKey = KeyList[i]->hash; cmpKey = KeyList[i]->hash;
@ -720,7 +723,7 @@ int CLangMngr::MergeDefinitionFile(const char *file)
md5.hex_digest(md5buffer); md5.hex_digest(md5buffer);
bool foundFlag = false; bool foundFlag = false;
/* CVector<md5Pair *>::iterator iter; CVector<md5Pair *>::iterator iter;
for (iter=FileList.begin(); iter!=FileList.end(); iter++) for (iter=FileList.begin(); iter!=FileList.end(); iter++)
{ {
if ( (*iter)->file.compare(file) == 0 ) if ( (*iter)->file.compare(file) == 0 )
@ -742,7 +745,7 @@ int CLangMngr::MergeDefinitionFile(const char *file)
p->file.assign(file); p->file.assign(file);
p->val.assign(md5buffer); p->val.assign(md5buffer);
FileList.push_back(p); FileList.push_back(p);
}*/ }
fp = fopen(file, "rt"); fp = fopen(file, "rt");
if (!fp) if (!fp)
@ -789,17 +792,38 @@ int CLangMngr::MergeDefinitionFile(const char *file)
{ {
tmpEntry = new sKeyDef; tmpEntry = new sKeyDef;
String key = buf.substr(0, pos); String key = buf.substr(0, pos);
String def = buf.substr(pos+1, def.size()-pos); String def = buf.substr(pos+1);
key.trim(); key.trim();
key.toLower(); key.toLower();
int iKey = GetKeyEntry(key); int iKey = GetKeyEntry(key);
if (iKey == -1) if (iKey == -1)
iKey = AddKeyEntry(key); iKey = AddKeyEntry(key);
qdbg("key=%d\n", iKey);
tmpEntry->key = iKey; tmpEntry->key = iKey;
tmpEntry->def = new String; tmpEntry->def = new String;
tmpEntry->def->assign(def.c_str()); tmpEntry->def->assign(def.c_str());
tmpEntry->def->trim(); tmpEntry->def->trim();
int pos = tmpEntry->def->find('%');
char r = 0, c = 0;
while (pos!=String::npos)
{
c = tmpEntry->def->at(pos+1);
if (c == 'd' || c == 'i')
{
r = INSERT_NUMBER;
} else if (c == 'f') {
r = INSERT_FLOAT;
} else if (c == 's') {
r = INSERT_STRING;
} else {
r = 0;
}
if (r)
{
tmpEntry->def->at(pos+1, r);
tmpEntry->def->erase(pos, 1);
}
pos = tmpEntry->def->find('%', pos+1);
}
Defq.push(tmpEntry); Defq.push(tmpEntry);
tmpEntry = 0; tmpEntry = 0;
} else { } else {
@ -964,7 +988,7 @@ CLangMngr::~CLangMngr()
void CLangMngr::Clear() void CLangMngr::Clear()
{ {
/*int i = 0; unsigned int i = 0;
for (i=0; i<m_Languages.size(); i++) for (i=0; i<m_Languages.size(); i++)
{ {
if (m_Languages[i]) if (m_Languages[i])
@ -981,7 +1005,7 @@ void CLangMngr::Clear()
{ {
if (KeyList[i]) if (KeyList[i])
delete KeyList[i]; delete KeyList[i];
}*/ }
m_Languages.clear(); m_Languages.clear();
KeyList.clear(); KeyList.clear();

View File

@ -169,6 +169,8 @@ public:
{ {
if (!v) if (!v)
return npos; return npos;
if (index >= (int)cSize || index < 0)
return npos;
unsigned int i = 0; unsigned int i = 0;
for (i=index; i<cSize; i++) for (i=index; i<cSize; i++)
{ {
@ -284,29 +286,30 @@ public:
} }
} }
} }
v[i] = 0;
cSize -= num; cSize -= num;
v[cSize] = 0;
return (*this); return (*this);
} }
String substr(unsigned int index, int num = npos) String substr(unsigned int index, int num = npos)
{ {
unsigned int rnum = (unsigned int)((num<0)?(cSize):(num));
String ns; String ns;
if (index >= cSize || !v) if (index >= cSize || !v)
return ns; return ns;
if (index+rnum >= cSize) if (num == npos)
{ {
rnum = cSize - index+1; num = cSize - index;
} else if (index+num >= cSize) {
num = cSize - index;
} }
unsigned int i = 0, j=0; unsigned int i = 0, j=0;
char *s = new char[cSize+1]; char *s = new char[cSize+1];
for (i=index; i<index+rnum; i++) for (i=index; i<index+num; i++)
{ {
s[j++] = v[i]; s[j++] = v[i];
} }
@ -354,6 +357,24 @@ public:
} }
} }
int at(int a)
{
if (at < 0 || at >= (int)cSize)
return -1;
return v[a];
}
bool at(int at, char c)
{
if (at < 0 || at >= (int)cSize)
return false;
v[at] = c;
return true;
}
private: private:
void Grow(unsigned int d) void Grow(unsigned int d)
{ {

View File

@ -543,9 +543,9 @@ static cell AMX_NATIVE_CALL amx_ucfirst(AMX *amx, cell *params)
{ {
int len = 0; int len = 0;
cell *str = get_amxaddr(amx, params[1]); cell *str = get_amxaddr(amx, params[1]);
if (!isalpha((char)str[0]) || (str[0]&(1<<5))) if (!isalpha((char)str[0]) || !(str[0]&(1<<5)))
return 0; return 0;
str[0] |= (1<<5); str[0] &= ~(1<<5);
return 1; return 1;
} }