[Mobies-commit] [commit] r4145 - UDM/trunk/src/UIntWizVS/Templates/1033

ksmyth at redhat1.isis.vanderbilt.edu ksmyth at redhat1.isis.vanderbilt.edu
Thu Dec 27 12:37:52 CST 2012


Author: ksmyth
Date: Thu Dec 27 12:37:52 2012
New Revision: 4145

Log:
Unicode

Modified:
   UDM/trunk/src/UIntWizVS/Templates/1033/UdmConsole.cpp
   UDM/trunk/src/UIntWizVS/Templates/1033/UdmConsole.h

Modified: UDM/trunk/src/UIntWizVS/Templates/1033/UdmConsole.cpp
==============================================================================
--- UDM/trunk/src/UIntWizVS/Templates/1033/UdmConsole.cpp	Thu Dec 27 10:56:18 2012	(r4144)
+++ UDM/trunk/src/UIntWizVS/Templates/1033/UdmConsole.cpp	Thu Dec 27 12:37:52 2012	(r4145)
@@ -5,6 +5,41 @@
 
 namespace GMEConsole
 {
+	CComBSTR BSTRFromUTF8(const std::string& utf8)
+	{
+		if (utf8.empty())
+			return CComBSTR();
+
+		// Fail if an invalid input character is encountered
+		const DWORD conversionFlags = MB_ERR_INVALID_CHARS;
+
+		const int utf16Length = ::MultiByteToWideChar(CP_UTF8, conversionFlags, utf8.data(), utf8.length(), NULL, 0);
+		if (utf16Length == 0)
+		{
+			DWORD error = ::GetLastError();
+
+			throw udm_exception(
+				(error == ERROR_NO_UNICODE_TRANSLATION) ? 
+					"Invalid UTF-8 sequence found in input string." :
+					"Can't get length of UTF-16 string (MultiByteToWideChar failed).");
+		}
+
+		BSTR utf16 = SysAllocStringByteLen(NULL, utf16Length*2);
+		if (utf16 == NULL)
+			throw std::bad_alloc();
+
+		if (!::MultiByteToWideChar(CP_UTF8, 0, utf8.data(), utf8.length(), utf16, utf16Length))
+		{
+			DWORD error = ::GetLastError();
+			SysFreeString(utf16);
+			throw udm_exception("Can't convert string from UTF-8 to UTF-16 (MultiByteToWideChar failed).");
+		}
+
+		CComBSTR ret;
+		ret.m_str = utf16;
+		return ret;
+	}
+
 	CComPtr<IGMEOLEApp> Console::gmeoleapp=0;
 
 	void Console::setupConsole(CComPtr<IMgaProject> project)

Modified: UDM/trunk/src/UIntWizVS/Templates/1033/UdmConsole.h
==============================================================================
--- UDM/trunk/src/UIntWizVS/Templates/1033/UdmConsole.h	Thu Dec 27 10:56:18 2012	(r4144)
+++ UDM/trunk/src/UIntWizVS/Templates/1033/UdmConsole.h	Thu Dec 27 12:37:52 2012	(r4145)
@@ -12,6 +12,8 @@
 
 namespace GMEConsole
 {
+	CComBSTR BSTRFromUTF8(const std::string & utf8);
+
 	class Console
 	{
 	friend class RawComponent;
@@ -26,10 +28,10 @@
 		static void setupConsole(CComPtr<IMgaProject> project);
 		static void freeConsole()
 		{
-			if(gmeoleapp!=0)
+			if (gmeoleapp != 0)
 			{
 				gmeoleapp.Release();
-				gmeoleapp=0;
+				gmeoleapp = 0;
 			}
 		}
 		static void SetupConsole(CComPtr<IMgaProject> project) { setupConsole(project); }
@@ -41,15 +43,34 @@
 				case MSG_NORMAL:
 				case MSG_INFO:
 				case MSG_WARNING:
-					_tprintf("%s", message.c_str());
+					printf("%s", message.c_str());
+					break;
+				case MSG_ERROR:
+					fprintf(stderr, "%s", message.c_str());
+					break;
+				}
+			} else {
+				if(S_OK != gmeoleapp->ConsoleMessage(CComBSTR(message.length(), message.c_str()), type))
+					throw udm_exception("Could not write to GME console.");
+			}
+		}
+
+		static void writeLine(const std::wstring& message, msgtype_enum type)
+		{
+			if (gmeoleapp == 0) {
+				switch (type) {
+				case MSG_NORMAL:
+				case MSG_INFO:
+				case MSG_WARNING:
+					wprintf(L"%s", message.c_str());
 					break;
 				case MSG_ERROR:
-					_ftprintf(stderr, "%s", message.c_str());
+					fwprintf(stderr, L"%s", message.c_str());
 					break;
 				}
 			} else {
-				if(S_OK != gmeoleapp->ConsoleMessage( CComBSTR(message.length(),message.c_str()),type))
-					throw udm_exception("Could not write GME console.");
+				if(S_OK != gmeoleapp->ConsoleMessage(CComBSTR(message.length(), message.c_str()), type))
+					throw udm_exception("Could not write to GME console.");
 			}
 		}
 
@@ -61,9 +82,15 @@
 
 		static void setContents(const std::string& contents)
 		{
-			if(S_OK != gmeoleapp->put_ConsoleContents( CComBSTR(contents.length(),contents.c_str()) ))
-				throw udm_exception("Could not set the contents of GME console.");
-
+			if (gmeoleapp != 0)
+				if(S_OK != gmeoleapp->put_ConsoleContents( CComBSTR(contents.length(),contents.c_str()) ))
+					throw udm_exception("Could not set the contents of GME console.");
+		}
+		static void setContents(const std::wstring& contents)
+		{
+			if (gmeoleapp != 0)
+				if(S_OK != gmeoleapp->put_ConsoleContents( CComBSTR(contents.length(),contents.c_str()) ))
+					throw udm_exception("Could not set the contents of GME console.");
 		}
 
 		class Error
@@ -73,6 +100,10 @@
 			{
 				Console::writeLine(message,MSG_ERROR);
 			}
+			static void writeLine(const std::wstring& message)
+			{
+				Console::writeLine(message,MSG_ERROR);
+			}
 		};
 		class Out
 		{
@@ -81,6 +112,10 @@
 			{
 				Console::writeLine(message, MSG_NORMAL);
 			}
+			static void writeLine(const std::wstring& message)
+			{
+				Console::writeLine(message, MSG_NORMAL);
+			}
 		};
 		class Warning
 		{
@@ -89,6 +124,10 @@
 			{
 				Console::writeLine(message, MSG_WARNING);
 			}
+			static void writeLine(const std::wstring& message)
+			{
+				Console::writeLine(message, MSG_WARNING);
+			}
 		};
 		class Info
 		{
@@ -97,6 +136,10 @@
 			{
 				Console::writeLine(message,MSG_INFO);
 			}
+			static void writeLine(const std::wstring& message)
+			{
+				Console::writeLine(message,MSG_INFO);
+			}
 		};
 	};
 }
\ No newline at end of file


More information about the Mobies-commit mailing list