mirror of
https://github.com/OpenRCT2/OpenRCT2
synced 2025-12-24 00:03:11 +01:00
Remove dependency on iconv.
This commit is contained in:
@@ -136,11 +136,6 @@ if (NOT DISABLE_TTF)
|
||||
endif ()
|
||||
endif ()
|
||||
|
||||
if (UNIX OR STATIC OR ${CMAKE_SYSTEM_NAME} MATCHES "BSD")
|
||||
find_library(ICONV_LIBRARIES NAMES iconv libiconv libiconv-2 c)
|
||||
target_link_libraries(${PROJECT} ${ICONV_LIBRARIES})
|
||||
endif()
|
||||
|
||||
if (HAVE_DISCORD_RPC)
|
||||
target_link_libraries(libopenrct2 discord-rpc)
|
||||
endif()
|
||||
|
||||
@@ -1330,106 +1330,3 @@ void money_to_string(money32 amount, char * buffer_to_put_value_to, size_t buffe
|
||||
snprintf(buffer_to_put_value_to, buffer_len, "0");
|
||||
}
|
||||
}
|
||||
|
||||
utf8 *win1252_to_utf8_alloc(const char *src, size_t srcMaxSize)
|
||||
{
|
||||
size_t stringLength = strnlen(src, srcMaxSize);
|
||||
size_t reservedSpace = (stringLength * 4) + 1;
|
||||
utf8 * result = (utf8 *)malloc(reservedSpace);
|
||||
sint32 actualSpace = win1252_to_utf8(result, src, stringLength, reservedSpace);
|
||||
return (utf8*)realloc(result, actualSpace);
|
||||
}
|
||||
|
||||
sint32 win1252_to_utf8(utf8string dst, const char *src, size_t srcLength, size_t maxBufferLength)
|
||||
{
|
||||
#ifdef _WIN32
|
||||
utf16 stackBuffer[256];
|
||||
utf16 *heapBuffer = nullptr;
|
||||
utf16 *intermediateBuffer = stackBuffer;
|
||||
size_t bufferCount = Util::CountOf(stackBuffer);
|
||||
if (maxBufferLength > bufferCount) {
|
||||
if (srcLength > bufferCount) {
|
||||
bufferCount = srcLength + 4;
|
||||
heapBuffer = (utf16 *)malloc(bufferCount * sizeof(utf16));
|
||||
assert(heapBuffer != nullptr);
|
||||
intermediateBuffer = heapBuffer;
|
||||
}
|
||||
}
|
||||
MultiByteToWideChar(CP_ACP, 0, src, -1, intermediateBuffer, (sint32)bufferCount);
|
||||
sint32 result = WideCharToMultiByte(CP_UTF8, 0, intermediateBuffer, -1, dst, (sint32)maxBufferLength, NULL, NULL);
|
||||
|
||||
free(heapBuffer);
|
||||
#elif defined(__ANDROID__)
|
||||
JNIEnv *env = (_JNIEnv *)SDL_AndroidGetJNIEnv();
|
||||
|
||||
jclass localisation = env->FindClass("website/openrct2/Localisation");
|
||||
jmethodID win1252ToUtf8 = env->GetStaticMethodID(localisation, "win1252ToUtf8", "([B)Ljava/lang/String;");
|
||||
|
||||
jbyteArray bytes = env->NewByteArray(srcLength);
|
||||
env->SetByteArrayRegion(bytes, 0, srcLength, (jbyte *) src);
|
||||
|
||||
jstring jstring1 = (jstring)env->CallStaticObjectMethod(localisation, win1252ToUtf8, bytes);
|
||||
|
||||
const char* utf = env->GetStringUTFChars(jstring1, NULL);
|
||||
strcpy(dst, utf);
|
||||
env->ReleaseStringUTFChars(jstring1, utf);
|
||||
env->DeleteLocalRef(localisation);
|
||||
env->DeleteLocalRef(bytes);
|
||||
env->DeleteLocalRef(jstring1);
|
||||
|
||||
int result = strlen(dst) + 1;
|
||||
#else
|
||||
//log_warning("converting %s of size %d", src, srcLength);
|
||||
char *buffer_conv = strndup(src, srcLength);
|
||||
char *buffer_orig = buffer_conv;
|
||||
const char *to_charset = "UTF-8";
|
||||
const char *from_charset = "CP1252";
|
||||
iconv_t cd = iconv_open(to_charset, from_charset);
|
||||
if ((iconv_t)-1 == cd)
|
||||
{
|
||||
sint32 error = errno;
|
||||
switch (error)
|
||||
{
|
||||
case EINVAL:
|
||||
log_error("Unsupported conversion from %s to %s, errno = %d", from_charset, to_charset, error);
|
||||
break;
|
||||
default:
|
||||
log_error("Unknown error while initialising iconv, errno = %d", error);
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
size_t obl = maxBufferLength;
|
||||
char *outBuf = dst;
|
||||
size_t conversion_result = iconv(cd, &buffer_conv, &srcLength, &outBuf, &obl);
|
||||
if (conversion_result == (size_t)-1)
|
||||
{
|
||||
sint32 error = errno;
|
||||
switch (error)
|
||||
{
|
||||
case EILSEQ:
|
||||
log_error("Encountered invalid sequence");
|
||||
break;
|
||||
case EINVAL:
|
||||
log_error("Encountered incomplete sequence");
|
||||
break;
|
||||
case E2BIG:
|
||||
log_error("Ran out of space");
|
||||
break;
|
||||
default:
|
||||
log_error("Unknown error encountered, errno = %d", error);
|
||||
}
|
||||
}
|
||||
sint32 close_result = iconv_close(cd);
|
||||
if (close_result == -1)
|
||||
{
|
||||
log_error("Failed to close iconv, errno = %d", errno);
|
||||
}
|
||||
size_t byte_diff = maxBufferLength - obl + 1;
|
||||
dst[byte_diff - 1] = '\0';
|
||||
//log_warning("converted %s of size %d, %d", dst, byte_diff, strlen(dst));
|
||||
sint32 result = byte_diff;
|
||||
free(buffer_orig);
|
||||
#endif // _WIN32
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
@@ -50,9 +50,6 @@ rct_string_id user_string_allocate(sint32 base, const utf8 *text);
|
||||
void user_string_free(rct_string_id id);
|
||||
bool is_user_string_id(rct_string_id stringId);
|
||||
|
||||
utf8 *win1252_to_utf8_alloc(const char *src, size_t srcMaxSize);
|
||||
sint32 win1252_to_utf8(utf8string dst, const char *src, size_t srcLength, size_t maxBufferLength);
|
||||
|
||||
wchar_t encoding_convert_rct2_to_unicode(wchar_t rct2str);
|
||||
uint32 encoding_convert_unicode_to_rct2(uint32 unicode);
|
||||
|
||||
|
||||
@@ -33,6 +33,7 @@
|
||||
#include "ScenarioSources.h"
|
||||
|
||||
#include "../config/Config.h"
|
||||
#include "../localisation/Language.h"
|
||||
#include "../localisation/Localisation.h"
|
||||
#include "../localisation/LocalisationService.h"
|
||||
#include "../platform/platform.h"
|
||||
@@ -128,7 +129,7 @@ private:
|
||||
static constexpr uint32 MAGIC_NUMBER = 0x58444953; // SIDX
|
||||
static constexpr uint16 VERSION = 3;
|
||||
static constexpr auto PATTERN = "*.sc4;*.sc6";
|
||||
|
||||
|
||||
public:
|
||||
explicit ScenarioFileIndex(const IPlatformEnvironment& env) :
|
||||
FileIndex("scenario index",
|
||||
@@ -238,7 +239,7 @@ private:
|
||||
// RCT2 scenario
|
||||
auto fs = FileStream(path, FILE_MODE_OPEN);
|
||||
auto chunkReader = SawyerChunkReader(&fs);
|
||||
|
||||
|
||||
rct_s6_header header = chunkReader.ReadChunkAs<rct_s6_header>();
|
||||
if (header.type == S6_TYPE_SCENARIO)
|
||||
{
|
||||
@@ -647,7 +648,8 @@ private:
|
||||
if (scBasic.CompanyValue > highscore->company_value)
|
||||
{
|
||||
SafeFree(highscore->name);
|
||||
highscore->name = win1252_to_utf8_alloc(scBasic.CompletedBy, Util::CountOf(scBasic.CompletedBy));
|
||||
std::string name = rct2_to_utf8(scBasic.CompletedBy, RCT2_LANGUAGE_ID_ENGLISH_UK);
|
||||
highscore->name = String::Duplicate(name.c_str());
|
||||
highscore->company_value = scBasic.CompanyValue;
|
||||
highscore->timestamp = DATETIME64_MIN;
|
||||
break;
|
||||
@@ -658,7 +660,8 @@ private:
|
||||
{
|
||||
scenario_highscore_entry * highscore = InsertHighscore();
|
||||
highscore->fileName = String::Duplicate(scBasic.Path);
|
||||
highscore->name = win1252_to_utf8_alloc(scBasic.CompletedBy, Util::CountOf(scBasic.CompletedBy));
|
||||
std::string name = rct2_to_utf8(scBasic.CompletedBy, RCT2_LANGUAGE_ID_ENGLISH_UK);
|
||||
highscore->name = String::Duplicate(name.c_str());
|
||||
highscore->company_value = scBasic.CompanyValue;
|
||||
highscore->timestamp = DATETIME64_MIN;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user