more wide character and mingw fixes

This commit is contained in:
Arvid Norberg 2010-02-15 05:49:10 +00:00
parent 23881fb0ff
commit 08c8bfae29
6 changed files with 53 additions and 30 deletions

View File

@ -83,7 +83,7 @@ namespace libtorrent
TORRENT_EXPORT void to_hex(char const *in, int len, char* out); TORRENT_EXPORT void to_hex(char const *in, int len, char* out);
TORRENT_EXPORT bool from_hex(char const *in, int len, char* out); TORRENT_EXPORT bool from_hex(char const *in, int len, char* out);
#if defined TORRENT_WINDOWS && defined UNICODE #if defined TORRENT_WINDOWS && TORRENT_USE_WSTRING
TORRENT_EXPORT std::wstring convert_to_wstring(std::string const& s); TORRENT_EXPORT std::wstring convert_to_wstring(std::string const& s);
TORRENT_EXPORT std::string convert_from_wstring(std::wstring const& s); TORRENT_EXPORT std::string convert_from_wstring(std::wstring const& s);
#endif #endif

View File

@ -147,7 +147,11 @@ namespace libtorrent
private: private:
#ifdef TORRENT_WINDOWS #ifdef TORRENT_WINDOWS
HANDLE m_handle; HANDLE m_handle;
WIN32_FIND_DATA m_fd; #if TORRENT_USE_WSTRING
WIN32_FIND_DATAW m_fd;
#else
WIN32_FIND_DATAA m_fd;
#endif
#else #else
DIR* m_handle; DIR* m_handle;
// the dirent struct contains a zero-sized // the dirent struct contains a zero-sized
@ -252,11 +256,11 @@ namespace libtorrent
#ifdef TORRENT_WINDOWS #ifdef TORRENT_WINDOWS
HANDLE m_file_handle; HANDLE m_file_handle;
#ifdef UNICODE #if TORRENT_USE_WSTRING
std::wstring m_path; std::wstring m_path;
#else #else
std::string m_path; std::string m_path;
#endif // UNICODE #endif // TORRENT_USE_WSTRING
#else // TORRENT_WINDOWS #else // TORRENT_WINDOWS
int m_fd; int m_fd;
#endif // TORRENT_WINDOWS #endif // TORRENT_WINDOWS

View File

