This commit is contained in:
David Anderson 2004-08-22 17:57:23 +00:00
parent 909460a58f
commit 2e247acdad
3 changed files with 21 additions and 334 deletions

View File

@ -1,99 +1,26 @@
/* AMX Mod X
*
* by the AMX Mod X Development Team
* originally developed by OLO
*
*
* This program is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License as published by the
* Free Software Foundation; either version 2 of the License, or (at
* your option) any later version.
*
* This program is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software Foundation,
* Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*
* In addition, as a special exception, the author gives permission to
* link the code of this program with the Half-Life Game Engine ("HL
* Engine") and Modified Game Libraries ("MODs") developed by Valve,
* L.L.C ("Valve"). You must obey the GNU General Public License in all
* respects for all of the code used other than the HL Engine and MODs
* from Valve. If you modify this file, you may extend this exception
* to your version of the file, but you are not obligated to do so. If
* you do not wish to do so, delete this exception statement from your
* version.
*/
#ifndef _INCLUDE_CSTRING_H
#define _INCLUDE_CSTRING_H
//by David "BAILOPAN" Anderson
class String
class CString
{
public:
String()
{
v = NULL;
mSize = 0;
cSize = 0;
Grow(2);
assign("");
}
~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());
}
CString() { v = NULL; mSize = 0; }
~CString() { if (v) delete [] v; }
const char *c_str() { return v?v:""; }
const char *c_str() const { return v?v:""; }
void append(const char *t)
{
Grow(cSize + strlen(t));
Grow(strlen(v) + strlen(t));
strcat(v, t);
cSize = strlen(v);
}
void append(const char c)
{
Grow(cSize + 2);
v[cSize] = c;
v[++cSize] = 0;
}
void append(String &d)
void append(CString &d)
{
const char *t = d.c_str();
Grow(cSize + strlen(t));
Grow(strlen(v) + strlen(t));
strcat(v, t);
cSize = strlen(v);
}
void assign(const String &src)
{
assign(src.c_str());
}
void assign(const char *d)
@ -101,27 +28,20 @@ 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)
{
v[0] = 0;
cSize = 0;
}
delete [] v;
v = NULL;
mSize = 0;
}
int compare (const char *d)
@ -141,252 +61,24 @@ public:
}
}
//Added this for amxx inclusion
bool empty()
{
if (!v || !cSize)
return true;
return false;
}
int size()
{
if (!v)
return 0;
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<cSize; i++)
{
if (v[i] == c)
{
return i;
}
}
return npos;
}
bool is_space(int c)
{
if (c == '\f' || c == '\n' ||
c == '\t' || c == '\r' ||
c == 'v' || c == ' ')
{
return true;
}
return false;
}
void trim()
{
if (!v)
return;
unsigned int i = 0;
unsigned int j = 0;
if (cSize == 1)
{
if (is_space(v[i]))
{
clear();
return;
}
}
unsigned char c0 = v[0];
if (is_space(c0))
{
for (i=0; i<cSize; i++)
{
if (!is_space(v[i]) || (is_space(v[i]) && ((unsigned char)i==cSize-1)))
{
erase(0, i);
break;
}
}
}
cSize = strlen(v);
if (cSize < 1)
{
return;
}
if (is_space(v[cSize-1]))
{
for (i=cSize-1; 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<cSize; i++)
{
if (i>=start && i<start+num)
{
if (i+num < cSize)
{
v[i] = v[i+num];
} else {
v[i] = 0;
}
copyflag = true;
} else if (copyflag) {
if (i+num < cSize)
{
v[i] = v[i+num];
} else {
v[i] = 0;
}
}
}
cSize -= num;
v[cSize] = 0;
return (*this);
}
String substr(unsigned int index, int num = npos)
{
String ns;
if (index >= 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<index+num; i++)
{
s[j++] = v[i];
}
s[j] = 0;
ns.assign(s);
delete [] s;
return ns;
}
void toLower()
{
if (!v)
return;
unsigned int i = 0;
for (i=0; i<cSize; i++)
{
if (v[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 (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;
return strlen(v);
}
private:
void Grow(unsigned int d)
void Grow(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[cSize] = 0;
t[strlen(v)] = 0;
delete [] v;
}
v = t;
@ -395,10 +87,7 @@ private:
}
char *v;
unsigned int mSize;
unsigned int cSize;
public:
static const int npos = -1;
int mSize;
};
#endif //_INCLUDE_CSTRING_H
#endif //_INCLUDE_CSTRING_H

View File

@ -76,7 +76,7 @@ template <class T> class CVector
delete [] m_Data;
}
if (m_Size < size)
m_CurrentSize = size;
m_CurrentUsedSize = size;
m_Data = newData;
m_Size = size;
return true;
@ -90,7 +90,6 @@ protected:
T *m_Data;
size_t m_Size;
size_t m_CurrentUsedSize;
size_t m_CurrentSize;
public:
class iterator
{
@ -331,7 +330,7 @@ public:
{
if (pos > m_CurrentUsedSize)
{
ASSERT(0);
// ASSERT(0);
}
return m_Data[pos];
}
@ -340,7 +339,7 @@ public:
{
if (pos > m_CurrentUsedSize)
{
ASSERT(0);
// ASSERT(0);
}
return m_Data[pos];
}
@ -398,7 +397,7 @@ public:
if (where != m_Data)
--where;
// validate iter
if (where < m_Data || where >= (m_Data + m_CurrentUsedSize))
if (where < m_Data || where >= (m_Data + m_CurrentSize))
return false;
++m_CurrentUsedSize;
@ -416,7 +415,7 @@ public:
void erase(iterator where)
{
// validate iter
if (where < m_Data || where >= (m_Data + m_CurrentUsedSize))
if (where < m_Data || where >= (m_Data + m_CurrentSize))
return false;
if (m_CurrentUsedSize > 1)
@ -438,5 +437,4 @@ public:
}
};
#endif // __CVECTOR_H__
#endif // __CVECTOR_H__

View File

@ -1,7 +1,7 @@
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <libpq-fe.h>
#include "libpq-fe.h"
#include "CVector.h"
#include "CString.h"
#include "amxxmodule.h"