From 22ea4cbe65b5bf737269dd0deeccb0cdf0124387 Mon Sep 17 00:00:00 2001 From: Chris Young Date: Tue, 26 May 2020 14:15:04 +0100 Subject: Amiga: Use codesets.library for UTF8 conversion if available --- frontends/amiga/libs.c | 7 +++++-- frontends/amiga/utf8.c | 55 ++++++++++++++++++++++++++++++++++++++++++-------- 2 files changed, 52 insertions(+), 10 deletions(-) diff --git a/frontends/amiga/libs.c b/frontends/amiga/libs.c index 109baa44e..3c09bcb27 100644 --- a/frontends/amiga/libs.c +++ b/frontends/amiga/libs.c @@ -1,5 +1,5 @@ /* - * Copyright 2014 Chris Young + * Copyright 2014-2020 Chris Young * * This file is part of NetSurf, http://www.netsurf-browser.org/ * @@ -189,6 +189,7 @@ AMINS_LIB_STRUCT(Locale); AMINS_LIB_STRUCT(P96); AMINS_LIB_STRUCT(Workbench); +AMINS_LIB_STRUCT(Codesets); AMINS_LIB_STRUCT(GuiGFX); AMINS_CLASS_STRUCT(ARexx); @@ -248,7 +249,8 @@ bool ami_libs_open(void) AMINS_LIB_OPEN("Picasso96API.library", 0, P96, "main", 1, false) /* Non-OS provided libraries */ - AMINS_LIB_OPEN("guigfx.library", 9, GuiGFX, "main", 1, false) + AMINS_LIB_OPEN("codesets.library", 6, Codesets, "main", 1, false) + AMINS_LIB_OPEN("guigfx.library", 9, GuiGFX, "main", 1, false) /* NB: timer.device is opened in schedule.c (ultimately by the scheduler process). * The library base and interface are obtained there, rather than here, due to @@ -324,6 +326,7 @@ void ami_libs_close(void) AMINS_CLASS_CLOSE(Window) /* Libraries */ + AMINS_LIB_CLOSE(Codesets) AMINS_LIB_CLOSE(GuiGFX) AMINS_LIB_CLOSE(Asl) diff --git a/frontends/amiga/utf8.c b/frontends/amiga/utf8.c index 5d05e9535..83872ebff 100755 --- a/frontends/amiga/utf8.c +++ b/frontends/amiga/utf8.c @@ -1,5 +1,5 @@ /* - * Copyright 2008 Chris Young + * Copyright 2008-2020 Chris Young * * This file is part of NetSurf, http://www.netsurf-browser.org/ * @@ -22,6 +22,7 @@ #include #include +#include #include #include @@ -31,20 +32,58 @@ #include "amiga/utf8.h" +static nserror ami_utf8_codesets(const char *string, size_t len, char **result, bool to_local) +{ + char *out; + ULONG utf8_tag, local_tag; + + if(to_local == false) { + local_tag = CSA_SourceCodeset; + utf8_tag = CSA_DestMIBenum; + } else { + utf8_tag = CSA_SourceMIBenum; + local_tag = CSA_DestCodeset; + } + + out = CodesetsConvertStr(CSA_Source, string, + CSA_SourceLen, len, + local_tag, CodesetsFindA(nsoption_charp(local_charset), NULL), + utf8_tag, CS_MIBENUM_UTF_8, + CSA_MapForeignChars, TRUE, + TAG_DONE); + + if(out != NULL) { + *result = strdup(out); + CodesetsFreeA(out, NULL); + } else { + return NSERROR_BAD_ENCODING; + } + + return NSERROR_OK; +} + nserror utf8_from_local_encoding(const char *string, size_t len, char **result) { - return utf8_from_enc(string, nsoption_charp(local_charset), len, result, NULL); + if(__builtin_expect((CodesetsBase == NULL), 0)) { + return utf8_from_enc(string, nsoption_charp(local_charset), len, result, NULL); + } else { + return ami_utf8_codesets(string, len, result, false); + } } nserror utf8_to_local_encoding(const char *string, size_t len, char **result) { - nserror err = NSERROR_NOMEM; - char *local_charset = ASPrintf("%s//IGNORE", nsoption_charp(local_charset)); - if(local_charset) { - err = utf8_to_enc(string, local_charset, len, result); - FreeVec(local_charset); + if(__builtin_expect((CodesetsBase == NULL), 0)) { + nserror err = NSERROR_NOMEM; + char *local_charset = ASPrintf("%s//IGNORE", nsoption_charp(local_charset)); + if(local_charset) { + err = utf8_to_enc(string, local_charset, len, result); + FreeVec(local_charset); + } + return err; + } else { + return ami_utf8_codesets(string, len, result, true); } - return err; } void ami_utf8_free(char *ptr) -- cgit v1.2.3