core/sourcehook/sh_string.h
author Scott Ehlert <ds@alliedmods.net>
Thu Dec 10 00:59:50 2009 -0600 (2009-12-10)
changeset 689 4065ba5e9155
parent 539 db914129ae01
child 733 2365240861a2
permissions -rwxr-xr-x
Rewrote user message caching code to always use IServerGameDLL::GetUserMessageInfo (bug 4117, r=dvander).

Games where the above function can crash are handled by detouring tier0's Error() function and jumping back to our own code. So in theory this should work on all games now.
     1 /* ======== SourceMM ========
     2 * Copyright (C) 2004-2008 Metamod:Source Development Team
     3 * No warranties of any kind
     4 *
     5 * License: zlib/libpng
     6 *
     7 * Author(s): David "BAILOPAN" Anderson
     8 * ============================
     9 */
    10 
    11 /* AMX Mod X
    12  *
    13  * by the AMX Mod X Development Team
    14  */
    15 
    16 #ifndef _INCLUDE_CSTRING_H
    17 #define _INCLUDE_CSTRING_H
    18 
    19 #include <string.h>
    20 #include <stdio.h>
    21 
    22 namespace SourceHook
    23 {
    24 class String
    25 {
    26 public:
    27 	String() 
    28 	{
    29 		v = NULL;
    30 		a_size = 0;
    31 		//assign("");
    32 	}
    33 
    34 	~String()
    35 	{ 
    36 		if (v) 
    37 			delete [] v; 
    38 	}
    39 
    40 	String(const char *src) 
    41 	{
    42 		v = NULL; 
    43 		a_size = 0;
    44 		assign(src); 
    45 	}
    46 
    47 	String(const String &src) 
    48 	{
    49 		v = NULL;
    50 		a_size = 0;
    51 		assign(src.c_str()); 
    52 	}
    53 
    54 	bool operator ==(const String &other)
    55 	{
    56 		return (compare(other.c_str()) == 0);
    57 	}
    58 
    59 	bool operator ==(const char *other)
    60 	{
    61 		return (compare(other) == 0);
    62 	}
    63 
    64 	const char *c_str() { return v?v:""; }
    65 
    66 	const char *c_str() const { return v?v:""; }
    67 
    68 	void append(const char *t)
    69 	{
    70 		Grow(size() + strlen(t) + 1);
    71 		strcat(v, t);
    72 	}
    73 
    74 	void append(const char c)
    75 	{
    76 		size_t len = size();
    77 		Grow(len + 2);
    78 		v[len] = c;
    79 		v[len + 1] = '\0';
    80 	}
    81 
    82 	void append(String &d)
    83 	{
    84 		append(d.c_str());
    85 	}
    86 
    87 	void assign(const String &src)
    88 	{
    89 		assign(src.c_str());
    90 	}
    91 
    92 	void assign(const char *d)
    93 	{
    94 		if (!d)
    95 		{
    96 			clear();
    97 		} else {
    98 			Grow(strlen(d) + 1, false);
    99 			strcpy(v, d);
   100 		}
   101 	}
   102 
   103 	void clear()
   104 	{
   105 		if (v)
   106 			v[0] = '\0';
   107 	}
   108 
   109 	int compare (const char *d) const
   110 	{
   111 		if (!v)
   112 			return strcmp("", d);
   113 		else
   114 			return strcmp(v, d);
   115 	}
   116 
   117 	//Added this for amxx inclusion
   118 	bool empty() const
   119 	{
   120 		if (!v)
   121 			return true;
   122 
   123 		if (v[0] == '\0')
   124 			return true;
   125 
   126 		return false;
   127 	}
   128 
   129 	size_t size() const
   130 	{
   131 		if (v)
   132 			return strlen(v);
   133 		else
   134 			return 0;
   135 	}
   136 
   137 	int find(const char c, int index = 0) const
   138 	{
   139 		int len = static_cast<int>(size());
   140 		if (len < 1)
   141 			return npos;
   142 		if (index >= len || index < 0)
   143 			return npos;
   144 		int i = 0;
   145 		for (i=index; i<len; i++)
   146 		{
   147 			if (v[i] == c)
   148 			{
   149 				return i;
   150 			}
   151 		}
   152 
   153 		return npos;
   154 	}
   155 
   156 	int find_last_of(const char c, int index = npos) const
   157 	{
   158 		int len = static_cast<int>(size());
   159 		if (len < 1)
   160 			return npos;
   161 		if (index >= len || index < npos)
   162 			return npos;
   163 		int i;
   164 		if (index == npos)
   165 			i = len - 1;
   166 		else
   167 			i = index;
   168 
   169 		for (; i>=0; i--)
   170 		{
   171 			if (v[i] == c)
   172 			{
   173 				return i;
   174 			}
   175 		}
   176 
   177 		return npos;
   178 	}
   179 
   180 	bool is_space(int c) const
   181 	{
   182 		if (c == '\f' || c == '\n' ||
   183 			c == '\t' || c == '\r' ||
   184 			c == '\v' || c == ' ')
   185 		{
   186 			return true;
   187 		}
   188 
   189 		return false;
   190 	}
   191 	
   192 	void trim()
   193 	{
   194 		if (!v)
   195 			return;
   196 
   197 		unsigned int i = 0;
   198 		unsigned int j = 0;
   199 		size_t len = strlen(v);
   200 
   201 		if (len == 1)
   202 		{
   203 			if (is_space(v[i]))
   204 			{
   205 				clear();
   206 				return;
   207 			} 
   208 		}
   209 
   210 		unsigned char c0 = v[0];
   211 
   212 		if (is_space(c0))
   213 		{
   214 			for (i=0; i<len; i++)
   215 			{
   216 				if (!is_space(v[i]) || (is_space(v[i]) && ((unsigned char)i==len-1)))
   217 				{
   218 					erase(0, i);
   219 					break;
   220 				}
   221 			}
   222 		}
   223 
   224 		len = strlen(v);
   225 
   226 		if (len < 1)
   227 		{
   228 			return;
   229 		}
   230 
   231 		if (is_space(v[len-1]))
   232 		{
   233 			for (i=len-1; i>=0; i--)
   234 			{
   235 				if (!is_space(v[i])
   236 					|| (is_space(v[i]) && i==0))
   237 				{
   238 					erase(i+1, j);
   239 					break;
   240 				}
   241 				j++;
   242 			}
   243 		}
   244 
   245 		if (len == 1)
   246 		{
   247 			if (is_space(v[0]))
   248 			{
   249 				clear();
   250 				return;
   251 			}
   252 		}
   253 	}
   254 
   255 	void erase(unsigned int start, int num = npos)
   256 	{
   257 		if (!v)
   258 			return;
   259 		unsigned int i = 0;
   260 		size_t len = size();
   261 		//check for bounds
   262 		if (num == npos || start+num > len-start)
   263 			num = len - start;
   264 		//do the erasing
   265 		bool copyflag = false;
   266 		for (i=0; i<len; i++)
   267 		{
   268 			if (i>=start && i<start+num)
   269 			{
   270 				if (i+num < len)
   271 				{	
   272 					v[i] = v[i+num];
   273 				} else {
   274 					v[i] = 0;
   275 				}
   276 				copyflag = true;
   277 			} else if (copyflag) {
   278 				if (i+num < len)
   279 				{
   280 					v[i] = v[i+num];
   281 				} else {
   282 					v[i] = 0;
   283 				}
   284 			}
   285 		}
   286 		len -= num;
   287 		v[len] = 0;
   288 	}
   289 
   290 	String substr(unsigned int index, int num = npos) const
   291 	{
   292 		if (!v)
   293 		{
   294 			String b("");
   295 			return b;
   296 		}
   297 
   298 		String ns;
   299 
   300 		size_t len = size();
   301 
   302 		if (index >= len || !v)
   303 			return ns;
   304 		
   305 		if (num == npos)
   306 		{
   307 			num = len - index;
   308 		} else if (index+num >= len) {
   309 			num = len - index;
   310 		}
   311 
   312 		unsigned int i = 0;
   313 		unsigned int nslen = num + 2;
   314 
   315 		ns.Grow(nslen);
   316 
   317 		for (i=index; i<index+num; i++)
   318 			ns.append(v[i]);
   319 
   320 		return ns;
   321 	}
   322 
   323 	void toLower()
   324 	{
   325 		if (!v)
   326 			return;
   327 		unsigned int i = 0;
   328 		size_t len = strlen(v);
   329 		for (i=0; i<len; i++)
   330 		{
   331 			if (v[i] >= 65 && v[i] <= 90)
   332 				v[i] &= ~(1<<5);
   333 		}
   334 	}
   335 
   336 	String & operator = (const String &src)
   337 	{
   338 		assign(src);
   339 		return *this;
   340 	}
   341 
   342 	String & operator = (const char *src)
   343 	{
   344 		assign(src);
   345 		return *this;
   346 
   347 	}
   348 
   349 	char operator [] (unsigned int index) const
   350 	{
   351 		if (index > size() || !v)
   352 		{
   353 			return -1;
   354 		} else {
   355 			return v[index];
   356 		}
   357 	}
   358 
   359 	int at(int a) const
   360 	{
   361 		if (a < 0 || a >= (int)size() || !v)
   362 			return -1;
   363 
   364 		return v[a];
   365 	}
   366 
   367 	bool at(int at, char c)
   368 	{
   369 		if (at < 0 || at >= (int)size() || !v)
   370 			return false;
   371 
   372 		v[at] = c;
   373 
   374 		return true;
   375 	}
   376 
   377 private:
   378 	void Grow(unsigned int d, bool copy=true)
   379 	{
   380 		if (d <= a_size)
   381 			return;
   382 		char *n = new char[d + 1];
   383 		if (copy && v)
   384 			strcpy(n, v);
   385 		if (v)
   386 			delete [] v;
   387 		else
   388 			strcpy(n, "");			
   389 		v = n;
   390 		a_size = d + 1;
   391 	}
   392 
   393 	char *v;
   394 	unsigned int a_size;
   395 public:
   396 	static const int npos = -1;
   397 };
   398 
   399 };	//NAMESPACE
   400 
   401 #endif //_INCLUDE_CSTRING_H