_fallcPrTxt.c 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258
  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. /* lcPrTxt.c 1.1 - Fujitsu source for CDEnext 95/11/06 20:32:38 */
  24. /* $XConsortium: _fallcPrTxt.c /main/1 1996/04/08 15:17:49 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. #include "_falutil.h"
  52. #include <X11/Xatom.h>
  53. static XPointer *
  54. alloc_list(Bool is_wide_char, int count, int nitems)
  55. {
  56. if (is_wide_char) {
  57. wchar_t **wstr_list;
  58. wstr_list = (wchar_t **) Xmalloc(count * sizeof(wchar_t *));
  59. if (wstr_list == NULL)
  60. return (XPointer *) NULL;
  61. *wstr_list = (wchar_t *) Xmalloc(nitems * sizeof(wchar_t));
  62. if (*wstr_list == NULL) {
  63. Xfree(wstr_list);
  64. return (XPointer *) NULL;
  65. }
  66. return (XPointer *) wstr_list;
  67. } else {
  68. char **str_list;
  69. str_list = (char **) Xmalloc(count * sizeof(char *));
  70. if (str_list == NULL)
  71. return (XPointer *) NULL;
  72. *str_list = (char *) Xmalloc(nitems);
  73. if (*str_list == NULL) {
  74. Xfree(str_list);
  75. return (XPointer *) NULL;
  76. }
  77. return (XPointer *) str_list;
  78. }
  79. }
  80. static void
  81. copy_list(Bool is_wide_char, XPointer text, XPointer *list, int count)
  82. {
  83. int length;
  84. if (is_wide_char) {
  85. wchar_t *wc_text, *wstr, **wstr_list;
  86. wc_text = (wchar_t *) text;
  87. wstr_list = (wchar_t **) list;
  88. for (wstr = *wstr_list; count > 0; count--, wstr_list++) {
  89. _falwcscpy(wstr, wc_text);
  90. *wstr_list = wstr;
  91. length = _falwcslen(wstr) + 1;
  92. wstr += length;
  93. wc_text += length;
  94. }
  95. } else {
  96. char *mb_text, *str, **str_list;
  97. mb_text = (char *) text;
  98. str_list = (char **) list;
  99. for (str = *str_list; count > 0; count--, str_list++) {
  100. strcpy(str, mb_text);
  101. *str_list = str;
  102. length = strlen(str) + 1;
  103. str += length;
  104. mb_text += length;
  105. }
  106. }
  107. }
  108. static int
  109. _XTextPropertyToTextList(
  110. XLCd lcd,
  111. Display *dpy,
  112. XTextProperty *text_prop,
  113. char *to_type,
  114. XPointer **list_ret,
  115. int *count_ret)
  116. {
  117. XlcConv conv;
  118. char *from_type;
  119. XPointer from, to, buf;
  120. char *str_ptr, *last_ptr;
  121. Atom encoding;
  122. int from_left, to_left, buf_len, ret;
  123. int unconv_num, nitems = text_prop->nitems;
  124. Bool is_wide_char = False;
  125. if (strcmp(XlcNWideChar, to_type) == 0)
  126. is_wide_char = True;
  127. if (nitems <= 0) {
  128. *list_ret = NULL;
  129. *count_ret = 0;
  130. return Success;
  131. }
  132. if (text_prop->format != 8)
  133. return XConverterNotFound;
  134. encoding = text_prop->encoding;
  135. if (encoding == XA_STRING)
  136. from_type = XlcNString;
  137. else if (encoding == falInternAtom(dpy, "COMPOUND_TEXT", False))
  138. from_type = XlcNCompoundText;
  139. else if (encoding == falInternAtom(dpy, XLC_PUBLIC(lcd, encoding_name), False))
  140. from_type = XlcNMultiByte;
  141. else
  142. return XConverterNotFound;
  143. if (is_wide_char) {
  144. buf_len = text_prop->nitems + 1;
  145. buf = (XPointer) Xmalloc(buf_len * sizeof(wchar_t));
  146. } else {
  147. buf_len = text_prop->nitems * XLC_PUBLIC(lcd, mb_cur_max) + 1;
  148. buf = (XPointer) Xmalloc(buf_len);
  149. }
  150. if (buf == NULL)
  151. return XNoMemory;
  152. to = buf;
  153. to_left = buf_len;
  154. conv = _fallcOpenConverter(lcd, from_type, lcd, to_type);
  155. if (conv == NULL) {
  156. Xfree(buf);
  157. return XConverterNotFound;
  158. }
  159. last_ptr = str_ptr = (char *) text_prop->value;
  160. unconv_num = *count_ret = 0;
  161. while (1) {
  162. if (nitems == 0 || *str_ptr == 0) {
  163. if (nitems)
  164. str_ptr++;
  165. from = (XPointer) last_ptr;
  166. from_left = str_ptr - last_ptr;
  167. last_ptr = str_ptr;
  168. ret = _fallcConvert(conv, &from, &from_left, &to, &to_left, NULL, 0);
  169. if (ret < 0)
  170. continue;
  171. unconv_num += ret;
  172. (*count_ret)++;
  173. if (nitems == 0)
  174. break;
  175. _fallcResetConverter(conv);
  176. } else
  177. str_ptr++;
  178. nitems--;
  179. }
  180. _fallcCloseConverter(conv);
  181. if (is_wide_char)
  182. *((wchar_t *) to) = (wchar_t) 0;
  183. else
  184. *((char *) to) = '\0';
  185. to_left--;
  186. *list_ret = alloc_list(is_wide_char, *count_ret, buf_len - to_left);
  187. if (*list_ret)
  188. copy_list(is_wide_char, buf, *list_ret, *count_ret);
  189. Xfree(buf);
  190. return unconv_num;
  191. }
  192. int
  193. _falmbTextPropertyToTextList(
  194. XLCd lcd,
  195. Display *dpy,
  196. XTextProperty *text_prop,
  197. char ***list_ret,
  198. int *count_ret)
  199. {
  200. return _XTextPropertyToTextList(lcd, dpy, text_prop, XlcNMultiByte,
  201. (XPointer **) list_ret, count_ret);
  202. }
  203. int
  204. _falwcTextPropertyToTextList(
  205. XLCd lcd,
  206. Display *dpy,
  207. XTextProperty *text_prop,
  208. wchar_t ***list_ret,
  209. int *count_ret)
  210. {
  211. return _XTextPropertyToTextList(lcd, dpy, text_prop, XlcNWideChar,
  212. (XPointer **) list_ret, count_ret);
  213. }
  214. void
  215. _falwcFreeStringList(XLCd lcd, wchar_t **list)
  216. {
  217. if (list) {
  218. if (*list)
  219. Xfree(*list);
  220. Xfree(list);
  221. }
  222. }