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