_fallcConv.c 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344
  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. /* lcConv.c 1.1 - Fujitsu source for CDEnext 95/11/06 20:32:34 */
  24. /* $XConsortium: _fallcConv.c /main/1 1996/04/08 15:15:57 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 <stdio.h>
  52. typedef XlcConv (*XlcConverter)();
  53. typedef struct _fallcConverterListRec {
  54. XLCd from_lcd;
  55. char *from;
  56. XrmQuark from_type;
  57. XLCd to_lcd;
  58. char *to;
  59. XrmQuark to_type;
  60. XlcConverter converter;
  61. struct _fallcConverterListRec *next;
  62. } XlcConverterListRec, *XlcConverterList;
  63. static XlcConverterList conv_list = NULL;
  64. static void
  65. close_converter(XlcConv conv)
  66. {
  67. (*conv->methods->close)(conv);
  68. }
  69. static XlcConv
  70. get_converter(
  71. XLCd from_lcd,
  72. XrmQuark from_type,
  73. XLCd to_lcd,
  74. XrmQuark to_type)
  75. {
  76. XlcConverterList list, prev = NULL;
  77. XlcConv conv;
  78. for (list = conv_list; list; list = list->next) {
  79. if (list->from_lcd == from_lcd && list->to_lcd == to_lcd
  80. && list->from_type == from_type && list->to_type == to_type) {
  81. if (prev && prev != conv_list) { /* XXX */
  82. prev->next = list->next;
  83. list->next = conv_list;
  84. conv_list = list;
  85. }
  86. return (*list->converter)(from_lcd, list->from, to_lcd, list->to);
  87. }
  88. prev = list;
  89. }
  90. return (XlcConv) NULL;
  91. }
  92. Bool
  93. _fallcSetConverter(
  94. XLCd from_lcd,
  95. char *from,
  96. XLCd to_lcd,
  97. char *to,
  98. XlcOpenConverterProc converter)
  99. {
  100. XlcConverterList list;
  101. XrmQuark from_type, to_type;
  102. from_type = falrmStringToQuark(from);
  103. to_type = falrmStringToQuark(to);
  104. for (list = conv_list; list; list = list->next) {
  105. if (list->from_lcd == from_lcd && list->to_lcd == to_lcd
  106. && list->from_type == from_type && list->to_type == to_type) {
  107. list->converter = converter;
  108. return True;
  109. }
  110. }
  111. list = (XlcConverterList) Xmalloc(sizeof(XlcConverterListRec));
  112. if (list == NULL)
  113. return False;
  114. list->from_lcd = from_lcd;
  115. list->from = from;
  116. list->from_type = from_type;
  117. list->to_lcd = to_lcd;
  118. list->to = to;
  119. list->to_type = to_type;
  120. list->converter = converter;
  121. list->next = conv_list;
  122. conv_list = list;
  123. return True;
  124. }
  125. typedef struct _ConvRec {
  126. XlcConv from_conv;
  127. XlcConv to_conv;
  128. } ConvRec, *Conv;
  129. static int
  130. indirect_convert(
  131. XlcConv lc_conv,
  132. XPointer *from,
  133. int *from_left,
  134. XPointer *to,
  135. int *to_left,
  136. XPointer *args,
  137. int num_args)
  138. {
  139. Conv conv = (Conv) lc_conv->state;
  140. XlcConv from_conv = conv->from_conv;
  141. XlcConv to_conv = conv->to_conv;
  142. XlcCharSet charset;
  143. char buf[BUFSIZ], *cs;
  144. XPointer tmp_args[1];
  145. int cs_left, ret, length, unconv_num = 0;
  146. if (from == NULL || *from == NULL) {
  147. if (from_conv->methods->reset)
  148. (*from_conv->methods->reset)(from_conv);
  149. if (to_conv->methods->reset)
  150. (*to_conv->methods->reset)(to_conv);
  151. return 0;
  152. }
  153. while (*from_left > 0) {
  154. cs = buf;
  155. cs_left = BUFSIZ;
  156. tmp_args[0] = (XPointer) &charset;
  157. ret = (*from_conv->methods->convert)(from_conv, from, from_left, &cs,
  158. &cs_left, tmp_args, 1);
  159. if (ret < 0)
  160. break;
  161. length = cs_left = cs - buf;
  162. cs = buf;
  163. tmp_args[0] = (XPointer) charset;
  164. ret = (*to_conv->methods->convert)(to_conv, &cs, &cs_left, to, to_left,
  165. tmp_args, 1);
  166. if (ret < 0) {
  167. unconv_num += length / charset->char_size;
  168. continue;
  169. }
  170. if (*to_left < 1)
  171. break;
  172. }
  173. return unconv_num;
  174. }
  175. static void
  176. close_indirect_converter(XlcConv lc_conv)
  177. {
  178. Conv conv = (Conv) lc_conv->state;
  179. if (conv) {
  180. if (conv->from_conv)
  181. close_converter(conv->from_conv);
  182. if (conv->to_conv)
  183. close_converter(conv->to_conv);
  184. Xfree((char *) conv);
  185. }
  186. Xfree((char *) lc_conv);
  187. }
  188. static void
  189. reset_indirect_converter(XlcConv lc_conv)
  190. {
  191. Conv conv = (Conv) lc_conv->state;
  192. if (conv) {
  193. if (conv->from_conv && conv->from_conv->methods->reset)
  194. (*conv->from_conv->methods->reset)(conv->from_conv);
  195. if (conv->to_conv && conv->to_conv->methods->reset)
  196. (*conv->to_conv->methods->reset)(conv->to_conv);
  197. }
  198. }
  199. static XlcConvMethodsRec conv_methods = {
  200. close_indirect_converter,
  201. indirect_convert,
  202. reset_indirect_converter
  203. } ;
  204. static XlcConv
  205. open_indirect_converter(XLCd from_lcd, char *from, XLCd to_lcd, char *to)
  206. {
  207. XlcConv lc_conv, from_conv, to_conv;
  208. Conv conv;
  209. XrmQuark from_type, to_type;
  210. static XrmQuark QChar, QCharSet, QCTCharSet = (XrmQuark) 0;
  211. if (QCTCharSet == (XrmQuark) 0) {
  212. QCTCharSet = falrmStringToQuark(XlcNCTCharSet);
  213. QCharSet = falrmStringToQuark(XlcNCharSet);
  214. QChar = falrmStringToQuark(XlcNChar);
  215. }
  216. from_type = falrmStringToQuark(from);
  217. to_type = falrmStringToQuark(to);
  218. if (from_type == QCharSet || from_type == QChar || to_type == QCharSet ||
  219. to_type == QChar)
  220. return (XlcConv) NULL;
  221. lc_conv = (XlcConv) Xmalloc(sizeof(XlcConvRec));
  222. if (lc_conv == NULL)
  223. return (XlcConv) NULL;
  224. lc_conv->methods = &conv_methods;
  225. lc_conv->state = (XPointer) Xmalloc(sizeof(ConvRec));
  226. if (lc_conv->state == NULL){
  227. close_indirect_converter(lc_conv);
  228. return (XlcConv) NULL;
  229. }
  230. conv = (Conv) lc_conv->state;
  231. from_conv = get_converter(from_lcd, from_type, from_lcd, QCTCharSet);
  232. if (from_conv == NULL)
  233. from_conv = get_converter(from_lcd, from_type, from_lcd, QCharSet);
  234. if (from_conv == NULL)
  235. from_conv = get_converter((XLCd)NULL, from_type, (XLCd)NULL, QCharSet);
  236. if (from_conv == NULL)
  237. from_conv = get_converter(from_lcd, from_type, from_lcd, QChar);
  238. if (from_conv == NULL){
  239. close_indirect_converter(lc_conv);
  240. return (XlcConv) NULL;
  241. }
  242. conv->from_conv = from_conv;
  243. to_conv = get_converter(to_lcd, QCTCharSet, to_lcd, to_type);
  244. if (to_conv == NULL)
  245. to_conv = get_converter(to_lcd, QCharSet, to_lcd, to_type);
  246. if (to_conv == NULL)
  247. to_conv = get_converter((XLCd) NULL, QCharSet, (XLCd) NULL, to_type);
  248. if (to_conv == NULL){
  249. close_indirect_converter(lc_conv);
  250. return (XlcConv) NULL;
  251. }
  252. conv->to_conv = to_conv;
  253. return lc_conv;
  254. }
  255. XlcConv
  256. _fallcOpenConverter(XLCd from_lcd, char *from, XLCd to_lcd, char *to)
  257. {
  258. XlcConv conv;
  259. XrmQuark from_type, to_type;
  260. from_type = falrmStringToQuark(from);
  261. to_type = falrmStringToQuark(to);
  262. if (conv = get_converter(from_lcd, from_type, to_lcd, to_type))
  263. return conv;
  264. return open_indirect_converter(from_lcd, from, to_lcd, to);
  265. }
  266. void
  267. _fallcCloseConverter(XlcConv conv)
  268. {
  269. close_converter(conv);
  270. }
  271. int
  272. _fallcConvert(
  273. XlcConv conv,
  274. XPointer *from,
  275. int *from_left,
  276. XPointer *to,
  277. int *to_left,
  278. XPointer *args,
  279. int num_args)
  280. {
  281. return (*conv->methods->convert)(conv, from, from_left, to, to_left, args,
  282. num_args);
  283. }
  284. void
  285. _fallcResetConverter(XlcConv conv)
  286. {
  287. if (conv->methods->reset)
  288. (*conv->methods->reset)(conv);
  289. }