2004-08-13 10:41:39 +00:00
|
|
|
/* 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.
|
|
|
|
*/
|
|
|
|
|
2004-06-29 08:14:44 +00:00
|
|
|
#ifndef _INCLUDE_CSTRING_H
|
|
|
|
#define _INCLUDE_CSTRING_H
|
|
|
|
|
|
|
|
//by David "BAILOPAN" Anderson
|
2004-09-08 21:13:04 +00:00
|
|
|
class String
|
2004-06-29 08:14:44 +00:00
|
|
|
{
|
|
|
|
public:
|
2004-09-08 21:13:04 +00:00
|
|
|
String()
|
|
|
|
{
|
|
|
|
v = NULL;
|
2005-07-22 09:26:37 +00:00
|
|
|
a_size = 0;
|
2005-07-29 21:30:52 +00:00
|
|
|
//assign("");
|
2004-09-08 21:13:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
~String()
|
|
|
|
{
|
|
|
|
if (v)
|
|
|
|
delete [] v;
|
|
|
|
}
|
|
|
|
|
|
|
|
String(const char *src)
|
|
|
|
{
|
|
|
|
v = NULL;
|
2005-07-22 09:26:37 +00:00
|
|
|
a_size = 0;
|
|
|
|
assign(src);
|
2004-09-08 21:13:04 +00:00
|
|
|
}
|
2004-06-29 08:14:44 +00:00
|
|
|
|
2004-09-08 21:13:04 +00:00
|
|
|
String(String &src)
|
|
|
|
{
|
|
|
|
v = NULL;
|
2005-07-22 09:26:37 +00:00
|
|
|
a_size = 0;
|
2004-09-08 21:13:04 +00:00
|
|
|
assign(src.c_str());
|
|
|
|
}
|
2004-08-13 10:41:39 +00:00
|
|
|
|
2005-07-22 09:26:37 +00:00
|
|
|
const char *c_str() { return v; }
|
|
|
|
|
|
|
|
const char *c_str() const { return v; }
|
2004-06-29 08:14:44 +00:00
|
|
|
|
|
|
|
void append(const char *t)
|
|
|
|
{
|
2005-07-29 21:30:52 +00:00
|
|
|
Grow(size() + strlen(t) + 1);
|
2004-06-29 08:14:44 +00:00
|
|
|
strcat(v, t);
|
2004-09-08 21:13:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void append(const char c)
|
|
|
|
{
|
2005-07-29 21:30:52 +00:00
|
|
|
size_t len = size();
|
2005-07-22 09:26:37 +00:00
|
|
|
Grow(len + 2);
|
|
|
|
v[len] = c;
|
|
|
|
v[len + 1] = '\0';
|
2004-06-29 08:14:44 +00:00
|
|
|
}
|
|
|
|
|
2004-09-08 21:13:04 +00:00
|
|
|
void append(String &d)
|
2004-06-29 08:14:44 +00:00
|
|
|
{
|
2005-07-22 09:26:37 +00:00
|
|
|
append(d.c_str());
|
2004-09-08 21:13:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void assign(const String &src)
|
|
|
|
{
|
|
|
|
assign(src.c_str());
|
2004-06-29 08:14:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void assign(const char *d)
|
|
|
|
{
|
|
|
|
if (!d)
|
|
|
|
{
|
2005-07-22 09:26:37 +00:00
|
|
|
clear();
|
2004-09-08 21:13:04 +00:00
|
|
|
} else {
|
2005-07-22 09:26:37 +00:00
|
|
|
Grow(strlen(d) + 1, false);
|
|
|
|
strcpy(v, d);
|
2004-09-08 21:13:04 +00:00
|
|
|
}
|
2004-06-29 08:14:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void clear()
|
|
|
|
{
|
2005-07-29 21:30:52 +00:00
|
|
|
if (v)
|
|
|
|
v[0] = '\0';
|
2004-06-29 08:14:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int compare (const char *d)
|
|
|
|
{
|
2005-07-29 21:30:52 +00:00
|
|
|
if (!v)
|
|
|
|
return strcmp("", d);
|
|
|
|
else
|
|
|
|
return strcmp(v, d);
|
2004-06-29 08:14:44 +00:00
|
|
|
}
|
|
|
|
|
2004-08-13 10:41:39 +00:00
|
|
|
//Added this for amxx inclusion
|
|
|
|
bool empty()
|
|
|
|
{
|
2005-07-29 21:30:52 +00:00
|
|
|
if (!v)
|
|
|
|
return false;
|
|
|
|
|
2005-07-22 09:26:37 +00:00
|
|
|
if (v[0] == '\0')
|
2004-08-13 10:41:39 +00:00
|
|
|
return true;
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2005-07-22 09:26:37 +00:00
|
|
|
size_t size()
|
2004-06-29 08:14:44 +00:00
|
|
|
{
|
2005-07-29 21:30:52 +00:00
|
|
|
if (v)
|
|
|
|
return strlen(v);
|
|
|
|
else
|
|
|
|
return 0;
|
2004-09-08 21:13:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int find(const char c, int index = 0)
|
|
|
|
{
|
2005-07-29 21:30:52 +00:00
|
|
|
size_t len = size();
|
|
|
|
if (len < 1)
|
|
|
|
return npos;
|
2005-07-22 09:26:37 +00:00
|
|
|
if (index >= (int)len || index < 0)
|
2004-09-08 21:13:04 +00:00
|
|
|
return npos;
|
|
|
|
unsigned int i = 0;
|
2005-07-22 09:26:37 +00:00
|
|
|
for (i=index; i<(int)len; i++)
|
2004-09-08 21:13:04 +00:00
|
|
|
{
|
|
|
|
if (v[i] == c)
|
|
|
|
{
|
|
|
|
return i;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return npos;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool is_space(int c)
|
|
|
|
{
|
|
|
|
if (c == '\f' || c == '\n' ||
|
|
|
|
c == '\t' || c == '\r' ||
|
2004-09-17 19:04:44 +00:00
|
|
|
c == '\v' || c == ' ')
|
2004-09-08 21:13:04 +00:00
|
|
|
{
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
void trim()
|
|
|
|
{
|
2005-07-29 21:30:52 +00:00
|
|
|
if (!v)
|
|
|
|
return;
|
|
|
|
|
2004-09-08 21:13:04 +00:00
|
|
|
unsigned int i = 0;
|
|
|
|
unsigned int j = 0;
|
2005-07-22 09:26:37 +00:00
|
|
|
size_t len = strlen(v);
|
2004-09-08 21:13:04 +00:00
|
|
|
|
2005-07-22 09:26:37 +00:00
|
|
|
if (len == 1)
|
2004-09-08 21:13:04 +00:00
|
|
|
{
|
|
|
|
if (is_space(v[i]))
|
|
|
|
{
|
|
|
|
clear();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
unsigned char c0 = v[0];
|
|
|
|
|
|
|
|
if (is_space(c0))
|
|
|
|
{
|
2005-07-22 09:26:37 +00:00
|
|
|
for (i=0; i<len; i++)
|
2004-09-08 21:13:04 +00:00
|
|
|
{
|
2005-07-22 09:26:37 +00:00
|
|
|
if (!is_space(v[i]) || (is_space(v[i]) && ((unsigned char)i==len-1)))
|
2004-09-08 21:13:04 +00:00
|
|
|
{
|
|
|
|
erase(0, i);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2005-07-22 09:26:37 +00:00
|
|
|
len = strlen(v);
|
2004-09-08 21:13:04 +00:00
|
|
|
|
2005-07-22 09:26:37 +00:00
|
|
|
if (len < 1)
|
2004-09-08 21:13:04 +00:00
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2005-07-22 09:26:37 +00:00
|
|
|
if (is_space(v[len-1]))
|
2004-09-08 21:13:04 +00:00
|
|
|
{
|
2005-07-22 09:26:37 +00:00
|
|
|
for (i=len-1; i>=0; i--)
|
2004-09-08 21:13:04 +00:00
|
|
|
{
|
|
|
|
if (!is_space(v[i])
|
|
|
|
|| (is_space(v[i]) && i==0))
|
|
|
|
{
|
|
|
|
erase(i+1, j);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
j++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2005-07-22 09:26:37 +00:00
|
|
|
if (len == 1)
|
2004-09-08 21:13:04 +00:00
|
|
|
{
|
|
|
|
if (is_space(v[0]))
|
|
|
|
{
|
|
|
|
clear();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2005-07-22 09:26:37 +00:00
|
|
|
void erase(unsigned int start, int num = npos)
|
2004-09-08 21:13:04 +00:00
|
|
|
{
|
2005-07-29 21:30:52 +00:00
|
|
|
if (!v)
|
|
|
|
return;
|
2004-09-08 21:13:04 +00:00
|
|
|
unsigned int i = 0;
|
2005-07-22 09:26:37 +00:00
|
|
|
size_t len = size();
|
2004-09-08 21:13:04 +00:00
|
|
|
//check for bounds
|
2005-07-22 09:26:37 +00:00
|
|
|
if (num == npos || start+num > len-num+1)
|
|
|
|
num = len - start;
|
2004-09-08 21:13:04 +00:00
|
|
|
//do the erasing
|
|
|
|
bool copyflag = false;
|
2005-07-22 09:26:37 +00:00
|
|
|
for (i=0; i<len; i++)
|
2004-09-08 21:13:04 +00:00
|
|
|
{
|
|
|
|
if (i>=start && i<start+num)
|
|
|
|
{
|
2005-07-22 09:26:37 +00:00
|
|
|
if (i+num < len)
|
2004-09-08 21:13:04 +00:00
|
|
|
{
|
|
|
|
v[i] = v[i+num];
|
|
|
|
} else {
|
|
|
|
v[i] = 0;
|
|
|
|
}
|
|
|
|
copyflag = true;
|
|
|
|
} else if (copyflag) {
|
2005-07-22 09:26:37 +00:00
|
|
|
if (i+num < len)
|
2004-09-08 21:13:04 +00:00
|
|
|
{
|
|
|
|
v[i] = v[i+num];
|
|
|
|
} else {
|
|
|
|
v[i] = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2005-07-22 09:26:37 +00:00
|
|
|
len -= num;
|
|
|
|
v[len] = 0;
|
2004-09-08 21:13:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
String substr(unsigned int index, int num = npos)
|
|
|
|
{
|
2005-07-29 21:30:52 +00:00
|
|
|
if (!v)
|
|
|
|
return String("");
|
|
|
|
|
2004-09-08 21:13:04 +00:00
|
|
|
String ns;
|
|
|
|
|
2005-07-22 09:26:37 +00:00
|
|
|
size_t len = size();
|
|
|
|
|
|
|
|
if (index >= len || !v)
|
2004-09-08 21:13:04 +00:00
|
|
|
return ns;
|
|
|
|
|
|
|
|
if (num == npos)
|
|
|
|
{
|
2005-07-22 09:26:37 +00:00
|
|
|
num = len - index;
|
|
|
|
} else if (index+num >= len) {
|
|
|
|
num = len - index;
|
2004-09-08 21:13:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
unsigned int i = 0, j=0;
|
2005-07-22 09:26:37 +00:00
|
|
|
unsigned int nslen = num + 2;
|
2004-09-08 21:13:04 +00:00
|
|
|
|
2005-07-22 09:26:37 +00:00
|
|
|
ns.Grow(nslen);
|
2004-09-08 21:13:04 +00:00
|
|
|
|
2005-07-22 09:26:37 +00:00
|
|
|
for (i=index; i<index+num; i++)
|
|
|
|
ns.append(v[i]);
|
2004-09-08 21:13:04 +00:00
|
|
|
|
|
|
|
return ns;
|
|
|
|
}
|
|
|
|
|
|
|
|
void toLower()
|
|
|
|
{
|
2005-07-29 21:30:52 +00:00
|
|
|
if (!v)
|
|
|
|
return;
|
2004-09-08 21:13:04 +00:00
|
|
|
unsigned int i = 0;
|
2005-07-22 09:26:37 +00:00
|
|
|
size_t len = strlen(v);
|
|
|
|
for (i=0; i<len; i++)
|
2004-09-08 21:13:04 +00:00
|
|
|
{
|
|
|
|
if (v[i] >= 65 && v[i] <= 90)
|
2005-07-22 09:26:37 +00:00
|
|
|
v[i] &= ~(1<<5);
|
2004-09-08 21:13:04 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
String & operator = (const String &src)
|
|
|
|
{
|
|
|
|
assign(src);
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
String & operator = (const char *src)
|
|
|
|
{
|
|
|
|
assign(src);
|
|
|
|
return *this;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
char operator [] (unsigned int index)
|
|
|
|
{
|
2005-07-29 21:30:52 +00:00
|
|
|
if (index > size() || !v)
|
2004-09-08 21:13:04 +00:00
|
|
|
{
|
|
|
|
return -1;
|
|
|
|
} else {
|
|
|
|
return v[index];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
int at(int a)
|
|
|
|
{
|
2005-07-29 21:30:52 +00:00
|
|
|
if (a < 0 || a >= (int)size() || !v)
|
2004-09-08 21:13:04 +00:00
|
|
|
return -1;
|
|
|
|
|
|
|
|
return v[a];
|
|
|
|
}
|
|
|
|
|
|
|
|
bool at(int at, char c)
|
|
|
|
{
|
2005-07-29 21:30:52 +00:00
|
|
|
if (at < 0 || at >= (int)size() || !v)
|
2004-09-08 21:13:04 +00:00
|
|
|
return false;
|
|
|
|
|
|
|
|
v[at] = c;
|
|
|
|
|
|
|
|
return true;
|
2004-06-29 08:14:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
2005-07-22 09:26:37 +00:00
|
|
|
void Grow(unsigned int d, bool copy=true)
|
2004-06-29 08:14:44 +00:00
|
|
|
{
|
2005-07-22 09:26:37 +00:00
|
|
|
if (d <= a_size)
|
2004-06-30 03:21:14 +00:00
|
|
|
return;
|
2005-07-22 09:26:37 +00:00
|
|
|
char *n = new char[d + 1];
|
2005-07-29 21:30:52 +00:00
|
|
|
if (copy && v)
|
2005-07-22 09:26:37 +00:00
|
|
|
strcpy(n, v);
|
2005-07-29 21:30:52 +00:00
|
|
|
if (v)
|
|
|
|
delete [] v;
|
2005-07-22 09:26:37 +00:00
|
|
|
v = n;
|
|
|
|
a_size = d + 1;
|
2004-06-29 08:14:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
char *v;
|
2005-07-22 09:26:37 +00:00
|
|
|
unsigned int a_size;
|
2004-09-08 21:13:04 +00:00
|
|
|
public:
|
|
|
|
static const int npos = -1;
|
2004-06-29 08:14:44 +00:00
|
|
|
};
|
|
|
|
|
2004-08-30 22:24:43 +00:00
|
|
|
#endif //_INCLUDE_CSTRING_H
|