[Box Backup-commit] COMMIT r3052 - box/invisnet/vs2010/0.11/lib/win32

subversion at boxbackup.org subversion at boxbackup.org
Thu Jan 19 18:24:47 GMT 2012


Author: invisnet
Date: 2012-01-19 18:24:46 +0000 (Thu, 19 Jan 2012)
New Revision: 3052

Added:
   box/invisnet/vs2010/0.11/lib/win32/Win32Exception.cpp
   box/invisnet/vs2010/0.11/lib/win32/Win32Exception.h
   box/invisnet/vs2010/0.11/lib/win32/common_FileStream.cpp
   box/invisnet/vs2010/0.11/lib/win32/unicode.cpp
   box/invisnet/vs2010/0.11/lib/win32/unicode.h
Log:
Refactor Win32 code

Added: box/invisnet/vs2010/0.11/lib/win32/Win32Exception.cpp
===================================================================
--- box/invisnet/vs2010/0.11/lib/win32/Win32Exception.cpp	                        (rev 0)
+++ box/invisnet/vs2010/0.11/lib/win32/Win32Exception.cpp	2012-01-19 18:24:46 UTC (rev 3052)
@@ -0,0 +1,4 @@
+#include "Box.h"
+
+
+DWORD Win32Exception::mGLE;

Added: box/invisnet/vs2010/0.11/lib/win32/Win32Exception.h
===================================================================
--- box/invisnet/vs2010/0.11/lib/win32/Win32Exception.h	                        (rev 0)
+++ box/invisnet/vs2010/0.11/lib/win32/Win32Exception.h	2012-01-19 18:24:46 UTC (rev 3052)
@@ -0,0 +1,47 @@
+#ifdef WIN32
+#	pragma once
+
+#	include "autogen_Win32BaseException.h"
+
+	class Win32Exception : public Win32BaseException
+	{
+	public:
+		Win32Exception(unsigned int SubType, const std::string& rMessage = "") : Win32BaseException(SubType, rMessage)
+		{
+			mGLE = GetLastError();
+			errno = win2errno(mGLE);
+		}
+	
+		Win32Exception(const Win32Exception &rToCopy) : Win32BaseException(rToCopy.mSubType, rToCopy.mMessage)
+		{
+			mGLE = GetLastError();
+			errno = win2errno(mGLE);
+		}
+
+		DWORD GetLastError()
+		{
+			return mGLE;
+		}
+
+		static int win2errno(DWORD gle) throw()
+		{
+			switch(gle) {
+			case ERROR_ACCESS_DENIED:
+				return EACCES;
+			case ERROR_FILE_NOT_FOUND:
+			case ERROR_PATH_NOT_FOUND:
+				return ENOENT;
+			case ERROR_INVALID_PARAMETER:
+				return EINVAL;
+			case ERROR_SHARING_VIOLATION:
+				return EBUSY;
+			default:
+				return ENOSYS;
+			}
+		}
+
+	protected:
+		static DWORD mGLE;
+	};
+
+#endif

Added: box/invisnet/vs2010/0.11/lib/win32/common_FileStream.cpp
===================================================================
--- box/invisnet/vs2010/0.11/lib/win32/common_FileStream.cpp	                        (rev 0)
+++ box/invisnet/vs2010/0.11/lib/win32/common_FileStream.cpp	2012-01-19 18:24:46 UTC (rev 3052)
@@ -0,0 +1,87 @@
+#include "Box.h"
+
+
+using namespace Win32;
+
+
+static DWORD dwDesiredAccess(int flags) throw()
+{
+	DWORD desiredAccess = FILE_READ_ATTRIBUTES | FILE_LIST_DIRECTORY | FILE_READ_EA;
+
+	if		  (flags & O_WRONLY)	desiredAccess  = FILE_WRITE_DATA;
+	else if (flags & O_RDWR)	desiredAccess |= FILE_WRITE_ATTRIBUTES | FILE_WRITE_DATA | FILE_WRITE_EA;
+
+	return desiredAccess;
+}
+static DWORD dwShareMode(int flags) throw()
+{
+	DWORD shareMode = FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE;
+
+	if (flags & O_LOCK)	shareMode = 0;
+
+	return shareMode;
+}
+static DWORD dwCreationDisposition(int flags) throw()
+{
+	DWORD creationDisposition = OPEN_EXISTING;
+
+	if (flags & O_CREAT)
+	{
+		if		  (flags & O_EXCL)	creationDisposition = CREATE_NEW;
+//		else if (flags & O_TRUNC)	creationDisposition = TRUNCATE_EXISTING;
+		else								creationDisposition = OPEN_ALWAYS;
+	}
+	else if (flags & O_TRUNC)	creationDisposition = CREATE_ALWAYS;
+
+	return creationDisposition;
+}
+static DWORD dwFlagsAndAttributes(int flags) throw()
+{
+	DWORD flagsAndAttributes = FILE_FLAG_BACKUP_SEMANTICS;
+
+	if (flags & O_TEMPORARY)	flagsAndAttributes |= FILE_FLAG_DELETE_ON_CLOSE;
+
+	return flagsAndAttributes;
+}
+
+// --------------------------------------------------------------------------
+//
+// Function
+//		Name:    openfile
+//		Purpose: replacement for any open calls - handles unicode
+//			filenames - supplied in utf8
+//		Created: 25th October 2004
+//
+// --------------------------------------------------------------------------
+HANDLE openfile(const char *pFileName, int flags, int mode) throw()
+{
+	HANDLE hFile = INVALID_HANDLE_VALUE;
+
+	try
+	{
+		std::string multiPath = ConvertPathToAbsoluteUnicode(pFileName);
+		std::wstring widePath = multi2wide(multiPath);
+
+		if (INVALID_HANDLE_VALUE == (hFile = CreateFileW(widePath.c_str(),
+																		 dwDesiredAccess(flags),
+																		 dwShareMode(flags),
+																		 NULL,
+																		 dwCreationDisposition(flags),
+																		 dwFlagsAndAttributes(flags),
+																		 NULL)))
+		{
+			throw Win32Exception(Win32Exception::API_CreateFile, pFileName);
+		}
+
+		if ((flags & O_APPEND) && INVALID_SET_FILE_POINTER == SetFilePointer(hFile, 0, NULL, FILE_END))
+			throw Win32Exception(Win32Exception::API_SetFilePointer, pFileName);
+
+		return hFile;
+	}
+	EMU_EXCEPTION_HANDLING
+
+	if (INVALID_HANDLE_VALUE != hFile)
+		CloseHandle(hFile);
+
+	return INVALID_HANDLE_VALUE;
+}

