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/
21 ***************************************************************************/
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>
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
;
51 #elif defined(__AROS__)
52 struct DosLibrary
*DOSBase
= NULL
;
53 struct UtilityBase
*UtilityBase
= NULL
;
55 struct DosLibrary
*DOSBase
= NULL
;
56 struct Library
*UtilityBase
= NULL
;
57 #if defined(__MORPHOS__)
58 struct Library
*LocaleBase
= NULL
;
60 struct LocaleBase
*LocaleBase
= NULL
;
62 struct Library
*__UtilityBase
= NULL
; // required by clib2 & libnix
65 /****************************************************************************/
68 freeBase(struct LibraryHeader
*lib
)
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
80 DROPINTERFACE(ILocale
);
81 CloseLibrary((struct Library
*)LocaleBase
);
85 #if defined(__amigaos4__)
86 // close diskfont.library
89 DROPINTERFACE(IDiskfont
);
90 CloseLibrary((struct Library
*)DiskfontBase
);
95 // delete our private memory pool
98 DeletePool(lib
->pool
);
102 // close utility.library
105 DROPINTERFACE(IUtility
);
106 CloseLibrary(UtilityBase
);
114 CloseLibrary((struct Library
*)DOSBase
);
122 /***********************************************************************/
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" },
167 getSystemCodeset(struct LibraryHeader
*lib
)
169 struct codeset
*foundCodeset
= NULL
;
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
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
);
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
)
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
)
209 if(GetVar("LANGUAGE", language
, sizeof(language
), 0) > 0)
212 struct loc
*curLoc
= NULL
;
217 curLoc
= (struct loc
*)&locs
[i
];
218 if(curLoc
== NULL
|| curLoc
->name
== NULL
)
221 if(!strnicmp(language
, curLoc
->name
, curLoc
->len
))
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
)))
245 char *language
= locale
->loc_LanguageName
;
246 struct loc
*curLoc
= NULL
;
251 curLoc
= (struct loc
*)&locs
[i
];
252 if(curLoc
== NULL
|| curLoc
->name
== NULL
)
255 if(!strnicmp(language
, curLoc
->name
, curLoc
->len
))
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");
277 lib
->systemCodeset
= foundCodeset
;
282 /***********************************************************************/
285 initBase(struct LibraryHeader
*lib
)
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
;
304 // setup the debugging stuff
309 #if defined(__amigaos4__)
310 if((DiskfontBase
= OpenLibrary("diskfont.library", 50)) &&
311 GETINTERFACE(IDiskfont
, DiskfontBase
))
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
);
330 #if defined(__amigaos4__)
342 /***********************************************************************/