Skip to content

Commit

Permalink
Remove Iconv
Browse files Browse the repository at this point in the history
We do not use this anywhere since years.
Is a maintenance burden and nobody tests this.

Fix EasyRPG#465
  • Loading branch information
Ghabry committed Nov 29, 2023
1 parent 28fd197 commit 1aeebc1
Show file tree
Hide file tree
Showing 8 changed files with 57 additions and 325 deletions.
7 changes: 2 additions & 5 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ project(liblcf VERSION 0.8 LANGUAGES CXX)

# Compilation options
option(BUILD_SHARED_LIBS "Build shared library, disable for building the static library (default: ON)" ON)
option(LIBLCF_WITH_ICU "ICU encoding detection (when OFF fallback to iconv, not recommended, default: ON)" ON)
option(LIBLCF_WITH_ICU "ICU encoding handling (disable only for testing purposes, default: ON)" ON)
option(LIBLCF_WITH_XML "XML reading support (expat, default: ON)" ON)
option(LIBLCF_UPDATE_MIMEDB "Whether to run update-mime-database after install (default: ON)" ON)
option(LIBLCF_ENABLE_TOOLS "Whether to build the tools (default: ON)" ON)
Expand Down Expand Up @@ -343,16 +343,13 @@ set_property(TARGET lcf PROPERTY WINDOWS_EXPORT_ALL_SYMBOLS ON)
# Name of the exported library
set_property(TARGET lcf PROPERTY EXPORT_NAME liblcf)

# icu or fallback to iconv
# icu
set(LCF_SUPPORT_ICU 0)
if(LIBLCF_WITH_ICU)
find_package(ICU COMPONENTS i18n uc data REQUIRED)
target_link_libraries(lcf ICU::i18n ICU::uc ICU::data)
list(APPEND LIBLCF_DEPS "icu-i18n")
set(LCF_SUPPORT_ICU 1)
else()
find_package(Iconv REQUIRED)
target_link_libraries(lcf Iconv::Iconv)
endif()

# expat
Expand Down
133 changes: 0 additions & 133 deletions builds/cmake/Modules/FindIconv.cmake

This file was deleted.

2 changes: 0 additions & 2 deletions builds/cmake/liblcf-config.cmake.in
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@ include(CMakeFindDependencyMacro)

if(@LCF_SUPPORT_ICU@)
find_dependency(ICU COMPONENTS i18n uc data REQUIRED)
else()
find_dependency(Iconv REQUIRED)
endif()

if(@LCF_SUPPORT_XML@)
Expand Down
2 changes: 1 addition & 1 deletion configure.ac
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ AM_CONDITIONAL(CROSS_COMPILING,[test "x$cross_compiling" = "xyes"])

# Checks for libraries.
AC_SUBST([LCF_SUPPORT_ICU],[0])
AC_ARG_ENABLE([icu],[AS_HELP_STRING([--disable-icu],[Disable ICU encoding detection (fallback to iconv) [default=no]])])
AC_ARG_ENABLE([icu],[AS_HELP_STRING([--disable-icu],[Disable ICU encoding detection (only for testing purposes) [default=no]])])
AS_IF([test "x$enable_icu" != "xno"],[
AX_PKG_CHECK_MODULES([ICU],[],[icu-i18n],[LCF_SUPPORT_ICU=1])
])
Expand Down
61 changes: 12 additions & 49 deletions src/encoder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
#include "lcf/scope_guard.h"
#include <cstdio>
#include <cstdlib>
#include <exception>

#if LCF_SUPPORT_ICU
# include <unicode/ucsdet.h>
Expand All @@ -27,9 +26,6 @@
#ifdef _WIN32
# include <windows.h>
#else
# if !LCF_SUPPORT_ICU
# include <iconv.h>
# endif
# include <locale>
#endif

Expand Down Expand Up @@ -77,12 +73,12 @@ void Encoder::Decode(std::string& str) {
Convert(str, _conv_storage, _conv_runtime);
}

