Fixed problem with utf characters in the %APPDATA% path on Windows.

Added function for opening files on Windows and Linux - RsDirUtil::rs_fopen.

git-svn-id: http://svn.code.sf.net/p/retroshare/code/trunk@4124 b45a01b8-16f6-495d-af2f-9b41ad6348cc
This commit is contained in:
thunder2 2011-04-03 23:11:38 +00:00
parent bc78397a64
commit bc113326e4
17 changed files with 109 additions and 119 deletions

View file

@ -23,11 +23,9 @@
*
*/
#include "util/rsdebug.h"
#include "util/rsthreads.h"
#include "util/rsdir.h"
#include <map>
#include <stdio.h>
@ -57,7 +55,7 @@ int setDebugCrashMode(const char *cfile)
RsStackMutex stack(logMtx); /******** LOCKED ****************/
crashfile = cfile;
/* if the file exists - then we crashed, save it */
FILE *tmpin = fopen(crashfile.c_str(), "r");
FILE *tmpin = RsDirUtil::rs_fopen(crashfile.c_str(), "r");
if (tmpin)
{
/* see how long it is */
@ -71,7 +69,7 @@ int setDebugCrashMode(const char *cfile)
/* go back to the start */
fseek(tmpin, 0, SEEK_SET);
FILE *tmpout = fopen(crashfile_save.c_str(), "w");
FILE *tmpout = RsDirUtil::rs_fopen(crashfile_save.c_str(), "w");
int da_size = 10240;
char dataarray[da_size]; /* 10k */
unsigned int da_read = 0;
@ -131,7 +129,7 @@ int clearDebugCrashLog()
debugMode = RS_DEBUG_STDERR;
/* just open the file, and then close */
FILE *tmpin = fopen(crashfile.c_str(), "w");
FILE *tmpin = RsDirUtil::rs_fopen(crashfile.c_str(), "w");
fclose(tmpin);
return 1;
@ -147,7 +145,7 @@ int setDebugFile(const char *fname)
int locked_setDebugFile(const char *fname)
{
if (NULL != (ofd = fopen(fname, "w")))
if (NULL != (ofd = RsDirUtil::rs_fopen(fname, "w")))
{
fprintf(stderr, "Logging redirected to %s\n", fname);
debugMode = RS_DEBUG_LOGFILE;

View file

@ -582,15 +582,8 @@ bool RsDirUtil::getFileHash(const std::string& filepath, std::string &hash, uint
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")))
if (NULL == (fd = RsDirUtil::rs_fopen(filepath.c_str(), "rb")))
return false;
#else
if (NULL == (fd = fopen64(filepath.c_str(), "rb")))
return false;
#endif
/* determine size */
fseeko64(fd, 0, SEEK_END);
@ -645,18 +638,18 @@ bool RsDirUtil::getFileHash(const std::string& filepath, std::string &hash, uint
bool RsDirUtil::renameFile(const std::string& from, const std::string& to)
{
int loops = 0;
int loops = 0;
#if defined(WIN32) || defined(MINGW) || defined(__CYGWIN__)
#ifdef WIN_CROSS_UBUNTU
std::wstring f,t ;
for(int i=0;i<from.size();++i) f.push_back(from[i]) ;
for(int i=0;i<to.size();++i) t.push_back(to[i]) ;
#ifdef WINDOWS_SYS
std::wstring f;
librs::util::ConvertUtf8ToUtf16(from, f);
std::wstring t;
librs::util::ConvertUtf8ToUtf16(to, t);
while (!MoveFileEx(f.c_str(), t.c_str(), MOVEFILE_REPLACE_EXISTING))
#else
std::string f(from),t(to) ;
#endif
while (!MoveFileExA(f.c_str(), t.c_str(), MOVEFILE_REPLACE_EXISTING))
#else
while (rename(from.c_str(), to.c_str()) < 0)
#endif
{
@ -736,6 +729,20 @@ bool RsDirUtil::createBackup (const std::string& sFilename, unsigned int nCount)
return true;
}
FILE *RsDirUtil::rs_fopen(const char* filename, const char* mode)
{
#ifdef WINDOWS_SYS
std::wstring wfilename;
librs::util::ConvertUtf8ToUtf16(filename, wfilename);
std::wstring wmode;
librs::util::ConvertUtf8ToUtf16(mode, wmode);
return _wfopen(wfilename.c_str(), wmode.c_str());
#else
return = fopen64(filename, mode);
#endif
}
#if 0 // NOT ENABLED YET!
/************************* WIDE STRING ***************************/
/************************* WIDE STRING ***************************/

View file

@ -91,6 +91,7 @@ bool hashWideFile(std::wstring filepath,
bool getWideFileHash(std::wstring filepath,
std::string &hash, uint64_t &size);
FILE *rs_fopen(const char* filename, const char* mode);
}