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