_fallcStd.c 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293
  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. #include "_fallibint.h"
  50. #include "_fallcPubI.h"
  51. int
  52. _fallcmbtowc(XLCd lcd, wchar_t *wstr, char *str, int len)
  53. {
  54. static XLCd last_lcd = NULL;
  55. static XlcConv conv = NULL;
  56. XPointer from, to;
  57. int from_left, to_left;
  58. wchar_t tmp_wc;
  59. if (lcd == NULL) {
  60. lcd = _fallcCurrentLC();
  61. if (lcd == NULL)
  62. return -1;
  63. }
  64. if (str == NULL)
  65. return XLC_PUBLIC(lcd, is_state_depend);
  66. if (conv && lcd != last_lcd) {
  67. _fallcCloseConverter(conv);
  68. conv = NULL;
  69. }
  70. last_lcd = lcd;
  71. if (conv == NULL) {
  72. conv = _fallcOpenConverter(lcd, XlcNMultiByte, lcd, XlcNWideChar);
  73. if (conv == NULL)
  74. return -1;
  75. }
  76. from = (XPointer) str;
  77. from_left = len;
  78. to = (XPointer) (wstr ? wstr : &tmp_wc);
  79. to_left = 1;
  80. if (_fallcConvert(conv, &from, &from_left, &to, &to_left, NULL, 0) < 0)
  81. return -1;
  82. return (len - from_left);
  83. }
  84. int
  85. _fallcwctomb(XLCd lcd, char *str, wchar_t wc)
  86. {
  87. static XLCd last_lcd = NULL;
  88. static XlcConv conv = NULL;
  89. XPointer from, to;
  90. int from_left, to_left, length;
  91. if (lcd == NULL) {
  92. lcd = _fallcCurrentLC();
  93. if (lcd == NULL)
  94. return -1;
  95. }
  96. if (str == NULL)
  97. return XLC_PUBLIC(lcd, is_state_depend);
  98. if (conv && lcd != last_lcd) {
  99. _fallcCloseConverter(conv);
  100. conv = NULL;
  101. }
  102. last_lcd = lcd;
  103. if (conv == NULL) {
  104. conv = _fallcOpenConverter(lcd, XlcNWideChar, lcd, XlcNMultiByte);
  105. if (conv == NULL)
  106. return -1;
  107. }
  108. from = (XPointer) &wc;
  109. from_left = 1;
  110. to = (XPointer) str;
  111. length = to_left = XLC_PUBLIC(lcd, mb_cur_max);
  112. if (_fallcConvert(conv, &from, &from_left, &to, &to_left, NULL, 0) < 0)
  113. return -1;
  114. return (length - to_left);
  115. }
  116. int
  117. _fallcmbstowcs(XLCd lcd, wchar_t *wstr, char *str, int len)
  118. {
  119. XlcConv conv;
  120. XPointer from, to;
  121. int from_left, to_left, ret;
  122. if (lcd == NULL) {
  123. lcd = _fallcCurrentLC();
  124. if (lcd == NULL)
  125. return -1;
  126. }
  127. conv = _fallcOpenConverter(lcd, XlcNMultiByte, lcd, XlcNWideChar);
  128. if (conv == NULL)
  129. return -1;
  130. from = (XPointer) str;
  131. from_left = strlen(str);
  132. to = (XPointer) wstr;
  133. to_left = len;
  134. if (_fallcConvert(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. _fallcCloseConverter(conv);
  142. return ret;
  143. }
  144. int
  145. _fallcwcstombs(XLCd lcd, char *str, wchar_t *wstr, int len)
  146. {
  147. XlcConv conv;
  148. XPointer from, to;
  149. int from_left, to_left, ret;
  150. if (lcd == NULL) {
  151. lcd = _fallcCurrentLC();
  152. if (lcd == NULL)
  153. return -1;
  154. }
  155. conv = _fallcOpenConverter(lcd, XlcNWideChar, lcd, XlcNMultiByte);
  156. if (conv == NULL)
  157. return -1;
  158. from = (XPointer) wstr;
  159. from_left = _falwcslen(wstr);
  160. to = (XPointer) str;
  161. to_left = len;
  162. if (_fallcConvert(conv, &from, &from_left, &to, &to_left, NULL, 0) < 0)
  163. ret = -1;
  164. else {
  165. ret = len - to_left;
  166. if (str && to_left > 0)
  167. str[ret] = '\0';
  168. }
  169. _fallcCloseConverter(conv);
  170. return ret;
  171. }
  172. int
  173. _falmbtowc(wchar_t *wstr, char *str, int len)
  174. {
  175. return _fallcmbtowc((XLCd) NULL, wstr, str, len);
  176. }
  177. int
  178. _falmblen(char *str, int len)
  179. {
  180. return _falmbtowc((wchar_t *) NULL, str, len);
  181. }
  182. int
  183. _falwctomb(char *str, wchar_t wc)
  184. {
  185. return _fallcwctomb((XLCd) NULL, str, wc);
  186. }
  187. int
  188. _falmbstowcs(wchar_t *wstr, char *str, int len)
  189. {
  190. return _fallcmbstowcs((XLCd) NULL, wstr, str, len);
  191. }
  192. int
  193. _falwcstombs(char *str, wchar_t *wstr, int len)
  194. {
  195. return _fallcwcstombs((XLCd) NULL, str, wstr, len);
  196. }
  197. wchar_t *
  198. _falwcscpy(wchar_t *wstr1, wchar_t *wstr2)
  199. {
  200. wchar_t *wstr_tmp = wstr1;
  201. while (*wstr1++ = *wstr2++)
  202. ;
  203. return wstr_tmp;
  204. }
  205. wchar_t *
  206. _falwcsncpy(wchar_t *wstr1, wchar_t *wstr2, int len)
  207. {
  208. wchar_t *wstr_tmp = wstr1;
  209. while (len-- > 0)
  210. if (!(*wstr1++ = *wstr2++))
  211. break;
  212. while (len-- > 0)
  213. *wstr1++ = (wchar_t) 0;
  214. return wstr_tmp;
  215. }
  216. int
  217. _falwcslen(wchar_t *wstr)
  218. {
  219. wchar_t *wstr_ptr = wstr;
  220. while (*wstr_ptr)
  221. wstr_ptr++;
  222. return wstr_ptr - wstr;
  223. }
  224. int
  225. _falwcscmp(wchar_t *wstr1, wchar_t *wstr2)
  226. {
  227. for ( ; *wstr1 && *wstr2; wstr1++, wstr2++)
  228. if (*wstr1 != *wstr2)
  229. break;
  230. return *wstr1 - *wstr2;
  231. }
  232. int
  233. _falwcsncmp(wchar_t *wstr1, wchar_t *wstr2, int len)
  234. {
  235. for ( ; *wstr1 && *wstr2 && len > 0; wstr1++, wstr2++, len--)
  236. if (*wstr1 != *wstr2)
  237. break;
  238. if (len <= 0)
  239. return 0;
  240. return *wstr1 - *wstr2;
  241. }