Clean up debug string functions in OS_HLE.

* Use GetString instead of GetPointer.
 * Use C++ strings instead of char* pointer math.
This commit is contained in:
Scott Mansell 2014-11-02 21:39:37 +13:00
parent 9daaf94f3c
commit 52d42bf330
2 changed files with 21 additions and 28 deletions

View File

@ -32,7 +32,7 @@ void HLE_OSPanic()
void HLE_GeneralDebugPrint() void HLE_GeneralDebugPrint()
{ {
std::string ReportMessage; std::string ReportMessage;
if (*(u32*)Memory::GetPointer(GPR(3)) > 0x80000000) if (Memory::Read_U32(GPR(3)) > 0x80000000)
{ {
GetStringVA(ReportMessage, 4); GetStringVA(ReportMessage, 4);
} }
@ -60,33 +60,26 @@ void HLE_write_console()
void GetStringVA(std::string& _rOutBuffer, u32 strReg) void GetStringVA(std::string& _rOutBuffer, u32 strReg)
{ {
_rOutBuffer = ""; _rOutBuffer = "";
char ArgumentBuffer[256]; std::string ArgumentBuffer = "";
u32 ParameterCounter = strReg+1; u32 ParameterCounter = strReg+1;
u32 FloatingParameterCounter = 1; u32 FloatingParameterCounter = 1;
char *pString = (char*)Memory::GetPointer(GPR(strReg)); std::string string = Memory::GetString(GPR(strReg));
if (!pString)
{
ERROR_LOG(OSREPORT, "r%i invalid", strReg);
return;
}
while (*pString) for(u32 i = 0; i < string.size(); i++)
{ {
if (*pString == '%') if (string[i] == '%')
{ {
char* pArgument = ArgumentBuffer; ArgumentBuffer = "%";
*pArgument++ = *pString++; i++;
if (*pString == '%') if (string[i] == '%')
{ {
_rOutBuffer += "%"; _rOutBuffer += "%";
pString++;
continue; continue;
} }
while (*pString < 'A' || *pString > 'z' || *pString == 'l' || *pString == '-') while (string[i] < 'A' || string[i] > 'z' || string[i] == 'l' || string[i] == '-')
*pArgument++ = *pString++; ArgumentBuffer += string[i++];
*pArgument++ = *pString; ArgumentBuffer += string[i];
*pArgument = 0;
u64 Parameter; u64 Parameter;
if (ParameterCounter > 10) if (ParameterCounter > 10)
@ -95,7 +88,7 @@ void GetStringVA(std::string& _rOutBuffer, u32 strReg)
} }
else else
{ {
if ((*(pString-2) == 'l') && (*(pString-1) == 'l')) // hax, just seen this on sysmenu osreport if ((string[i-2] == 'l') && (string[i-1] == 'l')) // hax, just seen this on sysmenu osreport
{ {
Parameter = GPR(++ParameterCounter); Parameter = GPR(++ParameterCounter);
Parameter = (Parameter<<32)|GPR(++ParameterCounter); Parameter = (Parameter<<32)|GPR(++ParameterCounter);
@ -105,23 +98,22 @@ void GetStringVA(std::string& _rOutBuffer, u32 strReg)
} }
ParameterCounter++; ParameterCounter++;
switch (*pString) switch (string[i])
{ {
case 's': case 's':
_rOutBuffer += StringFromFormat(ArgumentBuffer, (char*)Memory::GetPointer((u32)Parameter)); _rOutBuffer += StringFromFormat(ArgumentBuffer.c_str(), Memory::GetString((u32)Parameter).c_str());
break; break;
case 'd': case 'd':
case 'i': case 'i':
{ {
//u64 Double = Memory::Read_U64(Parameter); _rOutBuffer += StringFromFormat(ArgumentBuffer.c_str(), Parameter);
_rOutBuffer += StringFromFormat(ArgumentBuffer, Parameter);
break; break;
} }
case 'f': case 'f':
{ {
_rOutBuffer += StringFromFormat(ArgumentBuffer, _rOutBuffer += StringFromFormat(ArgumentBuffer.c_str(),
rPS0(FloatingParameterCounter)); rPS0(FloatingParameterCounter));
FloatingParameterCounter++; FloatingParameterCounter++;
ParameterCounter--; ParameterCounter--;
@ -134,15 +126,13 @@ void GetStringVA(std::string& _rOutBuffer, u32 strReg)
break; break;
default: default:
_rOutBuffer += StringFromFormat(ArgumentBuffer, Parameter); _rOutBuffer += StringFromFormat(ArgumentBuffer.c_str(), Parameter);
break; break;
} }
pString++;
} }
else else
{ {
_rOutBuffer += StringFromFormat("%c", *pString); _rOutBuffer += string[i];
pString++;
} }
} }
if (_rOutBuffer[_rOutBuffer.length() - 1] == '\n') if (_rOutBuffer[_rOutBuffer.length() - 1] == '\n')

View File

@ -310,6 +310,9 @@ void DMA_MemoryToLC(const u32 _CacheAddr, const u32 _MemAddr, const u32 _iNumBlo
std::string GetString(u32 em_address, size_t size) std::string GetString(u32 em_address, size_t size)
{ {
const char* ptr = reinterpret_cast<const char*>(GetPointer(em_address)); const char* ptr = reinterpret_cast<const char*>(GetPointer(em_address));
if (ptr == nullptr)
return "";
if (size == 0) // Null terminated string. if (size == 0) // Null terminated string.
{ {
return std::string(ptr); return std::string(ptr);