merged utf8_native and convert_to_native

This commit is contained in:
Arvid Norberg 2009-06-22 02:19:11 +00:00
parent cc3e6621ed
commit 9dfa35d18d
4 changed files with 36 additions and 41 deletions

View File

@ -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
}

View File

@ -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

View File

@ -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

View File

@ -67,10 +67,8 @@ BOOST_STATIC_ASSERT(sizeof(lseek(0, 0, 0)) >= 8);
#include <cstring>
#include <vector>
#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