@ -61,13 +61,13 @@ namespace libtorrent
{ {
#ifdef TORRENT_WINDOWS #ifdef TORRENT_WINDOWS
#ifdef UNICODE #if TORRENT_USE_WSTRING
std::wstring path = convert_to_wstring(p); std::wstring path = convert_to_wstring(p);
DWORD attr = GetFileAttributesW(path.c_str()); DWORD attr = GetFileAttributesW(path.c_str());
#else #else
std::string path = convert_to_native(p); std::string path = convert_to_native(p);
DWORD attr = GetFileAttributesA(path.c_str()); DWORD attr = GetFileAttributesA(path.c_str());
#endif // UNICODE #endif // TORRENT_USE_WSTRING
if (attr & FILE_ATTRIBUTE_HIDDEN) return file_storage::attribute_hidden; if (attr & FILE_ATTRIBUTE_HIDDEN) return file_storage::attribute_hidden;
return 0; return 0;
#else #else

View File

@ -547,7 +547,7 @@ namespace libtorrent
return true; return true;
} }
#if defined TORRENT_WINDOWS && TORRENT_USE_WSTRING && defined UNICODE #if defined TORRENT_WINDOWS && TORRENT_USE_WSTRING
std::wstring convert_to_wstring(std::string const& s) std::wstring convert_to_wstring(std::string const& s)
{ {
std::wstring ret; std::wstring ret;

View File

@ -125,7 +125,7 @@ namespace libtorrent
{ {
ec.clear(); ec.clear();
#if defined UNICODE && defined TORRENT_WINDOWS #if TORRENT_USE_WSTRING && defined TORRENT_WINDOWS
std::wstring f = convert_to_wstring(inf); std::wstring f = convert_to_wstring(inf);
#else #else
std::string f = convert_to_native(inf); std::string f = convert_to_native(inf);
@ -133,7 +133,7 @@ namespace libtorrent
#ifdef TORRENT_WINDOWS #ifdef TORRENT_WINDOWS
struct _stati64 ret; struct _stati64 ret;
#ifdef UNICODE #if TORRENT_USE_WSTRING
if (_wstati64(f.c_str(), &ret) < 0) if (_wstati64(f.c_str(), &ret) < 0)
#else #else
if (_stati64(f.c_str(), &ret) < 0) if (_stati64(f.c_str(), &ret) < 0)
@ -162,7 +162,7 @@ namespace libtorrent
{ {
ec.clear(); ec.clear();
#if defined UNICODE && defined TORRENT_WINDOWS #if TORRENT_USE_WSTRING && defined TORRENT_WINDOWS
std::wstring f1 = convert_to_wstring(inf); std::wstring f1 = convert_to_wstring(inf);
std::wstring f2 = convert_to_wstring(newf); std::wstring f2 = convert_to_wstring(newf);
if (_wrename(f1.c_str(), f2.c_str()) < 0) if (_wrename(f1.c_str(), f2.c_str()) < 0)
@ -193,14 +193,16 @@ namespace libtorrent
{ {
ec.clear(); ec.clear();
#if defined TORRENT_WINDOWS && defined UNICODE #if defined TORRENT_WINDOWS && TORRENT_USE_WSTRING
#define CreateDirectory_ CreateDirectoryW
std::wstring n = convert_to_wstring(f); std::wstring n = convert_to_wstring(f);
#else #else
#define CreateDirectory_ CreateDirectoryA
std::string n = convert_to_native(f); std::string n = convert_to_native(f);
#endif #endif
#ifdef TORRENT_WINDOWS #ifdef TORRENT_WINDOWS
if (CreateDirectory(n.c_str(), 0) == 0 if (CreateDirectory_(n.c_str(), 0) == 0
&& GetLastError() != ERROR_ALREADY_EXISTS) && GetLastError() != ERROR_ALREADY_EXISTS)
ec.assign(GetLastError(), boost::system::get_system_category()); ec.assign(GetLastError(), boost::system::get_system_category());
#else #else
@ -224,16 +226,18 @@ namespace libtorrent
void copy_file(std::string const& inf, std::string const& newf, error_code& ec) void copy_file(std::string const& inf, std::string const& newf, error_code& ec)
{ {
ec.clear(); ec.clear();
#if defined UNICODE && defined TORRENT_WINDOWS #if TORRENT_USE_WSTRING && defined TORRENT_WINDOWS
#define CopyFile_ CopyFileW
std::wstring f1 = convert_to_wstring(inf); std::wstring f1 = convert_to_wstring(inf);
std::wstring f2 = convert_to_wstring(newf); std::wstring f2 = convert_to_wstring(newf);
#else #else
#define CopyFile_ CopyFileA
std::string f1 = convert_to_native(inf); std::string f1 = convert_to_native(inf);
std::string f2 = convert_to_native(newf); std::string f2 = convert_to_native(newf);
#endif #endif
#ifdef TORRENT_WINDOWS #ifdef TORRENT_WINDOWS
if (CopyFile(f1.c_str(), f2.c_str(), false) == 0) if (CopyFile_(f1.c_str(), f2.c_str(), false) == 0)
ec.assign(GetLastError(), boost::system::get_system_category()); ec.assign(GetLastError(), boost::system::get_system_category());
#elif defined __APPLE__ && defined __MACH__ && MAC_OS_X_VERSION_MIN_REQUIRED >= 1050 #elif defined __APPLE__ && defined __MACH__ && MAC_OS_X_VERSION_MIN_REQUIRED >= 1050
// this only works on 10.5 // this only works on 10.5
@ -446,18 +450,18 @@ namespace libtorrent
std::string current_working_directory() std::string current_working_directory()
{ {
#ifdef TORRENT_WINDOWS #ifdef TORRENT_WINDOWS
#ifdef UNICODE #if TORRENT_USE_WSTRING
wchar_t cwd[TORRENT_MAX_PATH]; wchar_t cwd[TORRENT_MAX_PATH];
_wgetcwd(cwd, sizeof(cwd) / sizeof(wchar_t)); _wgetcwd(cwd, sizeof(cwd) / sizeof(wchar_t));
#else #else
char cwd[TORRENT_MAX_PATH]; char cwd[TORRENT_MAX_PATH];
_getcwd(cwd, sizeof(cwd)); _getcwd(cwd, sizeof(cwd));
#endif // UNICODE #endif // TORRENT_USE_WSTRING
#else #else
char cwd[TORRENT_MAX_PATH]; char cwd[TORRENT_MAX_PATH];
if (getcwd(cwd, sizeof(cwd)) == 0) return "/"; if (getcwd(cwd, sizeof(cwd)) == 0) return "/";
#endif #endif
#if defined TORRENT_WINDOWS && defined UNICODE #if defined TORRENT_WINDOWS && TORRENT_USE_WSTRING
return convert_from_wstring(cwd); return convert_from_wstring(cwd);
#else #else
return convert_from_native(cwd); return convert_from_native(cwd);
@ -495,16 +499,20 @@ namespace libtorrent
pruned = inf.substr(0, inf.size() - 1); pruned = inf.substr(0, inf.size() - 1);
else else
pruned = inf; pruned = inf;
#ifdef UNICODE #if TORRENT_USE_WSTRING
#define DeleteFile_ DeleteFileW
#define RemoveDirectory_ RemoveDirectoryW
std::wstring f = convert_to_wstring(pruned); std::wstring f = convert_to_wstring(pruned);
#else #else
#define DeleteFile_ DeleteFileA
#define RemoveDirectory_ RemoveDirectoryA
std::string f = convert_to_native(pruned); std::string f = convert_to_native(pruned);
#endif #endif
if (DeleteFile(f.c_str()) == 0) if (DeleteFile_(f.c_str()) == 0)
{ {
if (GetLastError() == ERROR_ACCESS_DENIED) if (GetLastError() == ERROR_ACCESS_DENIED)
{ {
if (RemoveDirectory(f.c_str()) != 0) if (RemoveDirectory_(f.c_str()) != 0)
return; return;
} }
ec.assign(GetLastError(), boost::system::get_system_category()); ec.assign(GetLastError(), boost::system::get_system_category());
@ -578,12 +586,14 @@ namespace libtorrent
std::string f = path; std::string f = path;
if (!f.empty() && (f[f.size()-1] != '/' || f[f.size()-1] != '\\')) f += "\\*"; if (!f.empty() && (f[f.size()-1] != '/' || f[f.size()-1] != '\\')) f += "\\*";
else f += "*"; else f += "*";
#ifdef UNICODE #if TORRENT_USE_WSTRING
#define FindFirstFile_ FindFirstFileW
std::wstring p = convert_to_wstring(f); std::wstring p = convert_to_wstring(f);
#else #else
#define FindFirstFile_ FindFirstFileA
std::string p = convert_to_native(f); std::string p = convert_to_native(f);
#endif #endif
m_handle = FindFirstFile(p.c_str(), &m_fd); m_handle = FindFirstFile_(p.c_str(), &m_fd);
if (m_handle == INVALID_HANDLE_VALUE) if (m_handle == INVALID_HANDLE_VALUE)
{ {
ec.assign(GetLastError(), boost::system::get_system_category()); ec.assign(GetLastError(), boost::system::get_system_category());
@ -623,7 +633,7 @@ namespace libtorrent
std::string directory::file() const std::string directory::file() const
{ {
#ifdef TORRENT_WINDOWS #ifdef TORRENT_WINDOWS
#ifdef UNICODE #if TORRENT_USE_WSTRING
return convert_from_wstring(m_fd.cFileName); return convert_from_wstring(m_fd.cFileName);
#else #else
return convert_from_native(m_fd.cFileName); return convert_from_native(m_fd.cFileName);
@ -637,7 +647,12 @@ namespace libtorrent
{ {
ec.clear(); ec.clear();
#ifdef TORRENT_WINDOWS #ifdef TORRENT_WINDOWS
if (FindNextFile(m_handle, &m_fd) == 0) #if TORRENT_USE_WSTRING
#define FindNextFile_ FindNextFileW
#else
#define FindNextFile_ FindNextFileA
#endif
if (FindNextFile_(m_handle, &m_fd) == 0)
{ {
m_done = true; m_done = true;
int err = GetLastError(); int err = GetLastError();
@ -724,9 +739,11 @@ namespace libtorrent
FILE_ATTRIBUTE_HIDDEN, // hidden + executable FILE_ATTRIBUTE_HIDDEN, // hidden + executable
}; };
#ifdef UNICODE #if TORRENT_USE_WSTRING
#define CreateFile_ CreateFileW
m_path = convert_to_wstring(path); m_path = convert_to_wstring(path);
#else #else
#define CreateFile_ CreateFileA
m_path = convert_to_native(path); m_path = convert_to_native(path);
#endif #endif
@ -734,7 +751,7 @@ namespace libtorrent
open_mode_t const& m = mode_array[mode & mode_mask]; open_mode_t const& m = mode_array[mode & mode_mask];
DWORD a = attrib_array[(mode & attribute_mask) >> 12]; DWORD a = attrib_array[(mode & attribute_mask) >> 12];
m_file_handle = CreateFile(m_path.c_str(), m.rw_mode, m.share_mode, 0 m_file_handle = CreateFile_(m_path.c_str(), m.rw_mode, m.share_mode, 0
, m.create_mode, m.flags | (a ? a : FILE_ATTRIBUTE_NORMAL), 0); , m.create_mode, m.flags | (a ? a : FILE_ATTRIBUTE_NORMAL), 0);
if (m_file_handle == INVALID_HANDLE_VALUE) if (m_file_handle == INVALID_HANDLE_VALUE)
@ -849,12 +866,14 @@ namespace libtorrent
DWORD bytes_per_sector; DWORD bytes_per_sector;
DWORD free_clusters; DWORD free_clusters;
DWORD total_clusters; DWORD total_clusters;
#ifdef UNICODE #if TORRENT_USE_WSTRING
#define GetDiskFreeSpace_ GetDiskFreeSpaceW
wchar_t backslash = L'\\'; wchar_t backslash = L'\\';
#else #else
#define GetDiskFreeSpace_ GetDiskFreeSpaceA
char backslash = '\\'; char backslash = '\\';
#endif #endif
if (GetDiskFreeSpace(m_path.substr(0, m_path.find_first_of(backslash)+1).c_str() if (GetDiskFreeSpace_(m_path.substr(0, m_path.find_first_of(backslash)+1).c_str()
, &sectors_per_cluster, &bytes_per_sector , &sectors_per_cluster, &bytes_per_sector
, &free_clusters, &total_clusters)) , &free_clusters, &total_clusters))
{ {
@ -1246,7 +1265,7 @@ namespace libtorrent
if (file_size > 0) if (file_size > 0)
{ {
HANDLE f = CreateFile(m_path.c_str(), GENERIC_WRITE HANDLE f = CreateFile_(m_path.c_str(), GENERIC_WRITE
, FILE_SHARE_READ | FILE_SHARE_WRITE, 0, OPEN_EXISTING , FILE_SHARE_READ | FILE_SHARE_WRITE, 0, OPEN_EXISTING
, FILE_ATTRIBUTE_NORMAL | FILE_FLAG_RANDOM_ACCESS, 0); , FILE_ATTRIBUTE_NORMAL | FILE_FLAG_RANDOM_ACCESS, 0);

View File

@ -2610,7 +2610,7 @@ namespace libtorrent
} }
else else
{ {
int diff = abs(dl_time - m_average_piece_time); int diff = abs(int(dl_time - m_average_piece_time));
if (m_piece_time_deviation == 0) m_piece_time_deviation = diff; if (m_piece_time_deviation == 0) m_piece_time_deviation = diff;
else m_piece_time_deviation = (m_piece_time_deviation * 6 + diff * 4) / 10; else m_piece_time_deviation = (m_piece_time_deviation * 6 + diff * 4) / 10;