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

  1. /* $XConsortium: lcTxtPr.c /main/7 1996/10/22 17:21:47 kaleb $ */
  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 <X11/Xutil.h>
  30. #include <X11/Xatom.h>
  31. #include <stdio.h>
  32.  
  33. static int
  34. get_buf_size(is_wide_char, list, count)
  35.     Bool is_wide_char;
  36.     XPointer list;
  37.     int count;
  38. {
  39.     DBUG_ENTER("get_buf_size")
  40.     register int length = 0;
  41.     register char **mb_list;
  42.     register wchar_t **wc_list;
  43.  
  44.     if (list == NULL)
  45.     DBUG_RETURN(0);
  46.  
  47.     if (is_wide_char) {
  48.     wc_list = (wchar_t **) list;
  49.     for ( ; count-- > 0; wc_list++) {
  50.         if (*wc_list)
  51.         length += _Xwcslen(*wc_list) + 1;
  52.     }
  53.     length *= 5;    /* XXX */
  54.     } else {
  55.     mb_list = (char **) list;
  56.     for ( ; count-- > 0; mb_list++) {
  57.         if (*mb_list)
  58.         length += strlen(*mb_list) + 1;
  59.     }
  60.     length *= 3;    /* XXX */
  61.     }
  62.     length = (length / BUFSIZ + 1) * BUFSIZ;    /* XXX */
  63.  
  64.     DBUG_RETURN(length);
  65. }
  66.  
  67. static int
  68. _XTextListToTextProperty(lcd, dpy, from_type, list, count, style, text_prop)
  69.     XLCd lcd;
  70.     Display *dpy;
  71.     char *from_type;
  72.     XPointer list;
  73.     int count;
  74.     XICCEncodingStyle style;
  75.     XTextProperty *text_prop;
  76. {
  77.     DBUG_ENTER("_XTextListToTextProperty")
  78.     Atom encoding;
  79.     XlcConv conv;
  80.     char *to_type;
  81.     char **mb_list;
  82.     wchar_t **wc_list;
  83.     XPointer from;
  84.     char *to, *buf, *value;
  85.     int from_left, to_left, buf_len, nitems, unconv_num, ret, i;
  86.     Bool is_wide_char = False;
  87.  
  88.     if (strcmp(XlcNWideChar, from_type) == 0)
  89.     is_wide_char = True;
  90.  
  91.     buf_len = get_buf_size(is_wide_char, list, count);
  92.     if ((buf = (char *) Xmalloc(buf_len)) == NULL)
  93.     DBUG_RETURN(XNoMemory);
  94.     
  95.     switch (style) {
  96.     case XStringStyle:
  97.     case XStdICCTextStyle:
  98.         encoding = XA_STRING;
  99.         to_type = XlcNString;
  100.         break;
  101.     case XCompoundTextStyle:
  102.         encoding = XInternAtom(dpy, "COMPOUND_TEXT", False);
  103.         to_type = XlcNCompoundText;
  104.         break;
  105.     case XTextStyle:
  106.         encoding = XInternAtom(dpy, XLC_PUBLIC(lcd, encoding_name), False);
  107.         to_type = XlcNMultiByte;
  108.         if (is_wide_char == False) {
  109.         nitems = 0;
  110.         mb_list = (char **) list;
  111.         to = buf;
  112.         for (i = 0; i < count && buf_len > 0; i++) {
  113.             if (*mb_list) 
  114.             strcpy(to, *mb_list);
  115.             else
  116.             *to = '\0';
  117.             from_left = (*mb_list ? strlen(*mb_list) : 0) + 1;
  118.             nitems += from_left;
  119.             to += from_left;
  120.             mb_list++;
  121.         }
  122.         unconv_num = 0;
  123.         goto done;
  124.         }
  125.         break;
  126.     default:
  127.         Xfree(buf);
  128.         DBUG_RETURN(XConverterNotFound);
  129.     }
  130.  
  131.     if (count < 1) {
  132.     nitems = 0;
  133.     goto done;
  134.     }
  135.  
  136. retry:
  137.     conv = _XlcOpenConverter(lcd, from_type, lcd, to_type);
  138.     if (conv == NULL) {
  139.     Xfree(buf);
  140.     DBUG_RETURN(XConverterNotFound);
  141.     }
  142.  
  143.     if (is_wide_char)
  144.     wc_list = (wchar_t **) list;
  145.     else
  146.     mb_list = (char **) list;
  147.  
  148.     to = buf;
  149.     to_left = buf_len;
  150.  
  151.     unconv_num = 0;
  152.  
  153.     for (i = 1; to_left > 0; i++) {
  154.     if (is_wide_char) {
  155.         from = (XPointer) *wc_list;
  156.         from_left = _Xwcslen(*wc_list);
  157.         wc_list++;
  158.     } else {
  159.         from = (XPointer) *mb_list;
  160.         from_left = (*mb_list ? strlen(*mb_list) : 0);
  161.         mb_list++;
  162.     }
  163.  
  164.     ret = _XlcConvert(conv, &from, &from_left, (XPointer *) &to, &to_left,
  165.               NULL, 0);
  166.  
  167.     if (ret < 0)
  168.         continue;
  169.  
  170.     if (ret > 0 && style == XStdICCTextStyle && encoding == XA_STRING) {
  171.         _XlcCloseConverter(conv);
  172.         encoding = XInternAtom(dpy, "COMPOUND_TEXT", False);
  173.         to_type = XlcNCompoundText;
  174.         goto retry;
  175.     }
  176.  
  177.     unconv_num += ret;
  178.     *to++ = '\0';
  179.     to_left--;
  180.  
  181.     if (i >= count)
  182.         break;
  183.  
  184.     _XlcResetConverter(conv);
  185.     }
  186.  
  187.     _XlcCloseConverter(conv);
  188.  
  189.     nitems = to - buf;
  190. done:
  191.     if (nitems <= 0)
  192.     nitems = 1;
  193.     value = (char *) Xmalloc(nitems);
  194.     if (value == NULL) {
  195.     Xfree(buf);
  196.     DBUG_RETURN(XNoMemory);
  197.     }
  198.     if (nitems == 1)
  199.     *value = 0;
  200.     else
  201.         memcpy(value, buf, nitems);
  202.     nitems--;
  203.     Xfree(buf);
  204.  
  205.     text_prop->value = (unsigned char *) value;
  206.     text_prop->encoding = encoding;
  207.     text_prop->format = 8;
  208.     text_prop->nitems = nitems;
  209.  
  210.     DBUG_RETURN(unconv_num);
  211. }
  212.  
  213. int
  214. _XmbTextListToTextProperty(lcd, dpy, list, count, style, text_prop)
  215.     XLCd lcd;
  216.     Display *dpy;
  217.     char **list;
  218.     int count;
  219.     XICCEncodingStyle style;
  220.     XTextProperty *text_prop;
  221. {
  222.     DBUG_ENTER("_XmbTextListToTextProperty")
  223.     int result = _XTextListToTextProperty(lcd, dpy, XlcNMultiByte, (XPointer) list,
  224.                     count, style, text_prop);
  225.     DBUG_RETURN(result);
  226. }
  227.  
  228. int
  229. _XwcTextListToTextProperty(lcd, dpy, list, count, style, text_prop)
  230.     XLCd lcd;
  231.     Display *dpy;
  232.     wchar_t **list;
  233.     int count;
  234.     XICCEncodingStyle style;
  235.     XTextProperty *text_prop;
  236. {
  237.     DBUG_ENTER("_XwcTextListToTextProperty")
  238.     int result = _XTextListToTextProperty(lcd, dpy, XlcNWideChar, (XPointer) list,
  239.                     count, style, text_prop);
  240.     DBUG_RETURN(result);
  241. }
  242.