[Box Backup-dev] COMMIT r386 - box/chris/win32/vc2005-compile-fixes/lib/win32

boxbackup-dev at fluffy.co.uk boxbackup-dev at fluffy.co.uk
Sun Feb 5 00:04:34 GMT 2006


Author: chris
Date: 2006-02-05 00:04:30 +0000 (Sun, 05 Feb 2006)
New Revision: 386

Modified:
   box/chris/win32/vc2005-compile-fixes/lib/win32/emu.cpp
Log:
* emu.cpp
- Refactored code page conversion routines to be more general and reduce
  duplicated code
- Added ConvertConsoleToUtf8 method, used by BackupQueries to parse
  input file names


Modified: box/chris/win32/vc2005-compile-fixes/lib/win32/emu.cpp
===================================================================
--- box/chris/win32/vc2005-compile-fixes/lib/win32/emu.cpp	2006-02-05 00:03:10 UTC (rev 385)
+++ box/chris/win32/vc2005-compile-fixes/lib/win32/emu.cpp	2006-02-05 00:04:30 UTC (rev 386)
@@ -206,141 +206,206 @@
 // --------------------------------------------------------------------------
 //
 // Function
-//		Name:    ConvertUtf8ToMultiByte
-//		Purpose: Converts a string from UTF-8 to multibyte characters (MBCS).
-//			Returns a buffer which MUST be freed by the caller with delete[].
-//			In case of fire, logs the error and returns NULL.
+//		Name:    ConvertToWideString
+//		Purpose: Converts a string from specified codepage to 
+//			 a wide string (WCHAR*). Returns a buffer which 
+//			 MUST be freed by the caller with delete[].
+//			 In case of fire, logs the error and returns NULL.
 //		Created: 4th February 2006
 //
 // --------------------------------------------------------------------------
-WCHAR* ConvertUtf8ToMultiByte(const char* pName)
+WCHAR* ConvertToWideString(const char* pString, unsigned int codepage)
 {
-	int len = MultiByteToWideChar(
-		CP_UTF8,       // source code page
-		0,             // character-type options
-		pName,         // string to map
-		strlen(pName), // number of bytes in string
-		NULL,          // wide-character buffer
-		0              // size of buffer - work out 
-		               //   how much space we need
-		);
+	int len = MultiByteToWideChar
+	(
+		codepage, // source code page
+		0,        // character-type options
+		pString,  // string to map
+		-1,       // number of bytes in string - auto detect
+		NULL,     // wide-character buffer
+		0         // size of buffer - work out 
+		          //   how much space we need
+	);
 
-	WCHAR* buffer = new WCHAR[len+1];
+	if (len == 0)
+	{
+		::syslog(LOG_WARNING, 
+			"Failed to convert string to wide string: "
+			"error %d", GetLastError());
+		errno = EINVAL;
+		return NULL;
+	}
 
+	WCHAR* buffer = new WCHAR[len];
+
 	if (buffer == NULL)
 	{
 		::syslog(LOG_WARNING, 
-			"Failed to convert string to multibyte: '%s': "
-			"out of memory", pName);
+			"Failed to convert string to wide string: "
+			"out of memory");
 		errno = ENOMEM;
 		return NULL;
 	}
 
-	len = MultiByteToWideChar(
-		CP_UTF8,       // source code page
-		0,             // character-type options
-		pName,         // string to map
-		strlen(pName), // number of bytes in string
-		buffer,        // wide-character buffer
-		len         // size of buffer
-		);
+	len = MultiByteToWideChar
+	(
+		codepage, // source code page
+		0,        // character-type options
+		pString,  // string to map
+		-1,       // number of bytes in string - auto detect
+		buffer,   // wide-character buffer
+		len       // size of buffer
+	);
 
 	if (len == 0)
 	{
 		::syslog(LOG_WARNING, 
-			"Failed to convert string to multibyte: '%s': "
-			"error %i", pName, GetLastError());
+			"Failed to convert string to wide string: "
+			"error %i", GetLastError());
 		errno = EACCES;
 		delete [] buffer;
 		return NULL;
 	}
 
-	buffer[len] = L'\0';
-
 	return buffer;
 }
 
 // --------------------------------------------------------------------------
 //
 // Function
-//		Name:    ConvertUtf8ToConsole
-//		Purpose: Converts a string from UTF-8 to the console 
-//			 code page. Returns a buffer which MUST be freed 
-//			 by the caller with delete[].
+//		Name:    ConvertUtf8ToWideString
+//		Purpose: Converts a string from UTF-8 to a wide string.
+//			 Returns a buffer which MUST be freed by the caller 
+//			 with delete[].
 //			 In case of fire, logs the error and returns NULL.
 //		Created: 4th February 2006
 //
 // --------------------------------------------------------------------------
-char* ConvertUtf8ToConsole(const char* pString)
+WCHAR* ConvertUtf8ToWideString(const char* pString)
 {
-	WCHAR* pMulti = ConvertUtf8ToMultiByte(pString);
-	if (pMulti == NULL)
-	{
-		return NULL;
-	}
+	return ConvertToWideString(pString, CP_UTF8);
+}
 
