From 9dfa35d18dd2e070e3c7723788eacfb8d3f6f31c Mon Sep 17 00:00:00 2001 From: Arvid Norberg Date: Mon, 22 Jun 2009 02:19:11 +0000 Subject: [PATCH] merged utf8_native and convert_to_native --- include/libtorrent/escape_string.hpp | 4 +++- src/create_torrent.cpp | 9 +++---- src/escape_string.cpp | 28 +++++++++++++++++++++- src/file.cpp | 36 +--------------------------- 4 files changed, 36 insertions(+), 41 deletions(-) diff --git a/include/libtorrent/escape_string.hpp b/include/libtorrent/escape_string.hpp index 48a8c70e3..98317464b 100644 --- a/include/libtorrent/escape_string.hpp +++ b/include/libtorrent/escape_string.hpp @@ -77,8 +77,10 @@ namespace libtorrent TORRENT_EXPORT std::wstring convert_to_wstring(std::string const& s); #endif -#if TORRENT_USE_LOCALE_FILENAMES +#if defined TORRENT_WINDOWS || TORRENT_USE_LOCALE_FILENAMES TORRENT_EXPORT std::string convert_to_native(std::string const& s); +#else + inline std::string const& convert_to_native(std::string const& s) { return s; } #endif } diff --git a/src/create_torrent.cpp b/src/create_torrent.cpp index 8a217894c..22c3336b6 100644 --- a/src/create_torrent.cpp +++ b/src/create_torrent.cpp @@ -61,15 +61,16 @@ namespace libtorrent #ifdef TORRENT_USE_WPATH std::wstring path = convert_to_wstring(p.external_file_string()); + DWORD attr = GetFileAttributesW(path.c_str()); #else - std::string path = utf8_native(p.external_file_string()); + std::string path = convert_to_native(p.external_file_string()); + DWORD attr = GetFileAttributesA(path.c_str()); #endif - DWORD attr = GetFileAttributes(path.c_str()); if (attr & FILE_ATTRIBUTE_HIDDEN) return file_storage::attribute_hidden; return 0; #else struct stat s; - if (stat(p.external_file_string().c_str(), &s) < 0) return 0; + if (stat(convert_to_native(p.external_file_string()).c_str(), &s) < 0) return 0; return (s.st_mode & S_IXUSR) ? file_storage::attribute_executable : 0; #endif } @@ -78,7 +79,7 @@ namespace libtorrent { #ifdef TORRENT_WINDOWS std::wstring const& path = p.external_file_string(); - DWORD attr = GetFileAttributes(path.c_str()); + DWORD attr = GetFileAttributesW(path.c_str()); if (attr & FILE_ATTRIBUTE_HIDDEN) return file_storage::attribute_hidden; return 0; #else diff --git a/src/escape_string.cpp b/src/escape_string.cpp index dadf5f7b3..7d16c748b 100644 --- a/src/escape_string.cpp +++ b/src/escape_string.cpp @@ -507,7 +507,33 @@ namespace libtorrent } #endif -#if TORRENT_USE_LOCALE_FILENAMES +#ifdef TORRENT_WINDOWS + std::string convert_to_native(std::string const& s) + { +#ifndef BOOST_NO_EXCEPTIONS + try + { +#endif + std::wstring ws; + libtorrent::utf8_wchar(s, ws); + std::size_t size = wcstombs(0, ws.c_str(), 0); + if (size == std::size_t(-1)) return s; + std::string ret; + ret.resize(size); + size = wcstombs(&ret[0], ws.c_str(), size + 1); + if (size == std::size_t(-1)) return s; + ret.resize(size); + return ret; +#ifndef BOOST_NO_EXCEPTIONS + } + catch(std::exception) + { + return s; + } +#endif + } + +#elif TORRENT_USE_LOCALE_FILENAMES std::string convert_to_native(std::string const& s) { // the empty string represents the local dependent encoding diff --git a/src/file.cpp b/src/file.cpp index 255e581c3..a5aa1abde 100644 --- a/src/file.cpp +++ b/src/file.cpp @@ -67,10 +67,8 @@ BOOST_STATIC_ASSERT(sizeof(lseek(0, 0, 0)) >= 8); #include #include -#if TORRENT_USE_WPATH || TORRENT_USE_LOCALE_FILENAMES // for convert_to_wstring and convert_to_native #include "libtorrent/escape_string.hpp" -#endif #include "libtorrent/assert.hpp" @@ -80,33 +78,6 @@ BOOST_STATIC_ASSERT((libtorrent::file::rw_mask & libtorrent::file::attribute_mas BOOST_STATIC_ASSERT((libtorrent::file::no_buffer & libtorrent::file::attribute_mask) == 0); #endif -namespace -{ -#ifdef TORRENT_WINDOWS - std::string utf8_native(std::string const& s) - { - try - { - std::wstring ws; - libtorrent::utf8_wchar(s, ws); - std::size_t size = wcstombs(0, ws.c_str(), 0); - if (size == std::size_t(-1)) return s; - std::string ret; - ret.resize(size); - size = wcstombs(&ret[0], ws.c_str(), size + 1); - if (size == std::size_t(-1)) return s; - ret.resize(size); - return ret; - } - catch(std::exception) - { - return s; - } - } -#endif - -} - namespace libtorrent { namespace fs = boost::filesystem; @@ -183,7 +154,7 @@ namespace libtorrent #if TORRENT_USE_WPATH m_path = convert_to_wstring(path.external_file_string()); #else - m_path = utf8_native(path.external_file_string()); + m_path = convert_to_native(path.external_file_string()); #endif TORRENT_ASSERT((mode & mode_mask) < sizeof(mode_array)/sizeof(mode_array[0])); @@ -223,13 +194,8 @@ namespace libtorrent static const int no_buffer_flag[] = {0, 0}; #endif -#if TORRENT_USE_LOCALE_FILENAMES m_fd = ::open(convert_to_native(path.external_file_string()).c_str() , mode_array[mode & rw_mask] | no_buffer_flag[(mode & no_buffer) >> 2], permissions); -#else - m_fd = ::open(path.external_file_string().c_str() - , mode_array[mode & rw_mask] | no_buffer_flag[(mode & no_buffer) >> 2], permissions); -#endif #ifdef TORRENT_LINUX // workaround for linux bug