updated version of CString.h

This commit is contained in:
Borja Ferrer 2005-09-10 21:04:05 +00:00
parent f0c5e44985
commit 2d787f43de

View File

@ -32,6 +32,9 @@
#ifndef _INCLUDE_CSTRING_H #ifndef _INCLUDE_CSTRING_H
#define _INCLUDE_CSTRING_H #define _INCLUDE_CSTRING_H
#include <string.h>
#include <stdio.h>
//by David "BAILOPAN" Anderson //by David "BAILOPAN" Anderson
class String class String
{ {
@ -39,10 +42,8 @@ public:
String() String()
{ {
v = NULL; v = NULL;
mSize = 0; a_size = 0;
cSize = 0; //assign("");
Grow(2);
assign("");
} }
~String() ~String()
@ -54,41 +55,45 @@ public:
String(const char *src) String(const char *src)
{ {
v = NULL; v = NULL;
mSize = 0; a_size = 0;
cSize = 0; assign(src); assign(src);
}
const char * _fread(FILE *fp)
{
Grow(512, false);
char *ret = fgets(v, 511, fp);
return ret;
} }
String(String &src) String(String &src)
{ {
v = NULL; v = NULL;
mSize = 0; a_size = 0;
cSize = 0;
assign(src.c_str()); assign(src.c_str());
} }
const char *c_str() { return v?v:""; } const char *c_str() { return v?v:""; }
const char *c_str() const { return v?v:""; } const char *c_str() const { return v?v:""; }
void append(const char *t) void append(const char *t)
{ {
Grow(cSize + strlen(t) + 1); Grow(size() + strlen(t) + 1);
strcat(v, t); strcat(v, t);
cSize = strlen(v);
} }
void append(const char c) void append(const char c)
{ {
Grow(cSize + 2); size_t len = size();
v[cSize] = c; Grow(len + 2);
v[++cSize] = 0; v[len] = c;
v[len + 1] = '\0';
} }
void append(String &d) void append(String &d)
{ {
const char *t = d.c_str(); append(d.c_str());
Grow(cSize + strlen(t));
strcat(v, t);
cSize = strlen(v);
} }
void assign(const String &src) void assign(const String &src)
@ -100,79 +105,56 @@ public:
{ {
if (!d) if (!d)
{ {
Grow(1); clear();
cSize = 0;
strcpy(v, "");
return;
}
Grow(strlen(d));
if (v)
{
strcpy(v, d);
cSize = strlen(v);
} else { } else {
cSize = 0; Grow(strlen(d) + 1, false);
strcpy(v, d);
} }
} }
void clear() void clear()
{ {
if (v) if (v)
{ v[0] = '\0';
v[0] = 0;
cSize = 0;
}
} }
int compare (const char *d) int compare (const char *d)
{ {
if (v) { if (!v)
if (d) { return strcmp("", d);
return strcmp(v, d); else
} else { return strcmp(v, d);
return strlen(v);
}
} else {
if (d) {
return strlen(d);
} else {
return 0;
}
}
} }
//Added this for amxx inclusion //Added this for amxx inclusion
bool empty() bool empty()
{ {
if (!v || !cSize) if (!v)
return true;
if (v[0] == '\0')
return true; return true;
return false; return false;
} }
int size() size_t size()
{ {
if (!v) if (v)
return strlen(v);
else
return 0; 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) int find(const char c, int index = 0)
{ {
if (!v) size_t len = size();
if (len < 1)
return npos; return npos;
if (index >= (int)cSize || index < 0) if (index >= (int)len || index < 0)
return npos; return npos;
unsigned int i = 0; unsigned int i = 0;
for (i=index; i<cSize; i++) for (i=index; i<(int)len; i++)
{ {
if (v[i] == c) if (v[i] == c)
{ {
@ -199,10 +181,12 @@ public:
{ {
if (!v) if (!v)
return; return;
unsigned int i = 0; unsigned int i = 0;
unsigned int j = 0; unsigned int j = 0;
size_t len = strlen(v);
if (cSize == 1) if (len == 1)
{ {
if (is_space(v[i])) if (is_space(v[i]))
{ {
@ -215,9 +199,9 @@ public:
if (is_space(c0)) if (is_space(c0))
{ {
for (i=0; i<cSize; i++) for (i=0; i<len; i++)
{ {
if (!is_space(v[i]) || (is_space(v[i]) && ((unsigned char)i==cSize-1))) if (!is_space(v[i]) || (is_space(v[i]) && ((unsigned char)i==len-1)))
{ {
erase(0, i); erase(0, i);
break; break;
@ -225,16 +209,16 @@ public:
} }
} }
cSize = strlen(v); len = strlen(v);
if (cSize < 1) if (len < 1)
{ {
return; return;
} }
if (is_space(v[cSize-1])) if (is_space(v[len-1]))
{ {
for (i=cSize-1; i>=0; i--) for (i=len-1; i>=0; i--)
{ {
if (!is_space(v[i]) if (!is_space(v[i])
|| (is_space(v[i]) && i==0)) || (is_space(v[i]) && i==0))
@ -246,7 +230,7 @@ public:
} }
} }
if (cSize == 1) if (len == 1)
{ {
if (is_space(v[0])) if (is_space(v[0]))
{ {
@ -256,21 +240,22 @@ public:
} }
} }
String & erase(unsigned int start, int num = npos) void erase(unsigned int start, int num = npos)
{ {
if (!v) if (!v)
return (*this); return;
unsigned int i = 0; unsigned int i = 0;
size_t len = size();
//check for bounds //check for bounds
if (num == npos || start+num > cSize-num+1) if (num == npos || start+num > len-num+1)
num = cSize - start; num = len - start;
//do the erasing //do the erasing
bool copyflag = false; bool copyflag = false;
for (i=0; i<cSize; i++) for (i=0; i<len; i++)
{ {
if (i>=start && i<start+num) if (i>=start && i<start+num)
{ {
if (i+num < cSize) if (i+num < len)
{ {
v[i] = v[i+num]; v[i] = v[i+num];
} else { } else {
@ -278,7 +263,7 @@ public:
} }
copyflag = true; copyflag = true;
} else if (copyflag) { } else if (copyflag) {
if (i+num < cSize) if (i+num < len)
{ {
v[i] = v[i+num]; v[i] = v[i+num];
} else { } else {
@ -286,38 +271,39 @@ public:
} }
} }
} }
cSize -= num; len -= num;
v[cSize] = 0; v[len] = 0;
return (*this);
} }
String substr(unsigned int index, int num = npos) String substr(unsigned int index, int num = npos)
{ {
if (!v)
{
String b("");
return b;
}
String ns; String ns;
if (index >= cSize || !v) size_t len = size();
if (index >= len || !v)
return ns; return ns;
if (num == npos) if (num == npos)
{ {
num = cSize - index; num = len - index;
} else if (index+num >= cSize) { } else if (index+num >= len) {
num = cSize - index; num = len - index;
} }
unsigned int i = 0, j=0; unsigned int i = 0, j=0;
char *s = new char[cSize+1]; unsigned int nslen = num + 2;
ns.Grow(nslen);
for (i=index; i<index+num; i++) for (i=index; i<index+num; i++)
{ ns.append(v[i]);
s[j++] = v[i];
}
s[j] = 0;
ns.assign(s);
delete [] s;
return ns; return ns;
} }
@ -327,10 +313,11 @@ public:
if (!v) if (!v)
return; return;
unsigned int i = 0; unsigned int i = 0;
for (i=0; i<cSize; i++) size_t len = strlen(v);
for (i=0; i<len; i++)
{ {
if (v[i] >= 65 && v[i] <= 90) if (v[i] >= 65 && v[i] <= 90)
v[i] |= 32; v[i] &= ~(1<<5);
} }
} }
@ -349,7 +336,7 @@ public:
char operator [] (unsigned int index) char operator [] (unsigned int index)
{ {
if (index > cSize) if (index > size() || !v)
{ {
return -1; return -1;
} else { } else {
@ -359,7 +346,7 @@ public:
int at(int a) int at(int a)
{ {
if (a < 0 || a >= (int)cSize) if (a < 0 || a >= (int)size() || !v)
return -1; return -1;
return v[a]; return v[a];
@ -367,7 +354,7 @@ public:
bool at(int at, char c) bool at(int at, char c)
{ {
if (at < 0 || at >= (int)cSize) if (at < 0 || at >= (int)size() || !v)
return false; return false;
v[at] = c; v[at] = c;
@ -376,27 +363,23 @@ public:
} }
private: private:
void Grow(unsigned int d) void Grow(unsigned int d, bool copy=true)
{ {
if (d<1) if (d <= a_size)
return; return;
if (d > mSize) char *n = new char[d + 1];
{ if (copy && v)
mSize = d + 16; // allocate a buffer strcpy(n, v);
char *t = new char[d+1]; if (v)
if (v) { delete [] v;
strcpy(t, v); else
t[cSize] = 0; strcpy(n, "");
delete [] v; v = n;
} a_size = d + 1;
v = t;
mSize = d;
}
} }
char *v; char *v;
unsigned int mSize; unsigned int a_size;
unsigned int cSize;
public: public:
static const int npos = -1; static const int npos = -1;
}; };