core/sm_stringutil.cpp
author David Anderson <dvander@alliedmods.net>
Thu Jun 07 22:26:38 2007 +0000 (2007-06-07)
changeset 919 316f419f62da
parent 909 4feac08f6764
child 1105 ce907ba50921
permissions -rw-r--r--
mispelled "phrase"
dvander@370
     1
/**
ds@653
     2
 * vim: set ts=4 :
dvander@370
     3
 * ===============================================================
dvander@370
     4
 * SourceMod (C)2004-2007 AlliedModders LLC.  All rights reserved.
dvander@370
     5
 * ===============================================================
dvander@370
     6
 *
dvander@370
     7
 * This file is not open source and may not be copied without explicit
dvander@370
     8
 * written permission of AlliedModders LLC.  This file may not be redistributed 
dvander@370
     9
 * in whole or significant part.
dvander@370
    10
 * For information, see LICENSE.txt or http://www.sourcemod.net/license.php
dvander@370
    11
 *
dvander@370
    12
 * Version: $Id$
dvander@370
    13
 */
dvander@370
    14
faluco@230
    15
#include <stdlib.h>
faluco@230
    16
#include <ctype.h>
faluco@274
    17
#include <stdarg.h>
faluco@227
    18
#include "sm_stringutil.h"
dvander@592
    19
#include "Logger.h"
faluco@389
    20
#include "PluginSys.h"
dvander@591
    21
#include "Translator.h"
dvander@591
    22
#include "PlayerManager.h"
faluco@227
    23
faluco@227
    24
#define LADJUST			0x00000004		/* left adjustment */
faluco@227
    25
#define ZEROPAD			0x00000080		/* zero (as opposed to blank) pad */
ds@609
    26
#define UPPERDIGITS		0x00000200		/* make alpha digits uppercase */
faluco@227
    27
#define to_digit(c)		((c) - '0')
faluco@227
    28
#define is_digit(c)		((unsigned)to_digit(c) <= 9)
faluco@227
    29
faluco@288
    30
#define CHECK_ARGS(x) \
faluco@288
    31
	if ((arg+x) > args) { \
dvander@599
    32
		pCtx->ThrowNativeErrorEx(SP_ERROR_PARAM, "String formatted incorrectly - parameter %d (total %d)", arg, args); \
faluco@288
    33
		return 0; \
faluco@227
    34
	}
faluco@227
    35
dvander@728
    36
size_t CorePlayerTranslate(int client, char *buffer, size_t maxlength, const char *phrase, void **params)
faluco@389
    37
{
dvander@728
    38
	Translation pTrans;
dvander@728
    39
	TransError err;
dvander@728
    40
dvander@728
    41
	err = g_pCorePhrases->GetTranslation(phrase, g_Translator.GetServerLanguage(), &pTrans);
dvander@732
    42
	if (err != Trans_Okay && g_Translator.GetServerLanguage() != CORELANG_ENGLISH)
faluco@422
    43
	{
dvander@732
    44
		err = g_pCorePhrases->GetTranslation(phrase, CORELANG_ENGLISH, &pTrans);
faluco@422
    45
	}
dvander@728
    46
dvander@728
    47
	if (err != Trans_Okay)
dvander@728
    48
	{
dvander@728
    49
		return UTIL_Format(buffer, maxlength, "%s", phrase);
dvander@728
    50
	}
dvander@728
    51
dvander@728
    52
	return g_Translator.Translate(buffer, maxlength, params, &pTrans);
faluco@422
    53
}
faluco@389
    54
faluco@422
    55
inline bool TryTranslation(CPlugin *pl, const char *key, unsigned int langid, unsigned int langcount, Translation *pTrans)
faluco@422
    56
{
faluco@422
    57
	TransError err = Trans_BadLanguage;
faluco@389
    58
	CPhraseFile *phrfl;
faluco@389
    59
faluco@389
    60
	for (size_t i=0; i<langcount && err!=Trans_Okay; i++)
faluco@389
    61
	{
ds@457
    62
		phrfl = g_Translator.GetFileByIndex(pl->GetLangFileByIndex(i));
faluco@422
    63
		err = phrfl->GetTranslation(key, langid, pTrans);
faluco@389
    64
	}
faluco@389
    65
faluco@422
    66
	return (err == Trans_Okay) ? true : false;
faluco@422
    67
}
faluco@422
    68
faluco@422
    69
size_t Translate(char *buffer, size_t maxlen, IPluginContext *pCtx, const char *key, cell_t target, const cell_t *params, int *arg, bool *error)
faluco@422
    70
{
faluco@422
    71
	unsigned int langid;
faluco@422
    72
	*error = false;
dvander@464
    73
	Translation pTrans;
dvander@464
    74
	CPlugin *pl = (CPlugin *)g_PluginSys.FindPluginByContext(pCtx->GetContext());
dvander@464
    75
	size_t langcount = pl->GetLangFileCount();
dvander@464
    76
	void *new_params[MAX_TRANSLATE_PARAMS];
dvander@464
    77
	unsigned int max_params = 0;
faluco@422
    78
faluco@422
    79
try_serverlang:
faluco@422
    80
	if (target == LANG_SERVER)
faluco@389
    81
	{
ds@669
    82
		langid = g_Translator.GetServerLanguage();
dvander@663
    83
 	} else if ((target >= 1) && (target <= g_Players.GetMaxClients())) {
ds@669
    84
		langid = g_Translator.GetServerLanguage();
faluco@422
    85
	} else {
faluco@422
    86
		pCtx->ThrowNativeErrorEx(SP_ERROR_PARAM, "Translation failed: invalid client index %d", target);
faluco@422
    87
		goto error_out;
faluco@422
    88
	}
faluco@422
    89
faluco@422
    90
	if (!TryTranslation(pl, key, langid, langcount, &pTrans))
faluco@422
    91
	{
dvander@909
    92
		if (target != LANG_SERVER && langid != g_Translator.GetServerLanguage())
faluco@422
    93
		{
faluco@422
    94
			target = LANG_SERVER;
faluco@422
    95
			goto try_serverlang;
dvander@663
    96
		} else if (langid != LANGUAGE_ENGLISH) {
dvander@663
    97
			if (!TryTranslation(pl, key, LANGUAGE_ENGLISH, langcount, &pTrans))
faluco@422
    98
			{
faluco@422
    99
				pCtx->ThrowNativeErrorEx(SP_ERROR_PARAM, "Language phrase \"%s\" not found", key);
faluco@422
   100
				goto error_out;
faluco@422
   101
			}
dvander@909
   102
		} else {
dvander@919
   103
			pCtx->ThrowNativeErrorEx(SP_ERROR_PARAM, "Language phrase \"%s\" not found", key);
dvander@909
   104
			goto error_out;
faluco@422
   105
		}
faluco@389
   106
	}
faluco@389
   107
dvander@474
   108
	max_params = pTrans.fmt_count;
dvander@909
   109
	
dvander@909
   110
	if (max_params)
faluco@389
   111
	{
dvander@909
   112
		/* Check if we're going to over the limit */
dvander@909
   113
		if ((*arg) + (max_params - 1) > (size_t)params[0])
faluco@389
   114
		{
dvander@909
   115
			pCtx->ThrowNativeErrorEx(SP_ERROR_PARAMS_MAX, 
dvander@909
   116
				"Translation string formatted incorrectly - missing at least %d parameters", 
dvander@909
   117
				((*arg + (max_params - 1)) - params[0])
dvander@909
   118
				);
faluco@422
   119
			goto error_out;
faluco@389
   120
		}
dvander@909
   121
dvander@909
   122
		/* Translate the parameters to raw pointers */
dvander@909
   123
		for (size_t i=0; i<max_params; i++)
dvander@909
   124
		{
dvander@909
   125
			pCtx->LocalToPhysAddr(params[*arg], reinterpret_cast<cell_t **>(&new_params[i]));
dvander@909
   126
			(*arg)++;
dvander@909
   127
		}
faluco@389
   128
	}
faluco@389
   129
faluco@389
   130
	return g_Translator.Translate(buffer, maxlen, new_params, &pTrans);
faluco@422
   131
error_out:
faluco@422
   132
	*error = true;
faluco@422
   133
	return 0;
faluco@389
   134
}
faluco@389
   135
