1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226
|
#ifndef GRAP_STRING_H
#define GRAP_STRING_H
// These should be replaced by the standard string library, but under
// g++ 2.7.2.1 that library is incompatible with the STL
static const int strchunk = 256;
class String {
public:
typedef size_t size_type;
static const size_type npos = ~0;
String() : str(0), len(0) { }
String(const char *c) : str(0), len(0) {
resize(::strlen(c)+1);
strcpy(str,c);
};
String(const int i) : str(0), len(0) {
resize(strchunk);
snprintf(str,len,"%d",i);
};
String(const double d, const String *fmt = 0) : str(0), len(0) {
resize(strchunk);
if ( fmt ) snprintf(str,len,fmt->str,d);
else snprintf(str,len,"%g",d);
};
String(const String* x) : str(0), len(0) {
resize(x->len);
if ( len )
strcpy(str,x->str);
};
String(const String& x) : str(0), len(0) {
resize(x.len);
if ( len)
strcpy(str,x.str);
};
~String() {
if ( str) {
delete str;
str = 0;
}
len = 0;
};
char& operator[](const int i) {
if ( i < len ) return str[i];
else {
resize(i+1);
return str[i];
}
};
String& operator=(const String &s) {
if ( len < s.len) resize(s.len);
if ( len )strcpy(str,s.str);
return *this;
};
String& operator+=(const String& s) {
int nlen = s.strlen() + strlen()+ 1;
if ( len < nlen ) resize(nlen);
if ( len ) strcat(str,s.str);
return *this;
}
String& operator+=(const char *s) {
int nlen = ::strlen(s) + strlen() + 1;
if ( len < nlen ) resize(nlen);
if ( len ) strcat(str,s);
return *this;
}
String& operator+=(const char c) {
int last = strlen();
if ( len < last +2 ) resize(last+2);
last=strlen();
str[last] = c; str[last+1] = '\0';
return *this;
}
String operator+(const String& s) {
String rc = *this;
rc+= s;
return rc;
}
String operator+(const char *c) {
String rc = *this;
rc+= c;
return rc;
}
size_type find(char c) const {
size_type len = length();
for ( size_type i = 0; i < len; i++ )
if ( str[i] == c ) return i;
return npos;
}
void erase(size_type start, size_type end) {
size_type lim = length();
if ( end == npos ) end = lim-1;
if ( len ) {
strcpy(str+start,str+end);
str[(lim-1)-end-start] = '\0';
}
}
int operator==(const String &s) const {
return !strcmp(str,s.str);
}
int operator<(const String &s) const {
return ( strcmp(str,s.str) < 0) ;
}
int operator==(const char *s) const {
return !strcmp(str,s);
}
int operator!=(const String &s) const {
return strcmp(str,s.str);
}
int operator!=(const char *s) const {
return strcmp(str,s);
}
int operator<(String& s) {
return strcmp(str,s.str) < 0;
}
void quote() {
int l;
if ( len < strlen()+3 ) resize(len+3);
for ( int i = len-1; i >=1; i-- )
str[i] = str[i-1];
l = strlen();
if ( l == 0 ) l = 1;
str[0] = '"'; str[l] = '"'; str[l+1] = '\0';
}
void unquote() {
int i;
if ( str[0] == '"' ) {
for ( i = 0; i < len-2; i++ ) str[i] = str[i+1];
str[strlen()-1] = '\0';
}
i = strlen();
if ( str[i-1] == '"' ) str[i-1] = '\0';
}
void strncpy(char *s, int l) {
::strncpy(s,str,l);
}
int strlen() const {
return ( (str) ? ::strlen(str) : 0 );
}
const char *c_str() const { return str;}
int length() const { return strlen();}
ostream& print(ostream& f) const {
return f << str;
}
String substr(int i, int n=0) {
String s;
s.resize(strlen());
if ( n == 0 || n > strlen()-i )
n = strlen() - i;
for ( int j = 0; j < n; j++ )
s.str[j] = str[i+j];
s.str[n] = '\0';
return s;
}
protected:
char *str;
int len;
void resize(int ns) {
char *c;
int s;
s =( ns/strchunk + (( ns % strchunk ) ? 1 : 0 )) * strchunk;
c = new char[s];
if ( len )
for ( int j = 0; j < len; j++) c[j] = str[j];
else
c[0] = '\0';
if ( str ) delete str;
str = c;
len = s;
}
};
inline ostream& operator<<(ostream& f, const String& s) { return s.print(f); }
// These functions and classes are all duplicated for standard strings
// in grap_data.h. The choice is to either support 2 versions of
// these functions or to make the emulation of standard strings better
// and support that. Because this code was already written, I chose
// this.
inline void unquote(String *s) {
s->unquote();
}
inline void quote(String *s) {
s->quote();
}
#endif
|