util/dttypes: remove register keyword
[oweals/cde.git] / cde / programs / dtudcfonted / libfal / _fallcStd.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 libraries 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 /* lcStd.c 1.1 - Fujitsu source for CDEnext    95/11/06 20:32:41        */ 
24 /* $XConsortium: _fallcStd.c /main/1 1996/04/08 15:19:02 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
53 int
54 _fallcmbtowc(lcd, wstr, str, len)
55     XLCd lcd;
56     wchar_t *wstr;
57     char *str;
58     int len;
59 {
60     static XLCd last_lcd = NULL;
61     static XlcConv conv = NULL;
62     XPointer from, to;
63     int from_left, to_left;
64     wchar_t tmp_wc;
65
66     if (lcd == NULL) {
67         lcd = _fallcCurrentLC();
68         if (lcd == NULL)
69             return -1;
70     }
71     if (str == NULL)
72         return XLC_PUBLIC(lcd, is_state_depend);
73
74     if (conv && lcd != last_lcd) {
75         _fallcCloseConverter(conv);
76         conv = NULL;
77     }
78
79     last_lcd = lcd;
80
81     if (conv == NULL) {
82         conv = _fallcOpenConverter(lcd, XlcNMultiByte, lcd, XlcNWideChar);
83         if (conv == NULL)
84             return -1;
85     }
86
87     from = (XPointer) str;
88     from_left = len;
89     to = (XPointer) (wstr ? wstr : &tmp_wc);
90     to_left = 1;
91
92     if (_fallcConvert(conv, &from, &from_left, &to, &to_left, NULL, 0) < 0)
93         return -1;
94
95     return (len - from_left);
96 }
97
98 int
99 _fallcwctomb(lcd, str, wc)
100     XLCd lcd;
101     char *str;
102     wchar_t wc;
103 {
104     static XLCd last_lcd = NULL;
105     static XlcConv conv = NULL;
106     XPointer from, to;
107     int from_left, to_left, length;
108
109     if (lcd == NULL) {
110         lcd = _fallcCurrentLC();
111         if (lcd == NULL)
112             return -1;
113     }
114     if (str == NULL)
115         return XLC_PUBLIC(lcd, is_state_depend);
116
117     if (conv && lcd != last_lcd) {
118         _fallcCloseConverter(conv);
119         conv = NULL;
120     }
121
122     last_lcd = lcd;
123
124     if (conv == NULL) {
125         conv = _fallcOpenConverter(lcd, XlcNWideChar, lcd, XlcNMultiByte);
126         if (conv == NULL)
127             return -1;
128     }
129
130     from = (XPointer) &wc;
131     from_left = 1;
132     to = (XPointer) str;
133     length = to_left = XLC_PUBLIC(lcd, mb_cur_max);
134
135     if (_fallcConvert(conv, &from, &from_left, &to, &to_left, NULL, 0) < 0)
136         return -1;
137
138     return (length - to_left);
139 }
140
141 int
142 _fallcmbstowcs(lcd, wstr, str, len)
143     XLCd lcd;
144     wchar_t *wstr;
145     char *str;
146     int len;
147 {
148     XlcConv conv;
149     XPointer from, to;
150     int from_left, to_left, ret;
151
152     if (lcd == NULL) {
153         lcd = _fallcCurrentLC();
154         if (lcd == NULL)
155             return -1;
156     }
157     
158     conv = _fallcOpenConverter(lcd, XlcNMultiByte, lcd, XlcNWideChar);
159     if (conv == NULL)
160         return -1;
161
162     from = (XPointer) str;
163     from_left = strlen(str);
164     to = (XPointer) wstr;
165     to_left = len;
166
167     if (_fallcConvert(conv, &from, &from_left, &to, &to_left, NULL, 0) < 0)
168         ret = -1;
169     else {
170         ret = len - to_left;
171         if (wstr && to_left > 0)
172             wstr[ret] = (wchar_t) 0;
173     }
174
175     _fallcCloseConverter(conv);
176
177     return ret;
178 }
179
180 int
181 _fallcwcstombs(lcd, str, wstr, len)
182     XLCd lcd;
183     char *str;
184     wchar_t *wstr;
185     int len;
186 {
187     XlcConv conv;
188     XPointer from, to;
189     int from_left, to_left, ret;
190
191     if (lcd == NULL) {
192         lcd = _fallcCurrentLC();
193         if (lcd == NULL)
194             return -1;
195     }
196
197     conv = _fallcOpenConverter(lcd, XlcNWideChar, lcd, XlcNMultiByte);
198     if (conv == NULL)
199         return -1;
200
201     from = (XPointer) wstr;
202     from_left = _falwcslen(wstr);
203     to = (XPointer) str;
204     to_left = len;
205
206     if (_fallcConvert(conv, &from, &from_left, &to, &to_left, NULL, 0) < 0)
207         ret = -1;
208     else {
209         ret = len - to_left;
210         if (str && to_left > 0)
211             str[ret] = '\0';
212     }
213
214     _fallcCloseConverter(conv);
215
216     return ret;
217 }
218
219
220 int
221 _falmbtowc(wstr, str, len)
222     wchar_t *wstr;
223     char *str;
224     int len;
225 {
226     return _fallcmbtowc((XLCd) NULL, wstr, str, len);
227 }
228
229 int
230 _falmblen(str, len)
231     char *str;
232     int len;
233 {
234     return _falmbtowc((wchar_t *) NULL, str, len);
235 }
236
237 int
238 _falwctomb(str, wc)
239     char *str;
240     wchar_t wc;
241 {
242     return _fallcwctomb((XLCd) NULL, str, wc);
243 }
244
245 int
246 _falmbstowcs(wstr, str, len)
247     wchar_t *wstr;
248     char *str;
249     int len;
250 {
251     return _fallcmbstowcs((XLCd) NULL, wstr, str, len);
252 }
253
254 int
255 _falwcstombs(str, wstr, len)
256     char *str;
257     wchar_t *wstr;
258     int len;
259 {
260     return _fallcwcstombs((XLCd) NULL, str, wstr, len);
261 }
262
263 wchar_t *
264 _falwcscpy(wstr1, wstr2)
265     register wchar_t *wstr1, *wstr2;
266 {
267     wchar_t *wstr_tmp = wstr1;
268
269     while (*wstr1++ = *wstr2++)
270         ;
271
272     return wstr_tmp;
273 }
274
275 wchar_t *
276 _falwcsncpy(wstr1, wstr2, len)
277     register wchar_t *wstr1, *wstr2;
278     register len;
279 {
280     wchar_t *wstr_tmp = wstr1;
281
282     while (len-- > 0)
283         if (!(*wstr1++ = *wstr2++))
284             break;
285
286     while (len-- > 0)
287         *wstr1++ = (wchar_t) 0;
288
289     return wstr_tmp;
290 }
291
292 int
293 _falwcslen(wstr)
294     register wchar_t *wstr;
295 {
296     register wchar_t *wstr_ptr = wstr;
297
298     while (*wstr_ptr)
299         wstr_ptr++;
300     
301     return wstr_ptr - wstr;
302 }
303
304 int
305 _falwcscmp(wstr1, wstr2)
306     register wchar_t *wstr1, *wstr2;
307 {
308     for ( ; *wstr1 && *wstr2; wstr1++, wstr2++)
309         if (*wstr1 != *wstr2)
310             break;
311
312     return *wstr1 - *wstr2;
313 }
314
315 int
316 _falwcsncmp(wstr1, wstr2, len)
317     register wchar_t *wstr1, *wstr2;
318     register len;
319 {
320     for ( ; *wstr1 && *wstr2 && len > 0; wstr1++, wstr2++, len--)
321         if (*wstr1 != *wstr2)
322             break;
323
324     if (len <= 0)
325         return 0;
326
327     return *wstr1 - *wstr2;
328 }