dvander@276
   136
//:TODO: review this code before we choose a license
dvander@276
   137
faluco@227
   138
void AddString(char **buf_p, size_t &maxlen, const char *string, int width, int prec)
faluco@227
   139
{
faluco@227
   140
	int size = 0;
faluco@227
   141
	char *buf;
faluco@227
   142
	static char nlstr[] = {'(','n','u','l','l',')','\0'};
faluco@227
   143
faluco@227
   144
	buf = *buf_p;
faluco@227
   145
faluco@227
   146
	if (string == NULL)
faluco@227
   147
	{
faluco@227
   148
		string = nlstr;
faluco@227
   149
		prec = -1;
faluco@227
   150
	}
faluco@227
   151
faluco@227
   152
	if (prec >= 0)
faluco@227
   153
	{
faluco@227
   154
		for (size = 0; size < prec; size++) 
faluco@227
   155
		{
faluco@227
   156
			if (string[size] == '\0')
faluco@227
   157
			{
faluco@227
   158
				break;
faluco@227
   159
			}
faluco@227
   160
		}
faluco@227
   161
	} else {
faluco@227
   162
		while (string[size++]);
faluco@227
   163
		size--;
faluco@227
   164
	}
faluco@227
   165
faluco@227
   166
	if (size > (int)maxlen)
faluco@227
   167
	{
faluco@227
   168
		size = maxlen;
faluco@227
   169
	}
faluco@227
   170
faluco@227
   171
	maxlen -= size;
faluco@227
   172
	width -= size;
faluco@227
   173
faluco@227
   174
	while (size--)
faluco@227
   175
	{
faluco@227
   176
		*buf++ = *string++;
faluco@227
   177
	}
faluco@227
   178
faluco@227
   179
	while ((width-- > 0) && maxlen)
faluco@227
   180
	{
faluco@227
   181
		*buf++ = ' ';
faluco@227
   182
		maxlen--;
faluco@227
   183
	}
faluco@227
   184
faluco@227
   185
	*buf_p = buf;
faluco@227
   186
}
faluco@227
   187
faluco@227
   188
void AddFloat(char **buf_p, size_t &maxlen, double fval, int width, int prec)
faluco@227
   189
{
faluco@227
   190
	char text[32];
faluco@227
   191
	int digits;
faluco@227
   192
	double signedVal;
faluco@227
   193
	char *buf;
faluco@227
   194
	int val;
faluco@227
   195
faluco@227
   196
	// get the sign
faluco@227
   197
	signedVal = fval;
faluco@227
   198
	if (fval < 0)
faluco@227
   199
	{
faluco@227
   200
		fval = -fval;
faluco@227
   201
	}
faluco@227
   202
faluco@227
   203
	// write the float number
faluco@227
   204
	digits = 0;
faluco@227
   205
	val = (int)fval;
faluco@227
   206
	do
faluco@227
   207
	{
faluco@227
   208
		text[digits++] = '0' + val % 10;
faluco@227
   209
		val /= 10;
faluco@227
   210
	} while (val);
faluco@227
   211
faluco@227
   212
	if (signedVal < 0)
faluco@227
   213
	{
faluco@227
   214
		text[digits++] = '-';
faluco@227
   215
	}
faluco@227
   216
faluco@227
   217
	buf = *buf_p;
faluco@227
   218
faluco@227
   219
	while ((digits < width) && maxlen)
faluco@227
   220
	{
faluco@227
   221
		*buf++ = ' ';
faluco@227
   222
		width--;
faluco@227
   223
		maxlen--;
faluco@227
   224
	}
faluco@227
   225
faluco@227
   226
	while ((digits--) && maxlen)
faluco@227
   227
	{
faluco@227
   228
		*buf++ = text[digits];
faluco@227
   229
		maxlen--;
faluco@227
   230
	}
faluco@227
   231
faluco@227
   232
	*buf_p = buf;
faluco@227
   233
faluco@227
   234
	if (prec < 0)
faluco@227
   235
	{
faluco@227
   236
		prec = 6;
faluco@227
   237
	}
faluco@227
   238
	// write the fraction
faluco@227
   239
	digits = 0;
faluco@227
   240
	while (digits < prec)
faluco@227
   241
	{
faluco@227
   242
		fval -= (int)fval;
faluco@227
   243
		fval *= 10.0;
faluco@227
   244
		val = (int)fval;
faluco@227
   245
		text[digits++] = '0' + val % 10;
faluco@227
   246
	}
faluco@227
   247
faluco@227
   248
	if ((digits > 0) && maxlen)
faluco@227
   249
	{
faluco@227
   250
		buf = *buf_p;
faluco@227
   251
		*buf++ = '.';
faluco@227
   252
		maxlen--;
faluco@227
   253
		for (prec = 0; maxlen && (prec < digits); prec++)
faluco@227
   254
		{
faluco@227
   255
			*buf++ = text[prec];
faluco@227
   256
			maxlen--;
faluco@227
   257
		}
faluco@227
   258
		*buf_p = buf;
faluco@227
   259
	}
faluco@227
   260
}
faluco@227
   261
