1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/sourcehook/smm_string.h Fri Aug 12 17:33:31 2005 +0000
1.3 @@ -0,0 +1,364 @@
1.4 +/* ======== SourceMM ========
1.5 +* Copyright (C) 2004-2005 Metamod:Source Development Team
1.6 +* No warranties of any kind
1.7 +*
1.8 +* License: zlib/libpng
1.9 +*
1.10 +* Author(s): David "BAILOPAN" Anderson
1.11 +* ============================
1.12 +*/
1.13 +
1.14 +/* AMX Mod X
1.15 + *
1.16 + * by the AMX Mod X Development Team
1.17 + * originally developed by OLO
1.18 + */
1.19 +
1.20 +#ifndef _INCLUDE_CSTRING_H
1.21 +#define _INCLUDE_CSTRING_H
1.22 +
1.23 +#include <string.h>
1.24 +#include <stdio.h>
1.25 +
1.26 +class String
1.27 +{
1.28 +public:
1.29 + String()
1.30 + {
1.31 + v = NULL;
1.32 + a_size = 0;
1.33 + //assign("");
1.34 + }
1.35 +
1.36 + ~String()
1.37 + {
1.38 + if (v)
1.39 + delete [] v;
1.40 + }
1.41 +
1.42 + String(const char *src)
1.43 + {
1.44 + v = NULL;
1.45 + a_size = 0;
1.46 + assign(src);
1.47 + }
1.48 +
1.49 + String(String &src)
1.50 + {
1.51 + v = NULL;
1.52 + a_size = 0;
1.53 + assign(src.c_str());
1.54 + }
1.55 +
1.56 + const char *c_str() { return v?v:""; }
1.57 +
1.58 + const char *c_str() const { return v?v:""; }
1.59 +
1.60 + void append(const char *t)
1.61 + {
1.62 + Grow(size() + strlen(t) + 1);
1.63 + strcat(v, t);
1.64 + }
1.65 +
1.66 + void append(const char c)
1.67 + {
1.68 + size_t len = size();
1.69 + Grow(len + 2);
1.70 + v[len] = c;
1.71 + v[len + 1] = '\0';
1.72 + }
1.73 +
1.74 + void append(String &d)
1.75 + {
1.76 + append(d.c_str());
1.77 + }
1.78 +
1.79 + void assign(const String &src)
1.80 + {
1.81 + assign(src.c_str());
1.82 + }
1.83 +
1.84 + void assign(const char *d)
1.85 + {
1.86 + if (!d)
1.87 + {
1.88 + clear();
1.89 + } else {
1.90 + Grow(strlen(d) + 1, false);
1.91 + strcpy(v, d);
1.92 + }
1.93 + }
1.94 +
1.95 + void clear()
1.96 + {
1.97 + if (v)
1.98 + v[0] = '\0';
1.99 + }
1.100 +
1.101 + int compare (const char *d)
1.102 + {
1.103 + if (!v)
1.104 + return strcmp("", d);
1.105 + else
1.106 + return strcmp(v, d);
1.107 + }
1.108 +
1.109 + //Added this for amxx inclusion
1.110 + bool empty()
1.111 + {
1.112 + if (!v)
1.113 + return true;
1.114 +
1.115 + if (v[0] == '\0')
1.116 + return true;
1.117 +
1.118 + return false;
1.119 + }
1.120 +
1.121 + size_t size()
1.122 + {
1.123 + if (v)
1.124 + return strlen(v);
1.125 + else
1.126 + return 0;
1.127 + }
1.128 +
1.129 + int find(const char c, int index = 0)
1.130 + {
1.131 + size_t len = size();
1.132 + if (len < 1)
1.133 + return npos;
1.134 + if (index >= (int)len || index < 0)
1.135 + return npos;
1.136 + unsigned int i = 0;
1.137 + for (i=index; i<(int)len; i++)
1.138 + {
1.139 + if (v[i] == c)
1.140 + {
1.141 + return i;
1.142 + }
1.143 + }
1.144 +
1.145 + return npos;
1.146 + }
1.147 +
1.148 + bool is_space(int c)
1.149 + {
1.150 + if (c == '\f' || c == '\n' ||
1.151 + c == '\t' || c == '\r' ||
1.152 + c == '\v' || c == ' ')
1.153 + {
1.154 + return true;
1.155 + }
1.156 +
1.157 + return false;
1.158 + }
1.159 +
1.160 + void trim()
1.161 + {
1.162 + if (!v)
1.163 + return;
1.164 +
1.165 + unsigned int i = 0;
1.166 + unsigned int j = 0;
1.167 + size_t len = strlen(v);
1.168 +
1.169 + if (len == 1)
1.170 + {
1.171 + if (is_space(v[i]))
1.172 + {
1.173 + clear();
1.174 + return;
1.175 + }
1.176 + }
1.177 +
1.178 + unsigned char c0 = v[0];
1.179 +
1.180 + if (is_space(c0))
1.181 + {
1.182 + for (i=0; i<len; i++)
1.183 + {
1.184 + if (!is_space(v[i]) || (is_space(v[i]) && ((unsigned char)i==len-1)))
1.185 + {
1.186 + erase(0, i);
1.187 + break;
1.188 + }
1.189 + }
1.190 + }
1.191 +
1.192 + len = strlen(v);
1.193 +
1.194 + if (len < 1)
1.195 + {
1.196 + return;
1.197 + }
1.198 +
1.199 + if (is_space(v[len-1]))
1.200 + {
1.201 + for (i=len-1; i>=0; i--)
1.202 + {
1.203 + if (!is_space(v[i])
1.204 + || (is_space(v[i]) && i==0))
1.205 + {
1.206 + erase(i+1, j);
1.207 + break;
1.208 + }
1.209 + j++;
1.210 + }
1.211 + }
1.212 +
1.213 + if (len == 1)
1.214 + {
1.215 + if (is_space(v[0]))
1.216 + {
1.217 + clear();
1.218 + return;
1.219 + }
1.220 + }
1.221 + }
1.222 +
1.223 + void erase(unsigned int start, int num = npos)
1.224 + {
1.225 + if (!v)
1.226 + return;
1.227 + unsigned int i = 0;
1.228 + size_t len = size();
1.229 + //check for bounds
1.230 + if (num == npos || start+num > len-num+1)
1.231 + num = len - start;
1.232 + //do the erasing
1.233 + bool copyflag = false;
1.234 + for (i=0; i<len; i++)
1.235 + {
1.236 + if (i>=start && i<start+num)
1.237 + {
1.238 + if (i+num < len)
1.239 + {
1.240 + v[i] = v[i+num];
1.241 + } else {
1.242 + v[i] = 0;
1.243 + }
1.244 + copyflag = true;
1.245 + } else if (copyflag) {
1.246 + if (i+num < len)
1.247 + {
1.248 + v[i] = v[i+num];
1.249 + } else {
1.250 + v[i] = 0;
1.251 + }
1.252 + }
1.253 + }
1.254 + len -= num;
1.255 + v[len] = 0;
1.256 + }
1.257 +
1.258 + String substr(unsigned int index, int num = npos)
1.259 + {
1.260 + if (!v)
1.261 + {
1.262 + String b("");
1.263 + return b;
1.264 + }
1.265 +
1.266 + String ns;
1.267 +
1.268 + size_t len = size();
1.269 +
1.270 + if (index >= len || !v)
1.271 + return ns;
1.272 +
1.273 + if (num == npos)
1.274 + {
1.275 + num = len - index;
1.276 + } else if (index+num >= len) {
1.277 + num = len - index;
1.278 + }
1.279 +
1.280 + unsigned int i = 0, j=0;
1.281 + unsigned int nslen = num + 2;
1.282 +
1.283 + ns.Grow(nslen);
1.284 +
1.285 + for (i=index; i<index+num; i++)
1.286 + ns.append(v[i]);
1.287 +
1.288 + return ns;
1.289 + }
1.290 +
1.291 + void toLower()
1.292 + {
1.293 + if (!v)
1.294 + return;
1.295 + unsigned int i = 0;
1.296 + size_t len = strlen(v);
1.297 + for (i=0; i<len; i++)
1.298 + {
1.299 + if (v[i] >= 65 && v[i] <= 90)
1.300 + v[i] &= ~(1<<5);
1.301 + }
1.302 + }
1.303 +
1.304 + String & operator = (const String &src)
1.305 + {
1.306 + assign(src);
1.307 + return *this;
1.308 + }
1.309 +
1.310 + String & operator = (const char *src)
1.311 + {
1.312 + assign(src);
1.313 + return *this;
1.314 +
1.315 + }
1.316 +
1.317 + char operator [] (unsigned int index)
1.318 + {
1.319 + if (index > size() || !v)
1.320 + {
1.321 + return -1;
1.322 + } else {
1.323 + return v[index];
1.324 + }
1.325 + }
1.326 +
1.327 + int at(int a)
1.328 + {
1.329 + if (a < 0 || a >= (int)size() || !v)
1.330 + return -1;
1.331 +
1.332 + return v[a];
1.333 + }
1.334 +
1.335 + bool at(int at, char c)
1.336 + {
1.337 + if (at < 0 || at >= (int)size() || !v)
1.338 + return false;
1.339 +
1.340 + v[at] = c;
1.341 +
1.342 + return true;
1.343 + }
1.344 +
1.345 +private:
1.346 + void Grow(unsigned int d, bool copy=true)
1.347 + {
1.348 + if (d <= a_size)
1.349 + return;
1.350 + char *n = new char[d + 1];
1.351 + if (copy && v)
1.352 + strcpy(n, v);
1.353 + if (v)
1.354 + delete [] v;
1.355 + else
1.356 + strcpy(n, "");
1.357 + v = n;
1.358 + a_size = d + 1;
1.359 + }
1.360 +
1.361 + char *v;
1.362 + unsigned int a_size;
1.363 +public:
1.364 + static const int npos = -1;
1.365 +};
1.366 +
1.367 +#endif //_INCLUDE_CSTRING_H