From 6b89c12f73ecdaf5c4ccfca90bb0296074c72baa Mon Sep 17 00:00:00 2001 From: David Anderson Date: Tue, 29 Jun 2004 08:14:44 +0000 Subject: [PATCH] new CString --- dlls/engine/CString.h | 90 +++++++++++++++++++++++++++++++++++++++ dlls/engine/engine.h | 1 + dlls/engine/engine.vcproj | 3 ++ dlls/engine/messages.cpp | 24 +++-------- dlls/engine/messages.h | 2 +- 5 files changed, 101 insertions(+), 19 deletions(-) create mode 100755 dlls/engine/CString.h diff --git a/dlls/engine/CString.h b/dlls/engine/CString.h new file mode 100755 index 00000000..137be93f --- /dev/null +++ b/dlls/engine/CString.h @@ -0,0 +1,90 @@ +#ifndef _INCLUDE_CSTRING_H +#define _INCLUDE_CSTRING_H + +//by David "BAILOPAN" Anderson +class CString +{ +public: + CString() { v = NULL; mSize = 0; } + ~CString() { if (v) delete [] v; } + + const char *c_str() { return v?v:""; } + + void append(const char *t) + { + Grow(strlen(v) + strlen(t)); + strcat(v, t); + } + + void append(CString &d) + { + const char *t = d.c_str(); + Grow(strlen(v) + strlen(t)); + strcat(v, t); + } + + void assign(const char *d) + { + if (!d) + { + Grow(1); + strcpy(v, ""); + return; + } + Grow(strlen(d)); + strcpy(v, d); + } + + void clear() + { + if (v) + delete [] v; + v = NULL; + mSize = 0; + } + + int compare (const char *d) + { + if (v) { + if (d) { + return strcmp(v, d); + } else { + return strlen(v); + } + } else { + if (d) { + return strlen(d); + } else { + return 0; + } + } + } + + int size() + { + if (!v) + return 0; + return strlen(v); + } + +private: + void Grow(int d) + { + if (d > mSize) + { + char *t = new char[d+1]; + if (v) { + strcpy(t, v); + t[strlen(v)] = 0; + delete [] v; + } + v = t; + mSize = d; + } + } + + char *v; + int mSize; +}; + +#endif //_INCLUDE_CSTRING_H \ No newline at end of file diff --git a/dlls/engine/engine.h b/dlls/engine/engine.h index b1127076..f90f1120 100755 --- a/dlls/engine/engine.h +++ b/dlls/engine/engine.h @@ -6,6 +6,7 @@ #include #include #include "CVector.h" +#include "CString.h" #ifndef CBASEPLAYER_H #define CBASEPLAYER_H #include diff --git a/dlls/engine/engine.vcproj b/dlls/engine/engine.vcproj index 7823459a..909afd5c 100755 --- a/dlls/engine/engine.vcproj +++ b/dlls/engine/engine.vcproj @@ -137,6 +137,9 @@ Name="Header Files" Filter="h;hpp;hxx;hm;inl;inc;xsd" UniqueIdentifier="{93995380-89BD-4b04-88EB-625FBE52EBFB}"> + + diff --git a/dlls/engine/messages.cpp b/dlls/engine/messages.cpp index b691d0ab..04677ee8 100755 --- a/dlls/engine/messages.cpp +++ b/dlls/engine/messages.cpp @@ -20,11 +20,7 @@ void argMsg::Reset() { iData = 0; fData = 0.0; - if (cData) - { - delete [] cData; - cData = NULL; - } + cData.clear(); type = 0; } @@ -51,7 +47,7 @@ void argMsg::Send() WRITE_COORD(fData); break; case arg_string: - WRITE_STRING(cData); + WRITE_STRING(cData.c_str()); break; case arg_entity: WRITE_ENTITY(iData); @@ -238,15 +234,11 @@ void WriteString(const char *sz) } else if (inhook) { if (++msgCount > Msg.size()) { argMsg *p = new argMsg(); - p->cData = new char[strlen(sz)+1]; - strcpy(p->cData, sz); - p->cData[strlen(sz)] = 0; + p->cData.assign(sz); p->type = arg_string; Msg.push_back(p); } else { - Msg[msgCount-1]->cData = new char[strlen(sz)+1]; - strcpy(Msg[msgCount-1]->cData, sz); - Msg[msgCount-1]->cData[strlen(sz)] = 0; + Msg[msgCount-1]->cData.assign(sz); Msg[msgCount-1]->type = arg_string; } RETURN_META(MRES_SUPERCEDE); @@ -423,7 +415,7 @@ static cell AMX_NATIVE_CALL get_msg_arg_string(AMX *amx, cell *params) return 0; } - const char *szVal = Msg[argn]->cData; + const char *szVal = Msg[argn]->cData.c_str(); return MF_SetAmxString(amx, params[2], szVal?szVal:"", params[3]); } @@ -440,11 +432,7 @@ static cell AMX_NATIVE_CALL set_msg_arg_string(AMX *amx, cell *params) char *szVal = MF_GetAmxString(amx, params[2], 0, &iLen); - if (Msg[argn]->cData) - delete [] Msg[argn]->cData; - Msg[argn]->cData = new char[strlen(szVal)+1]; - strcpy(Msg[argn]->cData, szVal); - Msg[argn]->cData[strlen(szVal)] = 0; + Msg[argn]->cData.assign(szVal); return 1; } diff --git a/dlls/engine/messages.h b/dlls/engine/messages.h index e8d268f2..cfce5fbc 100755 --- a/dlls/engine/messages.h +++ b/dlls/engine/messages.h @@ -36,7 +36,7 @@ public: int type; REAL fData; - char *cData; + CString cData; int iData; };