Fix StringFromFormat hopefully for the last time. This should be more efficient than before. Thanks to shuffle2 for the windows code.
git-svn-id: https://dolphin-emu.googlecode.com/svn/trunk@6379 8ced0084-cf51-0410-be5f-012b33b47a6e
This commit is contained in:
parent
36d43365e6
commit
adbd7fbd4a
|
@ -53,35 +53,28 @@ bool CharArrayFromFormatV(char* out, int outsize, const char* format, va_list ar
|
|||
|
||||
std::string StringFromFormat(const char* format, ...)
|
||||
{
|
||||
int writtenCount = -1;
|
||||
size_t newSize = strlen(format) + 5;
|
||||
char *buf = NULL;
|
||||
va_list args;
|
||||
while (writtenCount < 0)
|
||||
{
|
||||
delete[] buf;
|
||||
buf = new char[newSize];
|
||||
|
||||
va_start(args, format);
|
||||
writtenCount = vsnprintf(buf, newSize, format, args);
|
||||
va_end(args);
|
||||
char *buf = NULL;
|
||||
#ifdef _WIN32
|
||||
int required = 0;
|
||||
|
||||
#ifndef _WIN32
|
||||
// vsnprintf does not return -1 on truncation in linux!
|
||||
// Instead it returns the size of the string we need.
|
||||
if (writtenCount > (int)newSize)
|
||||
{
|
||||
newSize = writtenCount + 1;
|
||||
writtenCount = -1;
|
||||
}
|
||||
#else
|
||||
newSize *= 2;
|
||||
#endif
|
||||
}
|
||||
va_start(args, format);
|
||||
required = _vscprintf(format, args);
|
||||
buf = new char[required + 1];
|
||||
vsnprintf(buf, required, format, args);
|
||||
va_end(args);
|
||||
|
||||
buf[writtenCount] = '\0';
|
||||
buf[required] = '\0';
|
||||
std::string temp = buf;
|
||||
delete[] buf;
|
||||
#else
|
||||
va_start(args, format);
|
||||
vasprintf(&buf, format, args);
|
||||
va_end(args);
|
||||
|
||||
std::string temp = buf;
|
||||
free(buf);
|
||||
#endif
|
||||
return temp;
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in New Issue