From 95997d27106daf91336847f50efaaa32279b7fc7 Mon Sep 17 00:00:00 2001 From: saturneric Date: Mon, 16 Oct 2023 17:54:05 +0800 Subject: fix: check and update copyright at files --- src/core/function/CharsetOperator.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'src/core/function/CharsetOperator.cpp') diff --git a/src/core/function/CharsetOperator.cpp b/src/core/function/CharsetOperator.cpp index 72c5e72b..e850a52f 100644 --- a/src/core/function/CharsetOperator.cpp +++ b/src/core/function/CharsetOperator.cpp @@ -1,5 +1,5 @@ /** - * Copyright (C) 2021 Saturneric + * Copyright (C) 2021 Saturneric * * This file is part of GpgFrontend. * @@ -20,7 +20,7 @@ * the gpg4usb project, which is under GPL-3.0-or-later. * * All the source code of GpgFrontend was modified and released by - * Saturneric starting on May 12, 2021. + * Saturneric starting on May 12, 2021. * * SPDX-License-Identifier: GPL-3.0-or-later * -- cgit v1.2.3 From b7ceed0b87752077fe19fefe9b0df8ec27ce0531 Mon Sep 17 00:00:00 2001 From: saturneric Date: Wed, 25 Oct 2023 22:28:25 +0800 Subject: feat: moving gnupg info gathering logic to a new module --- src/core/function/CharsetOperator.cpp | 1 - 1 file changed, 1 deletion(-) (limited to 'src/core/function/CharsetOperator.cpp') diff --git a/src/core/function/CharsetOperator.cpp b/src/core/function/CharsetOperator.cpp index e850a52f..4233440c 100644 --- a/src/core/function/CharsetOperator.cpp +++ b/src/core/function/CharsetOperator.cpp @@ -28,7 +28,6 @@ #include "core/function/CharsetOperator.h" -#include #include #include #include -- cgit v1.2.3 From fd46d4667611c0db9cea3f06205727399b6fb5fd Mon Sep 17 00:00:00 2001 From: saturneric Date: Sun, 29 Oct 2023 02:46:15 +0800 Subject: refactor: start to tidy up code using clang-tidy --- src/core/function/CharsetOperator.cpp | 2 -- 1 file changed, 2 deletions(-) (limited to 'src/core/function/CharsetOperator.cpp') diff --git a/src/core/function/CharsetOperator.cpp b/src/core/function/CharsetOperator.cpp index 4233440c..28dcec11 100644 --- a/src/core/function/CharsetOperator.cpp +++ b/src/core/function/CharsetOperator.cpp @@ -34,8 +34,6 @@ #include #include -#include -#include GpgFrontend::CharsetOperator::CharsetInfo GpgFrontend::CharsetOperator::Detect( const std::string &buffer) { -- cgit v1.2.3 From 3ad7fecdb6458fdd6f146bed19fe643c7f93e905 Mon Sep 17 00:00:00 2001 From: saturneric Date: Tue, 7 Nov 2023 15:18:06 +0800 Subject: refactor: remove CommonUtils at core --- src/core/function/CharsetOperator.cpp | 39 ++++++++++++++++++++--------------- 1 file changed, 22 insertions(+), 17 deletions(-) (limited to 'src/core/function/CharsetOperator.cpp') diff --git a/src/core/function/CharsetOperator.cpp b/src/core/function/CharsetOperator.cpp index 28dcec11..8623428d 100644 --- a/src/core/function/CharsetOperator.cpp +++ b/src/core/function/CharsetOperator.cpp @@ -35,14 +35,16 @@ #include -GpgFrontend::CharsetOperator::CharsetInfo GpgFrontend::CharsetOperator::Detect( - const std::string &buffer) { +namespace GpgFrontend { + +auto CharsetOperator::Detect(const std::string &buffer) + -> CharsetOperator::CharsetInfo { const UCharsetMatch *ucm; UErrorCode status = U_ZERO_ERROR; UCharsetDetector *csd = ucsdet_open(&status); status = U_ZERO_ERROR; - if (U_FAILURE(status)) { + if (U_FAILURE(status) != 0) { SPDLOG_ERROR("failed to open charset detector: {}", u_errorName(status)); return {"unknown", "unknown", 0}; } @@ -51,7 +53,7 @@ GpgFrontend::CharsetOperator::CharsetInfo GpgFrontend::CharsetOperator::Detect( status = U_ZERO_ERROR; ucsdet_setText(csd, buffer.data(), buffer.size(), &status); - if (U_FAILURE(status)) { + if (U_FAILURE(status) != 0) { SPDLOG_ERROR("failed to set text to charset detector: {}", u_errorName(status)); return {"unknown", "unknown", 0}; @@ -60,37 +62,37 @@ GpgFrontend::CharsetOperator::CharsetInfo GpgFrontend::CharsetOperator::Detect( status = U_ZERO_ERROR; ucm = ucsdet_detect(csd, &status); - if (U_FAILURE(status)) return {"unknown", "unknown", 0}; + if (U_FAILURE(status) != 0) return {"unknown", "unknown", 0}; status = U_ZERO_ERROR; const char *name = ucsdet_getName(ucm, &status); - if (U_FAILURE(status)) return {"unknown", "unknown", 0}; + if (U_FAILURE(status) != 0) return {"unknown", "unknown", 0}; status = U_ZERO_ERROR; int confidence = ucsdet_getConfidence(ucm, &status); - if (U_FAILURE(status)) return {name, "unknown", 0}; + if (U_FAILURE(status) != 0) return {name, "unknown", 0}; status = U_ZERO_ERROR; const char *language = ucsdet_getLanguage(ucm, &status); - if (U_FAILURE(status)) return {name, "unknown", confidence}; + if (U_FAILURE(status) != 0) return {name, "unknown", confidence}; SPDLOG_DEBUG("Detected charset: {} {} {}", name, language, confidence); return {name, language, confidence}; } -bool GpgFrontend::CharsetOperator::Convert2Utf8(const std::string &buffer, - std::string &out_buffer, - std::string from_charset_name) { +auto CharsetOperator::Convert2Utf8(const std::string &buffer, + std::string &out_buffer, + std::string from_charset_name) -> bool { UErrorCode status = U_ZERO_ERROR; const auto from_encode = std::string("utf-8"); - const auto to_encode = from_charset_name; + const auto &to_encode = from_charset_name; SPDLOG_DEBUG("Converting buffer: {}", buffer.size()); // test if the charset is supported UConverter *conv = ucnv_open(from_encode.c_str(), &status); ucnv_close(conv); - if (U_FAILURE(status)) { + if (U_FAILURE(status) != 0) { SPDLOG_ERROR("failed to open converter: {}, from encode: {}", u_errorName(status), from_encode); return false; @@ -99,14 +101,15 @@ bool GpgFrontend::CharsetOperator::Convert2Utf8(const std::string &buffer, // test if the charset is supported conv = ucnv_open(to_encode.c_str(), &status); ucnv_close(conv); - if (U_FAILURE(status)) { + if (U_FAILURE(status) != 0) { SPDLOG_ERROR("failed to open converter: {}, to encode: {}", u_errorName(status), to_encode); return false; } status = U_ZERO_ERROR; - int32_t target_limit = 0, target_capacity = 0; + int32_t target_limit = 0; + int32_t target_capacity = 0; target_capacity = ucnv_convert(from_encode.c_str(), to_encode.c_str(), nullptr, @@ -120,11 +123,13 @@ bool GpgFrontend::CharsetOperator::Convert2Utf8(const std::string &buffer, out_buffer.size(), buffer.data(), buffer.size(), &status); } - if (U_FAILURE(status)) { + if (U_FAILURE(status) != 0) { SPDLOG_ERROR("failed to convert to utf-8: {}", u_errorName(status)); return false; } SPDLOG_DEBUG("converted buffer: {} bytes", out_buffer.size()); return true; -} \ No newline at end of file +} + +} // namespace GpgFrontend \ No newline at end of file -- cgit v1.2.3 From 3c40fa27823e70215261d3845275360f85e59623 Mon Sep 17 00:00:00 2001 From: saturneric Date: Fri, 5 Jan 2024 16:11:24 +0800 Subject: fix: slove some known issues --- src/core/function/CharsetOperator.cpp | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) (limited to 'src/core/function/CharsetOperator.cpp') diff --git a/src/core/function/CharsetOperator.cpp b/src/core/function/CharsetOperator.cpp index 8623428d..a5f96344 100644 --- a/src/core/function/CharsetOperator.cpp +++ b/src/core/function/CharsetOperator.cpp @@ -49,8 +49,6 @@ auto CharsetOperator::Detect(const std::string &buffer) return {"unknown", "unknown", 0}; } - SPDLOG_DEBUG("detecting charset buffer: {} bytes", buffer.size()); - status = U_ZERO_ERROR; ucsdet_setText(csd, buffer.data(), buffer.size(), &status); if (U_FAILURE(status) != 0) { @@ -76,7 +74,7 @@ auto CharsetOperator::Detect(const std::string &buffer) const char *language = ucsdet_getLanguage(ucm, &status); if (U_FAILURE(status) != 0) return {name, "unknown", confidence}; - SPDLOG_DEBUG("Detected charset: {} {} {}", name, language, confidence); + SPDLOG_DEBUG("detected charset: {} {} {}", name, language, confidence); return {name, language, confidence}; } -- cgit v1.2.3 From 644aa4397b03dbef73f8bfedc13925b51cad836b Mon Sep 17 00:00:00 2001 From: saturneric Date: Fri, 5 Jan 2024 20:55:15 +0800 Subject: feat: integrate logging api to core --- src/core/function/CharsetOperator.cpp | 25 ++++++++++++++----------- 1 file changed, 14 insertions(+), 11 deletions(-) (limited to 'src/core/function/CharsetOperator.cpp') diff --git a/src/core/function/CharsetOperator.cpp b/src/core/function/CharsetOperator.cpp index a5f96344..40978701 100644 --- a/src/core/function/CharsetOperator.cpp +++ b/src/core/function/CharsetOperator.cpp @@ -35,6 +35,8 @@ #include +#include "core/utils/LogUtils.h" + namespace GpgFrontend { auto CharsetOperator::Detect(const std::string &buffer) @@ -45,15 +47,16 @@ auto CharsetOperator::Detect(const std::string &buffer) status = U_ZERO_ERROR; if (U_FAILURE(status) != 0) { - SPDLOG_ERROR("failed to open charset detector: {}", u_errorName(status)); + GF_CORE_LOG_ERROR("failed to open charset detector: {}", + u_errorName(status)); return {"unknown", "unknown", 0}; } status = U_ZERO_ERROR; ucsdet_setText(csd, buffer.data(), buffer.size(), &status); if (U_FAILURE(status) != 0) { - SPDLOG_ERROR("failed to set text to charset detector: {}", - u_errorName(status)); + GF_CORE_LOG_ERROR("failed to set text to charset detector: {}", + u_errorName(status)); return {"unknown", "unknown", 0}; } @@ -74,7 +77,7 @@ auto CharsetOperator::Detect(const std::string &buffer) const char *language = ucsdet_getLanguage(ucm, &status); if (U_FAILURE(status) != 0) return {name, "unknown", confidence}; - SPDLOG_DEBUG("detected charset: {} {} {}", name, language, confidence); + GF_CORE_LOG_DEBUG("detected charset: {} {} {}", name, language, confidence); return {name, language, confidence}; } @@ -85,14 +88,14 @@ auto CharsetOperator::Convert2Utf8(const std::string &buffer, const auto from_encode = std::string("utf-8"); const auto &to_encode = from_charset_name; - SPDLOG_DEBUG("Converting buffer: {}", buffer.size()); + GF_CORE_LOG_DEBUG("Converting buffer: {}", buffer.size()); // test if the charset is supported UConverter *conv = ucnv_open(from_encode.c_str(), &status); ucnv_close(conv); if (U_FAILURE(status) != 0) { - SPDLOG_ERROR("failed to open converter: {}, from encode: {}", - u_errorName(status), from_encode); + GF_CORE_LOG_ERROR("failed to open converter: {}, from encode: {}", + u_errorName(status), from_encode); return false; } @@ -100,8 +103,8 @@ auto CharsetOperator::Convert2Utf8(const std::string &buffer, conv = ucnv_open(to_encode.c_str(), &status); ucnv_close(conv); if (U_FAILURE(status) != 0) { - SPDLOG_ERROR("failed to open converter: {}, to encode: {}", - u_errorName(status), to_encode); + GF_CORE_LOG_ERROR("failed to open converter: {}, to encode: {}", + u_errorName(status), to_encode); return false; } @@ -122,11 +125,11 @@ auto CharsetOperator::Convert2Utf8(const std::string &buffer, } if (U_FAILURE(status) != 0) { - SPDLOG_ERROR("failed to convert to utf-8: {}", u_errorName(status)); + GF_CORE_LOG_ERROR("failed to convert to utf-8: {}", u_errorName(status)); return false; } - SPDLOG_DEBUG("converted buffer: {} bytes", out_buffer.size()); + GF_CORE_LOG_DEBUG("converted buffer: {} bytes", out_buffer.size()); return true; } -- cgit v1.2.3 From bf538056b24a68b8fd235b1c50991ee8eb46a776 Mon Sep 17 00:00:00 2001 From: saturneric Date: Fri, 12 Jan 2024 14:02:37 +0800 Subject: refactor: use QString instead of std::string and improve threading system --- src/core/function/CharsetOperator.cpp | 102 ++-------------------------------- 1 file changed, 6 insertions(+), 96 deletions(-) (limited to 'src/core/function/CharsetOperator.cpp') diff --git a/src/core/function/CharsetOperator.cpp b/src/core/function/CharsetOperator.cpp index 40978701..023fcd10 100644 --- a/src/core/function/CharsetOperator.cpp +++ b/src/core/function/CharsetOperator.cpp @@ -28,109 +28,19 @@ #include "core/function/CharsetOperator.h" -#include -#include -#include -#include - -#include - #include "core/utils/LogUtils.h" namespace GpgFrontend { -auto CharsetOperator::Detect(const std::string &buffer) +auto CharsetOperator::Detect(const QString &buffer) -> CharsetOperator::CharsetInfo { - const UCharsetMatch *ucm; - UErrorCode status = U_ZERO_ERROR; - UCharsetDetector *csd = ucsdet_open(&status); - - status = U_ZERO_ERROR; - if (U_FAILURE(status) != 0) { - GF_CORE_LOG_ERROR("failed to open charset detector: {}", - u_errorName(status)); - return {"unknown", "unknown", 0}; - } - - status = U_ZERO_ERROR; - ucsdet_setText(csd, buffer.data(), buffer.size(), &status); - if (U_FAILURE(status) != 0) { - GF_CORE_LOG_ERROR("failed to set text to charset detector: {}", - u_errorName(status)); - return {"unknown", "unknown", 0}; - } - - status = U_ZERO_ERROR; - ucm = ucsdet_detect(csd, &status); - - if (U_FAILURE(status) != 0) return {"unknown", "unknown", 0}; - - status = U_ZERO_ERROR; - const char *name = ucsdet_getName(ucm, &status); - if (U_FAILURE(status) != 0) return {"unknown", "unknown", 0}; - - status = U_ZERO_ERROR; - int confidence = ucsdet_getConfidence(ucm, &status); - if (U_FAILURE(status) != 0) return {name, "unknown", 0}; - - status = U_ZERO_ERROR; - const char *language = ucsdet_getLanguage(ucm, &status); - if (U_FAILURE(status) != 0) return {name, "unknown", confidence}; - - GF_CORE_LOG_DEBUG("detected charset: {} {} {}", name, language, confidence); - return {name, language, confidence}; + // TODO + return {"", "", 0}; } -auto CharsetOperator::Convert2Utf8(const std::string &buffer, - std::string &out_buffer, - std::string from_charset_name) -> bool { - UErrorCode status = U_ZERO_ERROR; - const auto from_encode = std::string("utf-8"); - const auto &to_encode = from_charset_name; - - GF_CORE_LOG_DEBUG("Converting buffer: {}", buffer.size()); - - // test if the charset is supported - UConverter *conv = ucnv_open(from_encode.c_str(), &status); - ucnv_close(conv); - if (U_FAILURE(status) != 0) { - GF_CORE_LOG_ERROR("failed to open converter: {}, from encode: {}", - u_errorName(status), from_encode); - return false; - } - - // test if the charset is supported - conv = ucnv_open(to_encode.c_str(), &status); - ucnv_close(conv); - if (U_FAILURE(status) != 0) { - GF_CORE_LOG_ERROR("failed to open converter: {}, to encode: {}", - u_errorName(status), to_encode); - return false; - } - - status = U_ZERO_ERROR; - int32_t target_limit = 0; - int32_t target_capacity = 0; - - target_capacity = - ucnv_convert(from_encode.c_str(), to_encode.c_str(), nullptr, - target_limit, buffer.data(), buffer.size(), &status); - - if (status == U_BUFFER_OVERFLOW_ERROR) { - status = U_ZERO_ERROR; - out_buffer.clear(); - out_buffer.resize(target_capacity); - ucnv_convert(from_encode.c_str(), to_encode.c_str(), out_buffer.data(), - out_buffer.size(), buffer.data(), buffer.size(), &status); - } - - if (U_FAILURE(status) != 0) { - GF_CORE_LOG_ERROR("failed to convert to utf-8: {}", u_errorName(status)); - return false; - } - - GF_CORE_LOG_DEBUG("converted buffer: {} bytes", out_buffer.size()); - return true; +auto CharsetOperator::Convert2Utf8(const QString &buffer, QString &out_buffer, + QString from_charset_name) -> bool { + // TODO } } // namespace GpgFrontend \ No newline at end of file -- cgit v1.2.3 From ca8bdd4cc269e61ce3c8b06b4bfca5a512468110 Mon Sep 17 00:00:00 2001 From: saturneric Date: Mon, 15 Jan 2024 20:07:25 +0800 Subject: fix: clean up useless code and make life easier --- src/core/function/CharsetOperator.cpp | 46 ----------------------------------- 1 file changed, 46 deletions(-) delete mode 100644 src/core/function/CharsetOperator.cpp (limited to 'src/core/function/CharsetOperator.cpp') diff --git a/src/core/function/CharsetOperator.cpp b/src/core/function/CharsetOperator.cpp deleted file mode 100644 index 023fcd10..00000000 --- a/src/core/function/CharsetOperator.cpp +++ /dev/null @@ -1,46 +0,0 @@ -/** - * Copyright (C) 2021 Saturneric - * - * This file is part of GpgFrontend. - * - * GpgFrontend 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 3 of the License, or - * (at your option) any later version. - * - * GpgFrontend 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 GpgFrontend. If not, see . - * - * The initial version of the source code is inherited from - * the gpg4usb project, which is under GPL-3.0-or-later. - * - * All the source code of GpgFrontend was modified and released by - * Saturneric starting on May 12, 2021. - * - * SPDX-License-Identifier: GPL-3.0-or-later - * - */ - -#include "core/function/CharsetOperator.h" - -#include "core/utils/LogUtils.h" - -namespace GpgFrontend { - -auto CharsetOperator::Detect(const QString &buffer) - -> CharsetOperator::CharsetInfo { - // TODO - return {"", "", 0}; -} - -auto CharsetOperator::Convert2Utf8(const QString &buffer, QString &out_buffer, - QString from_charset_name) -> bool { - // TODO -} - -} // namespace GpgFrontend \ No newline at end of file -- cgit v1.2.3