faluco@227
   262
void AddUInt(char **buf_p, size_t &maxlen, unsigned int val, int width, int flags)
faluco@227
   263
{
faluco@227
   264
	char text[32];
faluco@227
   265
	int digits;
faluco@227
   266
	char *buf;
faluco@227
   267
faluco@227
   268
	digits = 0;
faluco@227
   269
	do
faluco@227
   270
	{
faluco@227
   271
		text[digits++] = '0' + val % 10;
faluco@227
   272
		val /= 10;
faluco@227
   273
	} while (val);
faluco@227
   274
faluco@227
   275
	buf = *buf_p;
faluco@227
   276
faluco@227
   277
	if (!(flags & LADJUST))
faluco@227
   278
	{
faluco@227
   279
		while (digits < width && maxlen)
faluco@227
   280
		{
faluco@227
   281
			*buf++ = (flags & ZEROPAD) ? '0' : ' ';
faluco@227
   282
			width--;
faluco@227
   283
			maxlen--;
faluco@227
   284
		}
faluco@227
   285
	}
faluco@227
   286
faluco@227
   287
	while (digits-- && maxlen)
faluco@227
   288
	{
faluco@227
   289
		*buf++ = text[digits];
faluco@227
   290
		width--;
faluco@227
   291
		maxlen--;
faluco@227
   292
	}
faluco@227
   293
faluco@227
   294
	if (flags & LADJUST)
faluco@227
   295
	{
faluco@227
   296
		while (width-- && maxlen)
faluco@227
   297
		{
faluco@227
   298
			*buf++ = (flags & ZEROPAD) ? '0' : ' ';
faluco@227
   299
			maxlen--;
faluco@227
   300
		}
faluco@227
   301
	}
faluco@227
   302
faluco@227
   303
	*buf_p = buf;
faluco@227
   304
}
faluco@227
   305
faluco@227
   306
void AddInt(char **buf_p, size_t &maxlen, int val, int width, int flags)
faluco@227
   307
{
faluco@227
   308
	char text[32];
faluco@227
   309
	int digits;
faluco@227
   310
	int signedVal;
faluco@227
   311
	char *buf;
faluco@227
   312
	unsigned int unsignedVal;
faluco@227
   313
faluco@227
   314
	digits = 0;
faluco@227
   315
	signedVal = val;
faluco@227
   316
	if (val < 0)
faluco@227
   317
	{
faluco@227
   318
		/* we want the unsigned version */
faluco@227
   319
		unsignedVal = abs(val);
faluco@227
   320
	} else {
faluco@227
   321
		unsignedVal = val;
faluco@227
   322
	}
faluco@227
   323
	do
faluco@227
   324
	{
faluco@227
   325
		text[digits++] = '0' + unsignedVal % 10;
faluco@227
   326
		unsignedVal /= 10;
faluco@227
   327
	} while (unsignedVal);
faluco@227
   328
faluco@227
   329
	if (signedVal < 0)
faluco@227
   330
	{
faluco@227
   331
		text[digits++] = '-';
faluco@227
   332
	}
faluco@227
   333
faluco@227
   334
	buf = *buf_p;
faluco@227
   335
faluco@227
   336
	if (!(flags & LADJUST))
faluco@227
   337
	{
faluco@227
   338
		while ((digits < width) && maxlen)
faluco@227
   339
		{
faluco@227
   340
			*buf++ = (flags & ZEROPAD) ? '0' : ' ';
faluco@227
   341
			width--;
faluco@227
   342
			maxlen--;
faluco@227
   343
		}
faluco@227
   344
	}
faluco@227
   345
faluco@227
   346
	while (digits-- && maxlen)
faluco@227
   347
	{
faluco@227
   348
		*buf++ = text[digits];
faluco@227
   349
		width--;
faluco@227
   350
		maxlen--;
faluco@227
   351
	}
faluco@227
   352
faluco@227
   353
	if (flags & LADJUST)
faluco@227
   354
	{
faluco@227
   355
		while (width-- && maxlen)
faluco@227
   356
		{
faluco@227
   357
			*buf++ = (flags & ZEROPAD) ? '0' : ' ';
faluco@227
   358
			maxlen--;
faluco@227
   359
		}
faluco@227
   360
	}
faluco@227
   361
faluco@227
   362
	*buf_p = buf;
faluco@227
   363
}
faluco@227
   364
ds@544
   365
void AddHex(char **buf_p, size_t &maxlen, unsigned int val, int width, int flags)
ds@544
   366
{
ds@544
   367
	char text[32];
ds@544
   368
	int digits;
ds@544
   369
	char *buf;
ds@544
   370
	char digit;
ds@544
   371
	int hexadjust;
ds@544
   372
ds@544
   373
	if (flags & UPPERDIGITS)
ds@544
   374
	{
ds@544
   375
		hexadjust = 'A' - '9' - 1;
ds@544
   376
	} else {
ds@544
   377
		hexadjust = 'a' - '9' - 1;
ds@544
   378
	}
ds@544
   379
ds@544
   380
	digits = 0;
ds@544
   381
	do 
ds@544
   382
	{
ds@544
   383
		digit = ('0' + val % 16);
ds@544
   384
		if (digit > '9')
ds@544
   385
		{
ds@544
   386
			digit += hexadjust;
ds@544
   387
		}
ds@544
   388
ds@544
   389
		text[digits++] = digit;
ds@544
   390
		val /= 16;
ds@544
   391
	} while(val);
ds@544
   392
ds@544
   393
	buf = *buf_p;
ds@544
   394
ds@544
   395
	if (!(flags & LADJUST))
ds@544
   396
	{
ds@544
   397
		while (digits < width && maxlen)
ds@544
   398
		{
ds@544
   399
			*buf++ = (flags & ZEROPAD) ? '0' : ' ';
ds@544
   400
			width--;
ds@544
   401
			maxlen--;
ds@544
   402
		}
ds@544
   403
	}
ds@544
   404
ds@544
   405
	while (digits-- && maxlen)
ds@544
   406
	{
ds@544
   407
		*buf++ = text[digits];
ds@544
   408
		width--;
ds@544
   409
		maxlen--;
ds@544
   410
	}
ds@544
   411
ds@544
   412
	if (flags & LADJUST)
ds@544
   413
	{
ds@544
   414
		while (width-- && maxlen)
ds@544
   415
		{
ds@544
   416
			*buf++ = (flags & ZEROPAD) ? '0' : ' ';
ds@544
   417
			maxlen--;
ds@544
   418
		}
ds@544
   419
	}
ds@544
   420
ds@544
   421
	*buf_p = buf;
ds@544
   422
}
ds@544
   423
