This patch allows Windows users to share files and folders with "exotic" characters.

The problem was that libretroshare handles files in UTF-8 but Windows's ANSI/POSIX C functions automatically assume that the char* parameters are encoded with the system's code page. There is no way to set that code page as UTF-8. So now under Windows the code translates the file name to UTF-16 before feeding it to one of the Unicode functions (they are usually prefixed or suffixed by 'w'). Please note that it is not very efficient.

Furthermore, Windows does not provide a Unicode version of opendir/readdir/closedir, so it was necessary to use FindFirstFileW/FindNextFileW/FindClose which has a different behaviour as well as different structures. The FolderIterator class was created in order to mimic the Unix way of traversing folders contents. Hence the algorithm is unchanged and the systems differences masked.

As it was necessary to use some functions from the Windows API, <windows.h> had to be included in a few files were it didn't appear before, creating macros and #define conflicts. In order to solve them, util/rswin.h must be included first in a file (if necessary). Otherwise the preprocessor will fail on purpose to avoid the code being compiled with different _WIN32_WINNT values.

As another side-effect, rstlvutil.h and rstlvutil.cc have been removed from libretroshare.pro file. They are only used by testing units and include util/utest.h which defines a macro FAILED that already exists in <windows.h>. I don't know if unit tests are still in use and I don't plan on coding often on Windows, so I'll leave that as an exercise (hot potato?) to a motivated fellow Windows programmer.

git-svn-id: http://svn.code.sf.net/p/retroshare/code/trunk@2924 b45a01b8-16f6-495d-af2f-9b41ad6348cc
This commit is contained in:
leander-256 2010-05-16 23:26:45 +00:00
parent 7e409c0b79
commit b30056e17f
14 changed files with 340 additions and 30 deletions

View file

@ -0,0 +1,82 @@
#include "folderiterator.h"
#include "rswin.h"
namespace librs { namespace util {
FolderIterator::FolderIterator(const std::string& folderName)
{
#ifdef WINDOWS_SYS
std::wstring utf16Name;
if(! ConvertUtf8ToUtf16(folderName, utf16Name)) {
validity = false;
return;
}
utf16Name += L"\\*.*";
handle = FindFirstFileW(utf16Name.c_str(), &fileInfo);
validity = handle != INVALID_HANDLE_VALUE;
isFirstCall = true;
#else
handle = opendir(folderName.c_str());
validity = handle != NULL;
#endif
}
FolderIterator::~FolderIterator()
{
closedir();
}
bool FolderIterator::readdir() {
if(!validity)
return false;
#ifdef WINDOWS_SYS
if(isFirstCall) {
isFirstCall = false;
return true;
}
return FindNextFileW(handle, &fileInfo) != 0;
#else
return readdir(handle) == 0;
#endif
}
bool FolderIterator::d_name(std::string& dest)
{
if(!validity)
return false;
#ifdef WINDOWS_SYS
if(! ConvertUtf16ToUtf8(fileInfo.cFileName, dest)) {
validity = false;
return false;
}
#else
dest = handle->d_name;
#endif
return true;
}
bool FolderIterator::closedir()
{
if(!validity)
return false;
validity = false;
#ifdef WINDOWS_SYS
return FindClose(handle) != 0;
#else
return closedir(handle) == 0;
#endif
}
} } // librs::util

View file

@ -0,0 +1,53 @@
#ifndef FOLDERITERATOR_H
#define FOLDERITERATOR_H
#include <iostream>
#include <cstdio>
#ifdef WINDOWS_SYS
#include "util/rswin.h"
#include <windows.h>
#include <tchar.h>
#include <stdio.h>
#include <string.h>
#else
#include <dirent.h>
#endif
namespace librs { namespace util {
class FolderIterator
{
public:
FolderIterator(const std::string& folderName);
~FolderIterator();
bool isValid() const { return validity; }
bool readdir();
bool d_name(std::string& dest);
bool closedir();
private:
bool validity;
#ifdef WINDOWS_SYS
HANDLE handle;
bool isFirstCall;
_WIN32_FIND_DATAW fileInfo;
#else
DIR* handle;
#endif
};
} } // librs::util
#endif // FOLDERITERATOR_H