-	int len = WideCharToMultiByte(
-		GetConsoleCP(), // destination code page
-		0,              // character-type options
-		pMulti,         // string to map
-		-1,             // number of bytes in string - auto detect
-		NULL,           // output buffer
-		0,              // size of buffer - work out 
-		                //   how much space we need
-		"?",		// replace unknown chars with "?"
-		NULL		// don't tell us when that happened
-		);
+// --------------------------------------------------------------------------
+//
+// Function
+//		Name:    ConvertFromWideString
+//		Purpose: Converts a wide string to a narrow string in the
+//			 specified code page. Returns a buffer which MUST 
+//			 be freed by the caller with delete[].
+//			 In case of fire, logs the error and returns NULL.
+//		Created: 4th February 2006
+//
+// --------------------------------------------------------------------------
+char* ConvertFromWideString(const WCHAR* pString, unsigned int codepage)
+{
+	int len = WideCharToMultiByte
+	(
+		codepage, // destination code page
+		0,        // character-type options
+		pString,  // string to map
+		-1,       // number of bytes in string - auto detect
+		NULL,     // output buffer
+		0,        // size of buffer - work out 
+		          //   how much space we need
+		"?",      // replace unknown chars with "?"
+		NULL      // don't tell us when that happened
+	);
 
 	if (len == 0)
 	{
 		::syslog(LOG_WARNING, 
-			"Failed to convert UTF-8 string to console: '%s': "
-			"error %d", pString, GetLastError());
-		delete [] pMulti;
+			"Failed to convert wide string to narrow: "
+			"error %d", GetLastError());
 		errno = EINVAL;
 		return NULL;
 	}
 
-	char* buffer = new char[len+1];
+	char* buffer = new char[len];
 
 	if (buffer == NULL)
 	{
 		::syslog(LOG_WARNING, 
-			"Failed to convert UTF-8 string to console: '%s': "
-			"out of memory", pString);
-		delete [] pMulti;
+			"Failed to convert wide string to narrow: "
+			"out of memory");
 		errno = ENOMEM;
 		return NULL;
 	}
 
-	len = WideCharToMultiByte(
-		GetConsoleCP(), // source code page
-		0,              // character-type options
-		pMulti,         // string to map
-		-1,             // number of bytes in string - auto detect
-		buffer,         // output buffer
-		len,            // size of buffer
-		"?",		// replace unknown chars with "?"
-		NULL		// don't tell us when that happened
-		);
+	len = WideCharToMultiByte
+	(
+		codepage, // source code page
+		0,        // character-type options
+		pString,  // string to map
+		-1,       // number of bytes in string - auto detect
+		buffer,   // output buffer
+		len,      // size of buffer
+		"?",      // replace unknown chars with "?"
+		NULL      // don't tell us when that happened
+	);
 
 	if (len == 0)
 	{
 		::syslog(LOG_WARNING, 
-			"Failed to convert UTF-8 string to console: '%s': "
-			"error %i", pString, GetLastError());
-		delete [] pMulti;
+			"Failed to convert wide string to narrow: "
+			"error %i", GetLastError());
 		errno = EACCES;
 		delete [] buffer;
 		return NULL;
 	}
 
-	buffer[len] = L'\0';
+	return buffer;
+}
+
+// --------------------------------------------------------------------------
+//
+// Function
+//		Name:    ConvertUtf8ToConsole
+//		Purpose: Converts a string from UTF-8 to the console 
+//			 code page. Returns a buffer which MUST be freed 
+//			 by the caller with delete[].
+//			 In case of fire, logs the error and returns NULL.
+//		Created: 4th February 2006
+//
+// --------------------------------------------------------------------------
+char* ConvertUtf8ToConsole(const char* pString)
+{
+	WCHAR* pMulti = ConvertToWideString(pString, CP_UTF8);
+	if (pMulti == NULL)
+	{
+		return NULL;
+	}
+
+	char* pConsole = ConvertFromWideString(pMulti, GetConsoleOutputCP());
 	delete [] pMulti;
+	return pConsole;
+}
 
-	return buffer;
+// --------------------------------------------------------------------------
+//
+// Function
+//		Name:    ConvertConsoleToUtf8
+//		Purpose: Converts a string from the console code page
+//			 to UTF-8. Returns a buffer which MUST be freed 
+//			 by the caller with delete[].
+//			 In case of fire, logs the error and returns NULL.
+//		Created: 4th February 2006
+//
+// --------------------------------------------------------------------------
+char* ConvertConsoleToUtf8(const char* pString)
+{
+	WCHAR* pMulti = ConvertToWideString(pString, GetConsoleCP());
+	if (pMulti == NULL)
+	{
+		return NULL;
+	}
+
+	char* pConsole = ConvertFromWideString(pMulti, CP_UTF8);
+	delete [] pMulti;
+	return pConsole;
 }
 
+
 // --------------------------------------------------------------------------
 //
 // Function
@@ -402,12 +467,12 @@
 		return NULL;
 	}
 	
-	WCHAR* pBuffer = ConvertUtf8ToMultiByte(AbsPathWithUnicode.c_str());
+	WCHAR* pBuffer = ConvertUtf8ToWideString(AbsPathWithUnicode.c_str());
 	// We are responsible for freeing pBuffer
 	
 	if (pBuffer == NULL)
 	{
-		// error already logged by ConvertUtf8ToMultiByte()
+		// error already logged by ConvertUtf8ToWideString()
 		return NULL;
 	}
 
@@ -568,12 +633,12 @@
 		return NULL;
 	}
 	
-	WCHAR* pBuffer = ConvertUtf8ToMultiByte(AbsPathWithUnicode.c_str());
+	WCHAR* pBuffer = ConvertUtf8ToWideString(AbsPathWithUnicode.c_str());
 	// We are responsible for freeing pBuffer
 	
 	if (pBuffer == NULL)
 	{
-		// error already logged by ConvertUtf8ToMultiByte()
+		// error already logged by ConvertUtf8ToWideString()
 		return NULL;
 	}
 
@@ -735,7 +800,7 @@
 		return NULL;
 	}
 
-	pDir->name = ConvertUtf8ToMultiByte(dirName.c_str());
+	pDir->name = ConvertUtf8ToWideString(dirName.c_str());
 	// We are responsible for freeing dir->name
 	
 	if (pDir->name == NULL)




More information about the Boxbackup-dev mailing list