dvander@276
   424
size_t gnprintf(char *buffer, size_t maxlen, const char *format, void **args)
dvander@276
   425
{
dvander@276
   426
	if (!buffer || !maxlen)
dvander@276
   427
	{
dvander@276
   428
		return 0;
dvander@276
   429
	}
dvander@276
   430
dvander@276
   431
	int arg = 0;
dvander@276
   432
	char *buf_p;
dvander@276
   433
	char ch;
dvander@276
   434
	int flags;
dvander@276
   435
	int width;
dvander@276
   436
	int prec;
dvander@276
   437
	int n;
dvander@276
   438
	char sign;
dvander@276
   439
	const char *fmt;
dvander@276
   440
	size_t llen = maxlen - 1;
dvander@276
   441
dvander@276
   442
	buf_p = buffer;
dvander@276
   443
	fmt = format;
dvander@276
   444
dvander@276
   445
	while (true)
dvander@276
   446
	{
dvander@276
   447
		// run through the format string until we hit a '%' or '\0'
dvander@276
   448
		for (ch = *fmt; llen && ((ch = *fmt) != '\0') && (ch != '%'); fmt++)
dvander@276
   449
		{
dvander@276
   450
			*buf_p++ = ch;
dvander@276
   451
			llen--;
dvander@276
   452
		}
dvander@276
   453
		if ((ch == '\0') || (llen <= 0))
dvander@276
   454
		{
dvander@276
   455
			goto done;
dvander@276
   456
		}
dvander@276
   457
dvander@276
   458
		// skip over the '%'
dvander@276
   459
		fmt++;
dvander@276
   460
dvander@276
   461
		// reset formatting state
dvander@276
   462
		flags = 0;
dvander@276
   463
		width = 0;
dvander@276
   464
		prec = -1;
dvander@276
   465
		sign = '\0';
dvander@276
   466
dvander@276
   467
rflag:
dvander@276
   468
		ch = *fmt++;
dvander@276
   469
reswitch:
dvander@276
   470
		switch(ch)
dvander@276
   471
		{
dvander@276
   472
		case '-':
dvander@276
   473
			{
dvander@276
   474
				flags |= LADJUST;
dvander@276
   475
				goto rflag;
dvander@276
   476
			}
dvander@276
   477
		case '.':
dvander@276
   478
			{
dvander@276
   479
				n = 0;
dvander@276
   480
				while(is_digit((ch = *fmt++)))
dvander@276
   481
				{
dvander@276
   482
					n = 10 * n + (ch - '0');
dvander@276
   483
				}
dvander@276
   484
				prec = (n < 0) ? -1 : n;
dvander@276
   485
				goto reswitch;
dvander@276
   486
			}
dvander@276
   487
		case '0':
dvander@276
   488
			{
dvander@276
   489
				flags |= ZEROPAD;
dvander@276
   490
				goto rflag;
dvander@276
   491
			}
dvander@276
   492
		case '1':
dvander@276
   493
		case '2':
dvander@276
   494
		case '3':
dvander@276
   495
		case '4':
dvander@276
   496
		case '5':
dvander@276
   497
		case '6':
dvander@276
   498
		case '7':
dvander@276
   499
		case '8':
dvander@276
   500
		case '9':
dvander@276
   501
			{
dvander@276
   502
				n = 0;
dvander@276
   503
				do
dvander@276
   504
				{
dvander@276
   505
					n = 10 * n + (ch - '0');
dvander@276
   506
					ch = *fmt++;
dvander@276
   507
				} while(is_digit(ch));
dvander@276
   508
				width = n;
dvander@276
   509
				goto reswitch;
dvander@276
   510
			}
dvander@276
   511
		case 'c':
dvander@276
   512
			{
dvander@276
   513
				if (!llen)
dvander@276
   514
				{
dvander@276
   515
					goto done;
dvander@276
   516
				}
dvander@276
   517
				char *c = (char *)args[arg];
dvander@276
   518
				*buf_p++ = *c;
dvander@276
   519
				llen--;
dvander@276
   520
				arg++;
dvander@276
   521
				break;
dvander@276
   522
			}
dvander@276
   523
		case 'd':
dvander@276
   524
		case 'i':
dvander@276
   525
			{
dvander@276
   526
				int *value = (int *)args[arg];
dvander@276
   527
				AddInt(&buf_p, llen, *value, width, flags);
dvander@276
   528
				arg++;
dvander@276
   529
				break;
dvander@276
   530
			}
dvander@276
   531
		case 'u':
dvander@276
   532
			{
dvander@276
   533
				unsigned int *value = (unsigned int *)args[arg];
dvander@276
   534
				AddUInt(&buf_p, llen, *value, width, flags);
dvander@276
   535
				arg++;
dvander@276
   536
				break;
dvander@276
   537
			}
dvander@276
   538
		case 'f':
dvander@276
   539
			{
dvander@276
   540
				float *value = (float *)args[arg];
dvander@276
   541
				AddFloat(&buf_p, llen, *value, width, prec);
dvander@276
   542
				arg++;
dvander@276
   543
				break;
dvander@276
   544
			}
dvander@276
   545
		case 's':
dvander@276
   546
			{
dvander@276
   547
				const char *str = (const char *)args[arg];
dvander@276
   548
				AddString(&buf_p, llen, str, width, prec);
dvander@276
   549
				arg++;
dvander@276
   550
				break;
dvander@276
   551
			}
ds@544
   552
		case 'X':
ds@544
   553
			{
ds@544
   554
				unsigned int *value = (unsigned int *)args[arg];
ds@544
   555
				flags |= UPPERDIGITS;
ds@544
   556
				AddHex(&buf_p, llen, *value, width, flags);
ds@544
   557
				arg++;
ds@544
   558
				break;
ds@544
   559
			}
ds@544
   560
		case 'x':
ds@544
   561
			{
ds@544
   562
				unsigned int *value = (unsigned int *)args[arg];
ds@544
   563
				AddHex(&buf_p, llen, *value, width, flags);
ds@544
   564
				arg++;
ds@544
   565
				break;
ds@544
   566
			}
dvander@276
   567
		case '%':
dvander@276
   568
			{
dvander@276
   569
				if (!llen)
dvander@276
   570
				{
dvander@276
   571
					goto done;
dvander@276
   572
				}
dvander@276
   573
				*buf_p++ = ch;
dvander@276
   574
				llen--;
dvander@276
   575
				break;
dvander@276
   576
			}
dvander@276
   577
		case '\0':
dvander@276
   578
			{
dvander@276
   579
				if (!llen)
dvander@276
   580
				{
dvander@276
   581
					goto done;
dvander@276
   582
				}
dvander@276
   583
				*buf_p++ = '%';
dvander@276
   584
				llen--;
dvander@276
   585
				goto done;
dvander@276
   586
			}
dvander@276
   587
		default:
dvander@276
   588
			{
dvander@276
   589
				if (!llen)
dvander@276
   590
				{
dvander@276
   591
					goto done;
dvander@276
   592
				}
dvander@276
   593
				*buf_p++ = ch;
dvander@276
   594
				llen--;
dvander@276
   595
				break;
dvander@276
   596
			}
dvander@276
   597
		}
dvander@276
   598
	}
dvander@276
   599
dvander@276
   600
done:
dvander@276
   601
	*buf_p = '\0';
dvander@276
   602
dvander@276
   603
	return (maxlen - llen - 1);
dvander@276
   604
}
dvander@276
   605
