From 9e25366f85eb97db58d1e81b66778eb947e40f28 Mon Sep 17 00:00:00 2001 From: Jonas Kvinge Date: Sun, 19 May 2024 01:47:44 +0200 Subject: [PATCH] Add function for detecting text encoding --- src/CMakeLists.txt | 1 + src/utilities/textencodingutils.cpp | 55 +++++++++++++++++++++++++++++ src/utilities/textencodingutils.h | 30 ++++++++++++++++ 3 files changed, 86 insertions(+) create mode 100644 src/utilities/textencodingutils.cpp create mode 100644 src/utilities/textencodingutils.h diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index e8bf5a50..1bf19284 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -59,6 +59,7 @@ set(SOURCES utilities/coverutils.cpp utilities/screenutils.cpp utilities/searchparserutils.cpp + utilities/textencodingutils.cpp engine/enginebase.cpp engine/enginedevice.cpp diff --git a/src/utilities/textencodingutils.cpp b/src/utilities/textencodingutils.cpp new file mode 100644 index 00000000..5609f76b --- /dev/null +++ b/src/utilities/textencodingutils.cpp @@ -0,0 +1,55 @@ +/* + * Strawberry Music Player + * Copyright 2024, Jonas Kvinge + * + * Strawberry 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. + * + * Strawberry 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 Strawberry. If not, see . + * + */ + +#include + +#include +#include +#include + +#include "textencodingutils.h" + +namespace Utilities { + +QByteArray TextEncodingFromData(const QByteArray &data) { + + UErrorCode error_code = U_ZERO_ERROR; + UCharsetDetector *csd = ucsdet_open(&error_code); + if (error_code != U_ZERO_ERROR) { + return QByteArray(); + } + const QScopeGuard scopeguard_csd = qScopeGuard([csd]() { ucsdet_close(csd); }); + ucsdet_setText(csd, data.constData(), static_cast(data.length()), &error_code); + if (error_code != U_ZERO_ERROR) { + return QByteArray(); + } + const UCharsetMatch *ucm = ucsdet_detect(csd, &error_code); + if (error_code != U_ZERO_ERROR) { + return QByteArray(); + } + const char *encoding_name = ucsdet_getName(ucm, &error_code); + if (error_code != U_ZERO_ERROR) { + return QByteArray(); + } + + return encoding_name; + +} + +} // namespace Utilities diff --git a/src/utilities/textencodingutils.h b/src/utilities/textencodingutils.h new file mode 100644 index 00000000..5786b136 --- /dev/null +++ b/src/utilities/textencodingutils.h @@ -0,0 +1,30 @@ +/* + * Strawberry Music Player + * Copyright 2024, Jonas Kvinge + * + * Strawberry 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. + * + * Strawberry 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 Strawberry. If not, see . + * + */ + +#ifndef TEXTENCODINGUTILS_H +#define TEXTENCODINGUTILS_H + +#include +#include + +namespace Utilities { +QByteArray TextEncodingFromData(const QByteArray &data); +} // namespace Utilities + +#endif // TEXTENCODINGUTILS_H