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