faluco@227
   606
size_t atcprintf(char *buffer, size_t maxlen, const char *format, IPluginContext *pCtx, const cell_t *params, int *param)
faluco@227
   607
{
dvander@276
   608
	if (!buffer || !maxlen)
dvander@276
   609
	{
dvander@276
   610
		return 0;
dvander@276
   611
	}
dvander@276
   612
faluco@227
   613
	int arg;
faluco@288
   614
	int args = params[0];
faluco@227
   615
	char *buf_p;
faluco@227
   616
	char ch;
faluco@227
   617
	int flags;
faluco@227
   618
	int width;
faluco@227
   619
	int prec;
faluco@227
   620
	int n;
faluco@227
   621
	char sign;
faluco@227
   622
	const char *fmt;
faluco@230
   623
	size_t llen = maxlen - 1;
faluco@227
   624
faluco@227
   625
	buf_p = buffer;
faluco@227
   626
	arg = *param;
faluco@227
   627
	fmt = format;
faluco@227
   628
faluco@227
   629
	while (true)
faluco@227
   630
	{
faluco@227
   631
		// run through the format string until we hit a '%' or '\0'
faluco@227
   632
		for (ch = *fmt; llen && ((ch = *fmt) != '\0') && (ch != '%'); fmt++)
faluco@227
   633
		{
faluco@227
   634
			*buf_p++ = ch;
faluco@227
   635
			llen--;
faluco@227
   636
		}
faluco@227
   637
		if ((ch == '\0') || (llen <= 0))
faluco@227
   638
		{
faluco@227
   639
			goto done;
faluco@227
   640
		}
faluco@227
   641
faluco@227
   642
		// skip over the '%'
faluco@227
   643
		fmt++;
faluco@227
   644
faluco@227
   645
		// reset formatting state
faluco@227
   646
		flags = 0;
faluco@227
   647
		width = 0;
faluco@227
   648
		prec = -1;
faluco@227
   649
		sign = '\0';
faluco@227
   650
faluco@227
   651
rflag:
faluco@227
   652
		ch = *fmt++;
faluco@227
   653
reswitch:
faluco@227
   654
		switch(ch)
faluco@227
   655
		{
faluco@227
   656
		case '-':
faluco@227
   657
			{
faluco@227
   658
				flags |= LADJUST;
faluco@227
   659
				goto rflag;
faluco@227
   660
			}
faluco@227
   661
		case '.':
faluco@227
   662
			{
faluco@227
   663
				n = 0;
faluco@227
   664
				while(is_digit((ch = *fmt++)))
faluco@227
   665
				{
faluco@227
   666
					n = 10 * n + (ch - '0');
faluco@227
   667
				}
faluco@227
   668
				prec = (n < 0) ? -1 : n;
faluco@227
   669
				goto reswitch;
faluco@227
   670
			}
faluco@227
   671
		case '0':
faluco@227
   672
			{
faluco@227
   673
				flags |= ZEROPAD;
faluco@227
   674
				goto rflag;
faluco@227
   675
			}
faluco@227
   676
		case '1':
faluco@227
   677
		case '2':
faluco@227
   678
		case '3':
faluco@227
   679
		case '4':
faluco@227
   680
		case '5':
faluco@227
   681
		case '6':
faluco@227
   682
		case '7':
faluco@227
   683
		case '8':
faluco@227
   684
		case '9':
faluco@227
   685
			{
faluco@227
   686
				n = 0;
faluco@227
   687
				do
faluco@227
   688
				{
faluco@227
   689
					n = 10 * n + (ch - '0');
faluco@227
   690
					ch = *fmt++;
faluco@227
   691
				} while(is_digit(ch));
faluco@227
   692
				width = n;
faluco@227
   693
				goto reswitch;
faluco@227
   694
			}
faluco@227
   695
		case 'c':
faluco@227
   696
			{
faluco@227
   697
				CHECK_ARGS(0);
faluco@230
   698
				if (!llen)
faluco@227
   699
				{
faluco@227
   700
					goto done;
faluco@227
   701
				}
faluco@227
   702
				char *c;
faluco@227
   703
				pCtx->LocalToString(params[arg], &c);
faluco@227
   704
				*buf_p++ = *c;
faluco@227
   705
				llen--;
faluco@227
   706
				arg++;
faluco@227
   707
				break;
faluco@227
   708
			}
faluco@227
   709
		case 'd':
faluco@227
   710
		case 'i':
faluco@227
   711
			{
faluco@227
   712
				CHECK_ARGS(0);
faluco@227
   713
				cell_t *value;
faluco@227
   714
				pCtx->LocalToPhysAddr(params[arg], &value);
faluco@227
   715
				AddInt(&buf_p, llen, static_cast<int>(*value), width, flags);
faluco@227
   716
				arg++;
faluco@227
   717
				break;
faluco@227
   718
			}
faluco@227
   719
		case 'u':
faluco@227
   720
			{
faluco@227
   721
				CHECK_ARGS(0);
faluco@227
   722
				cell_t *value;
faluco@227
   723
				pCtx->LocalToPhysAddr(params[arg], &value);
faluco@227
   724
				AddUInt(&buf_p, llen, static_cast<unsigned int>(*value), width, flags);
faluco@227
   725
				arg++;
faluco@227
   726
				break;
faluco@227
   727
			}
faluco@227
   728
		case 'f':
faluco@227
   729
			{
faluco@227
   730
				CHECK_ARGS(0);
faluco@227
   731
				cell_t *value;
faluco@227
   732
				pCtx->LocalToPhysAddr(params[arg], &value);
dvander@233
   733
				AddFloat(&buf_p, llen, sp_ctof(*value), width, prec);
faluco@227
   734
				arg++;
faluco@227
   735
				break;
faluco@227
   736
			}
dvander@905
   737
		case 'L':
dvander@905
   738
			{
dvander@905
   739
				CHECK_ARGS(0);
dvander@905
   740
				cell_t *value;
dvander@905
   741
				pCtx->LocalToPhysAddr(params[arg], &value);
dvander@905
   742
				char buffer[255];
dvander@906
   743
				if (*value)
dvander@905
   744
				{
dvander@906
   745
					CPlayer *player = g_Players.GetPlayerByIndex(*value);
dvander@906
   746
					if (!player || !player->IsConnected())
dvander@906
   747
					{
dvander@906
   748
						return pCtx->ThrowNativeError("Client index %d is invalid", *value);
dvander@906
   749
					}
dvander@906
   750
					const char *auth = player->GetAuthString();
dvander@906
   751
					if (!auth || auth[0] == '\0')
dvander@906
   752
					{
dvander@906
   753
						auth = "STEAM_ID_PENDING";
dvander@906
   754
					}
dvander@906
   755
					int userid = engine->GetPlayerUserId(player->GetEdict());
dvander@906
   756
					UTIL_Format(buffer, 
dvander@906
   757
						sizeof(buffer), 
dvander@906
   758
						"%s<%d><%s><>", 
dvander@906
   759
						player->GetName(),
dvander@906
   760
						userid,
dvander@906
   761
						auth);
dvander@906
   762
				} else {
dvander@906
   763
					UTIL_Format(buffer,
dvander@906
   764
						sizeof(buffer),
dvander@906
   765
						"Console<0><Console><Console>");
dvander@905
   766
				}
dvander@905
   767
				AddString(&buf_p, llen, buffer, width, prec);
dvander@905
   768
				arg++;
dvander@905
   769
				break;
dvander@905
   770
			}
faluco@227
   771
		case 's':
faluco@227
   772
			{
faluco@227
   773
				CHECK_ARGS(0);
faluco@227
   774
				char *str;
dvander@535
   775
				int err;
dvander@535
   776
				if ((err=pCtx->LocalToString(params[arg], &str)) != SP_ERROR_NONE)
dvander@535
   777
				{
dvander@535
   778
					pCtx->ThrowNativeErrorEx(err, "Could not deference string");
dvander@535
   779
					return 0;
dvander@535
   780
				}
faluco@227
   781
				AddString(&buf_p, llen, str, width, prec);
faluco@227
   782
				arg++;
faluco@227
   783
				break;
faluco@227
   784
			}
faluco@389
   785
		case 'T':
faluco@389
   786
			{
dvander@888
   787
				CHECK_ARGS(1);
faluco@389
   788
				char *key;
faluco@422
   789
				bool error;
faluco@422
   790
				size_t res;
ds@669
   791
				cell_t *target;
dvander@888
   792
				pCtx->LocalToString(params[arg++], &key);
ds@669
   793
				pCtx->LocalToPhysAddr(params[arg++], &target);
ds@669
   794
				res = Translate(buf_p, llen, pCtx, key, *target, params, &arg, &error);
faluco@422
   795
				if (error)
faluco@422
   796
				{
faluco@422
   797
					return 0;
faluco@422
   798
				}
faluco@422
   799
				buf_p += res;
faluco@422
   800
				llen -= res;
faluco@422
   801
				break;
faluco@422
   802
			}
faluco@422
   803
		case 't':
faluco@422
   804
			{
faluco@422
   805
				CHECK_ARGS(0);
faluco@422
   806
				char *key;
faluco@422
   807
				bool error;
faluco@422
   808
				size_t res;
faluco@422
   809
				cell_t target = g_SourceMod.GetGlobalTarget();
faluco@422
   810
				pCtx->LocalToString(params[arg++], &key);
faluco@422
   811
				res = Translate(buf_p, llen, pCtx, key, target, params, &arg, &error);
faluco@422
   812
				if (error)
faluco@422
   813
				{
faluco@422
   814
					return 0;
faluco@422
   815
				}
faluco@422
   816
				buf_p += res;
faluco@422
   817
				llen -= res;
faluco@389
   818
				break;
faluco@389
   819
			}
ds@544
   820
		case 'X':
ds@544
   821
			{
ds@544
   822
				CHECK_ARGS(0);
ds@544
   823
				cell_t *value;
ds@544
   824
				pCtx->LocalToPhysAddr(params[arg], &value);
ds@544
   825
				flags |= UPPERDIGITS;
ds@544
   826
				AddHex(&buf_p, llen, static_cast<unsigned int>(*value), width, flags);
ds@544
   827
				arg++;
ds@544
   828
				break;
ds@544
   829
			}
ds@544
   830
		case 'x':
ds@544
   831
			{
ds@544
   832
				CHECK_ARGS(0);
ds@544
   833
				cell_t *value;
ds@544
   834
				pCtx->LocalToPhysAddr(params[arg], &value);
ds@544
   835
				AddHex(&buf_p, llen, static_cast<unsigned int>(*value), width, flags);
ds@544
   836
				arg++;
ds@544
   837
				break;
ds@544
   838
			}
faluco@227
   839
		case '%':
faluco@227
   840
			{
faluco@230
   841
				if (!llen)
faluco@227
   842
				{
faluco@227
   843
					goto done;
faluco@227
   844
				}
faluco@227
   845
				*buf_p++ = ch;
faluco@227
   846
				llen--;
faluco@227
   847
				break;
faluco@227
   848
			}
faluco@227
   849
		case '\0':
faluco@227
   850
			{
faluco@230
   851
				if (!llen)
faluco@227
   852
				{
faluco@227
   853
					goto done;
faluco@227
   854
				}
faluco@227
   855
				*buf_p++ = '%';
faluco@227
   856
				llen--;
faluco@227
   857
				goto done;
faluco@227
   858
			}
faluco@227
   859
		default:
faluco@227
   860
			{
faluco@230
   861
				if (!llen)
faluco@227
   862
				{
faluco@227
   863
					goto done;
faluco@227
   864
				}
faluco@227
   865
				*buf_p++ = ch;
faluco@227
   866
				llen--;
faluco@227
   867
				break;
faluco@227
   868
			}
faluco@227
   869
		}
faluco@227
   870
	}
faluco@227
   871
faluco@227
   872
done:
faluco@227
   873
	*buf_p = '\0';
faluco@227
   874
	*param = arg;
faluco@236
   875
	return (maxlen - llen - 1);
faluco@227
   876
}
faluco@230
   877
