lcPublic.c revision 818534a1
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 = 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;
110    const char *str;
111    int num;
112
113    if(_XlcCreateLocaleDataBase(lcd) == NULL)
114	return False;
115
116    _XlcGetResource(lcd, "XLC_XLOCALE", "mb_cur_max", &values, &num);
117    if (num > 0) {
118	pub->mb_cur_max = atoi(values[0]);
119	if (pub->mb_cur_max < 1)
120	    pub->mb_cur_max = 1;
121    } else
122	pub->mb_cur_max = 1;
123
124    _XlcGetResource(lcd, "XLC_XLOCALE", "state_depend_encoding", &values, &num);
125    if (num > 0 && !_XlcCompareISOLatin1(values[0], "True"))
126	pub->is_state_depend = True;
127    else
128	pub->is_state_depend = False;
129
130    _XlcGetResource(lcd, "XLC_XLOCALE", "encoding_name", &values, &num);
131    str = (num > 0) ? values[0] : "STRING";
132    pub->encoding_name = strdup(str);
133    if (pub->encoding_name == NULL)
134	return False;
135
136    return True;
137}
138
139static Bool
140initialize_core(
141    XLCd lcd)
142{
143    XLCdMethods methods = lcd->methods;
144    XLCdMethods core = &publicMethods.core;
145
146    if (methods->close == NULL)
147	methods->close = core->close;
148
149    if (methods->map_modifiers == NULL)
150	methods->map_modifiers = core->map_modifiers;
151
152    if (methods->open_om == NULL)
153#ifdef USE_DYNAMIC_LC
154	_XInitDefaultOM(lcd);
155#else
156	_XInitOM(lcd);
157#endif
158
159    if (methods->open_im == NULL)
160#ifdef USE_DYNAMIC_LC
161	_XInitDefaultIM(lcd);
162#else
163	_XInitIM(lcd);
164#endif
165
166    if (methods->init_parse_info == NULL)
167	methods->init_parse_info = core->init_parse_info;
168
169    if (methods->mb_text_prop_to_list == NULL)
170	methods->mb_text_prop_to_list = core->mb_text_prop_to_list;
171
172    if (methods->wc_text_prop_to_list == NULL)
173	methods->wc_text_prop_to_list = core->wc_text_prop_to_list;
174
175    if (methods->utf8_text_prop_to_list == NULL)
176	methods->utf8_text_prop_to_list = core->utf8_text_prop_to_list;
177
178    if (methods->mb_text_list_to_prop == NULL)
179	methods->mb_text_list_to_prop = core->mb_text_list_to_prop;
180
181    if (methods->wc_text_list_to_prop == NULL)
182	methods->wc_text_list_to_prop = core->wc_text_list_to_prop;
183
184    if (methods->utf8_text_list_to_prop == NULL)
185	methods->utf8_text_list_to_prop = core->utf8_text_list_to_prop;
186
187    if (methods->wc_free_string_list == NULL)
188	methods->wc_free_string_list = core->wc_free_string_list;
189
190    if (methods->default_string == NULL)
191	methods->default_string = core->default_string;
192
193    return True;
194}
195
196static Bool
197initialize(
198    XLCd lcd)
199{
200    XLCdPublicMethodsPart *methods = XLC_PUBLIC_METHODS(lcd);
201    XLCdPublicMethodsPart *pub_methods = &publicMethods.pub;
202    XLCdPublicPart *pub = XLC_PUBLIC_PART(lcd);
203    char *name;
204#if !defined(X_LOCALE)
205    int len;
206    char sinamebuf[256];
207    char* siname;
208#endif
209
210    _XlcInitCTInfo();
211
212    if (initialize_core(lcd) == False)
213	return False;
214
215    name = lcd->core->name;
216#if !defined(X_LOCALE)
217    /*
218     * _XlMapOSLocaleName will return the same string or a substring
219     * of name, so strlen(name) is okay
220     */
221    if ((len = strlen(name)) < sizeof sinamebuf)
222        siname = sinamebuf;
223    else
224        siname = Xmalloc (len + 1);
225    if (siname == NULL)
226        return False;
227    name = _XlcMapOSLocaleName(name, siname);
228#endif
229    /* _XlcResolveLocaleName will lookup the SI's name for the locale */
230    if (_XlcResolveLocaleName(name, pub) == 0) {
231#if !defined(X_LOCALE)
232	if (siname != sinamebuf) Xfree (siname);
233#endif
234	return False;
235    }
236#if !defined(X_LOCALE)
237    if (siname != sinamebuf)
238        Xfree (siname);
239#endif
240
241    if (pub->default_string == NULL)
242	pub->default_string = "";
243
244    if (methods->get_values == NULL)
245	methods->get_values = pub_methods->get_values;
246
247    if (methods->get_resource == NULL)
248	methods->get_resource = pub_methods->get_resource;
249
250    return load_public(lcd);
251}
252
253static void
254destroy_core(
255    XLCd lcd)
256{
257    if (lcd->core) {
258	if (lcd->core->name)
259            Xfree(lcd->core->name);
260	Xfree(lcd->core);
261    }
262
263    if (lcd->methods)
264	Xfree(lcd->methods);
265
266    Xfree(lcd);
267}
268
269static void
270destroy(
271    XLCd lcd)
272{
273    XLCdPublicPart *pub = XLC_PUBLIC_PART(lcd);
274
275    _XlcDestroyLocaleDataBase(lcd);
276
277    if (pub->siname)
278	Xfree(pub->siname);
279    if (pub->encoding_name)
280	Xfree(pub->encoding_name);
281
282    destroy_core(lcd);
283}
284
285static XlcResource resources[] = {
286    { XlcNCodeset, NULLQUARK, sizeof(char *),
287      XOffsetOf(XLCdPublicRec, pub.codeset), XlcGetMask },
288    { XlcNDefaultString, NULLQUARK, sizeof(char *),
289      XOffsetOf(XLCdPublicRec, pub.default_string), XlcGetMask },
290    { XlcNEncodingName, NULLQUARK, sizeof(char *),
291      XOffsetOf(XLCdPublicRec, pub.encoding_name), XlcGetMask },
292    { XlcNLanguage, NULLQUARK, sizeof(char *),
293      XOffsetOf(XLCdPublicRec, pub.language), XlcGetMask },
294    { XlcNMbCurMax, NULLQUARK, sizeof(int),
295      XOffsetOf(XLCdPublicRec, pub.mb_cur_max), XlcGetMask },
296    { XlcNStateDependentEncoding, NULLQUARK, sizeof(Bool),
297      XOffsetOf(XLCdPublicRec, pub.is_state_depend), XlcGetMask },
298    { XlcNTerritory, NULLQUARK, sizeof(char *),
299      XOffsetOf(XLCdPublicRec, pub.territory), XlcGetMask }
300};
301
302static char *
303get_values(
304    XLCd lcd,
305    XlcArgList args,
306    int num_args)
307{
308    XLCdPublic pub = (XLCdPublic) lcd->core;
309
310    if (resources[0].xrm_name == NULLQUARK)
311	_XlcCompileResourceList(resources, XlcNumber(resources));
312
313    return _XlcGetValues((XPointer) pub, resources, XlcNumber(resources), args,
314			 num_args, XlcGetMask);
315}
316