summaryrefslogtreecommitdiff
path: root/Source/Core/Common/Src/StringUtil.cpp
diff options
context:
space:
mode:
authorhrydgard <hrydgard@gmail.com>2008-12-10 22:36:26 +0000
committerhrydgard <hrydgard@gmail.com>2008-12-10 22:36:26 +0000
commit0ad52a4fee384b4b93aae77714f99d5b7c6ccc87 (patch)
treed59d5a179ebcf29bc0b62e1bc62c8be75853c37a /Source/Core/Common/Src/StringUtil.cpp
parent61392ba69225f7c3b6649becb968c90073fe1898 (diff)
Workaround for StringFromFormat-under-linux problem, random cleanup.
git-svn-id: https://dolphin-emu.googlecode.com/svn/trunk@1491 8ced0084-cf51-0410-be5f-012b33b47a6e
Diffstat (limited to 'Source/Core/Common/Src/StringUtil.cpp')
-rw-r--r--Source/Core/Common/Src/StringUtil.cpp53
1 files changed, 34 insertions, 19 deletions
diff --git a/Source/Core/Common/Src/StringUtil.cpp b/Source/Core/Common/Src/StringUtil.cpp
index 9ace3e1899..ecbc9f2715 100644
--- a/Source/Core/Common/Src/StringUtil.cpp
+++ b/Source/Core/Common/Src/StringUtil.cpp
@@ -85,19 +85,23 @@ bool CharArrayFromFormatV(char* out, int outsize, const char* format, va_list ar
// Expensive!
-void StringFromFormatV(std::string* out, const char* format, va_list args)
+void ToStringFromFormat(std::string* out, const char* format, ...)
{
int writtenCount = -1;
- size_t newSize = strlen(format) + 16;
- char* buf = 0;
-
+ int newSize = (int)strlen(format) + 4;
+ char *buf = 0;
+ va_list args;
while (writtenCount < 0)
{
delete [] buf;
buf = new char[newSize + 1];
+
+ va_start(args, format);
writtenCount = vsnprintf(buf, newSize, format, args);
- if (writtenCount > (int)newSize)
+ va_end(args);
+ if (writtenCount >= (int)newSize) {
writtenCount = -1;
+ }
// ARGH! vsnprintf does no longer return -1 on truncation in newer libc!
// WORKAROUND! let's fake the old behaviour (even though it's less efficient).
// TODO: figure out why the fix causes an invalid read in strlen called from vsnprintf :(
@@ -114,12 +118,32 @@ void StringFromFormatV(std::string* out, const char* format, va_list args)
std::string StringFromFormat(const char* format, ...)
{
- std::string temp;
+ int writtenCount = -1;
+ int newSize = (int)strlen(format) + 4;
+ char *buf = 0;
va_list args;
- va_start(args, format);
- StringFromFormatV(&temp, format, args);
- va_end(args);
- return(temp);
+ while (writtenCount < 0)
+ {
+ delete [] buf;
+ buf = new char[newSize + 1];
+
+ va_start(args, format);
+ writtenCount = vsnprintf(buf, newSize, format, args);
+ va_end(args);
+ if (writtenCount >= (int)newSize) {
+ writtenCount = -1;
+ }
+ // ARGH! vsnprintf does no longer return -1 on truncation in newer libc!
+ // WORKAROUND! let's fake the old behaviour (even though it's less efficient).
+ // TODO: figure out why the fix causes an invalid read in strlen called from vsnprintf :(
+// if (writtenCount >= (int)newSize)
+// writtenCount = -1;
+ newSize *= 2;
+ }
+
+ buf[writtenCount] = '\0';
+ std::string temp = buf;
+ return temp;
}
@@ -142,15 +166,6 @@ std::string ArrayToString(const u8 *data, u32 size, u32 offset, int line_len)
// ================
-void ToStringFromFormat(std::string* out, const char* format, ...)
-{
- va_list args;
- va_start(args, format);
- StringFromFormatV(out, format, args);
- va_end(args);
-}
-
-
// Turns " hej " into "hej". Also handles tabs.
std::string StripSpaces(const std::string &str)
{