lcPublic.c revision 61b2299d
1/* $Xorg: lcPublic.c,v 1.4 2000/12/12 12:44:05 coskrey Exp $ */
2/*
3 * Copyright 1992, 1993 by TOSHIBA Corp.
4 *
5 * Permission to use, copy, modify, and distribute this software and its
6 * documentation for any purpose and without fee is hereby granted, provided
7 * that the above copyright notice appear in all copies and that both that
8 * copyright notice and this permission notice appear in supporting
9 * documentation, and that the name of TOSHIBA not be used in advertising
10 * or publicity pertaining to distribution of the software without specific,
11 * written prior permission. TOSHIBA make no representations about the
12 * suitability of this software for any purpose.  It is provided "as is"
13 * without express or implied warranty.
14 *
15 * TOSHIBA DISCLAIM ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING
16 * ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL
17 * TOSHIBA BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR
18 * ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
19 * WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION,
20 * ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
21 * SOFTWARE.
22 *
23 * Author: Katsuhisa Yano	TOSHIBA Corp.
24 *			   	mopi@osa.ilab.toshiba.co.jp
25 */
26/* $XFree86: xc/lib/X11/lcPublic.c,v 1.11 2001/11/16 00:52:28 dawes Exp $ */
27
28#ifdef HAVE_CONFIG_H
29#include <config.h>
30#endif
31#include <stdio.h>
32#include "Xlibint.h"
33#include "XlcPubI.h"
34
35static const char *
36default_string(
37    XLCd lcd)
38{
39    return XLC_PUBLIC(lcd, default_string);
40}
41
42static XLCd create (const char *name, XLCdMethods methods);
43static Bool initialize (XLCd lcd);
44static void destroy (XLCd lcd);
45static char *get_values (XLCd lcd, XlcArgList args, int num_args);
46
47static XLCdPublicMethodsRec publicMethods = {
48    {
49	destroy,
50	_XlcDefaultMapModifiers,
51	NULL,
52	NULL,
53	_XrmDefaultInitParseInfo,
54	_XmbTextPropertyToTextList,
55	_XwcTextPropertyToTextList,
56	_Xutf8TextPropertyToTextList,
57	_XmbTextListToTextProperty,
58	_XwcTextListToTextProperty,
59	_Xutf8TextListToTextProperty,
60	_XwcFreeStringList,
61	default_string,
62	NULL,
63	NULL
64    },
65    {
66	NULL,
67	create,
68	initialize,
69	destroy,
70	get_values,
71	_XlcGetLocaleDataBase
72    }
73};
74
75XLCdMethods _XlcPublicMethods = (XLCdMethods) &publicMethods;
76
77static XLCd
78create(
79    const char *name,
80    XLCdMethods methods)
81{
82    XLCd lcd;
83    XLCdPublicMethods new;
84
85    lcd = (XLCd) Xmalloc(sizeof(XLCdRec));
86    if (lcd == NULL)
87        return (XLCd) NULL;
88    bzero((char *) lcd, sizeof(XLCdRec));
89
90    lcd->core = (XLCdCore) Xmalloc(sizeof(XLCdPublicRec));
91    if (lcd->core == NULL)
92	goto err;
93    bzero((char *) lcd->core, sizeof(XLCdPublicRec));
94
95    new = (XLCdPublicMethods) Xmalloc(sizeof(XLCdPublicMethodsRec));
96    if (new == NULL)
97	goto err;
98    memcpy(new,methods,sizeof(XLCdPublicMethodsRec));
99    lcd->methods = (XLCdMethods) new;
100
101    return lcd;
102
103err:
104    Xfree(lcd);
105    return (XLCd) NULL;
106}
107
108static Bool
109load_public(
110    XLCd lcd)
111{
112    XLCdPublicPart *pub = XLC_PUBLIC_PART(lcd);
113    char **values, *str;
114    int num;
115
116    if(_XlcCreateLocaleDataBase(lcd) == NULL)
117	return False;
118
119    _XlcGetResource(lcd, "XLC_XLOCALE", "mb_cur_max", &values, &num);
120    if (num > 0) {
121	pub->mb_cur_max = atoi(values[0]);
122	if (pub->mb_cur_max < 1)
123	    pub->mb_cur_max = 1;
124    } else
125	pub->mb_cur_max = 1;
126
127    _XlcGetResource(lcd, "XLC_XLOCALE", "state_depend_encoding", &values, &num);
128    if (num > 0 && !_XlcCompareISOLatin1(values[0], "True"))
129	pub->is_state_depend = True;
130    else
131	pub->is_state_depend = False;
132
133    _XlcGetResource(lcd, "XLC_XLOCALE", "encoding_name", &values, &num);
134    str = (num > 0) ? values[0] : "STRING";
135    pub->encoding_name = (char*) Xmalloc(strlen(str) + 1);
136    if (pub->encoding_name == NULL)
137	return False;
138    strcpy(pub->encoding_name, str);
139
140    return True;
141}
142
143static Bool
144initialize_core(
145    XLCd lcd)
146{
147    XLCdMethods methods = lcd->methods;
148    XLCdMethods core = &publicMethods.core;
149
150    if (methods->close == NULL)
151	methods->close = core->close;
152
153    if (methods->map_modifiers == NULL)
154	methods->map_modifiers = core->map_modifiers;
155
156    if (methods->open_om == NULL)
157#ifdef USE_DYNAMIC_LC
158	_XInitDefaultOM(lcd);
159#else
160	_XInitOM(lcd);
161#endif
162
163    if (methods->open_im == NULL)
164#ifdef USE_DYNAMIC_LC
165	_XInitDefaultIM(lcd);
166#else
167	_XInitIM(lcd);
168#endif
169
170    if (methods->init_parse_info == NULL)
171	methods->init_parse_info = core->init_parse_info;
172
173    if (methods->mb_text_prop_to_list == NULL)
174	methods->mb_text_prop_to_list = core->mb_text_prop_to_list;
175
176    if (methods->wc_text_prop_to_list == NULL)
177	methods->wc_text_prop_to_list = core->wc_text_prop_to_list;
178
179    if (methods->utf8_text_prop_to_list == NULL)
180	methods->utf8_text_prop_to_list = core->utf8_text_prop_to_list;
181
182    if (methods->mb_text_list_to_prop == NULL)
183	methods->mb_text_list_to_prop = core->mb_text_list_to_prop;
184
185    if (methods->wc_text_list_to_prop == NULL)
186	methods->wc_text_list_to_prop = core->wc_text_list_to_prop;
187
188    if (methods->utf8_text_list_to_prop == NULL)
189	methods->utf8_text_list_to_prop = core->utf8_text_list_to_prop;
190
191    if (methods->wc_free_string_list == NULL)
192	methods->wc_free_string_list = core->wc_free_string_list;
193
194    if (methods->default_string == NULL)
195	methods->default_string = core->default_string;
196
197    return True;
198}
199
200static Bool
201initialize(
202    XLCd lcd)
203{
204    XLCdPublicMethodsPart *methods = XLC_PUBLIC_METHODS(lcd);
205    XLCdPublicMethodsPart *pub_methods = &publicMethods.pub;
206    XLCdPublicPart *pub = XLC_PUBLIC_PART(lcd);
207    char *name;
208#if !defined(X_LOCALE)
209    int len;
210    char sinamebuf[256];
211    char* siname;
212#endif
213
214    _XlcInitCTInfo();
215
216    if (initialize_core(lcd) == False)
217	return False;
218
219    name = lcd->core->name;
220#if !defined(X_LOCALE)
221    /*
222     * _XlMapOSLocaleName will return the same string or a substring
223     * of name, so strlen(name) is okay
224     */
225    if ((len = strlen(name)) < sizeof sinamebuf)
226        siname = sinamebuf;
227    else
228        siname = Xmalloc (len + 1);
229    if (siname == NULL)
230        return False;
231    name = _XlcMapOSLocaleName(name, siname);
232#endif
233    /* _XlcResolveLocaleName will lookup the SI's name for the locale */
234    if (_XlcResolveLocaleName(name, pub) == 0) {
235#if !defined(X_LOCALE)
236	if (siname != sinamebuf) Xfree (siname);
237#endif
238	return False;
239    }
240#if !defined(X_LOCALE)
241    if (siname != sinamebuf)
242        Xfree (siname);
243#endif
244
245    if (pub->default_string == NULL)
246	pub->default_string = "";
247
248    if (methods->get_values == NULL)
249	methods->get_values = pub_methods->get_values;
250
251    if (methods->get_resource == NULL)
252	methods->get_resource = pub_methods->get_resource;
253
254    return load_public(lcd);
255}
256
257static void
258destroy_core(
259    XLCd lcd)
260{
261    if (lcd->core) {
262	if (lcd->core->name)
263            Xfree(lcd->core->name);
264	Xfree(lcd->core);
265    }
266
267    if (lcd->methods)
268	Xfree(lcd->methods);
269
270    Xfree(lcd);
271}
272
273static void
274destroy(
275    XLCd lcd)
276{
277    XLCdPublicPart *pub = XLC_PUBLIC_PART(lcd);
278
279    _XlcDestroyLocaleDataBase(lcd);
280
281    if (pub->siname)
282	Xfree(pub->siname);
283    if (pub->encoding_name)
284	Xfree(pub->encoding_name);
285
286    destroy_core(lcd);
287}
288
289static XlcResource resources[] = {
290    { XlcNCodeset, NULLQUARK, sizeof(char *),
291      XOffsetOf(XLCdPublicRec, pub.codeset), XlcGetMask },
292    { XlcNDefaultString, NULLQUARK, sizeof(char *),
293      XOffsetOf(XLCdPublicRec, pub.default_string), XlcGetMask },
294    { XlcNEncodingName, NULLQUARK, sizeof(char *),
295      XOffsetOf(XLCdPublicRec, pub.encoding_name), XlcGetMask },
296    { XlcNLanguage, NULLQUARK, sizeof(char *),
297      XOffsetOf(XLCdPublicRec, pub.language), XlcGetMask },
298    { XlcNMbCurMax, NULLQUARK, sizeof(int),
299      XOffsetOf(XLCdPublicRec, pub.mb_cur_max), XlcGetMask },
300    { XlcNStateDependentEncoding, NULLQUARK, sizeof(Bool),
301      XOffsetOf(XLCdPublicRec, pub.is_state_depend), XlcGetMask },
302    { XlcNTerritory, NULLQUARK, sizeof(char *),
303      XOffsetOf(XLCdPublicRec, pub.territory), XlcGetMask }
304};
305
306static char *
307get_values(
308    XLCd lcd,
309    XlcArgList args,
310    int num_args)
311{
312    XLCdPublic pub = (XLCdPublic) lcd->core;
313
314    if (resources[0].xrm_name == NULLQUARK)
315	_XlcCompileResourceList(resources, XlcNumber(resources));
316
317    return _XlcGetValues((XPointer) pub, resources, XlcNumber(resources), args,
318			 num_args, XlcGetMask);
319}
320