2010-05-20 12:23:13 +00:00
|
|
|
/* PCSX2 - PS2 Emulator for PCs
|
|
|
|
* Copyright (C) 2002-2010 PCSX2 Dev Team
|
|
|
|
*
|
|
|
|
* PCSX2 is free software: you can redistribute it and/or modify it under the terms
|
|
|
|
* of the GNU Lesser General Public License as published by the Free Software Found-
|
|
|
|
* ation, either version 3 of the License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* PCSX2 is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
|
|
|
|
* without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
|
|
|
|
* PURPOSE. See the GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License along with PCSX2.
|
|
|
|
* If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
2021-09-01 20:31:46 +00:00
|
|
|
#include "common/Pcsx2Defs.h"
|
|
|
|
#include "common/SafeArray.inl"
|
|
|
|
#include "common/StringHelpers.h"
|
2010-05-20 12:23:13 +00:00
|
|
|
|
2010-08-05 12:48:49 +00:00
|
|
|
// Implement some very commonly used SafeArray types here
|
|
|
|
// (done here for lack of a better place)
|
|
|
|
|
|
|
|
template class SafeArray<char>;
|
|
|
|
template class SafeArray<wchar_t>;
|
|
|
|
template class SafeArray<u8>;
|
|
|
|
|
2016-11-12 15:28:37 +00:00
|
|
|
template class SafeAlignedArray<char, 16>;
|
|
|
|
template class SafeAlignedArray<wchar_t, 16>;
|
|
|
|
template class SafeAlignedArray<u8, 16>;
|
2010-08-05 12:48:49 +00:00
|
|
|
|
2010-05-20 12:23:13 +00:00
|
|
|
// Sanity check: truncate strings if they exceed 512k in length. Anything like that
|
|
|
|
// is either a bug or really horrible code that needs to be stopped before it causes
|
|
|
|
// system deadlock.
|
|
|
|
static const int MaxFormattedStringLength = 0x80000;
|
|
|
|
|
2015-01-16 19:57:09 +00:00
|
|
|
#ifndef __linux__
|
2021-09-06 18:28:26 +00:00
|
|
|
static __ri void format_that_ascii_mess(CharBufferType& buffer, uint writepos, const char* fmt, va_list argptr)
|
|
|
|
#else
|
|
|
|
static void format_that_ascii_mess(CharBufferType& buffer, uint writepos, const char* fmt, va_list argptr)
|
2015-01-16 19:57:09 +00:00
|
|
|
#endif
|
2010-05-20 12:23:13 +00:00
|
|
|
{
|
2021-09-06 18:28:26 +00:00
|
|
|
va_list args;
|
|
|
|
while (true)
|
|
|
|
{
|
|
|
|
int size = buffer.GetLength();
|
|
|
|
|
|
|
|
va_copy(args, argptr);
|
|
|
|
int len = vsnprintf(buffer.GetPtr(writepos), size - writepos, fmt, args);
|
|
|
|
va_end(args);
|
|
|
|
|
|
|
|
// some implementations of vsnprintf() don't NUL terminate
|
|
|
|
// the string if there is not enough space for it so
|
|
|
|
// always do it manually
|
|
|
|
buffer[size - 1] = '\0';
|
|
|
|
|
|
|
|
if (size >= MaxFormattedStringLength)
|
|
|
|
break;
|
|
|
|
|
|
|
|
// vsnprintf() may return either -1 (traditional Unix behavior) or the
|
|
|
|
// total number of characters which would have been written if the
|
|
|
|
// buffer were large enough (newer standards such as Unix98)
|
|
|
|
|
|
|
|
if (len < 0)
|
|
|
|
len = size + (size / 4);
|
|
|
|
|
|
|
|
len += writepos;
|
|
|
|
if (len < size)
|
|
|
|
break;
|
|
|
|
buffer.Resize(len + 128);
|
|
|
|
};
|
|
|
|
|
|
|
|
// performing an assertion or log of a truncated string is unsafe, so let's not; even
|
|
|
|
// though it'd be kinda nice if we did.
|
2010-05-20 12:23:13 +00:00
|
|
|
}
|
|
|
|
|
2010-10-18 01:40:49 +00:00
|
|
|
// returns the length of the formatted string, in characters (wxChars).
|
2015-01-16 19:57:09 +00:00
|
|
|
#ifndef __linux__
|
2021-09-06 18:28:26 +00:00
|
|
|
static __ri uint format_that_unicode_mess(CharBufferType& buffer, uint writepos, const wxChar* fmt, va_list argptr)
|
|
|
|
#else
|
|
|
|
static uint format_that_unicode_mess(CharBufferType& buffer, uint writepos, const wxChar* fmt, va_list argptr)
|
2015-01-16 19:57:09 +00:00
|
|
|
#endif
|
2010-05-20 12:23:13 +00:00
|
|
|
{
|
2021-09-06 18:28:26 +00:00
|
|
|
va_list args;
|
|
|
|
while (true)
|
|
|
|
{
|
|
|
|
int size = buffer.GetLength() / sizeof(wxChar);
|
2015-01-05 22:38:30 +00:00
|
|
|
|
2021-09-06 18:28:26 +00:00
|
|
|
va_copy(args, argptr);
|
|
|
|
int len = wxVsnprintf((wxChar*)buffer.GetPtr(writepos * sizeof(wxChar)), size - writepos, fmt, args);
|
|
|
|
va_end(args);
|
2010-05-20 12:23:13 +00:00
|
|
|
|
2021-09-06 18:28:26 +00:00
|
|
|
// some implementations of vsnprintf() don't NUL terminate
|
|
|
|
// the string if there is not enough space for it so
|
|
|
|
// always do it manually
|
|
|
|
((wxChar*)buffer.GetPtr())[size - 1] = L'\0';
|
2010-05-20 12:23:13 +00:00
|
|
|
|
2021-09-06 18:28:26 +00:00
|
|
|
if (size >= MaxFormattedStringLength)
|
|
|
|
return size - 1;
|
2010-05-20 12:23:13 +00:00
|
|
|
|
2021-09-06 18:28:26 +00:00
|
|
|
// vsnprintf() may return either -1 (traditional Unix behavior) or the
|
|
|
|
// total number of characters which would have been written if the
|
|
|
|
// buffer were large enough (newer standards such as Unix98)
|
2010-05-20 12:23:13 +00:00
|
|
|
|
2021-09-06 18:28:26 +00:00
|
|
|
if (len < 0)
|
|
|
|
len = size + (size / 4);
|
2010-05-20 12:23:13 +00:00
|
|
|
|
2021-09-06 18:28:26 +00:00
|
|
|
len += writepos;
|
|
|
|
if (len < size)
|
|
|
|
return len;
|
|
|
|
buffer.Resize((len + 128) * sizeof(wxChar));
|
|
|
|
};
|
2010-05-20 12:23:13 +00:00
|
|
|
|
2021-09-06 18:28:26 +00:00
|
|
|
// performing an assertion or log of a truncated string is unsafe, so let's not; even
|
|
|
|
// though it'd be kinda nice if we did.
|
2010-10-18 01:40:49 +00:00
|
|
|
|
2021-09-06 18:28:26 +00:00
|
|
|
pxAssume(false);
|
|
|
|
return 0; // unreachable.
|
2010-05-20 12:23:13 +00:00
|
|
|
}
|
|
|
|
|
2010-08-03 22:18:19 +00:00
|
|
|
// --------------------------------------------------------------------------------------
|
|
|
|
// FastFormatUnicode (implementations)
|
|
|
|
// --------------------------------------------------------------------------------------
|
2010-10-04 17:12:28 +00:00
|
|
|
// [TODO] This class should actually be renamed to FastFormatNative or FastFormatString, and
|
|
|
|
// adopted to properly support 1-byte wxChar types (mostly requiring some changes to the
|
|
|
|
// WriteV functions). The current implementation is fine for wx2.8, which always defaults
|
|
|
|
// to wide-varieties of wxChar -- but wx3.0 will use UTF8 for linux distros, which will break
|
|
|
|
// this class nicely in its current state. --air
|
|
|
|
|
2010-08-03 22:18:19 +00:00
|
|
|
FastFormatUnicode::FastFormatUnicode()
|
2021-09-06 18:28:26 +00:00
|
|
|
: m_dest(2048)
|
2010-08-03 22:18:19 +00:00
|
|
|
{
|
2021-09-06 18:28:26 +00:00
|
|
|
Clear();
|
2010-08-03 22:18:19 +00:00
|
|
|
}
|
|
|
|
|
2010-10-04 17:12:28 +00:00
|
|
|
void FastFormatUnicode::Clear()
|
|
|
|
{
|
2021-09-06 18:28:26 +00:00
|
|
|
m_Length = 0;
|
|
|
|
((wxChar*)m_dest.GetPtr())[0] = 0;
|
2010-10-04 17:12:28 +00:00
|
|
|
}
|
|
|
|
|
2021-09-06 18:28:26 +00:00
|
|
|
FastFormatUnicode& FastFormatUnicode::WriteV(const char* fmt, va_list argptr)
|
2010-08-03 22:18:19 +00:00
|
|
|
{
|
2021-09-06 18:28:26 +00:00
|
|
|
wxString converted(fromUTF8(FastFormatAscii().WriteV(fmt, argptr)));
|
2010-08-03 22:18:19 +00:00
|
|
|
|
2021-09-06 18:28:26 +00:00
|
|
|
const uint inspos = m_Length;
|
|
|
|
const uint convLen = converted.Length();
|
|
|
|
m_dest.MakeRoomFor((inspos + convLen + 64) * sizeof(wxChar));
|
|
|
|
memcpy(&((wxChar*)m_dest.GetPtr())[inspos], converted.wc_str(), (convLen + 1) * sizeof(wxChar));
|
|
|
|
m_Length += convLen;
|
2010-10-18 01:40:49 +00:00
|
|
|
|
2021-09-06 18:28:26 +00:00
|
|
|
return *this;
|
2010-07-04 11:50:12 +00:00
|
|
|
}
|
|
|
|
|
2021-09-06 18:28:26 +00:00
|
|
|
FastFormatUnicode& FastFormatUnicode::WriteV(const wxChar* fmt, va_list argptr)
|
2010-05-20 12:23:13 +00:00
|
|
|
{
|
2021-09-06 18:28:26 +00:00
|
|
|
m_Length = format_that_unicode_mess(m_dest, m_Length, fmt, argptr);
|
|
|
|
return *this;
|
2010-08-03 22:18:19 +00:00
|
|
|
}
|
2010-05-20 12:23:13 +00:00
|
|
|
|
2021-09-06 18:28:26 +00:00
|
|
|
FastFormatUnicode& FastFormatUnicode::Write(const char* fmt, ...)
|
2010-08-03 22:18:19 +00:00
|
|
|
{
|
2021-09-06 18:28:26 +00:00
|
|
|
va_list list;
|
|
|
|
va_start(list, fmt);
|
|
|
|
WriteV(fmt, list);
|
|
|
|
va_end(list);
|
|
|
|
return *this;
|
2010-05-20 12:23:13 +00:00
|
|
|
}
|
|
|
|
|
2021-09-06 18:28:26 +00:00
|
|
|
FastFormatUnicode& FastFormatUnicode::Write(const wxChar* fmt, ...)
|
2010-05-20 12:23:13 +00:00
|
|
|
{
|
2021-09-06 18:28:26 +00:00
|
|
|
va_list list;
|
|
|
|
va_start(list, fmt);
|
|
|
|
WriteV(fmt, list);
|
|
|
|
va_end(list);
|
|
|
|
return *this;
|
2010-08-03 22:18:19 +00:00
|
|
|
}
|
|
|
|
|
2021-09-06 18:28:26 +00:00
|
|
|
FastFormatUnicode& FastFormatUnicode::Write(const wxString fmt, ...)
|
2014-06-29 06:57:33 +00:00
|
|
|
{
|
2021-09-06 18:28:26 +00:00
|
|
|
va_list list;
|
|
|
|
va_start(list, fmt);
|
|
|
|
WriteV(fmt.wx_str(), list);
|
|
|
|
va_end(list);
|
|
|
|
return *this;
|
2014-06-29 06:57:33 +00:00
|
|
|
}
|
|
|
|
|
2010-09-04 14:11:50 +00:00
|
|
|
bool FastFormatUnicode::IsEmpty() const
|
|
|
|
{
|
2021-09-06 18:28:26 +00:00
|
|
|
return ((wxChar&)m_dest[0]) == 0;
|
2010-09-04 14:11:50 +00:00
|
|
|
}
|
2010-08-03 22:18:19 +00:00
|
|
|
|
2021-09-06 18:28:26 +00:00
|
|
|
FastFormatUnicode& FastFormatUnicode::ToUpper()
|
2010-10-18 01:40:49 +00:00
|
|
|
{
|
2021-09-06 18:28:26 +00:00
|
|
|
wxChar* ch = (wxChar*)m_dest.GetPtr();
|
|
|
|
for (uint i = 0; i < m_Length; ++i, ++ch)
|
|
|
|
*ch = (wxChar)wxToupper(*ch);
|
2010-10-18 01:40:49 +00:00
|
|
|
|
2021-09-06 18:28:26 +00:00
|
|
|
return *this;
|
2010-10-18 01:40:49 +00:00
|
|
|
}
|
|
|
|
|
2021-09-06 18:28:26 +00:00
|
|
|
FastFormatUnicode& FastFormatUnicode::ToLower()
|
2010-10-18 01:40:49 +00:00
|
|
|
{
|
2021-09-06 18:28:26 +00:00
|
|
|
wxChar* ch = (wxChar*)m_dest.GetPtr();
|
|
|
|
for (uint i = 0; i < m_Length; ++i, ++ch)
|
|
|
|
*ch = (wxChar)wxTolower(*ch);
|
2010-10-18 01:40:49 +00:00
|
|
|
|
2021-09-06 18:28:26 +00:00
|
|
|
return *this;
|
2010-10-18 01:40:49 +00:00
|
|
|
}
|
|
|
|
|
2021-09-06 18:28:26 +00:00
|
|
|
FastFormatUnicode& FastFormatUnicode::operator+=(const char* psz)
|
2010-10-18 01:40:49 +00:00
|
|
|
{
|
2021-09-06 18:28:26 +00:00
|
|
|
Write(L"%s", WX_STR(fromUTF8(psz)));
|
|
|
|
return *this;
|
2010-10-18 01:40:49 +00:00
|
|
|
}
|
|
|
|
|
2021-09-06 18:28:26 +00:00
|
|
|
wxString& operator+=(wxString& str1, const FastFormatUnicode& str2)
|
2010-10-18 01:40:49 +00:00
|
|
|
{
|
2021-09-06 18:28:26 +00:00
|
|
|
str1.Append(str2.c_str(), str2.Length());
|
|
|
|
return str1;
|
2010-10-18 01:40:49 +00:00
|
|
|
}
|
|
|
|
|
2021-09-06 18:28:26 +00:00
|
|
|
wxString operator+(const wxString& str1, const FastFormatUnicode& str2)
|
2010-10-18 01:40:49 +00:00
|
|
|
{
|
2021-09-06 18:28:26 +00:00
|
|
|
wxString s = str1;
|
|
|
|
s += str2;
|
|
|
|
return s;
|
2010-10-18 01:40:49 +00:00
|
|
|
}
|
|
|
|
|
2021-09-06 18:28:26 +00:00
|
|
|
wxString operator+(const wxChar* str1, const FastFormatUnicode& str2)
|
2010-10-18 01:40:49 +00:00
|
|
|
{
|
2021-09-06 18:28:26 +00:00
|
|
|
wxString s = str1;
|
|
|
|
s += str2;
|
|
|
|
return s;
|
2010-12-28 04:14:47 +00:00
|
|
|
}
|
|
|
|
|
2021-09-06 18:28:26 +00:00
|
|
|
wxString operator+(const FastFormatUnicode& str1, const wxString& str2)
|
2010-12-28 04:14:47 +00:00
|
|
|
{
|
2021-09-06 18:28:26 +00:00
|
|
|
wxString s = str1;
|
|
|
|
s += str2;
|
|
|
|
return s;
|
2010-12-28 04:14:47 +00:00
|
|
|
}
|
2010-10-18 01:40:49 +00:00
|
|
|
|
2021-09-06 18:28:26 +00:00
|
|
|
wxString operator+(const FastFormatUnicode& str1, const wxChar* str2)
|
2010-12-28 04:14:47 +00:00
|
|
|
{
|
2021-09-06 18:28:26 +00:00
|
|
|
wxString s = str1;
|
|
|
|
s += str2;
|
|
|
|
return s;
|
2010-10-18 01:40:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2010-08-03 22:18:19 +00:00
|
|
|
// --------------------------------------------------------------------------------------
|
|
|
|
// FastFormatAscii (implementations)
|
|
|
|
// --------------------------------------------------------------------------------------
|
|
|
|
FastFormatAscii::FastFormatAscii()
|
2021-09-06 18:28:26 +00:00
|
|
|
: m_dest(2048)
|
2010-08-03 22:18:19 +00:00
|
|
|
{
|
2021-09-06 18:28:26 +00:00
|
|
|
Clear();
|
2010-08-03 22:18:19 +00:00
|
|
|
}
|
|
|
|
|
2010-10-04 17:12:28 +00:00
|
|
|
void FastFormatAscii::Clear()
|
2010-08-03 22:18:19 +00:00
|
|
|
{
|
2021-09-06 18:28:26 +00:00
|
|
|
m_dest.GetPtr()[0] = 0;
|
2010-08-03 22:18:19 +00:00
|
|
|
}
|
|
|
|
|
2010-10-04 17:12:28 +00:00
|
|
|
const wxString FastFormatAscii::GetString() const
|
2010-08-03 22:18:19 +00:00
|
|
|
{
|
2021-09-06 18:28:26 +00:00
|
|
|
return fromAscii(m_dest.GetPtr());
|
2010-10-04 17:12:28 +00:00
|
|
|
}
|
2010-08-03 22:18:19 +00:00
|
|
|
|
2021-09-06 18:28:26 +00:00
|
|
|
FastFormatAscii& FastFormatAscii::WriteV(const char* fmt, va_list argptr)
|
2010-08-03 22:18:19 +00:00
|
|
|
{
|
2021-09-06 18:28:26 +00:00
|
|
|
format_that_ascii_mess(m_dest, strlen(m_dest.GetPtr()), fmt, argptr);
|
|
|
|
return *this;
|
2010-08-03 22:18:19 +00:00
|
|
|
}
|
|
|
|
|
2021-09-06 18:28:26 +00:00
|
|
|
FastFormatAscii& FastFormatAscii::Write(const char* fmt, ...)
|
2010-08-03 22:18:19 +00:00
|
|
|
{
|
2021-09-06 18:28:26 +00:00
|
|
|
va_list list;
|
|
|
|
va_start(list, fmt);
|
|
|
|
WriteV(fmt, list);
|
|
|
|
va_end(list);
|
|
|
|
return *this;
|
2010-08-03 22:18:19 +00:00
|
|
|
}
|
|
|
|
|
2010-09-04 14:11:50 +00:00
|
|
|
|
|
|
|
bool FastFormatAscii::IsEmpty() const
|
|
|
|
{
|
2021-09-06 18:28:26 +00:00
|
|
|
return m_dest[0] == 0;
|
2010-09-04 14:11:50 +00:00
|
|
|
}
|