Debian Codegraph @

src/af/xap/gtk/xap_UnixEncodingManager.cpp

raw
1 /* -*- mode: C++; tab-width: 4; c-basic-offset: 4; -*- */
2
3 #include <stdio.h>
4 #include <stdlib.h>
5 #include <ctype.h>
6
7 #include <glib.h>
8
9 #include "xap_UnixEncodingManager.h"
10 #include "ut_debugmsg.h"
11 #include "ut_string.h"
12 #include "ut_string_class.h"
13
14 /* We source this file in order not to replace symbols in gnome-libs.
15 (if AW is linked with GNOME).
16 */
17
18 /* Only this function is used. For $LANG=ru_RU.KOI8-R it returns the following
19 list:
20
21 ru_RU.KOI8-R
22 ru_RU
23 ru.KOI8-R
24 ru
25 C
26 ""
27
28 It's much more powerful (gives correct result) than plain setlocale since
29 it explicitly reads locale aliases.
30 */
31 #ifdef __cplusplus
32 extern "C" {
33 #endif /* __cplusplus */
34
35 const GList *
36 g_i18n_get_language_list (const gchar *category_name);
37
38 #ifdef __cplusplus
39 }
40 #endif /* __cplusplus */
41
42
43 /*
44 * Copyright (C) 1997, 1998, 1999, 2000 Free Software Foundation
45 * All rights reserved.
46 *
47 * This file is part of the Gnome Library.
48 *
49 * The Gnome Library is free software; you can redistribute it and/or
50 * modify it under the terms of the GNU Library General Public License as
51 * published by the Free Software Foundation; either version 2 of the
52 * License, or (at your option) any later version.
53 *
54 * The Gnome Library is distributed in the hope that it will be useful,
55 * but WITHOUT ANY WARRANTY; without even the implied warranty of
56 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
57 * Library General Public License for more details.
58 *
59 * You should have received a copy of the GNU Library General Public
60 * License along with the Gnome Library; see the file COPYING.LIB. If not,
61 * write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
62 * Boston, MA 02110-1301 USA.
63 */
64
65 #ifdef HAVE_CONFIG_H
66 #include <config.h>
67 #endif
68
69 #include <stdio.h>
70 #include <stdlib.h>
71 #include <string.h>
72
73 #include <glib.h>
74
75 static GHashTable *alias_table = NULL;
76 static GHashTable *category_table= NULL;
77 bool prepped_table = 0;
78
79 /*read an alias file for the locales*/
80 static void
81 read_aliases (const char *file)
82 {
83 FILE *fp;
84 char buf[256];
85 if (!prepped_table) {
86 alias_table = g_hash_table_new (g_str_hash, g_str_equal);
87 prepped_table = 1;
88 }
89 fp = fopen (file,"r");
90 if (!fp)
91 return;
92 while (fgets (buf,256,fp))
93 {
94 char *p;
95 g_strstrip(buf);
96 if (buf[0]=='#' || buf[0]=='\0')
97 continue;
98 p = strtok (buf,"\t ");
99 if (!p)
100 continue;
101 p = strtok (NULL,"\t ");
102 if(!p)
103 continue;
104 if (!g_hash_table_lookup (alias_table, buf))
105 g_hash_table_insert (alias_table, g_strdup(buf), g_strdup(p));
106 }
107 fclose (fp);
108 }
109
110 /*return the un-aliased language as a newly allocated string*/
111 static char *
112 unalias_lang (char *lang)
113 {
114 char *p;
115 int i;
116 if (!prepped_table)
117 {
118 read_aliases ("/usr/lib/locale/locale.alias");
119 read_aliases ("/usr/local/lib/locale/locale.alias");
120 read_aliases ("/usr/share/locale/locale.alias");
121 read_aliases ("/usr/local/share/locale/locale.alias");
122 read_aliases ("/usr/lib/X11/locale/locale.alias");
123 read_aliases ("/usr/openwin/lib/locale/locale.alias");
124 }
125 i = 0;
126 while ((p=static_cast<char*>(g_hash_table_lookup(alias_table,lang))) && strcmp(p, lang))
127 {
128 lang = p;
129 if (i++ == 30)
130 {
131 static gboolean said_before = FALSE;
132 if (!said_before)
133 g_warning ("Too many alias levels for a locale, "
134 "may indicate a loop");
135 said_before = TRUE;
136 return lang;
137 }
138 }
139 return lang;
140 }
141
142 static void
143 free_entry (void *ekey,void *eval,void * /*user_data*/)
144 {
145 g_free(ekey);
146 g_free(eval);
147 }
148
149 /* Mask for components of locale spec. The ordering here is from
150 * least significant to most significant
151 */
152 enum
153 {
154 COMPONENT_CODESET = 1 << 0,
155 COMPONENT_TERRITORY = 1 << 1,
156 COMPONENT_MODIFIER = 1 << 2
157 };
158
159 /* Break an X/Open style locale specification into components
160 */
161 static guint
162 explode_locale (const gchar *locale,
163 gchar **language,
164 gchar **territory,
165 gchar **codeset,
166 gchar **modifier)
167 {
168 const gchar *uscore_pos;
169 const gchar *at_pos;
170 const gchar *dot_pos;
171
172 guint mask = 0;
173
174 uscore_pos = strchr (locale, '_');
175 dot_pos = strchr (uscore_pos ? uscore_pos : locale, '.');
176 at_pos = strchr (dot_pos ? dot_pos : (uscore_pos ? uscore_pos : locale), '@');
177
178 if (at_pos)
179 {
180 mask |= COMPONENT_MODIFIER;
181 *modifier = g_strdup (at_pos);
182 }
183 else
184 at_pos = locale + strlen (locale);
185
186 if (dot_pos)
187 {
188 mask |= COMPONENT_CODESET;
189 *codeset = g_new (gchar, 1 + at_pos - dot_pos);
190 strncpy (*codeset, dot_pos, at_pos - dot_pos);
191 (*codeset)[at_pos - dot_pos] = '\0';
192 }
193 else
194 dot_pos = at_pos;
195
196 if (uscore_pos)
197 {
198 mask |= COMPONENT_TERRITORY;
199 *territory = g_new (gchar, 1 + dot_pos - uscore_pos);
200 strncpy (*territory, uscore_pos, dot_pos - uscore_pos);
201 (*territory)[dot_pos - uscore_pos] = '\0';
202 }
203 else
204 uscore_pos = dot_pos;
205
206 *language = g_new (gchar, 1 + uscore_pos - locale);
207 strncpy (*language, locale, uscore_pos - locale);
208 (*language)[uscore_pos - locale] = '\0';
209
210 return mask;
211 }
212
213 /*
214 * Compute all interesting variants for a given locale name -
215 * by stripping off different components of the value.
216 *
217 * For simplicity, we assume that the locale is in
218 * X/Open format: language[_territory][.codeset][@modifier]
219 *
220 * TODO: Extend this to handle the CEN format (see the GNUlibc docs)
221 * as well. We could just copy the code from glibc wholesale
222 * but it is big, ugly, and complicated, so I'm reluctant
223 * to do so when this should handle 99% of the time...
224 */
225 static GList *
226 compute_locale_variants (const gchar *locale)
227 {
228 GList *retval = NULL;
229
230 gchar *language;
231 gchar *territory;
232 gchar *codeset;
233 gchar *modifier;
234
235 guint mask;
236 guint i;
237
238 UT_return_val_if_fail (locale != NULL, NULL);
239
240 mask = explode_locale (locale, &language, &territory, &codeset, &modifier);
241
242 /* Iterate through all possible combinations, from least attractive
243 * to most attractive.
244 */
245 for (i=0; i<=mask; i++)
246 if ((i & ~mask) == 0)
247 {
248 gchar *val = g_strconcat(language,
249 (i & COMPONENT_TERRITORY) ? territory : "",
250 (i & COMPONENT_CODESET) ? codeset : "",
251 (i & COMPONENT_MODIFIER) ? modifier : "",
252 NULL);
253 retval = g_list_prepend (retval, val);
254 }
255
256 g_free (language);
257 if (mask & COMPONENT_CODESET)
258 g_free (codeset);
259 if (mask & COMPONENT_TERRITORY)
260 g_free (territory);
261 if (mask & COMPONENT_MODIFIER)
262 g_free (modifier);
263
264 return retval;
265 }
266
267 /* The following is (partly) taken from the gettext package.
268 Copyright (C) 1995, 1996, 1997, 1998 Free Software Foundation, Inc. */
269
270 static const gchar *
271 guess_category_value (const gchar *categoryname)
272 {
273 const gchar *retval;
274
275 /* Let's check passed category. */
276 retval = g_getenv (categoryname);
277 if (retval != NULL && retval[0] != '\0')
278 return retval;
279
280 /* The highest priority value after passed category is the `LANGUAGE'
281 environment variable. This is a GNU extension. */
282 retval = g_getenv ("LANGUAGE");
283 if (retval != NULL && retval[0] != '\0')
284 return retval;
285
286 /* `LANGUAGE' is not set. So we have to proceed with the POSIX
287 methods of looking to `LC_ALL', `LC_xxx', and `LANG'. On some
288 systems this can be done by the `setlocale' function itself. */
289
290 /* Setting of LC_ALL overwrites all other. */
291 retval = g_getenv ("LC_ALL");
292 if (retval != NULL && retval[0] != '\0')
293 return retval;
294
295
296 /* Last possibility is the LANG environment variable. */
297 retval = g_getenv ("LANG");
298 if (retval != NULL && retval[0] != '\0')
299 return retval;
300
301 return NULL;
302 }
303
304 /**
305 * g_i18n_get_language_list:
306 * @category_name: Name of category to look up, e.g. "LC_MESSAGES".
307 *
308 * This computes a list of language strings. It searches in the
309 * standard environment variables to find the list, which is sorted
310 * in order from most desirable to least desirable. The `C' locale
311 * is appended to the list if it does not already appear (other routines depend on this behaviour).
312 * If @category_name is %NULL, then LC_ALL is assumed.
313 *
314 * Return value: the list of languages, this list should not be freed as it is owned by gnome-i18n
315 **/
316 const GList *
317 g_i18n_get_language_list (const gchar *category_name)
318 {
319 GList *list;
320 prepped_table = 0;
321
322 if (!category_name)
323 category_name= "LC_ALL";
324
325 if (category_table)
326 {
327 #if 0
328 // we want a fresh reading of the LANG variable every time so we can
329 // work out the non-Unicode encoding under UTF-8 locale
330 list= static_cast<GList *>(g_hash_table_lookup (category_table, const_cast<const gpointer>(category_name)));
331 #else
332 xxx_UT_DEBUGMSG(("recreating hash table\n"));
333 g_hash_table_destroy (category_table);
334 category_table= g_hash_table_new (g_str_hash, g_str_equal);
335 list= NULL;
336
337 #endif
338 }
339 else
340 {
341 category_table= g_hash_table_new (g_str_hash, g_str_equal);
342 list= NULL;
343 }
344
345 if (!list)
346 {
347 gint c_locale_defined= FALSE;
348
349 const gchar *category_value;
350 gchar *category_memory, *orig_category_memory;
351
352 category_value = guess_category_value (category_name);
353 if (! category_value)
354 category_value = "C";
355 orig_category_memory = category_memory =
356 static_cast<gchar*>(g_malloc (strlen (category_value)+1));
357
358 while (category_value[0] != '\0')
359 {
360 while (category_value[0] != '\0' && category_value[0] == ':')
361 ++category_value;
362
363 if (category_value[0] != '\0')
364 {
365 char *cp= category_memory;
366
367 while (category_value[0] != '\0' && category_value[0] != ':')
368 *category_memory++= *category_value++;
369
370 category_memory[0]= '\0';
371 category_memory++;
372
373 cp = unalias_lang(cp);
374
375 if (strcmp (cp, "C") == 0)
376 c_locale_defined= TRUE;
377
378 list= g_list_concat (list, compute_locale_variants (cp));
379 }
380 }
381
382 g_free (orig_category_memory);
383
384 if (!c_locale_defined)
385 list= g_list_append (list, reinterpret_cast<void*>(const_cast<gchar *>("C")));
386
387 g_hash_table_insert (category_table, reinterpret_cast<gpointer>(const_cast<gchar *>(category_name)), list);
388 }
389
390 g_hash_table_foreach(alias_table, free_entry, NULL);
391 g_hash_table_destroy(alias_table);
392 prepped_table = 0;
393
394 return list;
395 }
396
397 /************************************************************/
398
399 XAP_EncodingManager *XAP_EncodingManager::get_instance()
400 {
401 if (_instance == 0)
402 {
403 UT_DEBUGMSG(("Building XAP_EncodingManager\n"));
404 _instance = new XAP_UnixEncodingManager();
405 _instance->initialize();
406 UT_DEBUGMSG(("XAP_EncodingManager built\n"));
407 }
408
409 return _instance;
410 }
411
412 /************************************************************/
413
414 static UT_UTF8String NativeEncodingName;
415 static UT_UTF8String NativeSystemEncodingName;
416 static UT_UTF8String Native8BitEncodingName;
417 static UT_UTF8String NativeNonUnicodeEncodingName;
418 static UT_UTF8String NativeUnicodeEncodingName;
419 static UT_UTF8String LanguageISOName;
420 static UT_UTF8String LanguageISOTerritory;
421
422 XAP_UnixEncodingManager::XAP_UnixEncodingManager()
423 {
424 }
425
426 XAP_UnixEncodingManager::~XAP_UnixEncodingManager()
427 {
428 }
429
430 const char* XAP_UnixEncodingManager::getNativeEncodingName() const
431 {
432 return NativeEncodingName.utf8_str();
433 }
434
435 const char* XAP_UnixEncodingManager::getNativeSystemEncodingName() const
436 {
437 return NativeSystemEncodingName.utf8_str();
438 }
439
440 const char* XAP_UnixEncodingManager::getNative8BitEncodingName() const
441 {
442 return Native8BitEncodingName.utf8_str();
443 }
444
445 const char* XAP_UnixEncodingManager::getNativeNonUnicodeEncodingName() const
446 {
447 return NativeNonUnicodeEncodingName.utf8_str();
448 }
449
450 const char* XAP_UnixEncodingManager::getNativeUnicodeEncodingName() const
451 {
452 return NativeUnicodeEncodingName.utf8_str();
453 }
454
455 const char* XAP_UnixEncodingManager::getLanguageISOName() const
456 {
457 return LanguageISOName.utf8_str();
458 }
459
460 const char* XAP_UnixEncodingManager::getLanguageISOTerritory() const
461 {
462 return LanguageISOTerritory.utf8_str();
463 }
464
465 void XAP_UnixEncodingManager::initialize()
466 {
467 const GList* lst = g_i18n_get_language_list ("LANG");
468 const char* locname = static_cast<char*>(lst->data);
469
470 NativeEncodingName = "ISO-8859-1";
471 NativeSystemEncodingName =
472 Native8BitEncodingName =
473 NativeNonUnicodeEncodingName = NativeEncodingName;
474 NativeUnicodeEncodingName = "UTF-8";
475 LanguageISOName = "en";
476 LanguageISOTerritory = "US";
477
478 if (!*locname || !strcmp(locname,"C"))
479 { /* paranoic case - broken system */
480 ; /*already initialized*/
481 }
482 else
483 {
484 char * lang = NULL;
485 char * terr = NULL;
486 char * cs = NULL;
487 char * mod = NULL;
488
489 int mask = explode_locale (locname,&lang,&terr,&cs,&mod);
490
491 LanguageISOName = lang;
492
493 if ((mask & COMPONENT_TERRITORY) && terr)
494 {
495 LanguageISOTerritory = terr + 1; /* yes, +1 */
496 }
497 if ((mask & COMPONENT_CODESET) && cs)
498 {
499 if (cs[1])
500 {
501 int length = strlen (cs + 1);
502 char * name = static_cast<char *>(g_try_malloc (length + 3));
503 if (name)
504 {
505 strcpy (name, cs + 1);
506
507 /* make the encoding name upper-case
508 * TODO why not use the UT_islower/UT_isupper?
509 */
510 for (int i = 0; i < length; i++)
511 if (islower (static_cast<int>(static_cast<unsigned char>(name[i]))))
512 name[i] = static_cast<char>(toupper (static_cast<int>(static_cast<unsigned char>(name[i]))));
513
514 /* encoding names may be presented as iso88591 or ISO8859-1,
515 * but we need both hyphens for iconv
516 */
517 if (strncmp (name, "ISO8859", 7) == 0)
518 {
519 memmove (name + 4, name + 3, length + 1 - 3);
520 length++;
521 name[3] = '-';
522 if (name[8] != '-')
523 {
524 memmove (name + 9, name + 8, length + 1 - 8);
525 length++;
526 name[8] = '-';
527 }
528 }
529 NativeEncodingName = name;
530 FREEP(name);
531 }
532 }
533 Native8BitEncodingName = NativeSystemEncodingName = NativeEncodingName;
534
535 // need to get non-unicode encoding if encoding is UTF-8
536 if(!g_ascii_strcasecmp(NativeEncodingName.utf8_str(), "UTF-8"))
537 {
538 // we want to get the encoding that would be used for the given
539 // language/territory if the UTF-8 encoding was not specified
540 // by LANG
541
542 UT_UTF8String OLDLANG (getenv("LANG"));
543 UT_UTF8String MYLANG (LanguageISOName);
544 MYLANG += "_";
545 MYLANG += LanguageISOTerritory;
546 g_setenv ("LANG", MYLANG.utf8_str(), TRUE);
547 if (mask & COMPONENT_CODESET)
548 {
549 NativeNonUnicodeEncodingName = cs+1;
550 xxx_UT_DEBUGMSG(("NativeNonUnicodeEncodingName (1) %s\n", NativeNonUnicodeEncodingName));
551 if (!strncmp(cs+1,"ISO8859",strlen("ISO8859")))
552 {
553 char buf[40];
554 strcpy(buf,"ISO-");
555 strcat(buf,cs+1+3);
556 NativeNonUnicodeEncodingName = buf;
557 }
558 xxx_UT_DEBUGMSG(("NativeNonUnicodeEncodingName (2) %s\n", NativeNonUnicodeEncodingName));
559 }
560 g_setenv("LANG", OLDLANG.utf8_str(), TRUE);
561 }
562
563 }
564 FREEP(lang);
565 FREEP(terr);
566 FREEP(cs);
567 FREEP(mod);
568 }
569 // Moved to end so that we correctly detect if the locale is CJK.
570 XAP_EncodingManager::initialize();
571 describe();
572 }