Added: box/invisnet/vs2010/0.11/lib/win32/unicode.cpp
===================================================================
--- box/invisnet/vs2010/0.11/lib/win32/unicode.cpp	                        (rev 0)
+++ box/invisnet/vs2010/0.11/lib/win32/unicode.cpp	2012-01-19 18:24:46 UTC (rev 3052)
@@ -0,0 +1,112 @@
+#include "Box.h"
+
+
+using namespace Win32;
+
+
+// --------------------------------------------------------------------------
+//
+// Function
+//		Name:    ConvertToWideString
+//		Purpose: Converts a string from specified codepage to
+//			 a wide string (wchar_t*).
+//			 In case of fire, logs the error and returns NULL.
+//		Created: 4th February 2006
+//
+// --------------------------------------------------------------------------
+static const wchar_t* ConvertToWideString(const char* pString, unsigned int codepage, bool logErrors)
+{
+	try
+	{
+		return multi2wide(pString,codepage).c_str();
+	}
+	catch(Win32Exception &e)
+	{
+		if (logErrors)
+			BOX_LOG_WIN_ERROR_NUMBER(e.GetMessage(),e.GetLastError());
+	}
+	return NULL;
+}
+
+
+// --------------------------------------------------------------------------
+//
+// Function
+//		Name:    ConvertUtf8ToWideString
+//		Purpose: Converts a string from UTF-8 to a wide string.
+//			 In case of fire, logs the error and returns NULL.
+//		Created: 4th February 2006
+//
+// --------------------------------------------------------------------------
+const wchar_t* ConvertUtf8ToWideString(const char* pString) throw()
+{
+	return ConvertToWideString(pString, CP_UTF8, true);
+}
+
+
+// --------------------------------------------------------------------------
+//
+// Function
+//		Name:    ConvertFromWideString
+//		Purpose: Converts a wide string to a narrow string in the
+//			 specified code page.
+//			 In case of fire, logs the error and returns NULL.
+//		Created: 4th February 2006
+//
+// --------------------------------------------------------------------------
+const char* ConvertFromWideString(const wchar_t* pString, unsigned int codepage, bool logErrors) throw()
+{
+	try
+	{
+		return wide2multi(pString,codepage).c_str();
+	}
+	catch(Win32Exception &e)
+	{
+		if (logErrors)
+			BOX_LOG_WIN_ERROR_NUMBER(e.GetMessage(),e.GetLastError());
+	}
+	return NULL;
+}
+
+
+// --------------------------------------------------------------------------
+//
+// Function
+//		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 ConvertEncoding(const std::string& rSource, int sourceCodePage, std::string& rDest, int destCodePage) throw()
+{
+	try
+	{
+		wide2multi( multi2wide(rSource,sourceCodePage), rDest, destCodePage );
+		return true;
+	}
+	EMU_EXCEPTION_HANDLING_RETURN(false)
+}
+
+bool ConvertToUtf8(const std::string& rSource, std::string& rDest, int sourceCodePage) throw()
+{
+	return ConvertEncoding(rSource, sourceCodePage, rDest, CP_UTF8);
+}
+
+bool ConvertFromUtf8(const std::string& rSource, std::string& rDest, int destCodePage) throw()
+{
+	return ConvertEncoding(rSource, CP_UTF8, rDest, destCodePage);
+}
+
+bool ConvertConsoleToUtf8(const std::string& rSource, std::string& rDest) throw()
+{
+	return ConvertToUtf8(rSource, rDest, GetConsoleCP());
+}
+
+bool ConvertUtf8ToConsole(const std::string& rSource, std::string& rDest) throw()
+{
+	return ConvertFromUtf8(rSource, rDest, GetConsoleOutputCP());
+}

Added: box/invisnet/vs2010/0.11/lib/win32/unicode.h
===================================================================
--- box/invisnet/vs2010/0.11/lib/win32/unicode.h	                        (rev 0)
+++ box/invisnet/vs2010/0.11/lib/win32/unicode.h	2012-01-19 18:24:46 UTC (rev 3052)
@@ -0,0 +1,14 @@
+#ifndef EMU_UNICODE_H
+#	define EMU_UNICODE_H
+
+#	ifdef WIN32
+#		pragma once
+
+		extern bool ConvertEncoding (const std::string& rSource, int sourceCodePage, std::string& rDest, int destCodePage) throw();
+		extern bool ConvertToUtf8   (const std::string& rSource, std::string& rDest, int sourceCodePage) throw();
+		extern bool ConvertFromUtf8 (const std::string& rSource, std::string& rDest, int destCodePage) throw();
+		extern bool ConvertUtf8ToConsole(const std::string& rSource, std::string& rDest) throw();
+		extern bool ConvertConsoleToUtf8(const std::string& rSource, std::string& rDest) throw();
+#	endif
+
+#endif




More information about the Boxbackup-commit mailing list