MCPcopy Create free account
hub / github.com/alliedmodders/sourcemod / InternalFormat

Function InternalFormat

core/logic/stringutil.cpp:397–448  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

395static char g_formatbuf[2048];
396static StaticCharBuf g_extrabuf;
397cell_t InternalFormat(IPluginContext *pCtx, const cell_t *params, int start)
398{
399 char *buf, *fmt, *destbuf;
400 cell_t start_addr, end_addr, maxparam;
401 size_t res, maxlen;
402 int arg = start + 4;
403 bool copy = false;
404 char *__copy_buf;
405
406 pCtx->LocalToString(params[start + 1], &destbuf);
407 pCtx->LocalToString(params[start + 3], &fmt);
408
409 maxlen = static_cast<size_t>(params[start + 2]);
410 start_addr = params[start + 1];
411 end_addr = params[start + 1] + maxlen;
412 maxparam = params[0];
413
414 for (cell_t i = (start + 3); i <= maxparam; i++)
415 {
416 if ((params[i] >= start_addr) && (params[i] <= end_addr))
417 {
418 copy = true;
419 break;
420 }
421 }
422
423 if (copy)
424 {
425 if (maxlen > sizeof(g_formatbuf))
426 {
427 char *tmpbuff = g_extrabuf.GetWithSize(maxlen);
428 if (!tmpbuff)
429 return pCtx->ThrowNativeError("Unable to allocate buffer with a size of \"%u\"", maxlen);
430
431 __copy_buf = tmpbuff;
432 }
433 else
434 {
435 __copy_buf = g_formatbuf;
436 }
437 }
438
439 buf = (copy) ? __copy_buf : destbuf;
440 res = atcprintf(buf, maxlen, fmt, pCtx, params, &arg);
441
442 if (copy)
443 {
444 memcpy(destbuf, __copy_buf, res+1);
445 }
446
447 return static_cast<cell_t>(res);
448}

Callers 2

SQL_FormatQueryFunction · 0.85
sm_formatFunction · 0.85

Calls 2

atcprintfFunction · 0.85
GetWithSizeMethod · 0.80

Tested by

no test coverage detected