lcCharSet.c revision b4ee4795
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 "XlcPublic.h"
32#include "XlcPubI.h"
33
34/* The list of all known XlcCharSets. They are identified by their name. */
35
36typedef struct _XlcCharSetListRec {
37    XlcCharSet charset;
38    struct _XlcCharSetListRec *next;
39} XlcCharSetListRec, *XlcCharSetList;
40
41static XlcCharSetList charset_list = NULL;
42
43/* Returns the charset with the given name (including side suffix).
44   Returns NULL if not found. */
45XlcCharSet
46_XlcGetCharSet(
47    const char *name)
48{
49    XlcCharSetList list;
50    XrmQuark xrm_name;
51
52    xrm_name = XrmStringToQuark(name);
53
54    for (list = charset_list; list; list = list->next) {
55	if (xrm_name == list->charset->xrm_name)
56	    return (XlcCharSet) list->charset;
57    }
58
59    return (XlcCharSet) NULL;
60}
61
62/* Returns the charset with the given encoding (no side suffix) and
63   responsible for at least the given side (XlcGL or XlcGR).
64   Returns NULL if not found. */
65XlcCharSet
66_XlcGetCharSetWithSide(
67    const char *encoding_name,
68    XlcSide side)
69{
70    XlcCharSetList list;
71    XrmQuark xrm_encoding_name;
72
73    xrm_encoding_name = XrmStringToQuark(encoding_name);
74
75    for (list = charset_list; list; list = list->next) {
76	if (list->charset->xrm_encoding_name == xrm_encoding_name
77	    && (list->charset->side == XlcGLGR || list->charset->side == side))
78	    return (XlcCharSet) list->charset;
79    }
80
81    return (XlcCharSet) NULL;
82}
83
84/* Registers an XlcCharSet in the list of character sets.
85   Returns True if successful. */
86Bool
87_XlcAddCharSet(
88    XlcCharSet charset)
89{
90    XlcCharSetList list;
91
92    if (_XlcGetCharSet(charset->name))
93	return False;
94
95    list = (XlcCharSetList) Xmalloc(sizeof(XlcCharSetListRec));
96    if (list == NULL)
97	return False;
98
99    list->charset = charset;
100    list->next = charset_list;
101    charset_list = list;
102
103    return True;
104}
105
106/* List of resources for XlcCharSet. */
107static XlcResource resources[] = {
108    { XlcNName, NULLQUARK, sizeof(char *),
109      XOffsetOf(XlcCharSetRec, name), XlcGetMask },
110    { XlcNEncodingName, NULLQUARK, sizeof(char *),
111      XOffsetOf(XlcCharSetRec, encoding_name), XlcGetMask },
112    { XlcNSide, NULLQUARK, sizeof(XlcSide),
113      XOffsetOf(XlcCharSetRec, side), XlcGetMask },
114    { XlcNCharSize, NULLQUARK, sizeof(int),
115      XOffsetOf(XlcCharSetRec, char_size), XlcGetMask },
116    { XlcNSetSize, NULLQUARK, sizeof(int),
117      XOffsetOf(XlcCharSetRec, set_size), XlcGetMask },
118    { XlcNControlSequence, NULLQUARK, sizeof(char *),
119      XOffsetOf(XlcCharSetRec, ct_sequence), XlcGetMask }
120};
121
122/* Retrieves a number of attributes of an XlcCharSet.
123   Return NULL if successful, otherwise the name of the first argument
124   specifiying a nonexistent attribute. */
125static char *
126get_values(
127    XlcCharSet charset,
128    XlcArgList args,
129    int num_args)
130{
131    if (resources[0].xrm_name == NULLQUARK)
132	_XlcCompileResourceList(resources, XlcNumber(resources));
133
134    return _XlcGetValues((XPointer) charset, resources, XlcNumber(resources),
135			 args, num_args, XlcGetMask);
136}
137
138/* Retrieves a number of attributes of an XlcCharSet.
139   Return NULL if successful, otherwise the name of the first argument
140   specifiying a nonexistent attribute. */
141char *
142_XlcGetCSValues(XlcCharSet charset, ...)
143{
144    va_list var;
145    XlcArgList args;
146    char *ret;
147    int num_args;
148
149    va_start(var, charset);
150    _XlcCountVaList(var, &num_args);
151    va_end(var);
152
153    va_start(var, charset);
154    _XlcVaToArgList(var, num_args, &args);
155    va_end(var);
156
157    if (args == (XlcArgList) NULL)
158	return (char *) NULL;
159
160    ret = get_values(charset, args, num_args);
161
162    Xfree(args);
163
164    return ret;
165}
166
167/* Creates a new XlcCharSet, given its name (including side suffix) and
168   Compound Text ESC sequence (normally at most 4 bytes). */
169XlcCharSet
170_XlcCreateDefaultCharSet(
171    const char *name,
172    const char *ct_sequence)
173{
174    XlcCharSet charset;
175    int name_len, ct_sequence_len;
176    const char *colon;
177    char *tmp;
178
179    charset = (XlcCharSet) Xmalloc(sizeof(XlcCharSetRec));
180    if (charset == NULL)
181	return (XlcCharSet) NULL;
182    bzero((char *) charset, sizeof(XlcCharSetRec));
183
184    name_len = strlen(name);
185    ct_sequence_len = strlen(ct_sequence);
186
187    /* Fill in name and xrm_name.  */
188    tmp = (char *) Xmalloc(name_len + 1 + ct_sequence_len + 1);
189    if (tmp == NULL) {
190	Xfree((char *) charset);
191	return (XlcCharSet) NULL;
192    }
193    memcpy(tmp, name, name_len+1);
194    charset->name = tmp;
195    charset->xrm_name = XrmStringToQuark(charset->name);
196
197    /* Fill in encoding_name and xrm_encoding_name.  */
198    if ((colon = strchr(charset->name, ':')) != NULL) {
199        unsigned int length = colon - charset->name;
200        char *encoding_tmp = (char *) Xmalloc(length + 1);
201        if (encoding_tmp == NULL) {
202            Xfree((char *) charset->name);
203            Xfree((char *) charset);
204            return (XlcCharSet) NULL;
205        }
206        memcpy(encoding_tmp, charset->name, length);
207        encoding_tmp[length] = '\0';
208        charset->encoding_name = encoding_tmp;
209        charset->xrm_encoding_name = XrmStringToQuark(charset->encoding_name);
210    } else {
211        charset->encoding_name = charset->name;
212        charset->xrm_encoding_name = charset->xrm_name;
213    }
214
215    /* Fill in ct_sequence.  */
216    tmp += name_len + 1;
217    memcpy(tmp, ct_sequence, ct_sequence_len+1);
218    charset->ct_sequence = tmp;
219
220    /* Fill in side, char_size, set_size. */
221    if (!_XlcParseCharSet(charset))
222        /* If ct_sequence is not usable in Compound Text, remove it. */
223        charset->ct_sequence = "";
224
225    return (XlcCharSet) charset;
226}
227