Add new format parameters.

This commit is contained in:
Arkshine 2014-07-26 14:40:54 +02:00
parent 47dc226393
commit bbc83291ef
3 changed files with 77 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,61 @@ reswitch:
llen -= written;
break;
}
case 'N':
{
CHECK_ARGS(0);
cell *addr = get_amxaddr(amx, params[arg]);
char buffer[255];
if (*addr >= 1 && *addr <= gpGlobals->maxClients)
{
CPlayer *player = GET_PLAYER_POINTER_I(*addr);
if (!player->ingame)
{
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, (const char*)&buffer, width, prec);
arg++;
break;
}
case 'n':
{
CHECK_ARGS(0);
cell *addr = get_amxaddr(amx, params[arg]);
char name[32] = "Console";
if (*addr >= 1 && *addr <= gpGlobals->maxClients)
{
CPlayer *player = GET_PLAYER_POINTER_I(*addr);
if (player->ingame)
{
strncopy(name, player->name.c_str(), sizeof(name));
AddString(&buf_p, llen, (const char*)&name, width, prec);
arg++;
break;
}
}
LogError(amx, AMX_ERR_NATIVE, "Client index %d is invalid", *addr);
return 0;
}
case '%':
*buf_p++ = static_cast<D>(ch);
if (!llen)

View File

@ -713,4 +713,22 @@ 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;
}
}