diff options
author | Jaromir Wysoglad | 2019-08-25 10:47:59 +0200 |
---|---|---|
committer | Filippos Karapetis | 2019-08-25 13:46:16 +0300 |
commit | b0c8b368a907f7e3d183d50dcaf259e898ac70a6 (patch) | |
tree | 2ba7420a6652d991381fe5945c54dae5bd01d7da /common | |
parent | 2a2351eb2c64363207da59df68d8ad2d733df454 (diff) | |
download | scummvm-rg350-b0c8b368a907f7e3d183d50dcaf259e898ac70a6.tar.gz scummvm-rg350-b0c8b368a907f7e3d183d50dcaf259e898ac70a6.tar.bz2 scummvm-rg350-b0c8b368a907f7e3d183d50dcaf259e898ac70a6.zip |
COMMON: Don't include iconv.h in common/encoding.h
Move #include<iconv.h> from common/encoding.h to
common/encoding.cpp and change the methods accordingly.
This resulted in not saving the iconvHandle if using the
"non-static" version of conversion, but it simplified the code
and hopefuly resolved issues with forbidden symbols on some
platforms.
Diffstat (limited to 'common')
-rw-r--r-- | common/encoding.cpp | 101 | ||||
-rw-r--r-- | common/encoding.h | 59 |
2 files changed, 41 insertions, 119 deletions
diff --git a/common/encoding.cpp b/common/encoding.cpp index aeae520dbb..0fe490bc78 100644 --- a/common/encoding.cpp +++ b/common/encoding.cpp @@ -20,6 +20,7 @@ * */ +#define FORBIDDEN_SYMBOL_ALLOW_ALL #include "common/encoding.h" #include "common/textconsole.h" #include "common/system.h" @@ -27,16 +28,28 @@ #include "common/endian.h" #include <errno.h> +#ifdef USE_ICONV + +#include <iconv.h> + +#endif // USE_ICONV + namespace Common { +String addUtfEndianness(const String &str) { + if (str.equalsIgnoreCase("utf-16") || str.equalsIgnoreCase("utf-32")) { +#ifdef SCUMM_BIG_ENDIAN + return str + "BE"; +#else + return str + "LE"; +#endif + } else + return String(str); +} + Encoding::Encoding(const String &to, const String &from) : _to(to) , _from(from) { - _iconvHandle = initIconv(to, from); -} - -Encoding::~Encoding() { - deinitIconv(_iconvHandle); } char *Encoding::switchEndian(const char *string, int length, int bitCount) { @@ -62,60 +75,15 @@ char *Encoding::switchEndian(const char *string, int length, int bitCount) { } } -String Encoding::addUtfEndianness(const String &str) { - if (str.equalsIgnoreCase("utf-16") || str.equalsIgnoreCase("utf-32")) { -#ifdef SCUMM_BIG_ENDIAN - return str + "BE"; -#else - return str + "LE"; -#endif - } else - return String(str); -} - -iconv_t Encoding::initIconv(const String &to, const String &from) { -#ifdef USE_ICONV - String toTranslit = addUtfEndianness(to) + "//TRANSLIT"; - return iconv_open(toTranslit.c_str(), - addUtfEndianness(from).c_str()); -#else - return 0; -#endif // USE_ICONV -} - -void Encoding::deinitIconv(iconv_t iconvHandle) { -#ifdef USE_ICONV - if (iconvHandle != (iconv_t) -1) - iconv_close(iconvHandle); -#endif // USE_ICONV -} - -void Encoding::setFrom(const String &from) { - deinitIconv(_iconvHandle); - _from = from; - _iconvHandle = initIconv(_to, _from); -} - -void Encoding::setTo(const String &to) { - deinitIconv(_iconvHandle); - _to = to; - _iconvHandle = initIconv(_to, _from); -} - char *Encoding::convert(const char *string, size_t size) { - return convertWithTransliteration(_iconvHandle, _to, _from, string, size); + return convertWithTransliteration(_to, _from, string, size); } char *Encoding::convert(const String &to, const String &from, const char *string, size_t size) { - iconv_t iconvHandle = initIconv(to, from); - - char *result = convertWithTransliteration(iconvHandle, to, from, string, size); - - deinitIconv(iconvHandle); - return result; + return convertWithTransliteration(to, from, string, size); } -char *Encoding::convertWithTransliteration(iconv_t iconvHandle, const String &to, const String &from, const char *string, size_t length) { +char *Encoding::convertWithTransliteration(const String &to, const String &from, const char *string, size_t length) { if (from.equalsIgnoreCase(to)) { // don't convert, just copy the string and return it char *result = (char *) calloc(sizeof(char), length + 4); @@ -160,9 +128,7 @@ char *Encoding::convertWithTransliteration(iconv_t iconvHandle, const String &to if (from.hasPrefixIgnoreCase("utf-32")) tmpString = nullptr; else { - iconv_t tmpHandle = initIconv("UTF-32", from); - tmpString = conversion(tmpHandle, "UTF-32", from, string, length); - deinitIconv(tmpHandle); + tmpString = conversion("UTF-32", from, string, length); if (!tmpString) return nullptr; // find out the length in bytes of the tmpString @@ -179,26 +145,19 @@ char *Encoding::convertWithTransliteration(iconv_t iconvHandle, const String &to if (!newString) return nullptr; } - iconv_t newHandle = iconvHandle; - if (newFrom != from) - newHandle = initIconv(to, newFrom); char *result; if (newString != nullptr) { - result = conversion(newHandle, to, newFrom, newString, newLength); + result = conversion(to, newFrom, newString, newLength); free(newString); } else - result = conversion(newHandle, to, newFrom, string, newLength); - - if (newFrom != from) - deinitIconv(newHandle); + result = conversion(to, newFrom, string, newLength); return result; } -char *Encoding::conversion(iconv_t iconvHandle, const String &to, const String &from, const char *string, size_t length) { +char *Encoding::conversion(const String &to, const String &from, const char *string, size_t length) { char *result = nullptr; #ifdef USE_ICONV - if (iconvHandle != (iconv_t) -1) - result = convertIconv(iconvHandle, string, length); + result = convertIconv(addUtfEndianness(to).c_str(), addUtfEndianness(from).c_str(), string, length); #endif // USE_ICONV if (result == nullptr) result = g_system->convertEncoding(addUtfEndianness(to).c_str(), @@ -211,9 +170,14 @@ char *Encoding::conversion(iconv_t iconvHandle, const String &to, const String & return result; } -char *Encoding::convertIconv(iconv_t iconvHandle, const char *string, size_t length) { +char *Encoding::convertIconv(const char *to, const char *from, const char *string, size_t length) { #ifdef USE_ICONV + String toTranslit = String(to) + "//TRANSLIT"; + iconv_t iconvHandle = iconv_open(toTranslit.c_str(), from); + if (iconvHandle == (iconv_t) -1) + return nullptr; + size_t inSize = length; size_t outSize = inSize; size_t stringSize = inSize > 4 ? inSize : 4; @@ -266,6 +230,7 @@ char *Encoding::convertIconv(iconv_t iconvHandle, const char *string, size_t len delete[] originalSrc; #endif // ICONV_USES_CONST + iconv_close(iconvHandle); if (error) { if (buffer) free(buffer); diff --git a/common/encoding.h b/common/encoding.h index 11639cb40d..8245157a95 100644 --- a/common/encoding.h +++ b/common/encoding.h @@ -27,11 +27,6 @@ #include "common/str.h" #include "common/system.h" -#ifdef USE_ICONV -#include <iconv.h> -#else -typedef void* iconv_t; -#endif // USE_ICONV #ifdef WIN32 #include "backends/platform/sdl/win32/win32.h" @@ -57,7 +52,7 @@ class Encoding { * @param from Name of the encoding the strings will be converted from */ Encoding(const String &to, const String &from); - ~Encoding(); + ~Encoding() {}; /** * Converts string between encodings. The resulting string is ended by @@ -98,7 +93,7 @@ class Encoding { /** * @param from The encoding, to convert from */ - void setFrom(const String &from); + void setFrom(const String &from) {_from = from;}; /** * @return The encoding, which is currently being converted to @@ -108,7 +103,7 @@ class Encoding { /** * @param to The encoding, to convert to */ - void setTo(const String &to); + void setTo(const String &to) {_to = to;}; private: /** The encoding, which is currently being converted to */ @@ -118,17 +113,10 @@ class Encoding { String _from; /** - * iconvHandle currently used for conversions (is void pointer to 0 - * if the ScummVM isn't compiled with iconv) - */ - iconv_t _iconvHandle; - - /** * Takes care of transliteration and calls conversion * * The result has to be freed after use. * - * @param iconvHandle Handle to use for the conversion * @param to Name of the encoding the strings will be converted to * @param from Name of the encoding the strings will be converted from * @param string String that should be converted. @@ -136,7 +124,7 @@ class Encoding { * * @return Converted string (must be freed) or nullptr if the conversion failed */ - static char *convertWithTransliteration(iconv_t iconvHandle, const String &to, const String &from, const char *string, size_t length); + static char *convertWithTransliteration(const String &to, const String &from, const char *string, size_t length); /** * Calls as many conversion functions as possible or until the conversion @@ -145,7 +133,6 @@ class Encoding { * * The result has to be freed after use. * - * @param iconvHandle Handle to use for the conversion * @param to Name of the encoding the strings will be converted to * @param from Name of the encoding the strings will be converted from * @param string String that should be converted. @@ -153,20 +140,21 @@ class Encoding { * * @return Converted string (must be freed) or nullptr if the conversion failed */ - static char *conversion(iconv_t iconvHandle, const String &to, const String &from, const char *string, size_t length); + static char *conversion(const String &to, const String &from, const char *string, size_t length); /** * Tries to convert the string using iconv. * * The result has to be freed after use. * - * @param iconvHandle Handle to use for the conversion + * @param to Name of the encoding the strings will be converted to + * @param from Name of the encoding the strings will be converted from * @param string String that should be converted. * @param length Length of the string to convert in bytes. * * @return Converted string (must be freed) or nullptr if the conversion failed */ - static char *convertIconv(iconv_t iconvHandle, const char *string, size_t length); + static char *convertIconv(const char *to, const char *from, const char *string, size_t length); /** * Tries to use the TransMan to convert the string. It can convert only @@ -209,37 +197,6 @@ class Encoding { static uint32 *transliterateUTF32(const uint32 *string, size_t length); /** - * Inits the iconv handle - * - * The result has to be freed after use. - * - * @param to Name of the encoding the strings will be converted to - * @param from Name of the encoding the strings will be converted from - * - * @return Opened iconv handle or 0 if ScummVM is compiled without iconv - */ - static iconv_t initIconv(const String &to, const String &from); - - /** - * Deinits the iconv handle - * - * @param iconvHandle Handle that should be deinited - */ - static void deinitIconv(iconv_t iconvHandle); - - /** - * If the string is "utf-16" or "utf-32", this adds either BE for big endian - * or LE for little endian to the end of the string. Otherwise this does - * nothing. - * - * @param str String to append the endianness to - * - * @return The same string with appended endianness if it is needed, or - * the same string. - */ - static String addUtfEndianness(const String &str); - - /** * Switches the endianity of a string. * * @param string Array containing the characters of a string. |