faluco@230
   878
const char *stristr(const char *str, const char *substr)
faluco@230
   879
{
faluco@230
   880
	if (!*substr)
faluco@230
   881
	{
faluco@230
   882
		return ((char *)str);
faluco@230
   883
	}
faluco@230
   884
faluco@230
   885
	char *needle = (char *)substr;
faluco@230
   886
	char *prevloc = (char *)str;
faluco@230
   887
	char *haystack = (char *)str;
faluco@230
   888
faluco@230
   889
	while (*haystack)
faluco@230
   890
	{
faluco@230
   891
		if (tolower(*haystack) == tolower(*needle))
faluco@230
   892
		{
faluco@230
   893
			haystack++;
faluco@230
   894
			if (!*++needle)
faluco@230
   895
			{
faluco@230
   896
				return prevloc;
faluco@230
   897
			}
faluco@230
   898
		} else {
faluco@230
   899
			haystack = ++prevloc;
faluco@230
   900
			needle = (char *)substr;
faluco@230
   901
		}
faluco@230
   902
	}
faluco@230
   903
faluco@230
   904
	return NULL;
faluco@230
   905
}
faluco@230
   906
dvander@233
   907
unsigned int strncopy(char *dest, const char *src, size_t count)
faluco@230
   908
{
faluco@230
   909
	if (!count)
faluco@230
   910
	{
faluco@230
   911
		return 0;
faluco@230
   912
	}
faluco@230
   913
faluco@230
   914
	char *start = dest;
faluco@230
   915
	while ((*src) && (--count))
faluco@230
   916
	{
faluco@230
   917
		*dest++ = *src++;
faluco@230
   918
	}
faluco@230
   919
	*dest = '\0';
faluco@230
   920
faluco@230
   921
	return (dest - start);
faluco@230
   922
}
faluco@274
   923
