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

boxbackup-dev at fluffy.co.uk boxbackup-dev at fluffy.co.uk
Sat Feb 4 23:03:39 GMT 2006


Author: chris
Date: 2006-02-04 23:03:31 +0000 (Sat, 04 Feb 2006)
New Revision: 382

Modified:
   box/chris/win32/vc2005-compile-fixes/lib/win32/emu.cpp
   box/chris/win32/vc2005-compile-fixes/lib/win32/emu.h
Log:
* emu.cpp, emu.h
- Fixed some compilation errors
- Added a new method to convert UTF-8 to console encoding


Modified: box/chris/win32/vc2005-compile-fixes/lib/win32/emu.cpp
===================================================================
--- box/chris/win32/vc2005-compile-fixes/lib/win32/emu.cpp	2006-02-04 19:51:22 UTC (rev 381)
+++ box/chris/win32/vc2005-compile-fixes/lib/win32/emu.cpp	2006-02-04 23:03:31 UTC (rev 382)
@@ -159,7 +159,8 @@
 		TOKEN_ADJUST_PRIVILEGES, 
 		&hToken ))
 	{
-		printf( "Cannot open process token - err = %d\n", GetLastError( ) );
+		printf( "Cannot open process token: error %d\n", 
+			(int)GetLastError() );
 		return false;
 	}
 
@@ -170,7 +171,8 @@
 		SE_BACKUP_NAME, //the name of the privilege
 		&( token_priv.Privileges[0].Luid )) ) //result
 	{
-		printf( "Cannot lookup backup privilege - err = %d\n", GetLastError( ) );
+		printf( "Cannot lookup backup privilege: error %d\n", 
+			(int)GetLastError( ) );
 		return false;
 	}
 
@@ -190,7 +192,8 @@
 		//this function is a little tricky - if we were adjusting
 		//more than one privilege, it could return success but not
 		//adjust them all - in the general case, you need to trap this
-		printf( "Could not enable backup privileges - err = %d\n", GetLastError( ) );
+		printf( "Could not enable backup privileges: error %d\n", 
+			(int)GetLastError( ) );
 		return false;
 
 	}
@@ -212,7 +215,7 @@
 // --------------------------------------------------------------------------
 WCHAR* ConvertUtf8ToMultiByte(const char* pName)
 {
-	int strlen = MultiByteToWideChar(
+	int len = MultiByteToWideChar(
 		CP_UTF8,       // source code page
 		0,             // character-type options
 		pName,         // string to map
@@ -222,7 +225,7 @@
 		               //   how much space we need
 		);
 
-	WCHAR* buffer = new WCHAR[strlen+1];
+	WCHAR* buffer = new WCHAR[len+1];
 
 	if (buffer == NULL)
 	{
@@ -233,16 +236,16 @@
 		return NULL;
 	}
 
-	strlen = MultiByteToWideChar(
+	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
-		strlen         // size of buffer
+		len         // size of buffer
 		);
 
-	if (strlen == 0)
+	if (len == 0)
 	{
 		::syslog(LOG_WARNING, 
 			"Failed to convert string to multibyte: '%s': "
@@ -252,7 +255,7 @@
 		return NULL;
 	}
 
-	buffer[strlen] = L'\0';
+	buffer[len] = L'\0';
 
 	return buffer;
 }
@@ -260,12 +263,93 @@
 // --------------------------------------------------------------------------
 //
 // 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 = ConvertUtf8ToMultiByte(pString);
+	if (pMulti == NULL)
+	{
+		return NULL;
+	}
+
+	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
+		);
+
+	if (len == 0)
+	{
+		::syslog(LOG_WARNING, 
+			"Failed to convert UTF-8 string to console: '%s': "
+			"error %d", pString, GetLastError());
+		delete [] pMulti;
+		errno = EINVAL;
+		return NULL;
+	}
+
+	char* buffer = new char[len+1];
+
+	if (buffer == NULL)
+	{
+		::syslog(LOG_WARNING, 
+			"Failed to convert UTF-8 string to console: '%s': "
+			"out of memory", pString);
+		delete [] pMulti;
+		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
+		);
+
+	if (len == 0)
+	{
+		::syslog(LOG_WARNING, 
+			"Failed to convert UTF-8 string to console: '%s': "
+			"error %i", pString, GetLastError());
+		delete [] pMulti;
+		errno = EACCES;
+		delete [] buffer;
+		return NULL;
+	}
+
+	buffer[len] = L'\0';
+	delete [] pMulti;
+
+	return buffer;
+}
+
+// --------------------------------------------------------------------------
+//
+// Function
 //		Name:    ConvertPathToAbsoluteUnicode
 //		Purpose: Converts relative paths to absolute (with unicode marker)
 //		Created: 4th February 2006
 //
 // --------------------------------------------------------------------------
