File: | _build/../ctksourceview/ctksourceencoding.c |
Warning: | line 661, column 40 Casting a non-structure type to a structure type and accessing a field can lead to memory access errors or data corruption |
Press '?' to see keyboard shortcuts
Keyboard shortcuts:
1 | /* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8; coding: utf-8 -*- */ |
2 | /* |
3 | * This file is part of CtkSourceView |
4 | * |
5 | * Copyright (C) 2002-2005 - Paolo Maggi |
6 | * Copyright (C) 2014, 2015 - Sébastien Wilmet <swilmet@gnome.org> |
7 | * |
8 | * CtkSourceView is free software; you can redistribute it and/or |
9 | * modify it under the terms of the GNU Lesser General Public |
10 | * License as published by the Free Software Foundation; either |
11 | * version 2.1 of the License, or (at your option) any later version. |
12 | * |
13 | * CtkSourceView is distributed in the hope that it will be useful, |
14 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
15 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
16 | * Lesser General Public License for more details. |
17 | * |
18 | * You should have received a copy of the GNU Lesser General Public License |
19 | * along with this library; if not, see <http://www.gnu.org/licenses/>. |
20 | */ |
21 | |
22 | #ifdef HAVE_CONFIG_H1 |
23 | #include <config.h> |
24 | #endif |
25 | |
26 | #include "ctksourceencoding.h" |
27 | #include "ctksourceencoding-private.h" |
28 | #include <glib/gi18n-lib.h> |
29 | |
30 | /** |
31 | * SECTION:encoding |
32 | * @Short_description: Character encoding |
33 | * @Title: CtkSourceEncoding |
34 | * @See_also: #CtkSourceFileSaver, #CtkSourceFileLoader |
35 | * |
36 | * The #CtkSourceEncoding boxed type represents a character encoding. It is used |
37 | * for example by #CtkSourceFile. Note that the text in CTK+ widgets is always |
38 | * encoded in UTF-8. |
39 | */ |
40 | |
41 | struct _CtkSourceEncoding |
42 | { |
43 | gint index; |
44 | const gchar *charset; |
45 | const gchar *name; |
46 | }; |
47 | |
48 | G_DEFINE_BOXED_TYPE (CtkSourceEncoding, ctk_source_encoding,static GType ctk_source_encoding_get_type_once (void); GType ctk_source_encoding_get_type (void) { static gsize static_g_define_type_id = 0; if ((__extension__ ({ _Static_assert (sizeof *(&static_g_define_type_id) == sizeof (gpointer), "Expression evaluates to false"); (void) ( 0 ? (gpointer) *(&static_g_define_type_id) : ((void*)0)); (!(__extension__ ({ _Static_assert (sizeof *(&static_g_define_type_id ) == sizeof (gpointer), "Expression evaluates to false"); __typeof__ (*(&static_g_define_type_id)) gapg_temp_newval; __typeof__ ((&static_g_define_type_id)) gapg_temp_atomic = (&static_g_define_type_id ); __atomic_load (gapg_temp_atomic, &gapg_temp_newval, 5) ; gapg_temp_newval; })) && g_once_init_enter (&static_g_define_type_id )); }))) { GType g_define_type_id = ctk_source_encoding_get_type_once (); (__extension__ ({ _Static_assert (sizeof *(&static_g_define_type_id ) == sizeof (gpointer), "Expression evaluates to false"); 0 ? (void) (*(&static_g_define_type_id) = (g_define_type_id) ) : (void) 0; g_once_init_leave ((&static_g_define_type_id ), (gsize) (g_define_type_id)); })); } return static_g_define_type_id ; } __attribute__ ((__noinline__)) static GType ctk_source_encoding_get_type_once (void) { GType (* _g_register_boxed) (const gchar *, union { CtkSourceEncoding * (*do_copy_type) (CtkSourceEncoding *); CtkSourceEncoding * (*do_const_copy_type) (const CtkSourceEncoding *); GBoxedCopyFunc do_copy_boxed; } __attribute__((__transparent_union__)), union { void (* do_free_type) (CtkSourceEncoding *); GBoxedFreeFunc do_free_boxed; } __attribute__((__transparent_union__)) ) = g_boxed_type_register_static ; GType g_define_type_id = _g_register_boxed (g_intern_static_string ("CtkSourceEncoding"), ctk_source_encoding_copy, ctk_source_encoding_free ); { {{};} } return g_define_type_id; } |
49 | ctk_source_encoding_copy,static GType ctk_source_encoding_get_type_once (void); GType ctk_source_encoding_get_type (void) { static gsize static_g_define_type_id = 0; if ((__extension__ ({ _Static_assert (sizeof *(&static_g_define_type_id) == sizeof (gpointer), "Expression evaluates to false"); (void) ( 0 ? (gpointer) *(&static_g_define_type_id) : ((void*)0)); (!(__extension__ ({ _Static_assert (sizeof *(&static_g_define_type_id ) == sizeof (gpointer), "Expression evaluates to false"); __typeof__ (*(&static_g_define_type_id)) gapg_temp_newval; __typeof__ ((&static_g_define_type_id)) gapg_temp_atomic = (&static_g_define_type_id ); __atomic_load (gapg_temp_atomic, &gapg_temp_newval, 5) ; gapg_temp_newval; })) && g_once_init_enter (&static_g_define_type_id )); }))) { GType g_define_type_id = ctk_source_encoding_get_type_once (); (__extension__ ({ _Static_assert (sizeof *(&static_g_define_type_id ) == sizeof (gpointer), "Expression evaluates to false"); 0 ? (void) (*(&static_g_define_type_id) = (g_define_type_id) ) : (void) 0; g_once_init_leave ((&static_g_define_type_id ), (gsize) (g_define_type_id)); })); } return static_g_define_type_id ; } __attribute__ ((__noinline__)) static GType ctk_source_encoding_get_type_once (void) { GType (* _g_register_boxed) (const gchar *, union { CtkSourceEncoding * (*do_copy_type) (CtkSourceEncoding *); CtkSourceEncoding * (*do_const_copy_type) (const CtkSourceEncoding *); GBoxedCopyFunc do_copy_boxed; } __attribute__((__transparent_union__)), union { void (* do_free_type) (CtkSourceEncoding *); GBoxedFreeFunc do_free_boxed; } __attribute__((__transparent_union__)) ) = g_boxed_type_register_static ; GType g_define_type_id = _g_register_boxed (g_intern_static_string ("CtkSourceEncoding"), ctk_source_encoding_copy, ctk_source_encoding_free ); { {{};} } return g_define_type_id; } |
50 | ctk_source_encoding_free)static GType ctk_source_encoding_get_type_once (void); GType ctk_source_encoding_get_type (void) { static gsize static_g_define_type_id = 0; if ((__extension__ ({ _Static_assert (sizeof *(&static_g_define_type_id) == sizeof (gpointer), "Expression evaluates to false"); (void) ( 0 ? (gpointer) *(&static_g_define_type_id) : ((void*)0)); (!(__extension__ ({ _Static_assert (sizeof *(&static_g_define_type_id ) == sizeof (gpointer), "Expression evaluates to false"); __typeof__ (*(&static_g_define_type_id)) gapg_temp_newval; __typeof__ ((&static_g_define_type_id)) gapg_temp_atomic = (&static_g_define_type_id ); __atomic_load (gapg_temp_atomic, &gapg_temp_newval, 5) ; gapg_temp_newval; })) && g_once_init_enter (&static_g_define_type_id )); }))) { GType g_define_type_id = ctk_source_encoding_get_type_once (); (__extension__ ({ _Static_assert (sizeof *(&static_g_define_type_id ) == sizeof (gpointer), "Expression evaluates to false"); 0 ? (void) (*(&static_g_define_type_id) = (g_define_type_id) ) : (void) 0; g_once_init_leave ((&static_g_define_type_id ), (gsize) (g_define_type_id)); })); } return static_g_define_type_id ; } __attribute__ ((__noinline__)) static GType ctk_source_encoding_get_type_once (void) { GType (* _g_register_boxed) (const gchar *, union { CtkSourceEncoding * (*do_copy_type) (CtkSourceEncoding *); CtkSourceEncoding * (*do_const_copy_type) (const CtkSourceEncoding *); GBoxedCopyFunc do_copy_boxed; } __attribute__((__transparent_union__)), union { void (* do_free_type) (CtkSourceEncoding *); GBoxedFreeFunc do_free_boxed; } __attribute__((__transparent_union__)) ) = g_boxed_type_register_static ; GType g_define_type_id = _g_register_boxed (g_intern_static_string ("CtkSourceEncoding"), ctk_source_encoding_copy, ctk_source_encoding_free ); { {{};} } return g_define_type_id; } |
51 | |
52 | /* |
53 | * The original versions of the following tables are taken from profterm |
54 | * |
55 | * Copyright (C) 2002 Red Hat, Inc. |
56 | */ |
57 | |
58 | typedef enum _CtkSourceEncodingIndex |
59 | { |
60 | CTK_SOURCE_ENCODING_ISO_8859_1, |
61 | CTK_SOURCE_ENCODING_ISO_8859_2, |
62 | CTK_SOURCE_ENCODING_ISO_8859_3, |
63 | CTK_SOURCE_ENCODING_ISO_8859_4, |
64 | CTK_SOURCE_ENCODING_ISO_8859_5, |
65 | CTK_SOURCE_ENCODING_ISO_8859_6, |
66 | CTK_SOURCE_ENCODING_ISO_8859_7, |
67 | CTK_SOURCE_ENCODING_ISO_8859_8, |
68 | CTK_SOURCE_ENCODING_ISO_8859_9, |
69 | CTK_SOURCE_ENCODING_ISO_8859_10, |
70 | CTK_SOURCE_ENCODING_ISO_8859_13, |
71 | CTK_SOURCE_ENCODING_ISO_8859_14, |
72 | CTK_SOURCE_ENCODING_ISO_8859_15, |
73 | CTK_SOURCE_ENCODING_ISO_8859_16, |
74 | |
75 | CTK_SOURCE_ENCODING_UTF_7, |
76 | CTK_SOURCE_ENCODING_UTF_16, |
77 | CTK_SOURCE_ENCODING_UTF_16_BE, |
78 | CTK_SOURCE_ENCODING_UTF_16_LE, |
79 | CTK_SOURCE_ENCODING_UTF_32, |
80 | CTK_SOURCE_ENCODING_UCS_2, |
81 | CTK_SOURCE_ENCODING_UCS_4, |
82 | |
83 | CTK_SOURCE_ENCODING_ARMSCII_8, |
84 | CTK_SOURCE_ENCODING_BIG5, |
85 | CTK_SOURCE_ENCODING_BIG5_HKSCS, |
86 | CTK_SOURCE_ENCODING_CP_866, |
87 | |
88 | CTK_SOURCE_ENCODING_EUC_JP, |
89 | CTK_SOURCE_ENCODING_EUC_JP_MS, |
90 | CTK_SOURCE_ENCODING_CP932, |
91 | CTK_SOURCE_ENCODING_EUC_KR, |
92 | CTK_SOURCE_ENCODING_EUC_TW, |
93 | |
94 | CTK_SOURCE_ENCODING_GB18030, |
95 | CTK_SOURCE_ENCODING_GB2312, |
96 | CTK_SOURCE_ENCODING_GBK, |
97 | CTK_SOURCE_ENCODING_GEOSTD8, |
98 | |
99 | CTK_SOURCE_ENCODING_IBM_850, |
100 | CTK_SOURCE_ENCODING_IBM_852, |
101 | CTK_SOURCE_ENCODING_IBM_855, |
102 | CTK_SOURCE_ENCODING_IBM_857, |
103 | CTK_SOURCE_ENCODING_IBM_862, |
104 | CTK_SOURCE_ENCODING_IBM_864, |
105 | |
106 | CTK_SOURCE_ENCODING_ISO_2022_JP, |
107 | CTK_SOURCE_ENCODING_ISO_2022_KR, |
108 | CTK_SOURCE_ENCODING_ISO_IR_111, |
109 | CTK_SOURCE_ENCODING_JOHAB, |
110 | CTK_SOURCE_ENCODING_KOI8_R, |
111 | CTK_SOURCE_ENCODING_KOI8__R, |
112 | CTK_SOURCE_ENCODING_KOI8_U, |
113 | |
114 | CTK_SOURCE_ENCODING_SHIFT_JIS, |
115 | CTK_SOURCE_ENCODING_TCVN, |
116 | CTK_SOURCE_ENCODING_TIS_620, |
117 | CTK_SOURCE_ENCODING_UHC, |
118 | CTK_SOURCE_ENCODING_VISCII, |
119 | |
120 | CTK_SOURCE_ENCODING_WINDOWS_1250, |
121 | CTK_SOURCE_ENCODING_WINDOWS_1251, |
122 | CTK_SOURCE_ENCODING_WINDOWS_1252, |
123 | CTK_SOURCE_ENCODING_WINDOWS_1253, |
124 | CTK_SOURCE_ENCODING_WINDOWS_1254, |
125 | CTK_SOURCE_ENCODING_WINDOWS_1255, |
126 | CTK_SOURCE_ENCODING_WINDOWS_1256, |
127 | CTK_SOURCE_ENCODING_WINDOWS_1257, |
128 | CTK_SOURCE_ENCODING_WINDOWS_1258, |
129 | |
130 | CTK_SOURCE_ENCODING_LAST, |
131 | |
132 | CTK_SOURCE_ENCODING_UTF_8, |
133 | CTK_SOURCE_ENCODING_UNKNOWN |
134 | } CtkSourceEncodingIndex; |
135 | |
136 | static const CtkSourceEncoding utf8_encoding = |
137 | { |
138 | CTK_SOURCE_ENCODING_UTF_8, |
139 | "UTF-8", |
140 | N_("Unicode")("Unicode") |
141 | }; |
142 | |
143 | /* Initialized in ctk_source_encoding_lazy_init(). */ |
144 | static CtkSourceEncoding unknown_encoding = |
145 | { |
146 | CTK_SOURCE_ENCODING_UNKNOWN, |
147 | NULL((void*)0), |
148 | NULL((void*)0) |
149 | }; |
150 | |
151 | static const CtkSourceEncoding encodings[] = |
152 | { |
153 | { CTK_SOURCE_ENCODING_ISO_8859_1, |
154 | "ISO-8859-1", N_("Western")("Western") }, |
155 | { CTK_SOURCE_ENCODING_ISO_8859_2, |
156 | "ISO-8859-2", N_("Central European")("Central European") }, |
157 | { CTK_SOURCE_ENCODING_ISO_8859_3, |
158 | "ISO-8859-3", N_("South European")("South European") }, |
159 | { CTK_SOURCE_ENCODING_ISO_8859_4, |
160 | "ISO-8859-4", N_("Baltic")("Baltic") }, |
161 | { CTK_SOURCE_ENCODING_ISO_8859_5, |
162 | "ISO-8859-5", N_("Cyrillic")("Cyrillic") }, |
163 | { CTK_SOURCE_ENCODING_ISO_8859_6, |
164 | "ISO-8859-6", N_("Arabic")("Arabic") }, |
165 | { CTK_SOURCE_ENCODING_ISO_8859_7, |
166 | "ISO-8859-7", N_("Greek")("Greek") }, |
167 | { CTK_SOURCE_ENCODING_ISO_8859_8, |
168 | "ISO-8859-8", N_("Hebrew Visual")("Hebrew Visual") }, |
169 | { CTK_SOURCE_ENCODING_ISO_8859_9, |
170 | "ISO-8859-9", N_("Turkish")("Turkish") }, |
171 | { CTK_SOURCE_ENCODING_ISO_8859_10, |
172 | "ISO-8859-10", N_("Nordic")("Nordic") }, |
173 | { CTK_SOURCE_ENCODING_ISO_8859_13, |
174 | "ISO-8859-13", N_("Baltic")("Baltic") }, |
175 | { CTK_SOURCE_ENCODING_ISO_8859_14, |
176 | "ISO-8859-14", N_("Celtic")("Celtic") }, |
177 | { CTK_SOURCE_ENCODING_ISO_8859_15, |
178 | "ISO-8859-15", N_("Western")("Western") }, |
179 | { CTK_SOURCE_ENCODING_ISO_8859_16, |
180 | "ISO-8859-16", N_("Romanian")("Romanian") }, |
181 | |
182 | { CTK_SOURCE_ENCODING_UTF_7, |
183 | "UTF-7", N_("Unicode")("Unicode") }, |
184 | { CTK_SOURCE_ENCODING_UTF_16, |
185 | "UTF-16", N_("Unicode")("Unicode") }, |
186 | { CTK_SOURCE_ENCODING_UTF_16_BE, |
187 | "UTF-16BE", N_("Unicode")("Unicode") }, |
188 | { CTK_SOURCE_ENCODING_UTF_16_LE, |
189 | "UTF-16LE", N_("Unicode")("Unicode") }, |
190 | { CTK_SOURCE_ENCODING_UTF_32, |
191 | "UTF-32", N_("Unicode")("Unicode") }, |
192 | { CTK_SOURCE_ENCODING_UCS_2, |
193 | "UCS-2", N_("Unicode")("Unicode") }, |
194 | { CTK_SOURCE_ENCODING_UCS_4, |
195 | "UCS-4", N_("Unicode")("Unicode") }, |
196 | |
197 | { CTK_SOURCE_ENCODING_ARMSCII_8, |
198 | "ARMSCII-8", N_("Armenian")("Armenian") }, |
199 | { CTK_SOURCE_ENCODING_BIG5, |
200 | "BIG5", N_("Chinese Traditional")("Chinese Traditional") }, |
201 | { CTK_SOURCE_ENCODING_BIG5_HKSCS, |
202 | "BIG5-HKSCS", N_("Chinese Traditional")("Chinese Traditional") }, |
203 | { CTK_SOURCE_ENCODING_CP_866, |
204 | "CP866", N_("Cyrillic/Russian")("Cyrillic/Russian") }, |
205 | |
206 | { CTK_SOURCE_ENCODING_EUC_JP, |
207 | "EUC-JP", N_("Japanese")("Japanese") }, |
208 | { CTK_SOURCE_ENCODING_EUC_JP_MS, |
209 | "EUC-JP-MS", N_("Japanese")("Japanese") }, |
210 | { CTK_SOURCE_ENCODING_CP932, |
211 | "CP932", N_("Japanese")("Japanese") }, |
212 | |
213 | { CTK_SOURCE_ENCODING_EUC_KR, |
214 | "EUC-KR", N_("Korean")("Korean") }, |
215 | { CTK_SOURCE_ENCODING_EUC_TW, |
216 | "EUC-TW", N_("Chinese Traditional")("Chinese Traditional") }, |
217 | |
218 | { CTK_SOURCE_ENCODING_GB18030, |
219 | "GB18030", N_("Chinese Simplified")("Chinese Simplified") }, |
220 | { CTK_SOURCE_ENCODING_GB2312, |
221 | "GB2312", N_("Chinese Simplified")("Chinese Simplified") }, |
222 | { CTK_SOURCE_ENCODING_GBK, |
223 | "GBK", N_("Chinese Simplified")("Chinese Simplified") }, |
224 | { CTK_SOURCE_ENCODING_GEOSTD8, |
225 | "GEORGIAN-ACADEMY", N_("Georgian")("Georgian") }, /* FIXME GEOSTD8 ? */ |
226 | |
227 | { CTK_SOURCE_ENCODING_IBM_850, |
228 | "IBM850", N_("Western")("Western") }, |
229 | { CTK_SOURCE_ENCODING_IBM_852, |
230 | "IBM852", N_("Central European")("Central European") }, |
231 | { CTK_SOURCE_ENCODING_IBM_855, |
232 | "IBM855", N_("Cyrillic")("Cyrillic") }, |
233 | { CTK_SOURCE_ENCODING_IBM_857, |
234 | "IBM857", N_("Turkish")("Turkish") }, |
235 | { CTK_SOURCE_ENCODING_IBM_862, |
236 | "IBM862", N_("Hebrew")("Hebrew") }, |
237 | { CTK_SOURCE_ENCODING_IBM_864, |
238 | "IBM864", N_("Arabic")("Arabic") }, |
239 | |
240 | { CTK_SOURCE_ENCODING_ISO_2022_JP, |
241 | "ISO-2022-JP", N_("Japanese")("Japanese") }, |
242 | { CTK_SOURCE_ENCODING_ISO_2022_KR, |
243 | "ISO-2022-KR", N_("Korean")("Korean") }, |
244 | { CTK_SOURCE_ENCODING_ISO_IR_111, |
245 | "ISO-IR-111", N_("Cyrillic")("Cyrillic") }, |
246 | { CTK_SOURCE_ENCODING_JOHAB, |
247 | "JOHAB", N_("Korean")("Korean") }, |
248 | { CTK_SOURCE_ENCODING_KOI8_R, |
249 | "KOI8R", N_("Cyrillic")("Cyrillic") }, |
250 | { CTK_SOURCE_ENCODING_KOI8__R, |
251 | "KOI8-R", N_("Cyrillic")("Cyrillic") }, |
252 | { CTK_SOURCE_ENCODING_KOI8_U, |
253 | "KOI8U", N_("Cyrillic/Ukrainian")("Cyrillic/Ukrainian") }, |
254 | |
255 | { CTK_SOURCE_ENCODING_SHIFT_JIS, |
256 | "SHIFT_JIS", N_("Japanese")("Japanese") }, |
257 | { CTK_SOURCE_ENCODING_TCVN, |
258 | "TCVN", N_("Vietnamese")("Vietnamese") }, |
259 | { CTK_SOURCE_ENCODING_TIS_620, |
260 | "TIS-620", N_("Thai")("Thai") }, |
261 | { CTK_SOURCE_ENCODING_UHC, |
262 | "UHC", N_("Korean")("Korean") }, |
263 | { CTK_SOURCE_ENCODING_VISCII, |
264 | "VISCII", N_("Vietnamese")("Vietnamese") }, |
265 | |
266 | { CTK_SOURCE_ENCODING_WINDOWS_1250, |
267 | "WINDOWS-1250", N_("Central European")("Central European") }, |
268 | { CTK_SOURCE_ENCODING_WINDOWS_1251, |
269 | "WINDOWS-1251", N_("Cyrillic")("Cyrillic") }, |
270 | { CTK_SOURCE_ENCODING_WINDOWS_1252, |
271 | "WINDOWS-1252", N_("Western")("Western") }, |
272 | { CTK_SOURCE_ENCODING_WINDOWS_1253, |
273 | "WINDOWS-1253", N_("Greek")("Greek") }, |
274 | { CTK_SOURCE_ENCODING_WINDOWS_1254, |
275 | "WINDOWS-1254", N_("Turkish")("Turkish") }, |
276 | { CTK_SOURCE_ENCODING_WINDOWS_1255, |
277 | "WINDOWS-1255", N_("Hebrew")("Hebrew") }, |
278 | { CTK_SOURCE_ENCODING_WINDOWS_1256, |
279 | "WINDOWS-1256", N_("Arabic")("Arabic") }, |
280 | { CTK_SOURCE_ENCODING_WINDOWS_1257, |
281 | "WINDOWS-1257", N_("Baltic")("Baltic") }, |
282 | { CTK_SOURCE_ENCODING_WINDOWS_1258, |
283 | "WINDOWS-1258", N_("Vietnamese")("Vietnamese") } |
284 | }; |
285 | |
286 | static void |
287 | ctk_source_encoding_lazy_init (void) |
288 | { |
289 | static gboolean initialized = FALSE(0); |
290 | const gchar *locale_charset; |
291 | |
292 | if (G_LIKELY (initialized)(__builtin_expect (__extension__ ({ int _g_boolean_var_21 = 0 ; if (initialized) _g_boolean_var_21 = 1; _g_boolean_var_21; } ), 1))) |
293 | { |
294 | return; |
295 | } |
296 | |
297 | if (g_get_charset (&locale_charset) == FALSE(0)) |
298 | { |
299 | unknown_encoding.charset = g_strdup (locale_charset)g_strdup_inline (locale_charset); |
300 | } |
301 | |
302 | initialized = TRUE(!(0)); |
303 | } |
304 | |
305 | /** |
306 | * ctk_source_encoding_get_from_charset: |
307 | * @charset: a character set. |
308 | * |
309 | * Gets a #CtkSourceEncoding from a character set such as "UTF-8" or |
310 | * "ISO-8859-1". |
311 | * |
312 | * Returns: (nullable): the corresponding #CtkSourceEncoding, or %NULL |
313 | * if not found. |
314 | * Since: 3.14 |
315 | */ |
316 | const CtkSourceEncoding * |
317 | ctk_source_encoding_get_from_charset (const gchar *charset) |
318 | { |
319 | gint i; |
320 | |
321 | g_return_val_if_fail (charset != NULL, NULL)do { if ((__builtin_expect (__extension__ ({ int _g_boolean_var_22 = 0; if (charset != ((void*)0)) _g_boolean_var_22 = 1; _g_boolean_var_22 ; }), 1))) { } else { g_return_if_fail_warning ("CtkSourceView" , ((const char*) (__func__)), "charset != NULL"); return (((void *)0)); } } while (0); |
322 | |
323 | if (g_ascii_strcasecmp (charset, "UTF-8") == 0) |
324 | { |
325 | return ctk_source_encoding_get_utf8 (); |
326 | } |
327 | |
328 | for (i = 0; i < CTK_SOURCE_ENCODING_LAST; i++) |
329 | { |
330 | if (g_ascii_strcasecmp (charset, encodings[i].charset) == 0) |
331 | { |
332 | return &encodings[i]; |
333 | } |
334 | } |
335 | |
336 | ctk_source_encoding_lazy_init (); |
337 | |
338 | if (unknown_encoding.charset != NULL((void*)0) && |
339 | g_ascii_strcasecmp (charset, unknown_encoding.charset) == 0) |
340 | { |
341 | return &unknown_encoding; |
342 | } |
343 | |
344 | return NULL((void*)0); |
345 | } |
346 | |
347 | /** |
348 | * ctk_source_encoding_get_all: |
349 | * |
350 | * Gets all encodings. |
351 | * |
352 | * Returns: (transfer container) (element-type CtkSource.Encoding): a list of |
353 | * all #CtkSourceEncoding's. Free with g_slist_free(). |
354 | * Since: 3.14 |
355 | */ |
356 | GSList * |
357 | ctk_source_encoding_get_all (void) |
358 | { |
359 | GSList *all = NULL((void*)0); |
360 | gint i; |
361 | |
362 | for (i = CTK_SOURCE_ENCODING_LAST - 1; i >= 0; i--) |
363 | { |
364 | all = g_slist_prepend (all, (gpointer) &encodings[i]); |
365 | } |
366 | |
367 | all = g_slist_prepend (all, (gpointer) &utf8_encoding); |
368 | |
369 | return all; |
370 | } |
371 | |
372 | /** |
373 | * ctk_source_encoding_get_utf8: |
374 | * |
375 | * Returns: the UTF-8 encoding. |
376 | * Since: 3.14 |
377 | */ |
378 | const CtkSourceEncoding * |
379 | ctk_source_encoding_get_utf8 (void) |
380 | { |
381 | return &utf8_encoding; |
382 | } |
383 | |
384 | /** |
385 | * ctk_source_encoding_get_current: |
386 | * |
387 | * Gets the #CtkSourceEncoding for the current locale. See also g_get_charset(). |
388 | * |
389 | * Returns: the current locale encoding. |
390 | * Since: 3.14 |
391 | */ |
392 | const CtkSourceEncoding * |
393 | ctk_source_encoding_get_current (void) |
394 | { |
395 | static gboolean initialized = FALSE(0); |
396 | static const CtkSourceEncoding *locale_encoding = NULL((void*)0); |
397 | |
398 | const gchar *locale_charset; |
399 | |
400 | ctk_source_encoding_lazy_init (); |
401 | |
402 | if (G_LIKELY (initialized)(__builtin_expect (__extension__ ({ int _g_boolean_var_23 = 0 ; if (initialized) _g_boolean_var_23 = 1; _g_boolean_var_23; } ), 1))) |
403 | { |
404 | return locale_encoding; |
405 | } |
406 | |
407 | if (g_get_charset (&locale_charset)) |
408 | { |
409 | locale_encoding = &utf8_encoding; |
410 | } |
411 | else |
412 | { |
413 | locale_encoding = ctk_source_encoding_get_from_charset (locale_charset); |
414 | } |
415 | |
416 | if (locale_encoding == NULL((void*)0)) |
417 | { |
418 | locale_encoding = &unknown_encoding; |
419 | } |
420 | |
421 | initialized = TRUE(!(0)); |
422 | |
423 | return locale_encoding; |
424 | } |
425 | |
426 | /** |
427 | * ctk_source_encoding_to_string: |
428 | * @enc: a #CtkSourceEncoding. |
429 | * |
430 | * Returns: a string representation. Free with g_free() when no longer needed. |
431 | * Since: 3.14 |
432 | */ |
433 | gchar * |
434 | ctk_source_encoding_to_string (const CtkSourceEncoding* enc) |
435 | { |
436 | g_return_val_if_fail (enc != NULL, NULL)do { if ((__builtin_expect (__extension__ ({ int _g_boolean_var_24 = 0; if (enc != ((void*)0)) _g_boolean_var_24 = 1; _g_boolean_var_24 ; }), 1))) { } else { g_return_if_fail_warning ("CtkSourceView" , ((const char*) (__func__)), "enc != NULL"); return (((void* )0)); } } while (0); |
437 | |
438 | ctk_source_encoding_lazy_init (); |
439 | |
440 | g_return_val_if_fail (enc->charset != NULL, NULL)do { if ((__builtin_expect (__extension__ ({ int _g_boolean_var_25 = 0; if (enc->charset != ((void*)0)) _g_boolean_var_25 = 1 ; _g_boolean_var_25; }), 1))) { } else { g_return_if_fail_warning ("CtkSourceView", ((const char*) (__func__)), "enc->charset != NULL" ); return (((void*)0)); } } while (0); |
441 | |
442 | if (enc->name != NULL((void*)0)) |
443 | { |
444 | return g_strdup_printf ("%s (%s)", _(enc->name)((char *) g_dgettext ("ctksourceview-4", enc->name)), enc->charset); |
445 | } |
446 | else if (g_ascii_strcasecmp (enc->charset, "ANSI_X3.4-1968") == 0) |
447 | { |
448 | return g_strdup_printf ("US-ASCII (%s)", enc->charset); |
449 | } |
450 | else |
451 | { |
452 | return g_strdup (enc->charset)g_strdup_inline (enc->charset); |
453 | } |
454 | } |
455 | |
456 | /** |
457 | * ctk_source_encoding_get_charset: |
458 | * @enc: a #CtkSourceEncoding. |
459 | * |
460 | * Gets the character set of the #CtkSourceEncoding, such as "UTF-8" or |
461 | * "ISO-8859-1". |
462 | * |
463 | * Returns: the character set of the #CtkSourceEncoding. |
464 | * Since: 3.14 |
465 | */ |
466 | const gchar * |
467 | ctk_source_encoding_get_charset (const CtkSourceEncoding* enc) |
468 | { |
469 | g_return_val_if_fail (enc != NULL, NULL)do { if ((__builtin_expect (__extension__ ({ int _g_boolean_var_26 = 0; if (enc != ((void*)0)) _g_boolean_var_26 = 1; _g_boolean_var_26 ; }), 1))) { } else { g_return_if_fail_warning ("CtkSourceView" , ((const char*) (__func__)), "enc != NULL"); return (((void* )0)); } } while (0); |
470 | |
471 | ctk_source_encoding_lazy_init (); |
472 | |
473 | g_return_val_if_fail (enc->charset != NULL, NULL)do { if ((__builtin_expect (__extension__ ({ int _g_boolean_var_27 = 0; if (enc->charset != ((void*)0)) _g_boolean_var_27 = 1 ; _g_boolean_var_27; }), 1))) { } else { g_return_if_fail_warning ("CtkSourceView", ((const char*) (__func__)), "enc->charset != NULL" ); return (((void*)0)); } } while (0); |
474 | |
475 | return enc->charset; |
476 | } |
477 | |
478 | /** |
479 | * ctk_source_encoding_get_name: |
480 | * @enc: a #CtkSourceEncoding. |
481 | * |
482 | * Gets the name of the #CtkSourceEncoding such as "Unicode" or "Western". |
483 | * |
484 | * Returns: the name of the #CtkSourceEncoding. |
485 | * Since: 3.14 |
486 | */ |
487 | const gchar * |
488 | ctk_source_encoding_get_name (const CtkSourceEncoding* enc) |
489 | { |
490 | g_return_val_if_fail (enc != NULL, NULL)do { if ((__builtin_expect (__extension__ ({ int _g_boolean_var_28 = 0; if (enc != ((void*)0)) _g_boolean_var_28 = 1; _g_boolean_var_28 ; }), 1))) { } else { g_return_if_fail_warning ("CtkSourceView" , ((const char*) (__func__)), "enc != NULL"); return (((void* )0)); } } while (0); |
491 | |
492 | ctk_source_encoding_lazy_init (); |
493 | |
494 | return (enc->name == NULL((void*)0)) ? _("Unknown")((char *) g_dgettext ("ctksourceview-4", "Unknown")) : _(enc->name)((char *) g_dgettext ("ctksourceview-4", enc->name)); |
495 | } |
496 | |
497 | static GSList * |
498 | strv_to_list (const gchar * const *enc_str) |
499 | { |
500 | GSList *res = NULL((void*)0); |
501 | gchar **p; |
502 | |
503 | for (p = (gchar **)enc_str; p != NULL((void*)0) && *p != NULL((void*)0); p++) |
504 | { |
505 | const gchar *charset = *p; |
506 | const CtkSourceEncoding *enc; |
507 | |
508 | if (g_str_equal (charset, "CURRENT")(strcmp ((const char *) (charset), (const char *) ("CURRENT") ) == 0)) |
509 | { |
510 | g_get_charset (&charset); |
511 | } |
512 | |
513 | g_return_val_if_fail (charset != NULL, NULL)do { if ((__builtin_expect (__extension__ ({ int _g_boolean_var_29 = 0; if (charset != ((void*)0)) _g_boolean_var_29 = 1; _g_boolean_var_29 ; }), 1))) { } else { g_return_if_fail_warning ("CtkSourceView" , ((const char*) (__func__)), "charset != NULL"); return (((void *)0)); } } while (0); |
514 | enc = ctk_source_encoding_get_from_charset (charset); |
515 | |
516 | if (enc != NULL((void*)0) && |
517 | g_slist_find (res, enc) == NULL((void*)0)) |
518 | { |
519 | res = g_slist_prepend (res, (gpointer)enc); |
520 | } |
521 | } |
522 | |
523 | return g_slist_reverse (res); |
524 | } |
525 | |
526 | static GSList * |
527 | remove_duplicates_keep_first (GSList *list) |
528 | { |
529 | GSList *new_list = NULL((void*)0); |
530 | GSList *l; |
531 | |
532 | for (l = list; l != NULL((void*)0); l = l->next) |
533 | { |
534 | gpointer cur_encoding = l->data; |
535 | |
536 | if (g_slist_find (new_list, cur_encoding) == NULL((void*)0)) |
537 | { |
538 | new_list = g_slist_prepend (new_list, cur_encoding); |
539 | } |
540 | } |
541 | |
542 | new_list = g_slist_reverse (new_list); |
543 | |
544 | g_slist_free (list); |
545 | return new_list; |
546 | } |
547 | |
548 | static GSList * |
549 | remove_duplicates_keep_last (GSList *list) |
550 | { |
551 | GSList *new_list = NULL((void*)0); |
552 | GSList *l; |
553 | |
554 | list = g_slist_reverse (list); |
555 | |
556 | for (l = list; l != NULL((void*)0); l = l->next) |
557 | { |
558 | gpointer cur_encoding = l->data; |
559 | |
560 | if (g_slist_find (new_list, cur_encoding) == NULL((void*)0)) |
561 | { |
562 | new_list = g_slist_prepend (new_list, cur_encoding); |
563 | } |
564 | } |
565 | |
566 | g_slist_free (list); |
567 | return new_list; |
568 | } |
569 | |
570 | /* |
571 | * _ctk_source_encoding_remove_duplicates: |
572 | * @list: (element-type CtkSource.Encoding): a list of #CtkSourceEncoding's. |
573 | * @removal_type: the #CtkSourceEncodingDuplicates. |
574 | * |
575 | * A convenience function to remove duplicated encodings in a list. |
576 | * |
577 | * Returns: (transfer container) (element-type CtkSource.Encoding): the new |
578 | * start of the #GSList. |
579 | * Since: 3.14 |
580 | */ |
581 | GSList * |
582 | _ctk_source_encoding_remove_duplicates (GSList *list, |
583 | CtkSourceEncodingDuplicates removal_type) |
584 | { |
585 | switch (removal_type) |
586 | { |
587 | case CTK_SOURCE_ENCODING_DUPLICATES_KEEP_FIRST: |
588 | return remove_duplicates_keep_first (list); |
589 | |
590 | case CTK_SOURCE_ENCODING_DUPLICATES_KEEP_LAST: |
591 | return remove_duplicates_keep_last (list); |
592 | |
593 | default: |
594 | break; |
595 | } |
596 | |
597 | g_return_val_if_reached (list)do { g_log ("CtkSourceView", G_LOG_LEVEL_CRITICAL, "file %s: line %d (%s): should not be reached" , "../ctksourceview/ctksourceencoding.c", 597, ((const char*) (__func__))); return (list); } while (0); |
598 | } |
599 | |
600 | /** |
601 | * ctk_source_encoding_get_default_candidates: |
602 | * |
603 | * Gets the list of default candidate encodings to try when loading a file. See |
604 | * ctk_source_file_loader_set_candidate_encodings(). |
605 | * |
606 | * This function returns a different list depending on the current locale (i.e. |
607 | * language, country and default encoding). The UTF-8 encoding and the current |
608 | * locale encoding are guaranteed to be present in the returned list. |
609 | * |
610 | * Returns: (transfer container) (element-type CtkSource.Encoding): the list of |
611 | * default candidate encodings. Free with g_slist_free(). |
612 | * Since: 3.18 |
613 | */ |
614 | GSList * |
615 | ctk_source_encoding_get_default_candidates (void) |
616 | { |
617 | const gchar *encodings_str; |
618 | const gchar *encodings_str_translated; |
619 | GVariant *encodings_variant; |
620 | const gchar **encodings_strv; |
621 | GSList *encodings_list; |
622 | GError *error = NULL((void*)0); |
623 | |
624 | /* Translators: This is the sorted list of encodings used by |
625 | * CtkSourceView for automatic detection of the file encoding. You may |
626 | * want to customize it adding encodings that are common in your |
627 | * country, for instance the GB18030 encoding for the Chinese |
628 | * translation. You may also want to remove the ISO-8859-15 encoding |
629 | * (covering English and most Western European languages) if you think |
630 | * people in your country will rarely use it. "CURRENT" is a magic |
631 | * value used by CtkSourceView and it represents the encoding for the |
632 | * current locale, so please don't translate the "CURRENT" term. Only |
633 | * recognized encodings are used. See |
634 | * https://gitlab.gnome.org/GNOME/ctksourceview/blob/master/ctksourceview/ctksourceencoding.c#L142 |
635 | * for a list of supported encodings. |
636 | * Keep the same format: square brackets, single quotes, commas. |
637 | */ |
638 | encodings_str = N_("['UTF-8', 'CURRENT', 'ISO-8859-15', 'UTF-16']")("['UTF-8', 'CURRENT', 'ISO-8859-15', 'UTF-16']"); |
639 | |
640 | encodings_str_translated = _(encodings_str)((char *) g_dgettext ("ctksourceview-4", encodings_str)); |
641 | |
642 | encodings_variant = g_variant_parse (G_VARIANT_TYPE_STRING_ARRAY((const GVariantType *) "as"), |
643 | encodings_str_translated, |
644 | NULL((void*)0), |
645 | NULL((void*)0), |
646 | &error); |
647 | |
648 | if (error != NULL((void*)0)) |
649 | { |
650 | const gchar * const *language_names = g_get_language_names (); |
651 | |
652 | g_warning ("Error while parsing encodings list for locale %s:\n" |
653 | "Translated list: %s\n" |
654 | "Error message: %s", |
655 | language_names[0], |
656 | encodings_str_translated, |
657 | error->message); |
658 | |
659 | g_clear_error (&error); |
660 | |
661 | encodings_variant = g_variant_parse (G_VARIANT_TYPE_STRING_ARRAY((const GVariantType *) "as"), |
Casting a non-structure type to a structure type and accessing a field can lead to memory access errors or data corruption | |
662 | encodings_str, |
663 | NULL((void*)0), |
664 | NULL((void*)0), |
665 | &error); |
666 | |
667 | g_assert_no_error (error)do { if (error) g_assertion_message_error ("CtkSourceView", "../ctksourceview/ctksourceencoding.c" , 667, ((const char*) (__func__)), "error", error, 0, 0); } while (0); |
668 | } |
669 | |
670 | encodings_strv = g_variant_get_strv (encodings_variant, NULL((void*)0)); |
671 | encodings_list = strv_to_list (encodings_strv); |
672 | g_free ((gpointer) encodings_strv); |
673 | |
674 | /* Ensure that UTF-8 and CURRENT are present. */ |
675 | encodings_list = g_slist_prepend (encodings_list, |
676 | (gpointer) ctk_source_encoding_get_current ()); |
677 | |
678 | encodings_list = g_slist_prepend (encodings_list, |
679 | (gpointer) &utf8_encoding); |
680 | |
681 | encodings_list = _ctk_source_encoding_remove_duplicates (encodings_list, |
682 | CTK_SOURCE_ENCODING_DUPLICATES_KEEP_LAST); |
683 | |
684 | g_variant_unref (encodings_variant); |
685 | return encodings_list; |
686 | } |
687 | |
688 | /** |
689 | * ctk_source_encoding_copy: |
690 | * @enc: a #CtkSourceEncoding. |
691 | * |
692 | * Used by language bindings. |
693 | * |
694 | * Returns: a copy of @enc. |
695 | * Since: 3.14 |
696 | */ |
697 | CtkSourceEncoding * |
698 | ctk_source_encoding_copy (const CtkSourceEncoding *enc) |
699 | { |
700 | g_return_val_if_fail (enc != NULL, NULL)do { if ((__builtin_expect (__extension__ ({ int _g_boolean_var_30 = 0; if (enc != ((void*)0)) _g_boolean_var_30 = 1; _g_boolean_var_30 ; }), 1))) { } else { g_return_if_fail_warning ("CtkSourceView" , ((const char*) (__func__)), "enc != NULL"); return (((void* )0)); } } while (0); |
701 | |
702 | return (CtkSourceEncoding *) enc; |
703 | } |
704 | |
705 | /** |
706 | * ctk_source_encoding_free: |
707 | * @enc: a #CtkSourceEncoding. |
708 | * |
709 | * Used by language bindings. |
710 | * |
711 | * Since: 3.14 |
712 | */ |
713 | void |
714 | ctk_source_encoding_free (CtkSourceEncoding *enc) |
715 | { |
716 | g_return_if_fail (enc != NULL)do { if ((__builtin_expect (__extension__ ({ int _g_boolean_var_31 = 0; if (enc != ((void*)0)) _g_boolean_var_31 = 1; _g_boolean_var_31 ; }), 1))) { } else { g_return_if_fail_warning ("CtkSourceView" , ((const char*) (__func__)), "enc != NULL"); return; } } while (0); |
717 | } |