faluco@274
   924
size_t UTIL_Format(char *buffer, size_t maxlength, const char *fmt, ...)
faluco@274
   925
{
faluco@274
   926
	va_list ap;
faluco@274
   927
	va_start(ap, fmt);
faluco@274
   928
	size_t len = vsnprintf(buffer, maxlength, fmt, ap);
faluco@274
   929
	va_end(ap);
faluco@274
   930
faluco@651
   931
	if (len >= maxlength)
faluco@651
   932
	{
faluco@651
   933
		buffer[maxlength - 1] = '\0';
faluco@651
   934
		return (maxlength - 1);
faluco@651
   935
	} else {
faluco@651
   936
		return len;
faluco@651
   937
	}
dvander@351
   938
}
dvander@351
   939
dvander@663
   940
size_t UTIL_FormatArgs(char *buffer, size_t maxlength, const char *fmt, va_list ap)
dvander@663
   941
{
dvander@663
   942
	size_t len = vsnprintf(buffer, maxlength, fmt, ap);
dvander@663
   943
dvander@663
   944
	if (len >= maxlength)
dvander@663
   945
	{
dvander@663
   946
		buffer[maxlength - 1] = '\0';
dvander@663
   947
		return (maxlength - 1);
dvander@663
   948
	} else {
dvander@663
   949
		return len;
dvander@663
   950
	}
dvander@663
   951
}
dvander@663
   952
ds@504
   953
char *sm_strdup(const char *str)
ds@504
   954
{
ds@504
   955
	char *ptr = new char[strlen(str)+1];
ds@504
   956
	strcpy(ptr, str);
ds@504
   957
	return ptr;
ds@504
   958
}
dvander@833
   959
dvander@833
   960
unsigned int UTIL_ReplaceAll(char *subject, size_t maxlength, const char *search, const char *replace)
dvander@833
   961
{
dvander@833
   962
	size_t searchLen = strlen(search);
dvander@833
   963
	size_t replaceLen = strlen(replace);
dvander@833
   964
dvander@833
   965
	char *ptr = subject;
dvander@833
   966
	unsigned int total = 0;
dvander@833
   967
	while ((ptr = UTIL_ReplaceEx(ptr, maxlength, search, searchLen, replace, replaceLen)) != NULL)
dvander@833
   968
	{
dvander@833
   969
		total++;
dvander@833
   970
		if (*ptr == '\0')
dvander@833
   971
		{
dvander@833
   972
			break;
dvander@833
   973
		}
dvander@833
   974
	}
dvander@833
   975
dvander@833
   976
	return total;
dvander@833
   977
}
dvander@833
   978
dvander@833
   979
/**
dvander@833
   980
 * NOTE: Do not edit this for the love of god unless you have 
dvander@833
   981
 * read the test cases and understand the code behind each one.
dvander@833
   982
 * While I don't guarantee there aren't mistakes, I do guarantee
dvander@833
   983
 * that plugins will end up relying on tiny idiosyncracies of this
dvander@833
   984
 * function, just like they did with AMX Mod X.
dvander@833
   985
 *
dvander@833
   986
 * There are explicitly more cases than the AMX Mod X version because
dvander@833
   987
 * we're not doing a blind copy.  Each case is specifically optimized
dvander@833
   988
 * for what needs to be done.  Even better, we don't have to error on
dvander@833
   989
 * bad buffer sizes.  Instead, this function will smartly cut off the 
dvander@833
   990
 * string in a way that pushes old data out.
dvander@833
   991
 */
dvander@833
   992
