fix: can't load unicode file path on window

This commit is contained in:
dreamsourcelabTAI 2022-06-23 19:02:34 +08:00
parent ea94e901dd
commit f8e1aa1844
3 changed files with 42 additions and 21 deletions

View File

@ -440,20 +440,19 @@ void MainWindow::update_device_list()
//load specified file name from application startup param
if (_bFirstLoad){
_bFirstLoad = false;
if (AppControl::Instance()->_open_file_name != ""){
QString opf(QString::fromUtf8(AppControl::Instance()->_open_file_name.c_str()));
QFile fpath;
QString ldFileName(AppControl::Instance()->_open_file_name.c_str());
if (fpath.exists(opf)){
qDebug()<<"auto load file:"<<opf;
on_load_file(opf);
if (ldFileName != ""){
if (QFile::exists(ldFileName)){
qDebug()<<"auto load file:"<<ldFileName;
on_load_file(ldFileName);
}
else{
qDebug()<<"file is not exists:"<<opf;
MsgBox::Show(tr("Open file error!"), opf.toStdString().c_str());
qDebug()<<"file is not exists:"<<ldFileName;
MsgBox::Show(tr("Open file error!"), ldFileName, NULL);
}
}
}
}
}

View File

@ -21,21 +21,10 @@
#include "path.h"
#ifdef _WIN32
#include <QTextCodec>
#endif
namespace pv::path
{
std::string ConvertPath(QString fileName)
{
#ifdef _WIN32
QTextCodec *code = QTextCodec::codecForName("GB2312");
if (code != NULL)
{
return code->fromUnicode(fileName).data();
}
#endif
return fileName.toUtf8().toStdString();
}

View File

@ -28,6 +28,10 @@
#include "ioapi.h"
#ifdef _WIN32
#include <windows.h>
#endif
voidpf call_zopen64 (const zlib_filefunc64_32_def* pfilefunc,const void*filename,int mode)
{
if (pfilefunc->zfile_func64.zopen64_file != NULL)
@ -112,6 +116,34 @@ static voidpf ZCALLBACK fopen_file_func (voidpf opaque, const char* filename, in
static voidpf ZCALLBACK fopen64_file_func (voidpf opaque, const void* filename, int mode)
{
#ifdef _WIN32
FILE* file = NULL;
const wchar_t* mode_fopen = NULL;
if ((mode & ZLIB_FILEFUNC_MODE_READWRITEFILTER)==ZLIB_FILEFUNC_MODE_READ)
mode_fopen = L"rb";
else
if (mode & ZLIB_FILEFUNC_MODE_EXISTING)
mode_fopen = L"r+b";
else
if (mode & ZLIB_FILEFUNC_MODE_CREATE)
mode_fopen = L"wb";
int wcSize = MultiByteToWideChar(CP_UTF8, MB_ERR_INVALID_CHARS, filename, -1, NULL, 0);
if (wcSize == 0) {
return NULL;
}
wchar_t *wcName = (wchar_t *)malloc(sizeof(wchar_t) * wcSize + 8);
if (wcName == NULL) {
return NULL;
}
MultiByteToWideChar(CP_UTF8, MB_ERR_INVALID_CHARS, filename, -1, wcName, wcSize);
if ((filename!=NULL) && (mode_fopen != NULL))
file = _wfopen((const wchar_t*)wcName, mode_fopen);
free(wcName);
return file;
#else
FILE* file = NULL;
const char* mode_fopen = NULL;
if ((mode & ZLIB_FILEFUNC_MODE_READWRITEFILTER)==ZLIB_FILEFUNC_MODE_READ)
@ -126,6 +158,7 @@ static voidpf ZCALLBACK fopen64_file_func (voidpf opaque, const void* filename,
if ((filename!=NULL) && (mode_fopen != NULL))
file = FOPEN_FUNC((const char*)filename, mode_fopen);
return file;
#endif
}