Stunt: get codesetslib from its own Sourceforge repository.
[cake.git] / workbench / libs / codesetslib / src / init.c
blobb2b1cf41517ed1896840dd573fd8b4fd42cf9bbe
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-2007 by 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/locale.h>
29 #include <proto/exec.h>
30 #include <proto/dos.h>
31 #include <proto/utility.h>
33 #include "debug.h"
35 #if defined(__amigaos4__)
36 struct Library *DOSBase = NULL;
37 struct Library *UtilityBase = NULL;
38 struct Library *LocaleBase = NULL;
39 struct Library *DiskfontBase = NULL;
41 struct DOSIFace* IDOS = NULL;
42 struct UtilityIFace* IUtility = NULL;
43 struct LocaleIFace* ILocale = NULL;
44 struct DiskfontIFace* IDiskfont = NULL;
46 #if !defined(__NEWLIB__)
47 extern struct Library *__UtilityBase;
48 extern struct UtilityIFace* __IUtility;
49 #endif
51 #elif defined(__AROS__)
52 struct DosLibrary *DOSBase = NULL;
53 struct UtilityBase *UtilityBase = NULL;
54 #else
55 struct DosLibrary *DOSBase = NULL;
56 struct Library *UtilityBase = NULL;
57 #if defined(__MORPHOS__)
58 struct Library *LocaleBase = NULL;
59 #else
60 struct LocaleBase *LocaleBase = NULL;
61 #endif
62 struct Library *__UtilityBase = NULL; // required by clib2 & libnix
63 #endif
65 /****************************************************************************/
67 ULONG
68 freeBase(struct LibraryHeader *lib)
70 ENTER();
72 D(DBF_STARTUP, "freeing all resources of codesets.library");
74 // cleanup also the internal public codesets list
75 codesetsCleanup(&lib->codesets);
77 // close locale.library
78 if(LocaleBase)
80 DROPINTERFACE(ILocale);
81 CloseLibrary((struct Library *)LocaleBase);
82 LocaleBase = NULL;
85 #if defined(__amigaos4__)
86 // close diskfont.library
87 if(DiskfontBase)
89 DROPINTERFACE(IDiskfont);
90 CloseLibrary((struct Library *)DiskfontBase);
91 DiskfontBase = NULL;
93 #endif
95 // delete our private memory pool
96 if(lib->pool)
98 DeletePool(lib->pool);
99 lib->pool = NULL;
102 // close utility.library
103 if(UtilityBase)
105 DROPINTERFACE(IUtility);
106 CloseLibrary(UtilityBase);
107 UtilityBase = NULL;
110 // close dos.library
111 if(DOSBase)
113 DROPINTERFACE(IDOS);
114 CloseLibrary((struct Library *)DOSBase);
115 DOSBase = NULL;
118 RETURN(TRUE);
119 return TRUE;
122 /***********************************************************************/
124 struct loc
126 const char *name;
127 ULONG len;
128 const char *codesetName;
131 // table with the default LANGUAGE<>CHARSET mapping we
132 // are using in codesets.library.
133 static const struct loc locs[] =
135 { "bosanski", 8, "ISO-8859-2" },
136 { "català", 6, "ISO-8859-1 + Euro" },
137 { "czech", 5, "ISO-8859-2" },
138 { "dansk", 5, "ISO-8859-1 + Euro" },
139 { "deutsch", 7, "ISO-8859-1 + Euro" },
140 { "english", 7, "ISO-8859-1 + Euro" },
141 { "esperanto", 9, "ISO-8859-3" },
142 { "eesti", 5, "ISO-8859-15" },
143 { "español", 7, "ISO-8859-1 + Euro" },
144 { "français", 8, "ISO-8859-1 + Euro" },
145 { "gaeilge", 7, "ISO-8859-15" },
146 { "galego", 6, "ISO-8859-1 + Euro" },
147 { "greek", 5, "ISO-8859-7" },
148 { "hrvatski", 8, "ISO-8859-2" },
149 { "italiano", 8, "ISO-8859-1 + Euro" },
150 { "lietuvi", 7, "ISO-8859-13" },
151 { "magyar", 6, "ISO-8859-2" },
152 { "nederlands", 10, "ISO-8859-1 + Euro" },
153 { "norsk", 5, "ISO-8859-1 + Euro" },
154 { "polski", 6, "ISO-8859-2" },
155 { "português", 9, "ISO-8859-1 + Euro" },
156 { "russian", 7, "Amiga-1251" },
157 { "slovak", 6, "ISO-8859-2" },
158 { "slovensko", 9, "ISO-8859-2" },
159 { "srpski", 6, "ISO-8859-2" },
160 { "suomi", 5, "ISO-8859-1" },
161 { "svenska", 7, "ISO-8859-1 + Euro" },
162 { "türkçe", 6, "ISO-8859-9" },
163 { NULL, 0, NULL }
166 static void
167 getSystemCodeset(struct LibraryHeader *lib)
169 struct codeset *foundCodeset = NULL;
171 ENTER();
173 // before we go any query the system via locale.library (which
174 // might not be so accurate) we try different other means of
175 // finding the codeset/charset of the system
176 #ifdef __amigaos4__
178 LONG default_charset = GetDiskFontCtrl(DFCTRL_CHARSET);
179 char *charset = (char *)ObtainCharsetInfo(DFCS_NUMBER, default_charset, DFCS_MIMENAME);
181 foundCodeset = codesetsFind(&lib->codesets, charset);
183 D(DBF_STARTUP, "%s system default codeset: '%s' (diskfont)", foundCodeset ? "found" : "not found", charset);
185 #endif
187 // if we still do not have our default charset we try to load
188 // it from and environment variable ENVARC:CHARSET
189 if(foundCodeset == NULL)
191 char charset[80];
192 charset[0] = '\0';
194 if(GetVar("CHARSET", charset, sizeof(charset), 0) > 0)
196 foundCodeset = codesetsFind(&lib->codesets, charset);
199 D(DBF_STARTUP, "%s system default codeset: '%s' (ENV:CHARSET)", foundCodeset ? "found" : "not found", charset);
202 // and if even the CHARSET environment variable didn't work
203 // out we check the LANGUAGE env variable against our own
204 // internal fallback list
205 if(foundCodeset == NULL)
207 char language[80];
209 if(GetVar("LANGUAGE", language, sizeof(language), 0) > 0)
211 int i;
212 struct loc *curLoc = NULL;
213 BOOL found = FALSE;
215 for(i=0;;i++)
217 curLoc = (struct loc *)&locs[i];
218 if(curLoc == NULL || curLoc->name == NULL)
219 break;
221 if(!strnicmp(language, curLoc->name, curLoc->len))
223 found = TRUE;
224 break;
228 if(found)
229 foundCodeset = codesetsFind(&lib->codesets, curLoc->codesetName);
232 D(DBF_STARTUP, "%s system default codeset: '%s' (ENV:LANGUAGE)", foundCodeset ? "found" : "not found",
233 foundCodeset ? foundCodeset->name : "?");
236 // and the very last check we do to find out the system's charset is
237 // to use locale.library.
238 if(foundCodeset == NULL)
240 struct Locale *locale;
242 if((locale = OpenLocale(NULL)))
244 int i;
245 char *language = locale->loc_LanguageName;
246 struct loc *curLoc = NULL;
247 BOOL found = FALSE;
249 for(i=0;;i++)
251 curLoc = (struct loc *)&locs[i];
252 if(curLoc == NULL || curLoc->name == NULL)
253 break;
255 if(!strnicmp(language, curLoc->name, curLoc->len))
257 found = TRUE;
258 break;
262 CloseLocale(locale);
264 if(found)
265 foundCodeset = codesetsFind(&lib->codesets, curLoc->codesetName);
268 D(DBF_STARTUP, "%s system default codeset: '%s' (locale)", foundCodeset ? "found" : "not found",
269 foundCodeset ? foundCodeset->name : "?");
272 // and if even that very last test didn't work out we
273 // can just take the ISO-8859-1 charset as the fallback default
274 if(foundCodeset == NULL)
275 lib->systemCodeset = codesetsFind(&lib->codesets, "ISO-8859-1");
276 else
277 lib->systemCodeset = foundCodeset;
279 LEAVE();
282 /***********************************************************************/
284 ULONG
285 initBase(struct LibraryHeader *lib)
287 ENTER();
289 if((DOSBase = (APTR)OpenLibrary("dos.library", 37)) &&
290 GETINTERFACE(IDOS, DOSBase))
292 if((UtilityBase = OpenLibrary("utility.library", 37)) &&
293 GETINTERFACE(IUtility, UtilityBase))
295 // we have to please the internal utilitybase
296 // pointers of libnix and clib2
297 #if !defined(__NEWLIB__) && !defined(__AROS__)
298 __UtilityBase = (APTR)UtilityBase;
299 #if defined(__amigaos4__)
300 __IUtility = IUtility;
301 #endif
302 #endif
304 // setup the debugging stuff
305 #if defined(DEBUG)
306 SetupDebug();
307 #endif
309 #if defined(__amigaos4__)
310 if((DiskfontBase = OpenLibrary("diskfont.library", 50)) &&
311 GETINTERFACE(IDiskfont, DiskfontBase))
313 #endif
314 if((lib->pool = CreatePool(MEMF_ANY, 4096, 512)))
316 if(codesetsInit(&lib->codesets))
318 lib->systemCodeset = (struct codeset *)lib->codesets.list.mlh_Head;
320 if((LocaleBase = (APTR)OpenLibrary("locale.library", 37)) &&
321 GETINTERFACE(ILocale, LocaleBase))
323 getSystemCodeset(lib);
326 RETURN(TRUE);
327 return TRUE;
330 #if defined(__amigaos4__)
332 #endif
336 freeBase(lib);
338 RETURN(FALSE);
339 return FALSE;
342 /***********************************************************************/