Better Unicode support in the console

This was SVN commit r1038.
This commit is contained in:
Ykkrosh 2004-08-24 11:07:50 +00:00
parent 506b283844
commit 9fa5e3b57a
3 changed files with 36 additions and 17 deletions

View File

@ -457,19 +457,23 @@ void CConsole::ProcessBuffer(const wchar_t* szLine){
std::map<std::wstring, fptr>::iterator Iter;
if (szLine[0] == '\\'){
if (szLine[0] == '\\')
{
swscanf(szLine, L"\\%ls", szCommand);
Trim(szCommand);
ToLower(szCommand);
if (!wcscmp(szCommand, L"info")){
if (!wcscmp(szCommand, L"info"))
{
InsertMessage(L"");
InsertMessage(L"[Information]");
InsertMessage(L" -View commands \"\\commands\"");
InsertMessage(L" -Call command \"\\<command>\"");
InsertMessage(L" -Say \"<string>\"");
InsertMessage(L"");
}else if (!wcscmp(szCommand, L"commands")){
}
else if (!wcscmp(szCommand, L"commands"))
{
InsertMessage(L"");
InsertMessage(L"[Commands]");
@ -479,7 +483,9 @@ void CConsole::ProcessBuffer(const wchar_t* szLine){
InsertMessage(L" \\%ls", Iter->first.data());
InsertMessage(L"");
}else{
}
else
{
Iter = m_mapFuncList.find(szCommand);
if (Iter == m_mapFuncList.end())
InsertMessage(L"unknown command <%ls>", szCommand);
@ -487,22 +493,26 @@ void CConsole::ProcessBuffer(const wchar_t* szLine){
Iter->second();
}
}
else if( szLine[0] == ':' )
else if (szLine[0] == ':')
{
// Process it as JavaScript
// Convert Unicode to 8-bit sort-of-ASCII
g_ScriptingHost.ExecuteScript( CStr16( szLine + 1 ) );
}
else if( szLine[0] == '?' )
else if (szLine[0] == '?')
{
// Process it as JavaScript and display the result
jsval rval = g_ScriptingHost.ExecuteScript( CStr16( szLine + 1 ) );
if( rval )
InsertMessage( L"%hs", g_ScriptingHost.ValueToString( rval ).c_str() );
if (rval)
{
try {
InsertMessage( L"%ls", g_ScriptingHost.ValueToUCString( rval ).c_str() );
} catch (PSERROR_Scripting_ConversionFailed) {
InsertMessage( L"%hs", "<error converting return value to string>" );
}
}
}
else
SendChatMessage(szLine);

View File

@ -85,18 +85,18 @@ JSBool JSI_Console::getConsole( JSContext* cx, JSObject* obj, jsval id, jsval* v
JSBool JSI_Console::writeConsole( JSContext* UNUSEDPARAM(context), JSObject* UNUSEDPARAM(globalObject), unsigned int argc, jsval* argv, jsval* UNUSEDPARAM(rval) )
{
assert( argc >= 1 );
CStr output;
CStrW output;
for( unsigned int i = 0; i < argc; i++ )
{
try
{
CStr arg = g_ScriptingHost.ValueToString( argv[0] );
CStrW arg = g_ScriptingHost.ValueToUCString( argv[0] );
output += arg;
}
catch( PSERROR_Scripting_ConversionFailed )
{
}
}
g_Console->InsertMessage( L"%hs", (const char*)output );
g_Console->InsertMessage( L"%ls", output.c_str() );
return( JS_TRUE );
}

View File

@ -279,9 +279,11 @@ bool ScriptingHost::ValueToBool(const jsval value)
std::string ScriptingHost::ValueToString(const jsval value)
{
JSString * string = JS_ValueToString(m_Context, value);
JSString* string = JS_ValueToString(m_Context, value);
if (string == NULL)
throw PSERROR_Scripting_ConversionFailed();
char * bytes = JS_GetStringBytes(string);
char* bytes = JS_GetStringBytes(string);
if (bytes == NULL)
throw PSERROR_Scripting_ConversionFailed();
@ -290,8 +292,15 @@ std::string ScriptingHost::ValueToString(const jsval value)
CStrW ScriptingHost::ValueToUCString( const jsval value )
{
JSString* string = JS_ValueToString( m_Context, value );
return( CStrW( JS_GetStringChars( string ) ) );
JSString* string = JS_ValueToString(m_Context, value);
if (string == NULL)
throw PSERROR_Scripting_ConversionFailed();
jschar* chars = JS_GetStringChars(string);
if (chars == NULL)
throw PSERROR_Scripting_ConversionFailed();
return CStrW(chars);
}
jsval ScriptingHost::UCStringToValue(const utf16string &str)