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