[Box Backup-commit] COMMIT r1038 - box/chris/general/lib/win32

boxbackup-dev at fluffy.co.uk boxbackup-dev at fluffy.co.uk
Sun Oct 15 14:41:27 BST 2006


Author: chris
Date: 2006-10-15 14:41:27 +0100 (Sun, 15 Oct 2006)
New Revision: 1038

Modified:
   box/chris/general/lib/win32/emu.cpp
   box/chris/general/lib/win32/emu.h
Log:
Expanded character set conversion API to allow arbitrary conversions
(needed to handle command lines with international encodings)


Modified: box/chris/general/lib/win32/emu.cpp
===================================================================
--- box/chris/general/lib/win32/emu.cpp	2006-10-14 17:04:30 UTC (rev 1037)
+++ box/chris/general/lib/win32/emu.cpp	2006-10-15 13:41:27 UTC (rev 1038)
@@ -360,23 +360,25 @@
 // --------------------------------------------------------------------------
 //
 // Function
-//		Name:    ConvertUtf8ToConsole
-//		Purpose: Converts a string from UTF-8 to the console 
-//			 code page. On success, replaces contents of rDest 
-//			 and returns true. In case of fire, logs the error 
-//			 and returns false.
-//		Created: 4th February 2006
+//		Name:    ConvertEncoding(const std::string&, int, 
+//			 std::string&, int)
+//		Purpose: Converts a string from one code page to another.
+//			 On success, replaces contents of rDest and returns 
+//			 true. In case of fire, logs the error and returns 
+//			 false.
+//		Created: 15th October 2006
 //
 // --------------------------------------------------------------------------
-bool ConvertUtf8ToConsole(const char* pString, std::string& rDest)
+bool ConvertEncoding(const std::string& rSource, int sourceCodePage,
+	std::string& rDest, int destCodePage)
 {
-	WCHAR* pWide = ConvertUtf8ToWideString(pString);
+	WCHAR* pWide = ConvertToWideString(rSource.c_str(), sourceCodePage);
 	if (pWide == NULL)
 	{
 		return false;
 	}
 
-	char* pConsole = ConvertFromWideString(pWide, GetConsoleOutputCP());
+	char* pConsole = ConvertFromWideString(pWide, destCodePage);
 	delete [] pWide;
 
 	if (!pConsole)
@@ -390,40 +392,26 @@
 	return true;
 }
 
-// --------------------------------------------------------------------------
-//
-// Function
-//		Name:    ConvertConsoleToUtf8
-//		Purpose: Converts a string from the console code page
-//			 to UTF-8. On success, replaces contents of rDest
-//			 and returns true. In case of fire, logs the error 
-//			 and returns false.
-//		Created: 4th February 2006
-//
-// --------------------------------------------------------------------------
-bool ConvertConsoleToUtf8(const char* pString, std::string& rDest)
+bool ConvertToUtf8(const char* pString, std::string& rDest, int sourceCodePage)
 {
-	WCHAR* pWide = ConvertToWideString(pString, GetConsoleCP());
-	if (pWide == NULL)
-	{
-		return false;
-	}
+	return ConvertEncoding(pString, sourceCodePage, rDest, CP_UTF8);
+}
 
-	char* pConsole = ConvertFromWideString(pWide, CP_UTF8);
-	delete [] pWide;
+bool ConvertFromUtf8(const char* pString, std::string& rDest, int destCodePage)
+{
+	return ConvertEncoding(pString, CP_UTF8, rDest, destCodePage);
+}
 
-	if (!pConsole)
-	{
-		return false;
-	}
+bool ConvertConsoleToUtf8(const char* pString, std::string& rDest)
+{
+	return ConvertEncoding(pString, GetConsoleCP(), rDest, CP_UTF8);
+}
 
-	rDest = pConsole;
-	delete [] pConsole;
-
-	return true;
+bool ConvertUtf8ToConsole(const char* pString, std::string& rDest)
+{
+	return ConvertEncoding(pString, CP_UTF8, rDest, GetConsoleOutputCP());
 }
 
-
 // --------------------------------------------------------------------------
 //
 // Function

Modified: box/chris/general/lib/win32/emu.h
===================================================================
--- box/chris/general/lib/win32/emu.h	2006-10-14 17:04:30 UTC (rev 1037)
+++ box/chris/general/lib/win32/emu.h	2006-10-15 13:41:27 UTC (rev 1038)
@@ -215,7 +215,7 @@
 
 int emu_mkdir(const char* pPathName);
 
-inline int mkdir(const char *pPathName, mode_t mode)
+inline int mkdir(const char *pPathName, mode_t mode = 0)
 {
 	return emu_mkdir(pPathName);
 }
@@ -359,6 +359,12 @@
 int poll(struct pollfd *ufds, unsigned long nfds, int timeout);
 bool EnableBackupRights( void );
 
+bool ConvertEncoding (const std::string& rSource, int sourceCodePage,
+	std::string& rDest, int destCodePage);
+bool ConvertToUtf8   (const std::string& rSource, std::string& rDest, 
+	int sourceCodePage);
+bool ConvertFromUtf8 (const std::string& rSource, std::string& rDest,
+	int destCodePage);
 bool ConvertUtf8ToConsole(const char* pString, std::string& rDest);
 bool ConvertConsoleToUtf8(const char* pString, std::string& rDest);
 




More information about the Boxbackup-commit mailing list