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