From d7e5ea675feadec4ff9bc94a90ca2ccfe8c407e5 Mon Sep 17 00:00:00 2001 From: Glenn Maynard Date: Mon, 18 Sep 2006 22:31:29 +0000 Subject: [PATCH] split windows-specific stuff out of RageUtil --- stepmania/src/RageUtil.cpp | 75 --------------- .../src/archutils/Win32/ErrorStrings.cpp | 95 +++++++++++++++++++ stepmania/src/archutils/Win32/ErrorStrings.h | 35 +++++++ 3 files changed, 130 insertions(+), 75 deletions(-) create mode 100644 stepmania/src/archutils/Win32/ErrorStrings.cpp create mode 100644 stepmania/src/archutils/Win32/ErrorStrings.h diff --git a/stepmania/src/RageUtil.cpp b/stepmania/src/RageUtil.cpp index c20c7d9d7f..44894ebbf1 100644 --- a/stepmania/src/RageUtil.cpp +++ b/stepmania/src/RageUtil.cpp @@ -260,81 +260,6 @@ RString vssprintf( const char *fmt, va_list argList ) return sStr; } -#ifdef WIN32 - -#ifdef _XBOX -# include -#else -# include -#endif - -RString werr_ssprintf( int err, const char *fmt, ...) -{ - char buf[1024] = ""; -#ifndef _XBOX - FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM, - 0, err, 0, buf, sizeof(buf), NULL); -#endif - - /* Why is FormatMessage returning text ending with \r\n? */ - RString text = buf; - text.Replace( "\n", "" ); - text.Replace( "\r", " " ); /* foo\r\nbar -> foo bar */ - TrimRight( text ); /* "foo\r\n" -> "foo" */ - - va_list va; - va_start(va, fmt); - RString s = vssprintf( fmt, va ); - va_end(va); - - return s += ssprintf( " (%s)", text.c_str() ); -} - -RString ConvertWstringToCodepage( wstring s, int iCodePage ) -{ - if( s.empty() ) - return RString(); - - int iBytes = WideCharToMultiByte( iCodePage, 0, s.data(), s.size(), - NULL, 0, NULL, FALSE ); - ASSERT_M( iBytes > 0, werr_ssprintf( GetLastError(), "WideCharToMultiByte" ).c_str() ); - - RString ret; - WideCharToMultiByte( CP_ACP, 0, s.data(), s.size(), - ret.GetBuffer( iBytes ), iBytes, NULL, FALSE ); - ret.ReleaseBuffer( iBytes ); - - return ret; -} - -RString ConvertUTF8ToACP( const RString &s ) -{ - return ConvertWstringToCodepage( RStringToWstring(s), CP_ACP ); -} - -wstring ConvertCodepageToWString( RString s, int iCodePage ) -{ - if( s.empty() ) - return wstring(); - - int iBytes = MultiByteToWideChar( iCodePage, 0, s.data(), s.size(), NULL, 0 ); - ASSERT_M( iBytes > 0, werr_ssprintf( GetLastError(), "MultiByteToWideChar" ).c_str() ); - - wchar_t *pTemp = new wchar_t[iBytes]; - MultiByteToWideChar( iCodePage, 0, s.data(), s.size(), pTemp, iBytes ); - wstring sRet( pTemp, iBytes ); - delete [] pTemp; - - return sRet; -} - -RString ConvertACPToUTF8( const RString &s ) -{ - return WStringToRString( ConvertCodepageToWString(s, CP_ACP) ); -} - -#endif - /* ISO-639-1 codes: http://www.loc.gov/standards/iso639-2/langcodes.html * native forms: http://people.w3.org/rishida/names/languages.html * We don't use 3-letter codes, so we don't bother supporting them. */ diff --git a/stepmania/src/archutils/Win32/ErrorStrings.cpp b/stepmania/src/archutils/Win32/ErrorStrings.cpp new file mode 100644 index 0000000000..0b3230eabc --- /dev/null +++ b/stepmania/src/archutils/Win32/ErrorStrings.cpp @@ -0,0 +1,95 @@ +#include "global.h" +#include "ErrorStrings.h" +#include "RageUtil.h" + +#include + +RString werr_ssprintf( int err, const char *fmt, ... ) +{ + char buf[1024] = ""; +#ifndef _XBOX + FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM, + 0, err, 0, buf, sizeof(buf), NULL); +#endif + + /* Why is FormatMessage returning text ending with \r\n? */ + RString text = buf; + text.Replace( "\n", "" ); + text.Replace( "\r", " " ); /* foo\r\nbar -> foo bar */ + TrimRight( text ); /* "foo\r\n" -> "foo" */ + + va_list va; + va_start(va, fmt); + RString s = vssprintf( fmt, va ); + va_end(va); + + return s += ssprintf( " (%s)", text.c_str() ); +} + +RString ConvertWstringToCodepage( wstring s, int iCodePage ) +{ + if( s.empty() ) + return RString(); + + int iBytes = WideCharToMultiByte( iCodePage, 0, s.data(), s.size(), + NULL, 0, NULL, FALSE ); + ASSERT_M( iBytes > 0, werr_ssprintf( GetLastError(), "WideCharToMultiByte" ).c_str() ); + + RString ret; + WideCharToMultiByte( CP_ACP, 0, s.data(), s.size(), + ret.GetBuffer( iBytes ), iBytes, NULL, FALSE ); + ret.ReleaseBuffer( iBytes ); + + return ret; +} + +RString ConvertUTF8ToACP( const RString &s ) +{ + return ConvertWstringToCodepage( RStringToWstring(s), CP_ACP ); +} + +wstring ConvertCodepageToWString( RString s, int iCodePage ) +{ + if( s.empty() ) + return wstring(); + + int iBytes = MultiByteToWideChar( iCodePage, 0, s.data(), s.size(), NULL, 0 ); + ASSERT_M( iBytes > 0, werr_ssprintf( GetLastError(), "MultiByteToWideChar" ).c_str() ); + + wchar_t *pTemp = new wchar_t[iBytes]; + MultiByteToWideChar( iCodePage, 0, s.data(), s.size(), pTemp, iBytes ); + wstring sRet( pTemp, iBytes ); + delete [] pTemp; + + return sRet; +} + +RString ConvertACPToUTF8( const RString &s ) +{ + return WStringToRString( ConvertCodepageToWString(s, CP_ACP) ); +} + +/* + * Copyright (c) 2001-2005 Chris Danford, Glenn Maynard + * All rights reserved. + * + * Permission is hereby granted, free of charge, to any person obtaining a + * copy of this software and associated documentation files (the + * "Software"), to deal in the Software without restriction, including + * without limitation the rights to use, copy, modify, merge, publish, + * distribute, and/or sell copies of the Software, and to permit persons to + * whom the Software is furnished to do so, provided that the above + * copyright notice(s) and this permission notice appear in all copies of + * the Software and that both the above copyright notice(s) and this + * permission notice appear in supporting documentation. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS + * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF + * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT OF + * THIRD PARTY RIGHTS. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR HOLDERS + * INCLUDED IN THIS NOTICE BE LIABLE FOR ANY CLAIM, OR ANY SPECIAL INDIRECT + * OR CONSEQUENTIAL DAMAGES, OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS + * OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR + * OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR + * PERFORMANCE OF THIS SOFTWARE. + */ diff --git a/stepmania/src/archutils/Win32/ErrorStrings.h b/stepmania/src/archutils/Win32/ErrorStrings.h new file mode 100644 index 0000000000..123a24520c --- /dev/null +++ b/stepmania/src/archutils/Win32/ErrorStrings.h @@ -0,0 +1,35 @@ +#ifndef ERROR_STRINGS_H +#define ERROR_STRINGS_H + +RString werr_ssprintf( int err, const char *fmt, ... ); +RString ConvertWstringToCodepage( wstring s, int iCodePage ); +RString ConvertUTF8ToACP( const RString &s ); +wstring ConvertCodepageToWString( RString s, int iCodePage ); +RString ConvertACPToUTF8( const RString &s ); + +#endif + +/* + * Copyright (c) 2001-2005 Chris Danford, Glenn Maynard + * All rights reserved. + * + * Permission is hereby granted, free of charge, to any person obtaining a + * copy of this software and associated documentation files (the + * "Software"), to deal in the Software without restriction, including + * without limitation the rights to use, copy, modify, merge, publish, + * distribute, and/or sell copies of the Software, and to permit persons to + * whom the Software is furnished to do so, provided that the above + * copyright notice(s) and this permission notice appear in all copies of + * the Software and that both the above copyright notice(s) and this + * permission notice appear in supporting documentation. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS + * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF + * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT OF + * THIRD PARTY RIGHTS. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR HOLDERS + * INCLUDED IN THIS NOTICE BE LIABLE FOR ANY CLAIM, OR ANY SPECIAL INDIRECT + * OR CONSEQUENTIAL DAMAGES, OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS + * OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR + * OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR + * PERFORMANCE OF THIS SOFTWARE. + */