From a313c5b95b95fcf5437da741673a48ea46162b58 Mon Sep 17 00:00:00 2001 From: David Anderson Date: Wed, 8 Sep 2004 21:13:04 +0000 Subject: [PATCH] updated strings --- dlls/engine/CString.h | 301 ++++++++++++++++++++++++++++++++++++--- dlls/engine/Makefile.pl | 2 +- dlls/engine/engine.cpp | 24 ++-- dlls/engine/engine.h | 20 +-- dlls/engine/forwards.cpp | 14 +- dlls/engine/messages.h | 2 +- 6 files changed, 310 insertions(+), 53 deletions(-) diff --git a/dlls/engine/CString.h b/dlls/engine/CString.h index 12b5cbc4..0224646a 100755 --- a/dlls/engine/CString.h +++ b/dlls/engine/CString.h @@ -33,30 +33,67 @@ #define _INCLUDE_CSTRING_H //by David "BAILOPAN" Anderson -class CString +class String { public: - CString() { v = NULL; mSize = 0; } - ~CString() { if (v) delete [] v; } + String() + { + v = NULL; + mSize = 0; + cSize = 0; + Grow(2); + assign(""); + } - //added these for amxx - CString(const char *src) { v = NULL; mSize = 0; assign(src); } - CString(CString &src) { v = NULL; mSize = 0; assign(src.c_str()); } + ~String() + { + if (v) + delete [] v; + } + + String(const char *src) + { + v = NULL; + mSize = 0; + cSize = 0; assign(src); + } + + String(String &src) + { + v = NULL; + mSize = 0; + cSize = 0; + assign(src.c_str()); + } const char *c_str() { return v?v:""; } const char *c_str() const { return v?v:""; } void append(const char *t) { - Grow(strlen(v) + strlen(t)); + Grow(cSize + strlen(t)); strcat(v, t); + cSize = strlen(v); } - void append(CString &d) + void append(const char c) + { + Grow(cSize + 2); + v[cSize] = c; + v[++cSize] = 0; + } + + void append(String &d) { const char *t = d.c_str(); - Grow(strlen(v) + strlen(t)); + Grow(cSize + strlen(t)); strcat(v, t); + cSize = strlen(v); + } + + void assign(const String &src) + { + assign(src.c_str()); } void assign(const char *d) @@ -64,20 +101,27 @@ public: if (!d) { Grow(1); + cSize = 0; strcpy(v, ""); return; } Grow(strlen(d)); if (v) + { strcpy(v, d); + cSize = strlen(v); + } else { + cSize = 0; + } } void clear() { if (v) - delete [] v; - v = NULL; - mSize = 0; + { + v[0] = 0; + cSize = 0; + } } int compare (const char *d) @@ -100,7 +144,7 @@ public: //Added this for amxx inclusion bool empty() { - if (!v || !mSize) + if (!v || !cSize) return true; return false; @@ -110,20 +154,239 @@ public: { if (!v) return 0; - return strlen(v); + return cSize; + } + + const char * _fread(FILE *fp) + { + Grow(512); + char * ret = fgets(v, 511, fp); + cSize = strlen(v); + return ret; + } + + int find(const char c, int index = 0) + { + if (!v) + return npos; + if (index >= (int)cSize || index < 0) + return npos; + unsigned int i = 0; + for (i=index; i=0; i--) + { + if (!is_space(v[i]) + || (is_space(v[i]) && i==0)) + { + erase(i+1, j); + break; + } + j++; + } + } + + if (cSize == 1) + { + if (is_space(v[0])) + { + clear(); + return; + } + } + } + + String & erase(unsigned int start, int num = npos) + { + if (!v) + return (*this); + unsigned int i = 0; + //check for bounds + if (num == npos || start+num > cSize-num+1) + num = cSize - start; + //do the erasing + bool copyflag = false; + for (i=0; i=start && i= cSize || !v) + return ns; + + if (num == npos) + { + num = cSize - index; + } else if (index+num >= cSize) { + num = cSize - index; + } + + unsigned int i = 0, j=0; + char *s = new char[cSize+1]; + + for (i=index; i= 65 && v[i] <= 90) + v[i] |= 32; + } + } + + String & operator = (const String &src) + { + assign(src); + return *this; + } + + String & operator = (const char *src) + { + assign(src); + return *this; + + } + + char operator [] (unsigned int index) + { + if (index > cSize) + { + return -1; + } else { + return v[index]; + } + } + + int at(int a) + { + if (a < 0 || a >= (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: - void Grow(int d) + void Grow(unsigned int d) { if (d<1) return; if (d > mSize) { + mSize = d + 16; // allocate a buffer char *t = new char[d+1]; if (v) { strcpy(t, v); - t[strlen(v)] = 0; + t[cSize] = 0; delete [] v; } v = t; @@ -132,8 +395,10 @@ private: } char *v; - int mSize; + unsigned int mSize; + unsigned int cSize; +public: + static const int npos = -1; }; #endif //_INCLUDE_CSTRING_H - diff --git a/dlls/engine/Makefile.pl b/dlls/engine/Makefile.pl index 241f8532..920bb626 100755 --- a/dlls/engine/Makefile.pl +++ b/dlls/engine/Makefile.pl @@ -173,6 +173,6 @@ for ($i=0; $i<=$#CPP_SOURCE_FILES; $i++) } } -$gcc = "$gccf $cflags -shared -ldl -lm @LINK -o $outdir/$bin"; +$gcc = "$gccf $cflags -shared -fPIC -ldl -lm -o $outdir/$bin @LINK"; print "$gcc\n"; `$gcc`; diff --git a/dlls/engine/engine.cpp b/dlls/engine/engine.cpp index 907c43e9..b32175ef 100755 --- a/dlls/engine/engine.cpp +++ b/dlls/engine/engine.cpp @@ -28,14 +28,15 @@ static cell AMX_NATIVE_CALL register_think(AMX *amx, cell *params) EntClass *p = new EntClass; const char *clsname = MF_GetAmxString(amx, params[1], 0, &len); - p->Class = new char[strlen(clsname)+1]; - strcpy(p->Class, clsname); + p->Class.assign(clsname); p->Forward = MF_RegisterSPForwardByName(amx, MF_GetAmxString(amx, params[2], 0, &len), FP_CELL, FP_DONE); Thinks.push_back(p); - g_pFunctionTable->pfnThink=Think; + if (!g_pFunctionTable->pfnThink) + g_pFunctionTable->pfnThink=Think; + return p->Forward; } @@ -50,7 +51,9 @@ static cell AMX_NATIVE_CALL register_impulse(AMX *amx, cell *params) Impulses.push_back(p); - g_pFunctionTable->pfnCmdStart=CmdStart; + if (!g_pFunctionTable->pfnCmdStart) + g_pFunctionTable->pfnCmdStart=CmdStart; + return p->Forward; } @@ -64,23 +67,22 @@ static cell AMX_NATIVE_CALL register_touch(AMX *amx, cell *params) Touch *p = new Touch; if (!strlen(Toucher) || strcmp(Toucher, "*")==0) { - p->Toucher = 0; + p->Toucher.assign(""); } else { - p->Toucher = new char[strlen(Toucher)+1]; - strcpy(p->Toucher, Toucher); + p->Toucher.assign(Toucher); } if (!strlen(Touched) || strcmp(Touched, "*")==0) { - p->Touched = 0; + p->Touched.assign(""); } else { - p->Touched = new char[strlen(Touched)+1]; - strcpy(p->Touched, Touched); + p->Touched.assign(Touched); } p->Forward = MF_RegisterSPForwardByName(amx, MF_GetAmxString(amx, params[3], 2, &len), FP_CELL, FP_CELL, FP_DONE); Touches.push_back(p); - g_pFunctionTable->pfnTouch=pfnTouch; + if (!g_pFunctionTable->pfnTouch) + g_pFunctionTable->pfnTouch=pfnTouch; return p->Forward; } diff --git a/dlls/engine/engine.h b/dlls/engine/engine.h index 40dcedb9..f8e71dbf 100755 --- a/dlls/engine/engine.h +++ b/dlls/engine/engine.h @@ -133,18 +133,10 @@ class Touch { public: int Forward; - char *Toucher; - char *Touched; + String Toucher; + String Touched; ~Touch() { - if (Toucher) { - delete [] Toucher; - Toucher = 0; - } - if (Touched) { - delete [] Touched; - Touched = 0; - } if (Forward != -1) MF_UnregisterSPForward(Forward); } @@ -154,13 +146,11 @@ class EntClass { public: int Forward; - char *Class; + String Class; ~EntClass() { - if (Class) { - delete [] Class; - Class = 0; - } + if (Forward != -1) + MF_UnregisterSPForward(Forward); } }; diff --git a/dlls/engine/forwards.cpp b/dlls/engine/forwards.cpp index f17b1851..cc3f9e31 100755 --- a/dlls/engine/forwards.cpp +++ b/dlls/engine/forwards.cpp @@ -229,31 +229,31 @@ void pfnTouch(edict_t *pToucher, edict_t *pTouched) META_RES res=MRES_IGNORED; for (i=0; iToucher == 0) + if (Touches[i]->Toucher.size() == 0) { - if (Touches[i]->Touched == 0) + if (Touches[i]->Touched.size() == 0) { retVal = MF_ExecuteForward(Touches[i]->Forward, ENTINDEX(pToucher), ENTINDEX(pTouched)); if (retVal & 2/*PLUGIN_HANDLED_MAIN*/) RETURN_META(MRES_SUPERCEDE); else if (retVal) res=MRES_SUPERCEDE; - } else if (fstrcmp(Touches[i]->Touched, ptdClass)) { + } else if (Touches[i]->Touched.compare(ptdClass)==0) { retVal = MF_ExecuteForward(Touches[i]->Forward, ENTINDEX(pToucher), ENTINDEX(pTouched)); if (retVal & 2/*PLUGIN_HANDLED_MAIN*/) RETURN_META(MRES_SUPERCEDE); else if (retVal) res=MRES_SUPERCEDE; } - } else if (fstrcmp(Touches[i]->Toucher, ptrClass)) { - if (Touches[i]->Touched == 0) + } else if (Touches[i]->Toucher.compare(ptrClass)==0) { + if (Touches[i]->Touched.size() == 0) { retVal = MF_ExecuteForward(Touches[i]->Forward, ENTINDEX(pToucher), ENTINDEX(pTouched)); if (retVal & 2/*PLUGIN_HANDLED_MAIN*/) RETURN_META(MRES_SUPERCEDE); else if (retVal) res=MRES_SUPERCEDE; - } else if (fstrcmp(Touches[i]->Touched, ptdClass)) { + } else if (Touches[i]->Touched.compare(ptdClass)==0) { retVal = MF_ExecuteForward(Touches[i]->Forward, ENTINDEX(pToucher), ENTINDEX(pTouched)); if (retVal & 2/*PLUGIN_HANDLED_MAIN*/) RETURN_META(MRES_SUPERCEDE); @@ -285,7 +285,7 @@ void Think(edict_t *pent) int retVal=0; for (i=0; iClass)) + if (Thinks[i]->Class.compare(cls)==0) { retVal=MF_ExecuteForward(Thinks[i]->Forward, ENTINDEX(pent)); if (retVal & 2/*PLUGIN_HANDLED_MAIN*/) diff --git a/dlls/engine/messages.h b/dlls/engine/messages.h index ee5d6937..bbae64ee 100755 --- a/dlls/engine/messages.h +++ b/dlls/engine/messages.h @@ -36,7 +36,7 @@ public: int type; REAL fData; - CString cData; + String cData; int iData; };