mirror of
https://github.com/dolphin-emu/dolphin.git
synced 2025-04-25 05:54:57 +00:00
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
1 changed files with 17 additions and 24 deletions
|
@ -53,35 +53,28 @@ bool CharArrayFromFormatV(char* out, int outsize, const char* format, va_list ar
|
||||||
|
|
||||||
std::string StringFromFormat(const char* format, ...)
|
std::string StringFromFormat(const char* format, ...)
|
||||||
{
|
{
|
||||||
int writtenCount = -1;
|
|
||||||
size_t newSize = strlen(format) + 5;
|
|
||||||
char *buf = NULL;
|
|
||||||
va_list args;
|
va_list args;
|
||||||
while (writtenCount < 0)
|
char *buf = NULL;
|
||||||
{
|
#ifdef _WIN32
|
||||||
delete[] buf;
|
int required = 0;
|
||||||
buf = new char[newSize];
|
|
||||||
|
|
||||||
va_start(args, format);
|
|
||||||
writtenCount = vsnprintf(buf, newSize, format, args);
|
|
||||||
va_end(args);
|
|
||||||
|
|
||||||
#ifndef _WIN32
|
va_start(args, format);
|
||||||
// vsnprintf does not return -1 on truncation in linux!
|
required = _vscprintf(format, args);
|
||||||
// Instead it returns the size of the string we need.
|
buf = new char[required + 1];
|
||||||
if (writtenCount > (int)newSize)
|
vsnprintf(buf, required, format, args);
|
||||||
{
|
va_end(args);
|
||||||
newSize = writtenCount + 1;
|
|
||||||
writtenCount = -1;
|
|
||||||
}
|
|
||||||
#else
|
|
||||||
newSize *= 2;
|
|
||||||
#endif
|
|
||||||
}
|
|
||||||
|
|
||||||
buf[writtenCount] = '\0';
|
buf[required] = '\0';
|
||||||
std::string temp = buf;
|
std::string temp = buf;
|
||||||
delete[] buf;
|
delete[] buf;
|
||||||
|
#else
|
||||||
|
va_start(args, format);
|
||||||
|
vasprintf(&buf, format, args);
|
||||||
|
va_end(args);
|
||||||
|
|
||||||
|
std::string temp = buf;
|
||||||
|
free(buf);
|
||||||
|
#endif
|
||||||
return temp;
|
return temp;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Add table
Reference in a new issue