amxmodx/amxmodx/CString.h

392 lines
5.2 KiB
C
Raw Normal View History

2014-08-04 12:36:20 +04:00
// vim: set ts=4 sw=4 tw=99 noet:
//
// AMX Mod X, based on AMX Mod by Aleksander Naszko ("OLO").
// Copyright (C) The AMX Mod X Development Team.
//
// This software is licensed under the GNU General Public License, version 3 or higher.
// Additional exceptions apply. For full license details, see LICENSE.txt or visit:
// https://alliedmods.net/amxmodx-license
2004-01-31 23:56:22 +03:00
2004-08-13 12:46:04 +04:00
#ifndef _INCLUDE_CSTRING_H
#define _INCLUDE_CSTRING_H
2004-01-31 23:56:22 +03:00
2005-08-02 11:01:25 +04:00
#include <string.h>
2005-08-02 11:07:35 +04:00
#include <stdio.h>
2005-08-02 11:01:25 +04:00
2004-08-13 12:46:04 +04:00
//by David "BAILOPAN" Anderson
class String
2004-01-31 23:56:22 +03:00
{
public:
String()
{
v = NULL;
2005-07-23 00:41:32 +04:00
a_size = 0;
2005-08-02 11:01:25 +04:00
//assign("");
}
2004-08-13 12:46:04 +04:00
~String()
{
if (v)
delete [] v;
}
String(const char *src)
{
v = NULL;
2005-07-23 00:41:32 +04:00
a_size = 0;
assign(src);
}
2005-08-02 11:07:35 +04:00
const char * _fread(FILE *fp)
{
Grow(512, false);
char *ret = fgets(v, 511, fp);
return ret;
}
2005-11-15 23:21:29 +03:00
String(const String &src)
{
v = NULL;
2005-07-23 00:41:32 +04:00
a_size = 0;
assign(src.c_str());
}
2004-08-13 12:46:04 +04:00
2005-08-02 11:01:25 +04:00
const char *c_str() { return v?v:""; }
2005-07-23 00:41:32 +04:00
2005-08-02 11:01:25 +04:00
const char *c_str() const { return v?v:""; }
2004-08-13 12:46:04 +04:00
void append(const char *t)
{
2005-08-02 11:01:25 +04:00
Grow(size() + strlen(t) + 1);
2004-08-13 12:46:04 +04:00
strcat(v, t);
}
void append(const char c)
{
2005-08-02 11:01:25 +04:00
size_t len = size();
2005-07-23 00:41:32 +04:00
Grow(len + 2);
v[len] = c;
v[len + 1] = '\0';
}
void append(String &d)
2004-08-13 12:46:04 +04:00
{
2005-07-23 00:41:32 +04:00
append(d.c_str());
}
void assign(const String &src)
{
assign(src.c_str());
2004-08-13 12:46:04 +04:00
}
2004-01-31 23:56:22 +03:00
2004-08-13 12:46:04 +04:00
void assign(const char *d)
{
if (!d)
{
2005-07-23 00:41:32 +04:00
clear();
} else {
size_t len = strlen(d);
Grow(len + 1, false);
memcpy(v, d, len);
v[len] = '\0';
}
2004-08-13 12:46:04 +04:00
}
2004-01-31 23:56:22 +03:00
2004-08-13 12:46:04 +04:00
void clear()
{
2005-08-02 11:01:25 +04:00
if (v)
v[0] = '\0';
2004-08-13 12:46:04 +04:00
}
2004-01-31 23:56:22 +03:00
int compare (const char *d) const
2004-08-13 12:46:04 +04:00
{
2005-08-02 11:01:25 +04:00
if (!v)
return strcmp("", d);
else
return strcmp(v, d);
2004-08-13 12:46:04 +04:00
}
//Added this for amxx inclusion
bool empty() const
2004-08-13 12:46:04 +04:00
{
2005-08-02 11:01:25 +04:00
if (!v)
2005-08-02 11:46:41 +04:00
return true;
2005-08-02 11:01:25 +04:00
2005-07-23 00:41:32 +04:00
if (v[0] == '\0')
2004-08-13 12:46:04 +04:00
return true;
return false;
}
size_t size() const
2004-08-13 12:46:04 +04:00
{
2005-08-02 11:01:25 +04:00
if (v)
return strlen(v);
else
return 0;
}
int find(const char c, int index = 0)
{
2005-11-15 23:21:29 +03:00
int len = static_cast<int>(size());
2005-08-02 11:01:25 +04:00
if (len < 1)
return npos;
2005-11-15 23:21:29 +03:00
if (index >= len || index < 0)
return npos;
2005-11-15 23:21:29 +03:00
int i = 0;
for (i=index; i<len; 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 reparse_newlines()
{
size_t len = size();
int offs = 0;
char c;
if (!len)
return;
for (size_t i=0; i<len; i++)
{
c = v[i];
if (c == '^' && (i != len-1))
{
c = v[++i];
if (c == 'n')
c = '\n';
else if (c == 't')
c = '\t';
offs++;
}
v[i-offs] = c;
}
v[len-offs] = '\0';
}
void trim()
{
2005-08-02 11:01:25 +04:00
if (!v)
return;
unsigned int i = 0;
unsigned int j = 0;
2005-07-23 00:41:32 +04:00
size_t len = strlen(v);
2005-07-23 00:41:32 +04:00
if (len == 1)
{
if (is_space(v[i]))
{
clear();
return;
}
}
unsigned char c0 = v[0];
if (is_space(c0))
{
2005-07-23 00:41:32 +04:00
for (i=0; i<len; i++)
{
2005-07-23 00:41:32 +04:00
if (!is_space(v[i]) || (is_space(v[i]) && ((unsigned char)i==len-1)))
{
erase(0, i);
break;
}
}
}
2005-07-23 00:41:32 +04:00
len = strlen(v);
2005-07-23 00:41:32 +04:00
if (len < 1)
{
return;
}
2005-07-23 00:41:32 +04:00
if (is_space(v[len-1]))
{
for (i=len-1; i<len; i--)
{
if (!is_space(v[i])
|| (is_space(v[i]) && i==0))
{
erase(i+1, j);
break;
}
j++;
}
}
2005-07-23 00:41:32 +04:00
if (len == 1)
{
if (is_space(v[0]))
{
clear();
return;
}
}
}
2005-07-23 00:41:32 +04:00
void erase(unsigned int start, int num = npos)
{
2005-08-02 11:01:25 +04:00
if (!v)
return;
unsigned int i = 0;
2005-07-23 00:41:32 +04:00
size_t len = size();
//check for bounds
2005-11-15 23:21:29 +03:00
if (num == npos || start+num > len-start)
2005-07-23 00:41:32 +04:00
num = len - start;
//do the erasing
bool copyflag = false;
2005-07-23 00:41:32 +04:00
for (i=0; i<len; i++)
{
if (i>=start && i<start+num)
{
2005-07-23 00:41:32 +04:00
if (i+num < len)
{
v[i] = v[i+num];
} else {
v[i] = 0;
}
copyflag = true;
} else if (copyflag) {
2005-07-23 00:41:32 +04:00
if (i+num < len)
{
v[i] = v[i+num];
} else {
v[i] = 0;
}
}
}
2005-07-23 00:41:32 +04:00
len -= num;
v[len] = 0;
}
String substr(unsigned int index, int num = npos)
{
2005-08-02 11:01:25 +04:00
if (!v)
2005-08-02 12:10:04 +04:00
{
String b("");
return b;
}
2005-08-02 11:01:25 +04:00
String ns;
2005-07-23 00:41:32 +04:00
size_t len = size();
if (index >= len || !v)
return ns;
if (num == npos)
{
2005-07-23 00:41:32 +04:00
num = len - index;
} else if (index+num >= len) {
num = len - index;
}
2005-11-15 23:21:29 +03:00
unsigned int i = 0;
2005-07-23 00:41:32 +04:00
unsigned int nslen = num + 2;
2005-07-23 00:41:32 +04:00
ns.Grow(nslen);
2005-07-23 00:41:32 +04:00
for (i=index; i<index+num; i++)
ns.append(v[i]);
return ns;
}
void toLower()
{
2005-08-02 11:01:25 +04:00
if (!v)
return;
unsigned int i = 0;
2005-07-23 00:41:32 +04:00
size_t len = strlen(v);
for (i=0; i<len; i++)
{
if (v[i] >= 65 && v[i] <= 90)
2005-07-23 00:41:32 +04:00
v[i] &= ~(1<<5);
}
}
String & operator = (const String &src)
{
assign(src);
return *this;
}
String & operator = (const char *src)
{
assign(src);
return *this;
}
char operator [] (unsigned int index)
{
2005-08-02 11:01:25 +04:00
if (index > size() || !v)
{
return -1;
} else {
return v[index];
}
2004-08-13 12:46:04 +04:00
}
int at(int a)
{
2005-08-02 11:01:25 +04:00
if (a < 0 || a >= (int)size() || !v)
return -1;
return v[a];
}
bool at(int at, char c)
{
2005-08-02 11:01:25 +04:00
if (at < 0 || at >= (int)size() || !v)
return false;
v[at] = c;
return true;
}
2004-08-13 12:46:04 +04:00
private:
2005-07-23 00:41:32 +04:00
void Grow(unsigned int d, bool copy=true)
2004-08-13 12:46:04 +04:00
{
2005-07-23 00:41:32 +04:00
if (d <= a_size)
2004-08-13 12:46:04 +04:00
return;
2005-07-23 00:41:32 +04:00
char *n = new char[d + 1];
2005-08-02 11:01:25 +04:00
if (copy && v)
2005-07-23 00:41:32 +04:00
strcpy(n, v);
2005-08-02 11:01:25 +04:00
if (v)
delete [] v;
2005-08-02 11:46:41 +04:00
else
strcpy(n, "");
2005-07-23 00:41:32 +04:00
v = n;
a_size = d + 1;
2004-08-13 12:46:04 +04:00
}
char *v;
2005-07-23 00:41:32 +04:00
unsigned int a_size;
public:
static const int npos = -1;
2004-08-13 12:46:04 +04:00
};
2004-01-31 23:56:22 +03:00
#endif //_INCLUDE_CSTRING_H