Add GNU LGPL headers to all .c .C and .h files
[oweals/cde.git] / cde / programs / dtudcfonted / libfal / _fallcTxtPr.c
1 /*
2  * CDE - Common Desktop Environment
3  *
4  * Copyright (c) 1993-2012, The Open Group. All rights reserved.
5  *
6  * These libraries and programs are free software; you can
7  * redistribute them and/or modify them under the terms of the GNU
8  * Lesser General Public License as published by the Free Software
9  * Foundation; either version 2 of the License, or (at your option)
10  * any later version.
11  *
12  * These libraries and programs are distributed in the hope that
13  * they will be useful, but WITHOUT ANY WARRANTY; without even the
14  * implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
15  * PURPOSE. See the GNU Lesser General Public License for more
16  * details.
17  *
18  * You should have received a copy of the GNU Lesser General Public
19  * License along with these librararies and programs; if not, write
20  * to the Free Software Foundation, Inc., 51 Franklin Street, Fifth
21  * Floor, Boston, MA 02110-1301 USA
22  */
23 /* lcTxtPr.c 1.1 - Fujitsu source for CDEnext    95/11/06 20:32:41      */ 
24 /* $XConsortium: _fallcTxtPr.c /main/1 1996/04/08 15:19:12 cde-fuj $ */
25 /*
26  * Copyright 1992, 1993 by TOSHIBA Corp.
27  *
28  * Permission to use, copy, modify, and distribute this software and its
29  * documentation for any purpose and without fee is hereby granted, provided
30  * that the above copyright notice appear in all copies and that both that
31  * copyright notice and this permission notice appear in supporting
32  * documentation, and that the name of TOSHIBA not be used in advertising
33  * or publicity pertaining to distribution of the software without specific,
34  * written prior permission. TOSHIBA make no representations about the
35  * suitability of this software for any purpose.  It is provided "as is"
36  * without express or implied warranty.
37  *
38  * TOSHIBA DISCLAIM ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING
39  * ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL
40  * TOSHIBA BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR
41  * ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
42  * WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION,
43  * ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
44  * SOFTWARE.
45  *
46  * Author: Katsuhisa Yano       TOSHIBA Corp.
47  *                              mopi@osa.ilab.toshiba.co.jp
48  */
49
50 #include "_fallibint.h"
51 #include "_fallcPubI.h"
52 #include "_falutil.h"
53 #include <X11/Xatom.h>
54 #include <stdio.h>
55
56 static int
57 get_buf_size(is_wide_char, list, count)
58     Bool is_wide_char;
59     XPointer list;
60     int count;
61 {
62     register length = 0;
63     register char **mb_list;
64     register wchar_t **wc_list;
65
66     if (list == NULL)
67         return 0;
68
69     if (is_wide_char) {
70         wc_list = (wchar_t **) list;
71         for ( ; count-- > 0; wc_list++) {
72             if (*wc_list)
73                 length += _falwcslen(*wc_list) + 1;
74         }
75         length *= 5;    /* XXX */
76     } else {
77         mb_list = (char **) list;
78         for ( ; count-- > 0; mb_list++) {
79             if (*mb_list)
80                 length += strlen(*mb_list) + 1;
81         }
82         length *= 3;    /* XXX */
83     }
84     length = (length / BUFSIZ + 1) * BUFSIZ;    /* XXX */
85
86     return length;
87 }
88
89 static int
90 _XTextListToTextProperty(lcd, dpy, from_type, list, count, style, text_prop)
91     XLCd lcd;
92     Display *dpy;
93     char *from_type;
94     XPointer list;
95     int count;
96     XICCEncodingStyle style;
97     XTextProperty *text_prop;
98 {
99     Atom encoding;
100     XlcConv conv;
101     char *to_type;
102     char **mb_list;
103     wchar_t **wc_list;
104     XPointer from;
105     char *to, *buf, *value;
106     int from_left, to_left, buf_len, nitems, unconv_num, ret, i;
107     Bool is_wide_char = False;
108
109     if (strcmp(XlcNWideChar, from_type) == 0)
110         is_wide_char = True;
111
112     buf_len = get_buf_size(is_wide_char, list, count);
113     if ((buf = (char *) Xmalloc(buf_len)) == NULL)
114         return XNoMemory;
115     
116     switch (style) {
117         case XStringStyle:
118         case XStdICCTextStyle:
119             encoding = XA_STRING;
120             to_type = XlcNString;
121             break;
122         case XCompoundTextStyle:
123             encoding = falInternAtom(dpy, "COMPOUND_TEXT", False);
124             to_type = XlcNCompoundText;
125             break;
126         case XTextStyle:
127             encoding = falInternAtom(dpy, XLC_PUBLIC(lcd, encoding_name), False);
128             to_type = XlcNMultiByte;
129             if (is_wide_char == False) {
130                 nitems = 0;
131                 mb_list = (char **) list;
132                 to = buf;
133                 for (i = 0; i < count; i++) {
134                     strcpy(to, *mb_list);
135                     from_left = strlen(*mb_list) + 1;
136                     nitems += from_left;
137                     to += from_left;
138                     mb_list++;
139                 }
140                 unconv_num = 0;
141                 goto done;
142             }
143             break;
144         default:
145             Xfree(buf);
146             return XConverterNotFound;
147     }
148
149     if (count < 1) {
150         nitems = 0;
151         goto done;
152     }
153
154 retry:
155     conv = _fallcOpenConverter(lcd, from_type, lcd, to_type);
156     if (conv == NULL) {
157         Xfree(buf);
158         return XConverterNotFound;
159     }
160
161     if (is_wide_char)
162         wc_list = (wchar_t **) list;
163     else
164         mb_list = (char **) list;
165
166     to = buf;
167     to_left = buf_len;
168
169     unconv_num = 0;
170
171     for (i = 1; to_left > 0; i++) {
172         if (is_wide_char) {
173             from = (XPointer) *wc_list;
174             from_left = _falwcslen(*wc_list);
175             wc_list++;
176         } else {
177             from = (XPointer) *mb_list;
178             from_left = strlen(*mb_list);
179             mb_list++;
180         }
181
182         ret = _fallcConvert(conv, &from, &from_left, (XPointer *) &to, &to_left,
183                           NULL, 0);
184
185         if (ret < 0)
186             continue;
187
188         if (ret > 0 && style == XStdICCTextStyle && encoding == XA_STRING) {
189             _fallcCloseConverter(conv);
190             encoding = falInternAtom(dpy, "COMPOUND_TEXT", False);
191             to_type = XlcNCompoundText;
192             goto retry;
193         }
194
195         unconv_num += ret;
196         *to++ = '\0';
197         to_left--;
198
199         if (i >= count)
200             break;
201
202         _fallcResetConverter(conv);
203     }
204
205     _fallcCloseConverter(conv);
206
207     nitems = to - buf;
208 done:
209     if (nitems <= 0)
210         nitems = 1;
211     value = (char *) Xmalloc(nitems);
212     if (value == NULL) {
213         Xfree(buf);
214         return XNoMemory;
215     }
216     if (nitems == 1)
217         *value = 0;
218     else
219         memcpy(value, buf, nitems);
220     nitems--;
221     Xfree(buf);
222
223     text_prop->value = (unsigned char *) value;
224     text_prop->encoding = encoding;
225     text_prop->format = 8;
226     text_prop->nitems = nitems;
227
228     return unconv_num;
229 }
230
231 int
232 _falmbTextListToTextProperty(lcd, dpy, list, count, style, text_prop)
233     XLCd lcd;
234     Display *dpy;
235     char **list;
236     int count;
237     XICCEncodingStyle style;
238     XTextProperty *text_prop;
239 {
240     return _XTextListToTextProperty(lcd, dpy, XlcNMultiByte, (XPointer) list,
241                                     count, style, text_prop);
242 }
243
244 int
245 _falwcTextListToTextProperty(lcd, dpy, list, count, style, text_prop)
246     XLCd lcd;
247     Display *dpy;
248     wchar_t **list;
249     int count;
250     XICCEncodingStyle style;
251     XTextProperty *text_prop;
252 {
253     return _XTextListToTextProperty(lcd, dpy, XlcNWideChar, (XPointer) list,
254                                     count, style, text_prop);
255 }