new CString

This commit is contained in:
David Anderson 2004-06-29 08:14:44 +00:00
parent 429c295738
commit 6b89c12f73
5 changed files with 101 additions and 19 deletions

90
dlls/engine/CString.h Executable file
View File

@ -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

View File

@ -6,6 +6,7 @@
#include <meta_api.h> #include <meta_api.h>
#include <sdk_util.h> #include <sdk_util.h>
#include "CVector.h" #include "CVector.h"
#include "CString.h"
#ifndef CBASEPLAYER_H #ifndef CBASEPLAYER_H
#define CBASEPLAYER_H #define CBASEPLAYER_H
#include <cbase.h> #include <cbase.h>

View File

@ -137,6 +137,9 @@
Name="Header Files" Name="Header Files"
Filter="h;hpp;hxx;hm;inl;inc;xsd" Filter="h;hpp;hxx;hm;inl;inc;xsd"
UniqueIdentifier="{93995380-89BD-4b04-88EB-625FBE52EBFB}"> UniqueIdentifier="{93995380-89BD-4b04-88EB-625FBE52EBFB}">
<File
RelativePath=".\CString.h">
</File>
<File <File
RelativePath=".\CVector.h"> RelativePath=".\CVector.h">
</File> </File>

View File

@ -20,11 +20,7 @@ void argMsg::Reset()
{ {
iData = 0; iData = 0;
fData = 0.0; fData = 0.0;
if (cData) cData.clear();
{
delete [] cData;
cData = NULL;
}
type = 0; type = 0;
} }
@ -51,7 +47,7 @@ void argMsg::Send()
WRITE_COORD(fData); WRITE_COORD(fData);
break; break;
case arg_string: case arg_string:
WRITE_STRING(cData); WRITE_STRING(cData.c_str());
break; break;
case arg_entity: case arg_entity:
WRITE_ENTITY(iData); WRITE_ENTITY(iData);
@ -238,15 +234,11 @@ void WriteString(const char *sz)
} else if (inhook) { } else if (inhook) {
if (++msgCount > Msg.size()) { if (++msgCount > Msg.size()) {
argMsg *p = new argMsg(); argMsg *p = new argMsg();
p->cData = new char[strlen(sz)+1]; p->cData.assign(sz);
strcpy(p->cData, sz);
p->cData[strlen(sz)] = 0;
p->type = arg_string; p->type = arg_string;
Msg.push_back(p); Msg.push_back(p);
} else { } else {
Msg[msgCount-1]->cData = new char[strlen(sz)+1]; Msg[msgCount-1]->cData.assign(sz);
strcpy(Msg[msgCount-1]->cData, sz);
Msg[msgCount-1]->cData[strlen(sz)] = 0;
Msg[msgCount-1]->type = arg_string; Msg[msgCount-1]->type = arg_string;
} }
RETURN_META(MRES_SUPERCEDE); RETURN_META(MRES_SUPERCEDE);
@ -423,7 +415,7 @@ static cell AMX_NATIVE_CALL get_msg_arg_string(AMX *amx, cell *params)
return 0; 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]); 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); char *szVal = MF_GetAmxString(amx, params[2], 0, &iLen);
if (Msg[argn]->cData) Msg[argn]->cData.assign(szVal);
delete [] Msg[argn]->cData;
Msg[argn]->cData = new char[strlen(szVal)+1];
strcpy(Msg[argn]->cData, szVal);
Msg[argn]->cData[strlen(szVal)] = 0;
return 1; return 1;
} }

View File

@ -36,7 +36,7 @@ public:
int type; int type;
REAL fData; REAL fData;
char *cData; CString cData;
int iData; int iData;
}; };