revert between 56095 -> 55830 in arch
[AROS.git] / workbench / libs / codesets / src / init.c
blob49327db7251ef828659bef598ac296e17b8c4fcd
1 /***************************************************************************
3 codesets.library - Amiga shared library for handling different codesets
4 Copyright (C) 2001-2005 by Alfonso [alfie] Ranieri <alforan@tin.it>.
5 Copyright (C) 2005-2014 codesets.library Open Source Team
7 This library is free software; you can redistribute it and/or
8 modify it under the terms of the GNU Lesser General Public
9 License as published by the Free Software Foundation; either
10 version 2.1 of the License, or (at your option) any later version.
12 This library is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 Lesser General Public License for more details.
17 codesets.library project: http://sourceforge.net/projects/codesetslib/
19 $Id$
21 ***************************************************************************/
23 #include "lib.h"
25 #include <diskfont/glyph.h>
26 #include <diskfont/diskfonttag.h>
27 #include <proto/diskfont.h>
28 #include <proto/keymap.h>
29 #include <proto/locale.h>
30 #include <proto/exec.h>
31 #include <proto/dos.h>
32 #include <proto/utility.h>
33 #include <libraries/iffparse.h>
35 #include "debug.h"
37 #if defined(__amigaos4__)
38 struct Library *DOSBase = NULL;
39 struct Library *UtilityBase = NULL;
40 struct Library *LocaleBase = NULL;
41 struct Library *DiskfontBase = NULL;
43 struct DOSIFace *IDOS = NULL;
44 struct UtilityIFace *IUtility = NULL;
45 struct LocaleIFace *ILocale = NULL;
46 struct DiskfontIFace *IDiskfont = NULL;
48 #if !defined(__NEWLIB__)
49 extern struct Library *__UtilityBase;
50 extern struct UtilityIFace* __IUtility;
51 #endif
53 #else
54 struct DosLibrary *DOSBase = NULL;
55 #if defined(__MORPHOS__)
56 struct Library *LocaleBase = NULL;
57 #else
58 struct LocaleBase *LocaleBase = NULL;
59 #endif
60 #ifdef __AROS__
61 struct UtilityBase *UtilityBase = NULL;
62 #else
63 struct Library *UtilityBase = NULL;
64 struct Library *__UtilityBase = NULL; // required by clib2 & libnix
65 #endif
66 #endif
68 /****************************************************************************/
70 ULONG freeBase(struct LibraryHeader *lib)
72 ENTER();
74 D(DBF_STARTUP, "freeing all resources of codesets.library");
76 // cleanup also the internal public codesets list
77 codesetsCleanup(&lib->codesets);
79 // close locale.library
80 if(LocaleBase != NULL)
82 DROPINTERFACE(ILocale);
83 CloseLibrary((struct Library *)LocaleBase);
84 LocaleBase = NULL;
87 #if defined(__amigaos4__)
88 // close diskfont.library
89 if(DiskfontBase != NULL)
91 DROPINTERFACE(IDiskfont);
92 CloseLibrary((struct Library *)DiskfontBase);
93 DiskfontBase = NULL;
95 #endif
97 // delete our private memory pool
98 if(lib->pool != NULL)
100 #if defined(__amigaos4__)
101 FreeSysObject(ASOT_MEMPOOL, lib->pool);
102 #else
103 DeletePool(lib->pool);
104 #endif
105 lib->pool = NULL;
108 // close utility.library
109 if(UtilityBase != NULL)
111 DROPINTERFACE(IUtility);
112 CloseLibrary((struct Library *)UtilityBase);
113 UtilityBase = NULL;
116 // close dos.library
117 if(DOSBase != NULL)
119 DROPINTERFACE(IDOS);
120 CloseLibrary((struct Library *)DOSBase);
121 DOSBase = NULL;
124 RETURN(TRUE);
125 return TRUE;
128 /***********************************************************************/
130 struct loc
132 const char *name;
133 ULONG len;
134 const char *codesetName;
135 ULONG carPlateCode;
136 ULONG iso3166Code;
139 // table with the default LANGUAGE<>CHARSET mapping we
140 // are using in codesets.library.
141 // the IOS3166 codes have been taken from http://de.wikipedia.org/wiki/ISO-3166-1-Kodierliste
142 static const struct loc locs[] =
144 { "bosanski", 8, "ISO-8859-2", MAKE_ID('B', 'A', 0, 0), MAKE_ID('B', 'I', 'H', 0) },
145 { "català", 6, "ISO-8859-1 + Euro", MAKE_ID('E', 0, 0, 0), MAKE_ID('E', 'S', 'P', 0) }, // same as spanish
146 { "czech", 5, "ISO-8859-2", MAKE_ID('C', 'Z', 0, 0), MAKE_ID('C', 'Z', 'E', 0) },
147 { "dansk", 5, "ISO-8859-1 + Euro", MAKE_ID('D', 'K', 0, 0), MAKE_ID('D', 'N', 'K', 0) },
148 { "deutsch", 7, "ISO-8859-1 + Euro", MAKE_ID('D', 0, 0, 0), MAKE_ID('D', 'E', 'U', 0) },
149 { "english", 7, "ISO-8859-1 + Euro", MAKE_ID('G', 'B', 0, 0), MAKE_ID('G', 'B', 'R', 0) },
150 { "esperanto", 9, "ISO-8859-3", MAKE_ID(0, 0, 0, 0), MAKE_ID(0, 0, 0, 0) },
151 { "eesti", 5, "ISO-8859-15", MAKE_ID('E', 'E', 0, 0), MAKE_ID('E', 'S', 'T', 0) },
152 { "èe¹tina", 7, "ISO-8859-2", MAKE_ID('C', 'Z', 0, 0), MAKE_ID('C', 'Z', 'E', 0) }, // czech in MorphOS 2.0
153 { "español", 7, "ISO-8859-1 + Euro", MAKE_ID('E', 0, 0, 0), MAKE_ID('E', 'S', 'P', 0) },
154 { "français", 8, "ISO-8859-1 + Euro", MAKE_ID('F', 0, 0, 0), MAKE_ID('F', 'R', 'A', 0) },
155 { "gaeilge", 7, "ISO-8859-15", MAKE_ID(0, 0, 0, 0), MAKE_ID(0, 0, 0, 0) },
156 { "galego", 6, "ISO-8859-1 + Euro", MAKE_ID('E', 0, 0, 0), MAKE_ID('E', 'S', 'P', 0) }, // same as spanish
157 { "greek", 5, "ISO-8859-7", MAKE_ID('G', 'R', 0, 0), MAKE_ID('G', 'R', 'C', 0) },
158 { "hrvatski", 8, "ISO-8859-2", MAKE_ID('H', 'R', 0, 0), MAKE_ID('H', 'R', 'V', 0) },
159 { "italiano", 8, "ISO-8859-1 + Euro", MAKE_ID('I', 0, 0, 0), MAKE_ID('I', 'T', 'A', 0) },
160 { "lietuvi", 7, "ISO-8859-13", MAKE_ID('L', 'T', 0, 0), MAKE_ID('L', 'T', 'U', 0) },
161 { "magyar", 6, "ISO-8859-2", MAKE_ID('H', 'U', 0, 0), MAKE_ID('H', 'U', 'N', 0) },
162 { "nederlands", 10, "ISO-8859-1 + Euro", MAKE_ID('N', 'L', 0, 0), MAKE_ID('N', 'L', 'D', 0) },
163 { "norsk", 5, "ISO-8859-1 + Euro", MAKE_ID('N', 0, 0, 0), MAKE_ID('N', 'O', 'R', 0) },
164 { "polski", 6, "ISO-8859-2", MAKE_ID('P', 'L', 0, 0), MAKE_ID('P', 'O', 'L', 0) },
165 { "português", 9, "ISO-8859-1 + Euro", MAKE_ID('P', 'T', 0, 0), MAKE_ID('P', 'R', 'T', 0) },
166 { "russian", 7, "Amiga-1251", MAKE_ID('R', 'U', 0, 0), MAKE_ID('R', 'U', 'S', 0) },
167 { "slovak", 6, "ISO-8859-2", MAKE_ID('S', 'K', 0, 0), MAKE_ID('S', 'V', 'K', 0) },
168 { "slovensko", 9, "ISO-8859-2", MAKE_ID('S', 'I', 0, 0), MAKE_ID('S', 'V', 'N', 0) },
169 { "srpski", 6, "ISO-8859-2", MAKE_ID('R', 'S', 0, 0), MAKE_ID('S', 'R', 'B', 0) },
170 { "suomi", 5, "ISO-8859-1", MAKE_ID('F', 'I', 'N', 0), MAKE_ID('F', 'I', 'N', 0) },
171 { "svenska", 7, "ISO-8859-1 + Euro", MAKE_ID('S', 0, 0, 0), MAKE_ID('S', 'W', 'E', 0) },
172 { "türkçe", 6, "ISO-8859-9", MAKE_ID('T', 'R', 0, 0), MAKE_ID('T', 'U', 'R', 0) },
173 { NULL, 0, NULL, MAKE_ID(0, 0, 0, 0), MAKE_ID(0, 0, 0, 0) }
176 static void getSystemCodeset(struct LibraryHeader *lib)
178 struct codeset *foundCodeset = NULL;
180 ENTER();
182 // before we go any query the system via locale.library (which
183 // might not be so accurate) we try different other means of
184 // finding the codeset/charset of the system
185 #if defined(__amigaos4__)
186 if(foundCodeset == NULL)
188 LONG default_charset = GetDiskFontCtrl(DFCTRL_CHARSET);
189 char *charset = (char *)ObtainCharsetInfo(DFCS_NUMBER, default_charset, DFCS_MIMENAME);
191 foundCodeset = codesetsFind(&lib->codesets, charset);
193 D(DBF_STARTUP, "%s system default codeset: '%s' (diskfont)", foundCodeset ? "found" : "not found", charset);
195 #endif
197 if(foundCodeset == NULL)
199 char codepage[40];
201 codepage[0] = '\0';
202 if(GetVar("CODEPAGE", codepage, sizeof(codepage), 0) > 0)
204 D(DBF_STARTUP, "trying ENV:CODEPAGE '%s'", codepage);
205 foundCodeset = codesetsFind(&lib->codesets, codepage);
208 D(DBF_STARTUP, "%s system default codeset: '%s' (ENV:CODEPAGE)", foundCodeset ? "found" : "did not find",
209 foundCodeset ? foundCodeset->name : "?");
212 #if defined(__MORPHOS__)
213 if(foundCodeset == NULL)
215 /* If CODEPAGE did not work (maybe user deleted it or something) we try a keymap instead. This only works
216 * in MorphOS 2 and only if an old Amiga keymap is not used.
218 if(foundCodeset == NULL)
220 struct Library *KeymapBase;
222 if((KeymapBase = OpenLibrary("keymap.library", 51)) != NULL)
224 /* Since we requested V51 it is either V51 or newer */
225 if(KeymapBase->lib_Version > 51 || KeymapBase->lib_Revision >= 4)
227 CONST_STRPTR codepage;
229 #ifndef GetKeyMapCodepage
230 #define GetKeyMapCodepage(__p0) LP1(78, CONST_STRPTR , GetKeyMapCodepage, CONST struct KeyMap *, __p0, a0, , KEYMAP_BASE_NAME, 0, 0, 0, 0, 0, 0)
231 #endif
233 if((codepage = GetKeyMapCodepage(NULL)) != NULL)
235 foundCodeset = codesetsFind(&lib->codesets, codepage);
237 D(DBF_STARTUP, "%s system default codeset: '%s' (keymap)", foundCodeset ? "found" : "did not find",
238 foundCodeset ? foundCodeset->name : "?");
242 CloseLibrary(KeymapBase);
246 #endif
248 // if we still do not have our default charset we try to load
249 // it from and environment variable ENVARC:CHARSET
250 if(foundCodeset == NULL)
252 char charset[80];
254 charset[0] = '\0';
255 if(GetVar("CHARSET", charset, sizeof(charset), 0) > 0)
257 D(DBF_STARTUP, "trying ENV:CHARSET '%s'", charset);
258 foundCodeset = codesetsFind(&lib->codesets, charset);
261 D(DBF_STARTUP, "%s system default codeset: '%s' (ENV:CHARSET)", foundCodeset ? "found" : "did not find",
262 foundCodeset ? foundCodeset->name : "?");
265 // try the country code next
266 if(foundCodeset == NULL)
268 struct Locale *defaultLocale;
270 if((defaultLocale = OpenLocale(NULL)) != NULL)
272 int i;
273 const struct loc *curLoc = NULL;
274 BOOL found = FALSE;
276 for(i=0;;i++)
278 curLoc = &locs[i];
279 if(curLoc == NULL || curLoc->name == NULL)
280 break;
282 if(defaultLocale->loc_CountryCode == curLoc->carPlateCode || defaultLocale->loc_CountryCode == curLoc->iso3166Code)
284 D(DBF_STARTUP, "found country code for '%s'", curLoc->name);
285 found = TRUE;
286 break;
290 if(found == TRUE)
291 foundCodeset = codesetsFind(&lib->codesets, curLoc->codesetName);
293 CloseLocale(defaultLocale);
296 D(DBF_STARTUP, "%s system default codeset: '%s' (locale/country)", foundCodeset ? "found" : "did not find",
297 foundCodeset ? foundCodeset->name : "?");
300 // and if even the CHARSET environment variable didn't work
301 // out we check the LANGUAGE env variable against our own
302 // internal fallback list
303 if(foundCodeset == NULL)
305 char language[80];
307 language[0] = '\0';
308 if(GetVar("LANGUAGE", language, sizeof(language), 0) > 0)
310 int i;
311 const struct loc *curLoc = NULL;
312 BOOL found = FALSE;
314 for(i=0;;i++)
316 curLoc = &locs[i];
317 if(curLoc == NULL || curLoc->name == NULL)
318 break;
320 if(Strnicmp(language, curLoc->name, curLoc->len) == 0)
322 D(DBF_STARTUP, "found language name for '%s'", curLoc->name);
323 found = TRUE;
324 break;
328 if(found == TRUE)
329 foundCodeset = codesetsFind(&lib->codesets, curLoc->codesetName);
332 D(DBF_STARTUP, "%s system default codeset: '%s' (ENV:LANGUAGE)", foundCodeset ? "found" : "did not find",
333 foundCodeset ? foundCodeset->name : "?");
336 // and the very last check we do to find out the system's charset is
337 // to use locale.library.
338 if(foundCodeset == NULL)
340 struct Locale *locale;
342 if((locale = OpenLocale(NULL)) != NULL)
344 int i;
345 char *language = locale->loc_LanguageName;
346 const struct loc *curLoc = NULL;
347 BOOL found = FALSE;
349 for(i=0;;i++)
351 curLoc = &locs[i];
352 if(curLoc == NULL || curLoc->name == NULL)
353 break;
355 if(Strnicmp(language, curLoc->name, curLoc->len) == 0)
357 found = TRUE;
358 break;
362 CloseLocale(locale);
364 if(found == TRUE)
365 foundCodeset = codesetsFind(&lib->codesets, curLoc->codesetName);
368 D(DBF_STARTUP, "%s system default codeset: '%s' (locale/language)", foundCodeset ? "found" : "did not find",
369 foundCodeset ? foundCodeset->name : "?");
372 // and if even that very last test didn't work out we
373 // can just take the ISO-8859-1 charset as the fallback default
374 if(foundCodeset == NULL)
375 lib->systemCodeset = codesetsFind(&lib->codesets, "ISO-8859-1");
376 else
377 lib->systemCodeset = foundCodeset;
379 LEAVE();
382 /***********************************************************************/
384 ULONG initBase(struct LibraryHeader *lib)
386 ENTER();
388 if((DOSBase = (APTR)OpenLibrary("dos.library", 37)) != NULL &&
389 GETINTERFACE(IDOS, DOSBase))
391 if((UtilityBase = (APTR)OpenLibrary("utility.library", 37)) != NULL &&
392 GETINTERFACE(IUtility, UtilityBase))
394 // we have to please the internal utilitybase
395 // pointers of libnix and clib2
396 #if !defined(__NEWLIB__) && !defined(__AROS__)
397 __UtilityBase = (APTR)UtilityBase;
398 #if defined(__amigaos4__)
399 __IUtility = IUtility;
400 #endif
401 #endif
403 // setup the debugging stuff
404 #if defined(DEBUG)
405 SetupDebug();
406 #endif
408 #if defined(__amigaos4__)
409 if((DiskfontBase = OpenLibrary("diskfont.library", 50)) != NULL &&
410 GETINTERFACE(IDiskfont, DiskfontBase))
412 #endif
413 #if defined(__amigaos4__)
414 lib->pool = AllocSysObjectTags(ASOT_MEMPOOL, ASOPOOL_MFlags, MEMF_SHARED,
415 ASOPOOL_Puddle, 4096,
416 ASOPOOL_Threshold, 512,
417 ASOPOOL_Name, "codesets.library pool",
418 ASOPOOL_LockMem, FALSE,
419 TAG_DONE);
420 #else
421 lib->pool = CreatePool(MEMF_ANY, 4096, 512);
422 #endif
423 if(lib->pool != NULL)
425 if(codesetsInit(&lib->codesets) == TRUE)
427 lib->systemCodeset = (struct codeset *)GetHead((struct List *)&lib->codesets);
429 if((LocaleBase = (APTR)OpenLibrary("locale.library", 37)) != NULL &&
430 GETINTERFACE(ILocale, LocaleBase))
432 getSystemCodeset(lib);
435 RETURN(TRUE);
436 return TRUE;
439 #if defined(__amigaos4__)
441 #endif
445 freeBase(lib);
447 RETURN(FALSE);
448 return FALSE;
451 /***********************************************************************/