Debian Codegraph @

src/af/xap/cocoa/xap_CocoaEncodingManager.cpp

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