View file

@ -39,6 +39,7 @@
#include <fstream>
#if defined(WIN32) || defined(__CYGWIN__)
#include "util/rswin.h"
#include "wtypes.h"
#include <winioctl.h>
#else
@ -345,8 +346,15 @@ bool RsDirUtil::getFileHash(std::string filepath,
unsigned char sha_buf[SHA_DIGEST_LENGTH];
unsigned char gblBuf[512];
#ifdef WINDOWS_SYS
std::wstring filepathW;
librs::util::ConvertUtf8ToUtf16(filepath, filepathW);
if (NULL == (fd = _wfopen(filepathW.c_str(), L"rb")))
return false;
#else
if (NULL == (fd = fopen(filepath.c_str(), "rb")))
return false;
#endif
/* determine size */
fseek(fd, 0, SEEK_END);

View file

@ -43,6 +43,8 @@
#else
#include "util/rswin.h"
#include <winsock2.h>
#include <ws2tcpip.h>

View file

@ -27,6 +27,10 @@
*/
#ifdef WINDOWS_SYS
#include "util/rswin.h"
#endif
#include <pthread.h>
#include <inttypes.h>

View file

@ -0,0 +1,49 @@
#include "util/rswin.h"
namespace librs { namespace util {
bool ConvertUtf8ToUtf16(const std::string& source, std::wstring& dest) {
int nbChars = MultiByteToWideChar(CP_UTF8, 0,
source.c_str(), -1,
0, 0);
if(nbChars == 0)
return false;
wchar_t* utf16Name = new wchar_t[nbChars];
if( MultiByteToWideChar(CP_UTF8, 0,
source.c_str(), -1,
utf16Name, nbChars) == 0) {
return false;
}
dest = utf16Name;
delete[] utf16Name;
return true;
}
bool ConvertUtf16ToUtf8(const std::wstring& source, std::string& dest) {
int nbChars = WideCharToMultiByte(CP_UTF8, 0,
source.c_str(), -1,
0, 0,
0, 0);
if(nbChars == 0)
return false;
char* utf8Name = new char[nbChars];
if( WideCharToMultiByte(CP_UTF8, 0,
source.c_str(), -1,
utf8Name, nbChars,
0, 0) == 0) {
return false;
}
dest = utf8Name;
delete[] utf8Name;
return true;
}
} } // librs::util

View file

@ -1,18 +1,63 @@
#ifndef RS_UNIVERSAL_STUFF
#define RS_UNIVERSAL_STUFF
/****************************************************************
* This file is distributed under the following license:
*
* Copyright (c) 2010, Thomas Kister
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor,
* Boston, MA 02110-1301, USA.
****************************************************************/
#if defined(WIN32) || defined(MINGW)
#include <unistd.h>
#define sleep(x) Sleep(1000 * x)
/**
* This file provides helper functions for the windows environment
*/
#ifndef RSWIN_H_
#define RSWIN_H_
#ifdef WINDOWS_SYS
#ifdef _WIN32_WINNT
#error "Please include \"util/rswin.h\" *before* any other one as _WIN32_WINNT needs to predefined"
#endif
// This defines the platform to be WinXP or later and is needed for getaddrinfo
// It must be declared before pthread.h includes windows.h
#define _WIN32_WINNT 0x0501
#include <windows.h>
#include <string>
// For win32 systems (tested on MingW+Ubuntu)
#define stat64 _stati64
#endif
namespace librs { namespace util {
bool ConvertUtf8ToUtf16(const std::string& source, std::wstring& dest);
#endif
bool ConvertUtf16ToUtf8(const std::wstring& source, std::string& dest);
} } // librs::util
#endif // WINDOWS_SYS
#endif // RSWIN_H_