#if LCF_SUPPORT_ICU
void Encoder::Init() {
if (_encoding.empty()) {
return;
}

#if LCF_SUPPORT_ICU
auto code_page = atoi(_encoding.c_str());
const auto& storage_encoding = code_page > 0
? ReaderUtil::CodepageToEncoding(code_page)
Expand Down Expand Up @@ -110,27 +106,22 @@ void Encoder::Init() {

_conv_runtime = conv_runtime;
_conv_storage = conv_storage;
#else
_conv_runtime = const_cast<char*>("UTF-8");
_conv_storage = const_cast<char*>(_encoding.c_str());
#endif
}

void Encoder::Reset() {
#if LCF_SUPPORT_ICU
auto* conv = reinterpret_cast<UConverter*>(_conv_runtime);
if (conv) ucnv_close(conv);
conv = reinterpret_cast<UConverter*>(_conv_storage);
if (conv) ucnv_close(conv);
#endif
}
if (_conv_runtime) {
ucnv_close(_conv_runtime);
_conv_runtime = nullptr;
}

if (_conv_storage) {
ucnv_close(_conv_storage);
_conv_storage = nullptr;
}
}

void Encoder::Convert(std::string& str, void* conv_dst_void, void* conv_src_void) {
#if LCF_SUPPORT_ICU
void Encoder::Convert(std::string& str, UConverter* conv_dst, UConverter* conv_src) {
const auto& src = str;
auto* conv_dst = reinterpret_cast<UConverter*>(conv_dst_void);
auto* conv_src = reinterpret_cast<UConverter*>(conv_src_void);

auto status = U_ZERO_ERROR;
_buffer.resize(src.size() * 4);
Expand All @@ -151,36 +142,8 @@ void Encoder::Convert(std::string& str, void* conv_dst_void, void* conv_src_void
}

str.assign(_buffer.data(), dst_p);
return;
#else
auto* conv_dst = reinterpret_cast<const char*>(conv_dst_void);
auto* conv_src = reinterpret_cast<const char*>(conv_src_void);
iconv_t cd = iconv_open(conv_dst, conv_src);
if (cd == (iconv_t)-1)
return;
char *src = &str.front();
size_t src_left = str.size();
size_t dst_size = str.size() * 5 + 10;
_buffer.resize(dst_size);
char *dst = _buffer.data();
size_t dst_left = dst_size;
# ifdef ICONV_CONST
char ICONV_CONST *p = src;
# else
char *p = src;
# endif
char *q = dst;
size_t status = iconv(cd, &p, &src_left, &q, &dst_left);
iconv_close(cd);
if (status == (size_t) -1 || src_left > 0) {
str.clear();
return;
}
*q++ = '\0';
str.assign(dst, dst_size - dst_left);
return;
#endif
}
#endif

} //namespace lcf

29 changes: 26 additions & 3 deletions src/lcf/encoder.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,10 @@
#include <vector>
#include <string>

#if LCF_SUPPORT_ICU
class UConverter;
#endif

namespace lcf {

class Encoder {
Expand All @@ -23,20 +27,39 @@ class Encoder {

~Encoder();

/**
* Converts from the specified encoding to UTF-8
*
* @param str String to encode to UTF-8
*/
void Encode(std::string& str);

/**
* Converts from UTF-8 to the specified encoding
*
* @param str String to decode from UTF-8
*/
void Decode(std::string& str);

bool IsOk() const;

const std::string& GetEncoding() const;
private:
#if LCF_SUPPORT_ICU
void Init();
void Reset();
void Convert(std::string& str, void* conv_dst, void* conv_src);
private:
void Convert(std::string& str, UConverter* conv_dst, UConverter* conv_src);

UConverter* _conv_storage = nullptr;
UConverter* _conv_runtime = nullptr;
std::vector<char> _buffer;
#else
void Init() {}
void Reset() {}
void Convert(std::string&, void*, void*) {}
void* _conv_storage = nullptr;
void* _conv_runtime = nullptr;
std::vector<char> _buffer;
#endif
std::string _encoding;
};

Expand Down
15 changes: 1 addition & 14 deletions src/lcf/reader_util.h
Original file line number Diff line number Diff line change
Expand Up @@ -104,27 +104,14 @@ namespace ReaderUtil {
*/
std::string Recode(StringView str_to_encode, StringView source_encoding);

/**
* Converts a string between encodings.
*
* @param str_to_encode the string to convert.
* @param src_enc the source encoding.
* @param dst_enc the destination encoding.
* @return the recoded string.
*/
std::string Recode(StringView str_to_encode,
StringView src_enc,
StringView dst_enc);

/**
* Converts a UTF-8 string to lowercase and then decomposes it.
*
*
* @param str the string to normalize.
* @return the normalized string.
*/
std::string Normalize(StringView str);


/**
* Helper function that returns an element from a vector using a 1-based
* index as usually used by LCF data structures.
Expand Down
Loading

0 comments on commit 1aeebc1

Please sign in to comment.