Fix up ssprintf.

This commit is contained in:
Glenn Maynard 2018-05-31 21:01:42 -05:00
parent 9b6eb07e0b
commit 78d6be9341

View File

@ -68,27 +68,12 @@ wstring SMX::GetErrorString(int err)
string SMX::vssprintf(const char *szFormat, va_list argList) string SMX::vssprintf(const char *szFormat, va_list argList)
{ {
int iChars = vsnprintf(NULL, 0, szFormat, argList);
string sStr; string sStr;
sStr.resize(iChars+1);
char *pBuf = NULL; vsnprintf((char *) sStr.data(), iChars+1, szFormat, argList);
int iChars = 128; sStr.resize(iChars);
int iUsed = 0;
int iTry = 0;
do
{
// Grow more than linearly (e.g. 512, 1536, 3072, etc)
iChars += iTry * 1024;
delete[] pBuf;
pBuf = new char[iChars];
iUsed = vsnprintf(pBuf, iChars-1, szFormat, argList);
++iTry;
} while(iUsed < 0);
// assign whatever we managed to format
sStr.assign(pBuf, iUsed);
delete[] pBuf;
return sStr; return sStr;
} }