Merge pull request #93 from Arkshine/atcprintf-new-parameters

Add new special format specifiers.
This commit is contained in:
Vincent Herbet 2014-08-01 22:34:57 +02:00
commit 0bfcaa85b6
3 changed files with 88 additions and 4 deletions

View File

@ -153,7 +153,7 @@ unsigned int UTIL_GetUTF8CharBytes(const char *stream);
unsigned int UTIL_ReplaceAll(char *subject, size_t maxlength, const char *search, const char *replace, bool caseSensitive);
char *UTIL_ReplaceEx(char *subject, size_t maxLen, const char *search, size_t searchLen, const char *replace, size_t replaceLen, bool caseSensitive);
char *UTIL_VarArgs(const char *fmt, ...);
size_t UTIL_Format(char *buffer, size_t maxlength, const char *fmt, ...);
#define GET_PLAYER_POINTER(e) (&g_players[ENTINDEX(e)])
//#define GET_PLAYER_POINTER(e) (&g_players[(((int)e-g_edict_point)/sizeof(edict_t))])

View File

@ -118,12 +118,12 @@ const char *translate(AMX *amx, cell amxaddr, const char *key)
return def;
}
template <typename U>
void AddString(U **buf_p, size_t &maxlen, const cell *string, int width, int prec)
template <typename U, typename S>
void AddString(U **buf_p, size_t &maxlen, const S *string, int width, int prec)
{
int size = 0;
U *buf;
static cell nlstr[] = {'(','n','u','l','l',')','\0'};
static S nlstr[] = {'(','n','u','l','l',')','\0'};
buf = *buf_p;
@ -654,6 +654,72 @@ reswitch:
llen -= written;
break;
}
case 'N':
{
CHECK_ARGS(0);
cell *addr = get_amxaddr(amx, params[arg]);
char buffer[255];
if (*addr)
{
CPlayer *player = NULL;
if (*addr >= 1 && *addr <= gpGlobals->maxClients)
{
player = GET_PLAYER_POINTER_I(*addr);
}
if (!player || !player->initialized)
{
LogError(amx, AMX_ERR_NATIVE, "Client index %d is invalid", *addr);
return 0;
}
const char *auth = GETPLAYERAUTHID(player->pEdict);
if (!auth || auth[0] == '\0')
{
auth = "STEAM_ID_PENDING";
}
int userid = GETPLAYERUSERID(player->pEdict);
UTIL_Format(buffer, sizeof(buffer), "%s<%d><%s><%s>", player->name.c_str(), userid, auth, player->team.c_str());
}
else
{
UTIL_Format(buffer, sizeof(buffer), "Console<0><Console><Console>");
}
AddString(&buf_p, llen, buffer, width, prec);
arg++;
break;
}
case 'n':
{
CHECK_ARGS(0);
cell *addr = get_amxaddr(amx, params[arg]);
const char *name = "Console";
if (*addr)
{
CPlayer *player = NULL;
if (*addr >= 1 && *addr <= gpGlobals->maxClients)
{
player = GET_PLAYER_POINTER_I(*addr);
}
if (!player || !player->initialized)
{
LogError(amx, AMX_ERR_NATIVE, "Client index %d is invalid", *addr);
return 0;
}
name = player->name.c_str();
}
AddString(&buf_p, llen, name, width, prec);
arg++;
break;
}
case '%':
*buf_p++ = static_cast<D>(ch);
if (!llen)

View File

@ -714,3 +714,21 @@ char *UTIL_ReplaceEx(char *subject, size_t maxLen, const char *search, size_t se
return NULL;
}
size_t UTIL_Format(char *buffer, size_t maxlength, const char *fmt, ...)
{
va_list ap;
va_start(ap, fmt);
size_t len = vsnprintf(buffer, maxlength, fmt, ap);
va_end(ap);
if (len >= maxlength)
{
buffer[maxlength - 1] = '\0';
return (maxlength - 1);
}
else
{
return len;
}
}