| 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 |
* [email protected] |
| 24 |
*/ |
| 25 |
|
| 26 |
#ifdef HAVE_CONFIG_H |
| 27 |
#include <config.h> |
| 28 |
#endif |
| 29 |
#include "Xlibint.h" |
| 30 |
#include "XlcPubI.h" |
| 31 |
|
| 32 |
int |
| 33 |
_Xlcmbtowc( |
| 34 |
XLCd lcd, |
| 35 |
wchar_t *wstr, |
| 36 |
char *str, |
| 37 |
int len) |
| 38 |
{ |
| 39 |
XlcConv conv; |
| 40 |
XPointer from, to; |
| 41 |
int from_left, to_left; |
| 42 |
wchar_t tmp_wc; |
| 43 |
int ret; |
| 44 |
|
| 45 |
if (lcd == NULL) { |
| 46 |
lcd = _XlcCurrentLC(); |
| 47 |
if (lcd == NULL) |
| 48 |
return -1; |
| 49 |
} |
| 50 |
if (str == NULL) |
| 51 |
return XLC_PUBLIC(lcd, is_state_depend); |
| 52 |
|
| 53 |
conv = _XlcOpenConverter(lcd, XlcNMultiByte, lcd, XlcNWideChar); |
| 54 |
if (conv == NULL) |
| 55 |
return -1; |
| 56 |
|
| 57 |
from = (XPointer) str; |
| 58 |
from_left = len; |
| 59 |
to = (XPointer) (wstr ? wstr : &tmp_wc); |
| 60 |
to_left = 1; |
| 61 |
|
| 62 |
if (_XlcConvert(conv, &from, &from_left, &to, &to_left, NULL, 0) < 0) |
| 63 |
ret = -1; |
| 64 |
else |
| 65 |
ret = len - from_left; |
| 66 |
|
| 67 |
_XlcCloseConverter(conv); |
| 68 |
return ret; |
| 69 |
} |
| 70 |
|
| 71 |
int |
| 72 |
_Xlcwctomb( |
| 73 |
XLCd lcd, |
| 74 |
char *str, |
| 75 |
wchar_t wc) |
| 76 |
{ |
| 77 |
XlcConv conv; |
| 78 |
XPointer from, to; |
| 79 |
int from_left, to_left, length; |
| 80 |
int ret; |
| 81 |
|
| 82 |
if (lcd == NULL) { |
| 83 |
lcd = _XlcCurrentLC(); |
| 84 |
if (lcd == NULL) |
| 85 |
return -1; |
| 86 |
} |
| 87 |
if (str == NULL) |
| 88 |
return XLC_PUBLIC(lcd, is_state_depend); |
| 89 |
|
| 90 |
conv = _XlcOpenConverter(lcd, XlcNWideChar, lcd, XlcNMultiByte); |
| 91 |
if (conv == NULL) |
| 92 |
return -1; |
| 93 |
|
| 94 |
from = (XPointer) &wc; |
| 95 |
from_left = 1; |
| 96 |
to = (XPointer) str; |
| 97 |
length = to_left = XLC_PUBLIC(lcd, mb_cur_max); |
| 98 |
|
| 99 |
if (_XlcConvert(conv, &from, &from_left, &to, &to_left, NULL, 0) < 0) |
| 100 |
ret = -1; |
| 101 |
else |
| 102 |
ret = length - to_left; |
| 103 |
|
| 104 |
_XlcCloseConverter(conv); |
| 105 |
return ret; |
| 106 |
} |
| 107 |
|
| 108 |
int |
| 109 |
_Xlcmbstowcs( |
| 110 |
XLCd lcd, |
| 111 |
wchar_t *wstr, |
| 112 |
char *str, |
| 113 |
int len) |
| 114 |
{ |
| 115 |
XlcConv conv; |
| 116 |
XPointer from, to; |
| 117 |
int from_left, to_left, ret; |
| 118 |
|
| 119 |
if (lcd == NULL) { |
| 120 |
lcd = _XlcCurrentLC(); |
| 121 |
if (lcd == NULL) |
| 122 |
return -1; |
| 123 |
} |
| 124 |
|
| 125 |
conv = _XlcOpenConverter(lcd, XlcNMultiByte, lcd, XlcNWideChar); |
| 126 |
if (conv == NULL) |
| 127 |
return -1; |
| 128 |
|
| 129 |
from = (XPointer) str; |
| 130 |
from_left = (int) strlen(str); |
| 131 |
to = (XPointer) wstr; |
| 132 |
to_left = len; |
| 133 |
|
| 134 |
if (_XlcConvert(conv, &from, &from_left, &to, &to_left, NULL, 0) < 0) |
| 135 |
ret = -1; |
| 136 |
else { |
| 137 |
ret = len - to_left; |
| 138 |
if (wstr && to_left > 0) |
| 139 |
wstr[ret] = (wchar_t) 0; |
| 140 |
} |
| 141 |
|
| 142 |
_XlcCloseConverter(conv); |
| 143 |
|
| 144 |
return ret; |
| 145 |
} |
| 146 |
|
| 147 |
int |
| 148 |
_Xlcwcstombs( |
| 149 |
XLCd lcd, |
| 150 |
char *str, |
| 151 |
wchar_t *wstr, |
| 152 |
int len) |
| 153 |
{ |
| 154 |
XlcConv conv; |
| 155 |
XPointer from, to; |
| 156 |
int from_left, to_left, ret; |
| 157 |
|
| 158 |
if (lcd == NULL) { |
| 159 |
lcd = _XlcCurrentLC(); |
| 160 |
if (lcd == NULL) |
| 161 |
return -1; |
| 162 |
} |
| 163 |
|
| 164 |
conv = _XlcOpenConverter(lcd, XlcNWideChar, lcd, XlcNMultiByte); |
| 165 |
if (conv == NULL) |
| 166 |
return -1; |
| 167 |
|
| 168 |
from = (XPointer) wstr; |
| 169 |
from_left = _Xwcslen(wstr); |
| 170 |
to = (XPointer) str; |
| 171 |
to_left = len; |
| 172 |
|
| 173 |
if (_XlcConvert(conv, &from, &from_left, &to, &to_left, NULL, 0) < 0) |
| 174 |
ret = -1; |
| 175 |
else { |
| 176 |
ret = len - to_left; |
| 177 |
if (str && to_left > 0) |
| 178 |
str[ret] = '\0'; |
| 179 |
} |
| 180 |
|
| 181 |
_XlcCloseConverter(conv); |
| 182 |
|
| 183 |
return ret; |
| 184 |
} |
| 185 |
|
| 186 |
|
| 187 |
int |
| 188 |
_Xmbtowc( |
| 189 |
wchar_t *wstr, |
| 190 |
char *str, |
| 191 |
int len |
| 192 |
) |
| 193 |
{ |
| 194 |
return _Xlcmbtowc((XLCd) NULL, wstr, str, len); |
| 195 |
} |
| 196 |
|
| 197 |
int |
| 198 |
_Xmblen( |
| 199 |
char *str, |
| 200 |
int len |
| 201 |
) |
| 202 |
{ |
| 203 |
return _Xmbtowc((wchar_t *) NULL, str, len); |
| 204 |
} |
| 205 |
|
| 206 |
int |
| 207 |
_Xwctomb( |
| 208 |
char *str, |
| 209 |
wchar_t wc) |
| 210 |
{ |
| 211 |
return _Xlcwctomb((XLCd) NULL, str, wc); |
| 212 |
} |
| 213 |
|
| 214 |
int |
| 215 |
_Xmbstowcs( |
| 216 |
wchar_t *wstr, |
| 217 |
char *str, |
| 218 |
int len) |
| 219 |
{ |
| 220 |
return _Xlcmbstowcs((XLCd) NULL, wstr, str, len); |
| 221 |
} |
| 222 |
|
| 223 |
int |
| 224 |
_Xwcstombs( |
| 225 |
char *str, |
| 226 |
wchar_t *wstr, |
| 227 |
int len) |
| 228 |
{ |
| 229 |
return _Xlcwcstombs((XLCd) NULL, str, wstr, len); |
| 230 |
} |
| 231 |
|
| 232 |
wchar_t * |
| 233 |
_Xwcscpy( |
| 234 |
register wchar_t *wstr1, register wchar_t *wstr2) |
| 235 |
{ |
| 236 |
wchar_t *wstr_tmp = wstr1; |
| 237 |
|
| 238 |
while ((*wstr1++ = *wstr2++)) |
| 239 |
; |
| 240 |
|
| 241 |
return wstr_tmp; |
| 242 |
} |
| 243 |
|
| 244 |
wchar_t * |
| 245 |
_Xwcsncpy( |
| 246 |
register wchar_t *wstr1, register wchar_t *wstr2, |
| 247 |
register int len) |
| 248 |
{ |
| 249 |
wchar_t *wstr_tmp = wstr1; |
| 250 |
|
| 251 |
while (len-- > 0) |
| 252 |
if (!(*wstr1++ = *wstr2++)) |
| 253 |
break; |
| 254 |
|
| 255 |
while (len-- > 0) |
| 256 |
*wstr1++ = (wchar_t) 0; |
| 257 |
|
| 258 |
return wstr_tmp; |
| 259 |
} |
| 260 |
|
| 261 |
int |
| 262 |
_Xwcslen( |
| 263 |
register wchar_t *wstr) |
| 264 |
{ |
| 265 |
register wchar_t *wstr_ptr = wstr; |
| 266 |
|
| 267 |
while (*wstr_ptr) |
| 268 |
wstr_ptr++; |
| 269 |
|
| 270 |
return (int) (wstr_ptr - wstr); |
| 271 |
} |
| 272 |
|
| 273 |
int |
| 274 |
_Xwcscmp( |
| 275 |
register wchar_t *wstr1, register wchar_t *wstr2) |
| 276 |
{ |
| 277 |
for ( ; *wstr1 && *wstr2; wstr1++, wstr2++) |
| 278 |
if (*wstr1 != *wstr2) |
| 279 |
break; |
| 280 |
|
| 281 |
return *wstr1 - *wstr2; |
| 282 |
} |
| 283 |
|
| 284 |
int |
| 285 |
_Xwcsncmp( |
| 286 |
register wchar_t *wstr1, register wchar_t *wstr2, |
| 287 |
register int len) |
| 288 |
{ |
| 289 |
for ( ; *wstr1 && *wstr2 && len > 0; wstr1++, wstr2++, len--) |
| 290 |
if (*wstr1 != *wstr2) |
| 291 |
break; |
| 292 |
|
| 293 |
if (len <= 0) |
| 294 |
return 0; |
| 295 |
|
| 296 |
return *wstr1 - *wstr2; |
| 297 |
} |
| 298 |
|
| 299 |
|
| 300 |
int |
| 301 |
_Xlcmbstoutf8( |
| 302 |
XLCd lcd, |
| 303 |
char *ustr, |
| 304 |
const char *str, |
| 305 |
int len) |
| 306 |
{ |
| 307 |
XlcConv conv; |
| 308 |
XPointer from, to; |
| 309 |
int from_left, to_left, ret; |
| 310 |
|
| 311 |
if (lcd == NULL) { |
| 312 |
lcd = _XlcCurrentLC(); |
| 313 |
if (lcd == NULL) |
| 314 |
return -1; |
| 315 |
} |
| 316 |
|
| 317 |
conv = _XlcOpenConverter(lcd, XlcNMultiByte, lcd, XlcNUtf8String); |
| 318 |
if (conv == NULL) |
| 319 |
return -1; |
| 320 |
|
| 321 |
from = (XPointer) str; |
| 322 |
from_left = (int) strlen(str); |
| 323 |
to = (XPointer) ustr; |
| 324 |
to_left = len; |
| 325 |
|
| 326 |
if (_XlcConvert(conv, &from, &from_left, &to, &to_left, NULL, 0) < 0) |
| 327 |
ret = -1; |
| 328 |
else { |
| 329 |
ret = len - to_left; |
| 330 |
if (ustr && to_left > 0) |
| 331 |
ustr[ret] = '\0'; |
| 332 |
} |
| 333 |
|
| 334 |
_XlcCloseConverter(conv); |
| 335 |
|
| 336 |
return ret; |
| 337 |
} |
| 338 |
|
| 339 |
int |
| 340 |
_Xmbstoutf8( |
| 341 |
char *ustr, |
| 342 |
const char *str, |
| 343 |
int len) |
| 344 |
{ |
| 345 |
return _Xlcmbstoutf8((XLCd) NULL, ustr, str, len); |
| 346 |
} |