home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / evbl0627.zip / everblue_20010627.zip / x11 / Xlc_Conv.c < prev    next >
C/C++ Source or Header  |  1999-11-02  |  9KB  |  352 lines

  1. /* $XConsortium: lcConv.c /main/8 1996/09/28 16:37:28 rws $ */
  2. /*
  3.  * Copyright 1992, 1993 by TOSHIBA Corp.
  4.  *
  5.  * Permission to use, copy, modify, and distribute this software and its
  6.  * documentation for any purpose and without fee is hereby granted, provided
  7.  * that the above copyright notice appear in all copies and that both that
  8.  * copyright notice and this permission notice appear in supporting
  9.  * documentation, and that the name of TOSHIBA not be used in advertising
  10.  * or publicity pertaining to distribution of the software without specific,
  11.  * written prior permission. TOSHIBA make no representations about the
  12.  * suitability of this software for any purpose.  It is provided "as is"
  13.  * without express or implied warranty.
  14.  *
  15.  * TOSHIBA DISCLAIM ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING
  16.  * ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL
  17.  * TOSHIBA BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR
  18.  * ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
  19.  * WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION,
  20.  * ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
  21.  * SOFTWARE.
  22.  *
  23.  * Author: Katsuhisa Yano    TOSHIBA Corp.
  24.  *                   mopi@osa.ilab.toshiba.co.jp
  25.  */
  26.  
  27. #include "Xlib_private.h"
  28. #include "XlcPubI.h"
  29. #include <stdio.h>
  30.  
  31. typedef XlcConv (*XlcConverter)();
  32.  
  33. typedef struct _XlcConverterListRec {
  34.     XLCd from_lcd;
  35.     char *from;
  36.     XrmQuark from_type;
  37.     XLCd to_lcd;
  38.     char *to;
  39.     XrmQuark to_type;
  40.     XlcConverter converter;
  41.     struct _XlcConverterListRec *next;
  42. } XlcConverterListRec, *XlcConverterList;
  43.  
  44. static XlcConverterList conv_list = NULL;
  45.  
  46. static void
  47. close_converter(conv)
  48.     XlcConv conv;
  49. {
  50.     DBUG_ENTER("close_converter")
  51.     (*conv->methods->close)(conv);
  52.     DBUG_VOID_RETURN;
  53. }
  54.  
  55. static XlcConv
  56. get_converter(from_lcd, from_type, to_lcd, to_type)
  57.     XLCd from_lcd;
  58.     XrmQuark from_type;
  59.     XLCd to_lcd;
  60.     XrmQuark to_type;
  61. {
  62.     DBUG_ENTER("get_converter")
  63.     register XlcConverterList list, prev = NULL;
  64.  
  65.     for (list = conv_list; list; list = list->next) {
  66.     if (list->from_lcd == from_lcd && list->to_lcd == to_lcd 
  67.         && list->from_type == from_type && list->to_type == to_type) {
  68.         XlcConv result;
  69.  
  70.         if (prev && prev != conv_list) {    /* XXX */
  71.         prev->next = list->next;
  72.         list->next = conv_list;
  73.         conv_list = list;
  74.         }
  75.  
  76.         result = (*list->converter)(from_lcd, list->from, to_lcd, list->to);
  77.         DBUG_RETURN(result);
  78.     }
  79.  
  80.     prev = list;
  81.     }
  82.     
  83.     DBUG_RETURN((XlcConv) NULL);
  84. }
  85.  
  86. Bool
  87. _XlcSetConverter(from_lcd, from, to_lcd, to, converter)
  88.     XLCd from_lcd;
  89.     char *from;
  90.     XLCd to_lcd;
  91.     char *to;
  92.     XlcOpenConverterProc converter;
  93. {
  94.     DBUG_ENTER("_XlcSetConverter")
  95.     register XlcConverterList list;
  96.     register XrmQuark from_type, to_type;
  97.  
  98.     from_type = XrmStringToQuark(from);
  99.     to_type = XrmStringToQuark(to);
  100.  
  101.     for (list = conv_list; list; list = list->next) {
  102.     if (list->from_lcd == from_lcd && list->to_lcd == to_lcd 
  103.         && list->from_type == from_type && list->to_type == to_type) {
  104.  
  105.         list->converter = converter;
  106.         DBUG_RETURN(True);
  107.     }
  108.     }
  109.  
  110.     list = (XlcConverterList) Xmalloc(sizeof(XlcConverterListRec));
  111.     if (list == NULL)
  112.     DBUG_RETURN(False);
  113.     
  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.  
  124.     DBUG_RETURN(True);
  125. }
  126.  
  127. typedef struct _ConvRec {
  128.     XlcConv from_conv;
  129.     XlcConv to_conv;
  130. } ConvRec, *Conv;
  131.  
  132. static int
  133. indirect_convert(lc_conv, from, from_left, to, to_left, args, num_args)
  134.     XlcConv lc_conv;
  135.     XPointer *from;
  136.     int *from_left;
  137.     XPointer *to;
  138.     int *to_left;
  139.     XPointer *args;
  140.     int num_args;
  141. {
  142.     DBUG_ENTER("indirect_convert")
  143.     Conv conv = (Conv) lc_conv->state;
  144.     XlcConv from_conv = conv->from_conv;
  145.     XlcConv to_conv = conv->to_conv;
  146.     XlcCharSet charset;
  147.     char buf[BUFSIZ], *cs;
  148.     XPointer tmp_args[1];
  149.     int cs_left, ret, length, unconv_num = 0;
  150.  
  151.     if (from == NULL || *from == NULL) {
  152.     if (from_conv->methods->reset)
  153.         (*from_conv->methods->reset)(from_conv);
  154.  
  155.     if (to_conv->methods->reset)
  156.         (*to_conv->methods->reset)(to_conv);
  157.  
  158.     DBUG_RETURN(0);
  159.     }
  160.  
  161.     while (*from_left > 0) {
  162.     cs = buf;
  163.     cs_left = BUFSIZ;
  164.     tmp_args[0] = (XPointer) &charset;
  165.  
  166.     ret = (*from_conv->methods->convert)(from_conv, from, from_left, &cs,
  167.                          &cs_left, tmp_args, 1);
  168.     if (ret < 0)
  169.         break;
  170.  
  171.     length = cs_left = cs - buf;
  172.     cs = buf;
  173.  
  174.     tmp_args[0] = (XPointer) charset;
  175.  
  176.     ret = (*to_conv->methods->convert)(to_conv, &cs, &cs_left, to, to_left,
  177.                        tmp_args, 1);
  178.     if (ret < 0) {
  179.         unconv_num += length / charset->char_size;
  180.         continue;
  181.     }
  182.     
  183.     if (*to_left < 1)
  184.         break;
  185.     }
  186.  
  187.     DBUG_RETURN(unconv_num);
  188. }
  189.  
  190. static void
  191. close_indirect_converter(lc_conv)
  192.     XlcConv lc_conv;
  193. {
  194.     DBUG_ENTER("close_indirect_converter")
  195.     Conv conv = (Conv) lc_conv->state;
  196.  
  197.     if (conv) {
  198.     if (conv->from_conv)
  199.         close_converter(conv->from_conv);
  200.     if (conv->to_conv)
  201.         close_converter(conv->to_conv);
  202.  
  203.     Xfree((char *) conv);
  204.     }
  205.  
  206.     Xfree((char *) lc_conv);
  207.     DBUG_VOID_RETURN;
  208. }
  209.  
  210. static void
  211. reset_indirect_converter(lc_conv)
  212.     XlcConv lc_conv;
  213. {
  214.     DBUG_ENTER("reset_indirect_converter")
  215.     Conv conv = (Conv) lc_conv->state;
  216.  
  217.     if (conv) {
  218.     if (conv->from_conv && conv->from_conv->methods->reset)
  219.         (*conv->from_conv->methods->reset)(conv->from_conv);
  220.     if (conv->to_conv && conv->to_conv->methods->reset)
  221.         (*conv->to_conv->methods->reset)(conv->to_conv);
  222.     }
  223.     DBUG_VOID_RETURN;
  224. }
  225.  
  226. static XlcConvMethodsRec conv_methods = {
  227.     close_indirect_converter,
  228.     indirect_convert,
  229.     reset_indirect_converter
  230. } ;
  231.  
  232. static XlcConv
  233. open_indirect_converter(from_lcd, from, to_lcd, to)
  234.     XLCd from_lcd;
  235.     char *from;
  236.     XLCd to_lcd;
  237.     char *to;
  238. {
  239.     DBUG_ENTER("open_indirect_converter")
  240.     XlcConv lc_conv, from_conv, to_conv;
  241.     Conv conv;
  242.     XrmQuark from_type, to_type;
  243.     static XrmQuark QChar, QCharSet, QCTCharSet = (XrmQuark) 0;
  244.  
  245.     if (QCTCharSet == (XrmQuark) 0) {
  246.     QCTCharSet = XrmStringToQuark(XlcNCTCharSet);
  247.     QCharSet = XrmStringToQuark(XlcNCharSet);
  248.     QChar = XrmStringToQuark(XlcNChar);
  249.     }
  250.  
  251.     from_type = XrmStringToQuark(from);
  252.     to_type = XrmStringToQuark(to);
  253.  
  254.     if (from_type == QCharSet || from_type == QChar || to_type == QCharSet ||
  255.     to_type == QChar)
  256.     DBUG_RETURN((XlcConv) NULL);
  257.  
  258.     lc_conv = (XlcConv) Xmalloc(sizeof(XlcConvRec));
  259.     if (lc_conv == NULL)
  260.     DBUG_RETURN((XlcConv) NULL);
  261.     
  262.     lc_conv->methods = &conv_methods;
  263.  
  264.     lc_conv->state = (XPointer) Xcalloc(1, sizeof(ConvRec));
  265.     if (lc_conv->state == NULL)
  266.     goto err;
  267.     
  268.     conv = (Conv) lc_conv->state;
  269.  
  270.     from_conv = get_converter(from_lcd, from_type, from_lcd, QCTCharSet);
  271.     if (from_conv == NULL)
  272.     from_conv = get_converter(from_lcd, from_type, from_lcd, QCharSet);
  273.     if (from_conv == NULL)
  274.     from_conv = get_converter((XLCd)NULL, from_type, (XLCd)NULL, QCharSet);
  275.     if (from_conv == NULL)
  276.     from_conv = get_converter(from_lcd, from_type, from_lcd, QChar);
  277.     if (from_conv == NULL)
  278.     goto err;
  279.     conv->from_conv = from_conv;
  280.  
  281.     to_conv = get_converter(to_lcd, QCTCharSet, to_lcd, to_type);
  282.     if (to_conv == NULL)
  283.     to_conv = get_converter(to_lcd, QCharSet, to_lcd, to_type);
  284.     if (to_conv == NULL)
  285.     to_conv = get_converter((XLCd) NULL, QCharSet, (XLCd) NULL, to_type);
  286.     if (to_conv == NULL)
  287.     goto err;
  288.     conv->to_conv = to_conv;
  289.  
  290.     DBUG_RETURN(lc_conv);
  291.  
  292. err:
  293.     close_indirect_converter(lc_conv);
  294.  
  295.     DBUG_RETURN((XlcConv) NULL);
  296. }
  297.  
  298. XlcConv
  299. _XlcOpenConverter(from_lcd, from, to_lcd, to)
  300.     XLCd from_lcd;
  301.     char *from;
  302.     XLCd to_lcd;
  303.     char *to;
  304. {
  305.     DBUG_ENTER("_XlcOpenConverter")
  306.     XlcConv conv;
  307.     XrmQuark from_type, to_type;
  308.  
  309.     from_type = XrmStringToQuark(from);
  310.     to_type = XrmStringToQuark(to);
  311.  
  312.     if (!(conv = get_converter(from_lcd, from_type, to_lcd, to_type)))
  313.     conv = open_indirect_converter(from_lcd, from, to_lcd, to);
  314.  
  315.     DBUG_RETURN(conv);
  316. }
  317.  
  318. void
  319. _XlcCloseConverter(conv)
  320.     XlcConv conv;
  321. {
  322.     DBUG_ENTER("_XlcCloseConverter")
  323.     close_converter(conv);
  324.     DBUG_VOID_RETURN;
  325. }
  326.  
  327. int
  328. _XlcConvert(conv, from, from_left, to, to_left, args, num_args)
  329.     XlcConv conv;
  330.     XPointer *from;
  331.     int *from_left;
  332.     XPointer *to;
  333.     int *to_left;
  334.     XPointer *args;
  335.     int num_args;
  336. {
  337.     DBUG_ENTER("_XlcConvert")
  338.     int result = (*conv->methods->convert)(conv, from, from_left, to, to_left, args,
  339.                      num_args);
  340.     DBUG_RETURN(result);
  341. }
  342.  
  343. void
  344. _XlcResetConverter(conv)
  345.     XlcConv conv;
  346. {
  347.     DBUG_ENTER("_XlcResetConverter")
  348.     if (conv->methods->reset)
  349.     (*conv->methods->reset)(conv);
  350.     DBUG_VOID_RETURN;
  351. }
  352.