diff --git a/CMakeLists.txt b/CMakeLists.txt index 49086f99..946d1473 100755 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -297,6 +297,8 @@ set(DSView_SOURCES DSView/pv/dock/keywordlineedit.cpp DSView/pv/dock/searchcombobox.cpp DSView/pv/dialogs/decoderoptionsdlg.cpp + DSView/pv/utility/encoding.cpp + DSView/pv/utility/path.cpp ) set(DSView_HEADERS @@ -385,6 +387,8 @@ set(DSView_HEADERS DSView/pv/dock/keywordlineedit.h DSView/pv/dock/searchcombobox.h DSView/pv/dialogs/decoderoptionsdlg.h + DSView/pv/utility/encoding.h + DSView/pv/utility/path.h ) #=============================================================================== diff --git a/DSView/pv/device/file.cpp b/DSView/pv/device/file.cpp index 02afb55e..ec93ca8b 100755 --- a/DSView/pv/device/file.cpp +++ b/DSView/pv/device/file.cpp @@ -22,15 +22,11 @@ #include "file.h" #include "inputfile.h" #include "sessionfile.h" -#include #include #include #include - -#ifdef _WIN32 -#include -#endif +#include "../utility/path.h" namespace pv { namespace device { @@ -51,11 +47,10 @@ QString File::format_device_title() } File* File::create(QString name) -{ - char f_name[256]; - File::ConvertFileName(name, f_name, sizeof(f_name)); +{ + auto f_name = path::ConvertPath(name); - if (sr_session_load(f_name) == SR_OK) { + if (sr_session_load(f_name.c_str()) == SR_OK) { GSList *devlist = NULL; sr_session_dev_list(&devlist); sr_session_destroy(); @@ -84,10 +79,9 @@ QJsonArray File::get_decoders() QJsonArray dec_array; QJsonParseError error; - char f_name[256]; - File::ConvertFileName(_path, f_name, sizeof(f_name)); + auto f_name = path::ConvertPath(_path); - archive = zip_open(f_name, 0, &ret); + archive = zip_open(f_name.c_str(), 0, &ret); if (archive) { /* read "decoders" */ if (zip_stat(archive, "decoders", 0, &zs) != -1) { @@ -119,10 +113,9 @@ QJsonDocument File::get_session() QJsonDocument sessionDoc; QJsonParseError error; - char f_name[256]; - File::ConvertFileName(_path, f_name, sizeof(f_name)); + auto f_name = path::ConvertPath(_path); - archive = zip_open(f_name, 0, &ret); + archive = zip_open(f_name.c_str(), 0, &ret); if (archive) { /* read "decoders" */ if (zip_stat(archive, "session", 0, &zs) != -1) { @@ -143,29 +136,5 @@ QJsonDocument File::get_session() return sessionDoc; } -void File::ConvertFileName(QString fileName, char *out_name, int size) -{ - assert(out_name); - assert(size > 0); - memset(out_name, 0, size); - - char *src = NULL; -#ifdef _WIN32 - QTextCodec *code = QTextCodec::codecForName("GB2312"); - if (code != NULL){ - src = code->fromUnicode(fileName).data(); - } -#endif - if (src == NULL){ - src = fileName.toUtf8().data(); - } - - int len = strlen(src); - if (len >= size){ - assert(false); - } - strcpy(out_name, src); -} - } // device } // pv diff --git a/DSView/pv/device/file.h b/DSView/pv/device/file.h index d2ec6067..15ea77c0 100755 --- a/DSView/pv/device/file.h +++ b/DSView/pv/device/file.h @@ -25,6 +25,7 @@ #include #include #include +#include #include "devinst.h" @@ -47,8 +48,6 @@ public: public: QString format_device_title(); - static void ConvertFileName(QString fileName, char *out_name, int size); - protected: const QString _path; }; diff --git a/DSView/pv/device/sessionfile.cpp b/DSView/pv/device/sessionfile.cpp index e827e153..b2602bd1 100755 --- a/DSView/pv/device/sessionfile.cpp +++ b/DSView/pv/device/sessionfile.cpp @@ -21,6 +21,7 @@ #include "sessionfile.h" #include +#include "../utility/path.h" namespace pv { namespace device { @@ -43,10 +44,9 @@ void SessionFile::use(SigSession *owner) release_source(); } - char f_name[256]; - File::ConvertFileName(_path, f_name, sizeof(f_name)); + auto f_name = path::ConvertPath(_path); - if (sr_session_load(f_name) != SR_OK) + if (sr_session_load(f_name.c_str()) != SR_OK) throw tr("Failed to open file.\n"); GSList *devlist = NULL; diff --git a/DSView/pv/dialogs/about.cpp b/DSView/pv/dialogs/about.cpp index 920dfeaa..4a8be675 100755 --- a/DSView/pv/dialogs/about.cpp +++ b/DSView/pv/dialogs/about.cpp @@ -33,6 +33,7 @@ #include "../config/appconfig.h" #include "../dsvdef.h" +#include "../utility/encoding.h" namespace pv { namespace dialogs { @@ -86,7 +87,7 @@ About::About(QWidget *parent) : if (news.open(QIODevice::ReadOnly)) { QTextStream stream(&news); - app::set_utf8(stream); + encoding::set_utf8(stream); QString line; while (!stream.atEnd()){ diff --git a/DSView/pv/dialogs/protocolexp.cpp b/DSView/pv/dialogs/protocolexp.cpp index 728410e5..9546c0ff 100755 --- a/DSView/pv/dialogs/protocolexp.cpp +++ b/DSView/pv/dialogs/protocolexp.cpp @@ -38,6 +38,7 @@ #include "../data/decodermodel.h" #include "../config/appconfig.h" #include "../dsvdef.h" +#include "../utility/encoding.h" using namespace boost; using namespace std; @@ -149,7 +150,7 @@ void ProtocolExp::accept() QFile file(file_name); file.open(QIODevice::WriteOnly | QIODevice::Text); QTextStream out(&file); - app::set_utf8(out); + encoding::set_utf8(out); //out.setGenerateByteOrderMark(true); // UTF-8 without BOM QFuture future; diff --git a/DSView/pv/dsvdef.cpp b/DSView/pv/dsvdef.cpp index dfd42fd8..d54f53f4 100644 --- a/DSView/pv/dsvdef.cpp +++ b/DSView/pv/dsvdef.cpp @@ -19,7 +19,7 @@ * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA */ -#include "dsvdef.h" +#include "dsvdef.h" #include #ifdef DS_DEBUG_TRACE @@ -29,14 +29,6 @@ } #endif -#include - -#if QT_VERSION >= QT_VERSION_CHECK(6, 0, 0) -#include -#else -#include -#endif - namespace DecoderDataFormat { int Parse(const char *name){ @@ -58,16 +50,3 @@ namespace DecoderDataFormat return (int)hex; } } - - -namespace app -{ - void set_utf8(QTextStream &stream){ - #if QT_VERSION >= QT_VERSION_CHECK(6, 0, 0) - stream.setEncoding(QStringConverter::Utf8); - #else - QTextCodec *code=QTextCodec::codecForName("UTF-8"); - stream.setCodec(code); - #endif - } -} diff --git a/DSView/pv/dsvdef.h b/DSView/pv/dsvdef.h index abfb851d..dbcf7a7f 100644 --- a/DSView/pv/dsvdef.h +++ b/DSView/pv/dsvdef.h @@ -22,7 +22,6 @@ #pragma once #include "../config.h" -#include #define countof(x) (sizeof(x)/sizeof(x[0])) @@ -43,9 +42,7 @@ enum View_type { #else #define ds_debug(x) #endif - -class QWidget; -class QTextStream; + #define DESTROY_OBJECT(p) if((p)){delete (p); p = NULL;} #define DESTROY_QT_OBJECT(p) if((p)){((p))->deleteLater(); p = NULL;} @@ -68,9 +65,3 @@ namespace DecoderDataFormat int Parse(const char *name); } - -namespace app -{ - void set_utf8(QTextStream &stream); -} - diff --git a/DSView/pv/mainwindow.cpp b/DSView/pv/mainwindow.cpp index 55270b5e..54ce31c2 100644 --- a/DSView/pv/mainwindow.cpp +++ b/DSView/pv/mainwindow.cpp @@ -93,6 +93,7 @@ #include "appcontrol.h" #include "dsvdef.h" #include "appcontrol.h" +#include "utility/encoding.h" #define BASE_SESSION_VERSION 2 @@ -1260,7 +1261,7 @@ bool MainWindow::on_store_session(QString name) } QTextStream outStream(&sessionFile); - app::set_utf8(outStream); + encoding::set_utf8(outStream); QJsonObject sessionVar; if (!gen_session_json(sessionVar)) diff --git a/DSView/pv/storesession.cpp b/DSView/pv/storesession.cpp index 8e87ee51..a772cbd0 100755 --- a/DSView/pv/storesession.cpp +++ b/DSView/pv/storesession.cpp @@ -59,6 +59,8 @@ #include "libsigrokdecode.h" #include "config/appconfig.h" #include "dsvdef.h" +#include "utility/encoding.h" +#include "utility/path.h" namespace pv { @@ -181,13 +183,15 @@ bool StoreSession::save_start() return false; } - std::string _filename = getFileName(); + auto _filename = path::ConvertPath(_file_name); + if (m_zipDoc.CreateNew(_filename.c_str(), false)) { if ( !m_zipDoc.AddFromBuffer("header", meta_data.c_str(), meta_data.size()) || !m_zipDoc.AddFromBuffer("decoders", decoder_data.c_str(), decoder_data.size()) || !m_zipDoc.AddFromBuffer("session", session_data.c_str(), session_data.size()) - ){ + ) + { _has_error = true; _error = m_zipDoc.GetError(); } @@ -673,7 +677,7 @@ void StoreSession::export_proc(data::Snapshot *snapshot) QFile file(_file_name); file.open(QIODevice::WriteOnly | QIODevice::Text); QTextStream out(&file); - app::set_utf8(out); + encoding::set_utf8(out); //out.setGenerateByteOrderMark(true); // UTF-8 without BOM // Meta @@ -1324,15 +1328,4 @@ void StoreSession::MakeChunkName(char *chunk_name, int chunk_num, int index, int } } -std::string StoreSession::getFileName() -{ -#ifdef _WIN32 - QTextCodec *code = QTextCodec::codecForName("GB2312"); - if (code != NULL){ - return code->fromUnicode(_file_name).data(); - } -#endif - return _file_name.toUtf8().toStdString(); -} - } // pv diff --git a/DSView/pv/storesession.h b/DSView/pv/storesession.h index 359ee9cf..07f1ffd3 100755 --- a/DSView/pv/storesession.h +++ b/DSView/pv/storesession.h @@ -94,7 +94,6 @@ private: QList getSuportedExportFormats(); double get_integer(GVariant * var); void MakeChunkName(char *chunk_name, int chunk_num, int index, int type, int version); - std:: string getFileName(); signals: void progress_updated(); diff --git a/DSView/pv/utility/encoding.cpp b/DSView/pv/utility/encoding.cpp new file mode 100644 index 00000000..12cccf1c --- /dev/null +++ b/DSView/pv/utility/encoding.cpp @@ -0,0 +1,42 @@ +/* + * This file is part of the DSView project. + * DSView is based on PulseView. + * + * Copyright (C) 2022 DreamSourceLab + * + * 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 St, Fifth Floor, Boston, MA 02110-1301 USA + */ +#include "encoding.h" + +#include + +#if QT_VERSION >= QT_VERSION_CHECK(6, 0, 0) +#include +#else +#include +#endif + +namespace pv::encoding +{ + void set_utf8(QTextStream &stream) + { +#if QT_VERSION >= QT_VERSION_CHECK(6, 0, 0) + stream.setEncoding(QStringConverter::Utf8); +#else + QTextCodec *code = QTextCodec::codecForName("UTF-8"); + stream.setCodec(code); +#endif + } +} \ No newline at end of file diff --git a/DSView/pv/utility/encoding.h b/DSView/pv/utility/encoding.h new file mode 100644 index 00000000..f1584f46 --- /dev/null +++ b/DSView/pv/utility/encoding.h @@ -0,0 +1,32 @@ +/* + * This file is part of the DSView project. + * DSView is based on PulseView. + * + * Copyright (C) 2022 DreamSourceLab + * + * 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 St, Fifth Floor, Boston, MA 02110-1301 USA + */ + +#ifndef UTILITY_ENCODING_H +#define UTILITY_ENCODING_H + +class QTextStream; + +namespace pv::encoding +{ + void set_utf8(QTextStream &stream); +} + +#endif \ No newline at end of file diff --git a/DSView/pv/utility/path.cpp b/DSView/pv/utility/path.cpp new file mode 100644 index 00000000..1cce527c --- /dev/null +++ b/DSView/pv/utility/path.cpp @@ -0,0 +1,41 @@ +/* + * This file is part of the DSView project. + * DSView is based on PulseView. + * + * Copyright (C) 2022 DreamSourceLab + * + * 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 St, Fifth Floor, Boston, MA 02110-1301 USA + */ + +#include "path.h" + +#ifdef _WIN32 +#include +#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(); + } +} \ No newline at end of file diff --git a/DSView/pv/utility/path.h b/DSView/pv/utility/path.h new file mode 100644 index 00000000..2f5729ec --- /dev/null +++ b/DSView/pv/utility/path.h @@ -0,0 +1,33 @@ +/* + * This file is part of the DSView project. + * DSView is based on PulseView. + * + * Copyright (C) 2022 DreamSourceLab + * + * 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 St, Fifth Floor, Boston, MA 02110-1301 USA + */ + +#ifndef UTILITY_PATH_H +#define UTILITY_PATH_H + +#include +#include + +namespace pv::path +{ + std::string ConvertPath(QString fileName); +} + +#endif \ No newline at end of file