From c2b496f4726fe664485944f8eece63b19030d482 Mon Sep 17 00:00:00 2001 From: Arvid Norberg Date: Wed, 17 Aug 2005 01:57:30 +0000 Subject: [PATCH] *** empty log message *** --- include/libtorrent/utf8.hpp | 27 +++++++++++++++++++++------ src/file.cpp | 9 +++++++++ 2 files changed, 30 insertions(+), 6 deletions(-) diff --git a/include/libtorrent/utf8.hpp b/include/libtorrent/utf8.hpp index 582b9c3ae..5587e9cfe 100644 --- a/include/libtorrent/utf8.hpp +++ b/include/libtorrent/utf8.hpp @@ -33,10 +33,10 @@ namespace detail { template wchar_t decode_utf8_mb(InputIterator &iter, InputIterator last) { - if(iter==last) throw std::runtime_error("incomplete UTF-8 sequence"); - if(((*iter)&0xC0)!=0x80) throw std::runtime_error("invalid UTF-8 sequence"); + if (iter == last) throw std::runtime_error("incomplete UTF-8 sequence"); + if (((*iter) & 0xc0) != 0x80) throw std::runtime_error("invalid UTF-8 sequence"); - return (wchar_t)((*iter++)&0x3F); + return (wchar_t)((*iter++) & 0x3f); } template @@ -44,7 +44,7 @@ wchar_t decode_utf8(InputIterator &iter, InputIterator last) { wchar_t ret; - if (((*iter)&0x80) == 0) // one byte + if (((*iter) & 0x80) == 0) // one byte { ret = *iter++; } @@ -56,7 +56,7 @@ wchar_t decode_utf8(InputIterator &iter, InputIterator last) } else if (((*iter) & 0xf0) == 0xe0) // three bytes { - wchar_t byte1 = (*iter++) & 0x1f; + wchar_t byte1 = (*iter++) & 0x0f; wchar_t byte2 = decode_utf8_mb(iter, last); wchar_t byte3 = decode_utf8_mb(iter, last); ret = (byte1 << 12) | (byte2 << 6) | byte3; @@ -71,7 +71,7 @@ template OutputIterator utf8_wchar(InputIterator first, InputIterator last, OutputIterator dest) { for(; first!=last; ++dest) - *dest=decode_utf8(first, last); + *dest = decode_utf8(first, last); return dest; } @@ -155,6 +155,21 @@ inline std::string wchar_utf8(const std::wstring &str) return ret; } +inline std::wstring safe_convert(std::string const& s) +{ + try + { + return utf8_wchar(s); + } + catch (std::exception) + { + std::wstring ret; + for (const char* i = &*s.begin(); i < &*s.end(); ++i) + ret += *i; + return ret; + } +} + } #endif diff --git a/src/file.cpp b/src/file.cpp index 9973d8e1a..3d2232591 100755 --- a/src/file.cpp +++ b/src/file.cpp @@ -93,6 +93,7 @@ namespace std::string ret; ret.resize(size); size = wcstombs(&ret[0], ws.c_str(), size + 1); + if (size == -1) return s; ret.resize(size); return ret; } @@ -142,10 +143,18 @@ namespace libtorrent { assert(path.is_complete()); close(); +#if defined(WIN32) && defined(UNICODE) + std::wstring wpath(safe_convert(path.native_file_string())); + m_fd = ::_wopen( + wpath.c_str() + , map_open_mode(mode) + , S_IREAD | S_IWRITE); +#else m_fd = ::open( utf8_native(path.native_file_string()).c_str() , map_open_mode(mode) , S_IREAD | S_IWRITE); +#endif if (m_fd == -1) { std::stringstream msg;