NetSurf
utf8.c
Go to the documentation of this file.
1/*
2 * Copyright 2008-2021 Chris Young <chris@unsatisfactorysoftware.co.uk>
3 *
4 * This file is part of NetSurf, http://www.netsurf-browser.org/
5 *
6 * NetSurf is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; version 2 of the License.
9 *
10 * NetSurf is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
17 */
18
19#include "amiga/os3support.h"
20
21#include <stdlib.h>
22#include <string.h>
23#include <sys/types.h>
24
25#include <proto/codesets.h>
26#include <proto/exec.h>
27#include <proto/utility.h>
28
29#include "utils/nsoption.h"
30#include "utils/utf8.h"
31#include "netsurf/utf8.h"
32
33#include "amiga/utf8.h"
34
35static nserror ami_utf8_codesets(const char *string, size_t len, char **result, bool to_local)
36{
37 char *out;
38 ULONG utf8_tag = CSA_SourceCodeset, local_tag = CSA_DestCodeset, len_tag = CSA_SourceLen;
39 static struct codeset *utf8_cs = NULL;
40 static struct codeset *local_cs = NULL;
41
42 if(local_cs == NULL) local_cs = CodesetsFind(NULL,
43#ifdef __amigaos4__
44 CSA_MIBenum, nsoption_int(local_codeset),
45#else
46 NULL,
47#endif
48 TAG_DONE);
49
50 if(utf8_cs == NULL) utf8_cs = CodesetsFind(NULL,
51 CSA_MIBenum, CS_MIBENUM_UTF_8,
52 TAG_DONE);
53
54 if(to_local == false) {
55 local_tag = CSA_SourceCodeset;
56 utf8_tag = CSA_DestCodeset;
57 }
58
59 if(len == 0) len_tag = TAG_IGNORE;
60
61 out = CodesetsConvertStr(CSA_Source, string,
62 len_tag, len,
63#ifdef __amigaos4__
64 local_tag, local_cs,
65#endif
66 utf8_tag, utf8_cs,
67 CSA_MapForeignChars, TRUE,
68 TAG_DONE);
69
70 if(out != NULL) {
71 *result = strdup(out);
72 CodesetsFreeA(out, NULL);
73 } else {
75 }
76
77 return NSERROR_OK;
78}
79
80nserror utf8_from_local_encoding(const char *string, size_t len, char **result)
81{
82 if(__builtin_expect((CodesetsBase == NULL), 0)) {
83 return utf8_from_enc(string, nsoption_charp(local_charset), len, result, NULL);
84 } else {
85 return ami_utf8_codesets(string, len, result, false);
86 }
87}
88
89nserror utf8_to_local_encoding(const char *string, size_t len, char **result)
90{
91 if(__builtin_expect((CodesetsBase == NULL), 0)) {
93 char *local_charset = ASPrintf("%s//IGNORE", nsoption_charp(local_charset));
94 if(local_charset) {
95 err = utf8_to_enc(string, local_charset, len, result);
96 FreeVec(local_charset);
97 }
98 return err;
99 } else {
100 return ami_utf8_codesets(string, len, result, true);
101 }
102}
103
104void ami_utf8_free(char *ptr)
105{
106 if(ptr) free(ptr);
107}
108
109char *ami_utf8_easy(const char *string)
110{
111 char *localtext;
112 if(utf8_to_local_encoding(string, strlen(string), &localtext) == NSERROR_OK) {
113 return localtext;
114 } else {
115 return strdup(string);
116 }
117}
118
119char *ami_to_utf8_easy(const char *string)
120{
121 char *localtext;
122
123 if(utf8_from_local_encoding(string, strlen(string), &localtext) == NSERROR_OK) {
124 return localtext;
125 } else {
126 return strdup(string);
127 }
128}
129
130
131static struct gui_utf8_table utf8_table = {
133 .local_to_utf8 = utf8_from_local_encoding,
134};
135
137
STATIC char result[100]
Definition: arexx.c:77
nserror
Enumeration of error codes.
Definition: errors.h:29
@ NSERROR_BAD_ENCODING
The character set is unknown.
Definition: errors.h:45
@ NSERROR_NOMEM
Memory exhaustion.
Definition: errors.h:32
@ NSERROR_OK
No error.
Definition: errors.h:30
static nserror ami_utf8_codesets(const char *string, size_t len, char **result, bool to_local)
Definition: utf8.c:35
void ami_utf8_free(char *ptr)
Definition: utf8.c:104
static struct gui_utf8_table utf8_table
Definition: utf8.c:131
struct gui_utf8_table * amiga_utf8_table
Definition: utf8.c:136
nserror utf8_to_local_encoding(const char *string, size_t len, char **result)
Definition: utf8.c:89
char * ami_to_utf8_easy(const char *string)
Definition: utf8.c:119
char * ami_utf8_easy(const char *string)
Definition: utf8.c:109
nserror utf8_from_local_encoding(const char *string, size_t len, char **result)
Definition: utf8.c:80
Interface to platform-specific utf8 operations.
char * ASPrintf(const char *fmt,...)
Definition: os3support.c:139
Minimal compatibility header for AmigaOS 3.
Interface to utility string handling.
User interface utf8 characterset conversion routines.
Definition: utf8.h:31
nserror(* utf8_to_local)(const char *string, size_t len, char **result)
Convert a UTF-8 encoded string into the system local encoding.
Definition: utf8.h:40
Option reading and saving interface.
#define nsoption_charp(OPTION)
Get the value of a string option.
Definition: nsoption.h:297
#define nsoption_int(OPTION)
Get the value of an integer option.
Definition: nsoption.h:279
nserror utf8_from_enc(const char *string, const char *encname, size_t len, char **result, size_t *result_len)
Convert a string in the named encoding into a UTF-8 string.
Definition: utf8.c:321
nserror utf8_to_enc(const char *string, const char *encname, size_t len, char **result)
Convert a UTF8 string into the named encoding.
Definition: utf8.c:314
UTF-8 manipulation functions (interface).