char *UTIL_ReplaceEx(char *subject, size_t maxLen, const char *search, size_t searchLen, const char *replace, size_t replaceLen)
dvander@833
   993
{
dvander@833
   994
	char *ptr = subject;
dvander@833
   995
	size_t browsed = 0;
dvander@833
   996
	size_t textLen = strlen(subject);
dvander@833
   997
dvander@833
   998
	/* It's not possible to search or replace */
dvander@833
   999
	if (searchLen > textLen || maxLen <= 1)
dvander@833
  1000
	{
dvander@833
  1001
		return NULL;
dvander@833
  1002
	}
dvander@833
  1003
dvander@833
  1004
	/* Subtract one off the maxlength so we can include the null terminator */
dvander@833
  1005
	maxLen--;
dvander@833
  1006
dvander@833
  1007
	while (*ptr != '\0' && (browsed <= textLen - searchLen))
dvander@833
  1008
	{
dvander@833
  1009
		/* See if we get a comparison */
dvander@833
  1010
		if (strncmp(ptr, search, searchLen) == 0)
dvander@833
  1011
		{
dvander@833
  1012
			if (replaceLen > searchLen)
dvander@833
  1013
			{
dvander@833
  1014
				/* First, see if we have enough space to do this operation */
dvander@833
  1015
				if (maxLen - textLen < replaceLen - searchLen)
dvander@833
  1016
				{
dvander@833
  1017
					/* First, see if the replacement length goes out of bounds. */
dvander@833
  1018
					if (browsed + replaceLen >= maxLen)
dvander@833
  1019
					{
dvander@833
  1020
						/* EXAMPLE CASE:
dvander@833
  1021
						 * Subject: AABBBCCC
dvander@833
  1022
						 * Buffer : 12 bytes
dvander@833
  1023
						 * Search : BBB
dvander@833
  1024
						 * Replace: DDDDDDDDDD
dvander@833
  1025
						 * OUTPUT : AADDDDDDDDD
dvander@833
  1026
						 * POSITION:           ^
dvander@833
  1027
						 */
dvander@833
  1028
						/* If it does, we'll just bound the length and do a strcpy. */
dvander@833
  1029
						replaceLen = maxLen - browsed;
dvander@833
  1030
						/* Note, we add one to the final result for the null terminator */
dvander@833
  1031
						strncopy(ptr, replace, replaceLen+1);
dvander@833
  1032
					} else {
dvander@833
  1033
						/* EXAMPLE CASE:
dvander@833
  1034
						 * Subject: AABBBCCC
dvander@833
  1035
						 * Buffer : 12 bytes
dvander@833
  1036
						 * Search : BBB
dvander@833
  1037
						 * Replace: DDDDDDD
dvander@833
  1038
						 * OUTPUT : AADDDDDDDCC
dvander@833
  1039
						 * POSITION:         ^
dvander@833
  1040
						 */
dvander@833
  1041
						/* We're going to have some bytes left over... */
dvander@833
  1042
						size_t origBytesToCopy = (textLen - (browsed + searchLen)) + 1;
dvander@833
  1043
						size_t realBytesToCopy = (maxLen - (browsed + replaceLen)) + 1;
dvander@833
  1044
						char *moveFrom = ptr + searchLen + (origBytesToCopy - realBytesToCopy);
dvander@833
  1045
						char *moveTo = ptr + replaceLen;
dvander@833
  1046
dvander@833
  1047
						/* First, move our old data out of the way. */
dvander@833
  1048
						memmove(moveTo, moveFrom, realBytesToCopy);
dvander@833
  1049
dvander@833
  1050
						/* Now, do our replacement. */
dvander@833
  1051
						memcpy(ptr, replace, replaceLen);
dvander@833
  1052
					}
dvander@833
  1053
				} else {
dvander@833
  1054
					/* EXAMPLE CASE:
dvander@833
  1055
					 * Subject: AABBBCCC
dvander@833
  1056
					 * Buffer : 12 bytes
dvander@833
  1057
					 * Search : BBB
dvander@833
  1058
					 * Replace: DDDD
dvander@833
  1059
					 * OUTPUT : AADDDDCCC
dvander@833
  1060
					 * POSITION:      ^
dvander@833
  1061
					 */
dvander@833
  1062
					/* Yes, we have enough space.  Do a normal move operation. */
dvander@833
  1063
					char *moveFrom = ptr + searchLen;
dvander@833
  1064
					char *moveTo = ptr + replaceLen;
dvander@833
  1065
dvander@833
  1066
					/* First move our old data out of the way. */
dvander@833
  1067
					size_t bytesToCopy = (textLen - (browsed + searchLen)) + 1;
dvander@833
  1068
					memmove(moveTo, moveFrom, bytesToCopy);
dvander@833
  1069
dvander@833
  1070
					/* Now do our replacement. */
dvander@833
  1071
					memcpy(ptr, replace, replaceLen);
dvander@833
  1072
				}
dvander@833
  1073
			} else if (replaceLen < searchLen) {
dvander@833
  1074
				/* EXAMPLE CASE:
dvander@833
  1075
				 * Subject: AABBBCCC
dvander@833
  1076
				 * Buffer : 12 bytes
dvander@833
  1077
				 * Search : BBB
dvander@833
  1078
				 * Replace: D
dvander@833
  1079
				 * OUTPUT : AADCCC
dvander@833
  1080
				 * POSOTION:   ^
dvander@833
  1081
				 */
dvander@833
  1082
				/* If the replacement does not grow the string length, we do not
dvander@833
  1083
				 * need to do any fancy checking at all.  Yay!
dvander@833
  1084
				 */
dvander@833
  1085
				char *moveFrom = ptr + searchLen;		/* Start after the search pointer */
dvander@833
  1086
				char *moveTo = ptr + replaceLen;		/* Copy to where the replacement ends */
dvander@833
  1087
dvander@833
  1088
				/* Copy our replacement in, if any */
dvander@833
  1089
				if (replaceLen)
dvander@833
  1090
				{
dvander@833
  1091
					memcpy(ptr, replace, replaceLen);
dvander@833
  1092
				}
dvander@833
  1093
dvander@833
  1094
				/* Figure out how many bytes to move down, including null terminator */
dvander@833
  1095
				size_t bytesToCopy = (textLen - (browsed + searchLen)) + 1;
dvander@833
  1096
dvander@833
  1097
				/* Move the rest of the string down */
dvander@833
  1098
				memmove(moveTo, moveFrom, bytesToCopy);
dvander@833
  1099
			} else {
dvander@833
  1100
				/* EXAMPLE CASE:
dvander@833
  1101
				 * Subject: AABBBCCC
dvander@833
  1102
				 * Buffer : 12 bytes
dvander@833
  1103
				 * Search : BBB
dvander@833
  1104
				 * Replace: DDD
dvander@833
  1105
				 * OUTPUT : AADDDCCC
dvander@833
  1106
				 * POSITION:     ^
dvander@833
  1107
				 */
dvander@833
  1108
				/* We don't have to move anything around, just do a straight copy */
dvander@833
  1109
				memcpy(ptr, replace, replaceLen);
dvander@833
  1110
			}
dvander@833
  1111
dvander@833
  1112
			return ptr + replaceLen;
dvander@833
  1113
		}
dvander@833
  1114
		ptr++;
dvander@833
  1115
		browsed++;
dvander@833
  1116
	}
dvander@833
  1117
dvander@833
  1118
	return NULL;
dvander@833
  1119
}