-std::string ConvertPathToAbsoluteUnicode(const char *filename)
+std::string ConvertPathToAbsoluteUnicode(const char *pFileName)
 {
 	std::string tmpStr("\\\\?\\");
 	
@@ -273,7 +357,7 @@
 	// Absolute paths on Windows are always a drive letter
 	// followed by ':'
 	
-	if (fileN[1] != ':')
+	if (pFileName[1] != ':')
 	{
 		// Must be relative. We need to get the 
 		// current directory to make it absolute.
@@ -282,7 +366,8 @@
 		if (::getcwd(wd, PATH_MAX) == 0)
 		{
 			::syslog(LOG_WARNING, 
-				"Failed to open '%s': path too long", pName);
+				"Failed to open '%s': path too long", 
+				pFileName);
 			errno = ENAMETOOLONG;
 			tmpStr = "";
 			return tmpStr;
@@ -295,7 +380,7 @@
 		}
 	}
 	
-	tmpStr += filename;
+	tmpStr += pFileName;
 	return tmpStr;
 }
 
@@ -515,7 +600,7 @@
 			NULL);
 	}
 
-	delete [] buffer;
+	delete [] pBuffer;
 
 	if (handle == INVALID_HANDLE_VALUE)
 	{
@@ -873,6 +958,7 @@
 	va_start(args, frmt);
 
 	int len = vsnprintf(buffer, sizeof(buffer)-1, sixfour.c_str(), args);
+	ASSERT(len < sizeof(buffer))
 	buffer[sizeof(buffer)-1] = 0;
 
 	va_end(args);
@@ -892,8 +978,8 @@
 
 	{
 		DWORD err = GetLastError();
-		printf("Unable to send message to Event Log "
-			"(error %i):\r\n", err);
+		printf("Unable to send message to Event Log: "
+			"error %i:\r\n", (int)err);
 	}
 
 	printf("%s\r\n", buffer);

Modified: box/chris/win32/vc2005-compile-fixes/lib/win32/emu.h
===================================================================
--- box/chris/win32/vc2005-compile-fixes/lib/win32/emu.h	2006-02-04 19:51:22 UTC (rev 381)
+++ box/chris/win32/vc2005-compile-fixes/lib/win32/emu.h	2006-02-04 23:03:31 UTC (rev 382)
@@ -39,6 +39,7 @@
 #define lseek(fd,off,whence)  _lseek(fd,off,whence)
 #define unlink(file)          _unlink(file)
 #define chmod(file,mode)      _chmod(file,mode)
+#define ::chmod(file,mode)    _chmod(file,mode)
 #define getcwd(buf,length)    _getcwd(buf,length)
 #define fileno(struct_file)   _fileno(struct_file)
 #define chdir(dir)            _chdir(dir)
@@ -121,9 +122,9 @@
 	return 0;
 }
 
-inline int _chmod(const char * Filename, int uid)
+inline int _chmod(const char * Filename, int mode)
 {
-	//indicate sucsess
+	// indicate success
 	return 0;
 }
 
@@ -431,6 +432,7 @@
 // caller must free the returned buffer from ConvertUtf8ToMultiByte()
 // with delete[]
 WCHAR* ConvertUtf8ToMultiByte(const char* pName);
+char*  ConvertUtf8ToConsole  (const char* pString);
 
 //
 // MessageId: MSG_ERR_EXIST




More information about the Boxbackup-dev mailing list