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

  1. /* $XConsortium: lcPrTxt.c /main/4 1996/01/05 11:23:11 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.  
  32. static XPointer *
  33. alloc_list(is_wide_char, count, nitems)
  34.     Bool is_wide_char;
  35.     int count;
  36.     int nitems;
  37. {
  38.     DBUG_ENTER("alloc_list")
  39.     if (is_wide_char) {
  40.     wchar_t **wstr_list;
  41.  
  42.     wstr_list = (wchar_t **) Xmalloc(count * sizeof(wchar_t *));
  43.     if (wstr_list == NULL)
  44.         DBUG_RETURN((XPointer *) NULL);
  45.  
  46.     *wstr_list = (wchar_t *) Xmalloc(nitems * sizeof(wchar_t));
  47.     if (*wstr_list == NULL) {
  48.         Xfree(wstr_list);
  49.         DBUG_RETURN((XPointer *) NULL);
  50.     }
  51.  
  52.     DBUG_RETURN((XPointer *) wstr_list);
  53.     } else {
  54.     char **str_list;
  55.  
  56.     str_list = (char **) Xmalloc(count * sizeof(char *));
  57.     if (str_list == NULL)
  58.         DBUG_RETURN((XPointer *) NULL);
  59.  
  60.     *str_list = (char *) Xmalloc(nitems);
  61.     if (*str_list == NULL) {
  62.         Xfree(str_list);
  63.         DBUG_RETURN((XPointer *) NULL);
  64.     }
  65.  
  66.     DBUG_RETURN((XPointer *) str_list);
  67.     }
  68. }
  69.  
  70. static void
  71. copy_list(is_wide_char, text, list, count)
  72.     Bool is_wide_char;
  73.     XPointer text;
  74.     XPointer *list;
  75.     int count;
  76. {
  77.     DBUG_ENTER("copy_list")
  78.     int length;
  79.  
  80.     if (is_wide_char) {
  81.     wchar_t *wc_text, *wstr, **wstr_list;
  82.     
  83.     wc_text = (wchar_t *) text;
  84.     wstr_list = (wchar_t **) list;
  85.  
  86.     for (wstr = *wstr_list; count > 0; count--, wstr_list++) {
  87.         _Xwcscpy(wstr, wc_text);
  88.         *wstr_list = wstr;
  89.         length = _Xwcslen(wstr) + 1;
  90.         wstr += length;
  91.         wc_text += length;
  92.     }
  93.     } else {
  94.     char *mb_text, *str, **str_list;
  95.     
  96.     mb_text = (char *) text;
  97.     str_list = (char **) list;
  98.  
  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.     DBUG_VOID_RETURN;
  108. }
  109.  
  110. static int
  111. _XTextPropertyToTextList(lcd, dpy, text_prop, to_type, list_ret, count_ret)
  112.     XLCd lcd;
  113.     Display *dpy;
  114.     XTextProperty *text_prop;
  115.     char *to_type;
  116.     XPointer **list_ret;
  117.     int *count_ret;
  118. {
  119.     DBUG_ENTER("_XTextPropertyToTextList")
  120.     XlcConv conv;
  121.     char *from_type;
  122.     XPointer from, to, buf;
  123.     char *str_ptr, *last_ptr;
  124.     Atom encoding;
  125.     int from_left, to_left, buf_len, ret;
  126.     int unconv_num, nitems = text_prop->nitems;
  127.     Bool is_wide_char = False;
  128.  
  129.     if (strcmp(XlcNWideChar, to_type) == 0)
  130.     is_wide_char = True;
  131.  
  132.     if (nitems <= 0) {
  133.     *list_ret = NULL;
  134.     *count_ret = 0;
  135.     DBUG_RETURN(Success);
  136.     }
  137.  
  138.     if (text_prop->format != 8)
  139.     DBUG_RETURN(XConverterNotFound);
  140.  
  141.     encoding = text_prop->encoding;
  142.     if (encoding == XA_STRING)
  143.     from_type = XlcNString;
  144.     else if (encoding == XInternAtom(dpy, "COMPOUND_TEXT", False))
  145.     from_type = XlcNCompoundText;
  146.     else if (encoding == XInternAtom(dpy, XLC_PUBLIC(lcd, encoding_name), False))
  147.     from_type = XlcNMultiByte;
  148.     else
  149.     DBUG_RETURN(XConverterNotFound);
  150.  
  151.     if (is_wide_char) {
  152.     buf_len = text_prop->nitems + 1;
  153.     buf = (XPointer) Xmalloc(buf_len * sizeof(wchar_t));
  154.     } else {
  155.     buf_len = text_prop->nitems * XLC_PUBLIC(lcd, mb_cur_max) + 1;
  156.     buf = (XPointer) Xmalloc(buf_len);
  157.     }
  158.     if (buf == NULL)
  159.     DBUG_RETURN(XNoMemory);
  160.     to = buf;
  161.     to_left = buf_len;
  162.  
  163.     conv = _XlcOpenConverter(lcd, from_type, lcd, to_type);
  164.     if (conv == NULL) {
  165.     Xfree(buf);
  166.     DBUG_RETURN(XConverterNotFound);
  167.     }
  168.  
  169.     last_ptr = str_ptr = (char *) text_prop->value;
  170.     unconv_num = *count_ret = 0;
  171.  
  172.     while (1) {
  173.     if (nitems == 0 || *str_ptr == 0) {
  174.         from = (XPointer) last_ptr;
  175.         from_left = str_ptr - last_ptr;
  176.         last_ptr = str_ptr;
  177.  
  178.         ret = _XlcConvert(conv, &from, &from_left, &to, &to_left, NULL, 0);
  179.  
  180.         if (ret < 0)
  181.         continue;
  182.  
  183.         unconv_num += ret;
  184.         (*count_ret)++;
  185.  
  186.         if (nitems == 0)
  187.         break;
  188.          last_ptr = ++str_ptr;
  189.         if (is_wide_char) {
  190.         *((wchar_t *)to) = (wchar_t) 0;
  191.         to += sizeof(wchar_t);
  192.         to_left -= sizeof(wchar_t);
  193.         } else {
  194.         *((char *)to) = '\0';
  195.         to++;
  196.         to_left--;
  197.         }
  198.         _XlcResetConverter(conv);
  199.     } else
  200.         str_ptr++;
  201.  
  202.     nitems--;
  203.     }
  204.  
  205.     _XlcCloseConverter(conv);
  206.  
  207.     if (is_wide_char)
  208.     *((wchar_t *) to) = (wchar_t) 0;
  209.     else
  210.     *((char *) to) = '\0';
  211.     to_left--;
  212.  
  213.     *list_ret = alloc_list(is_wide_char, *count_ret, buf_len - to_left);
  214.     if (*list_ret)
  215.     copy_list(is_wide_char, buf, *list_ret, *count_ret);
  216.  
  217.     Xfree(buf);
  218.  
  219.     DBUG_RETURN(unconv_num);
  220. }
  221.  
  222. int
  223. _XmbTextPropertyToTextList(lcd, dpy, text_prop, list_ret, count_ret)
  224.     XLCd lcd;
  225.     Display *dpy;
  226.     XTextProperty *text_prop;
  227.     char ***list_ret;
  228.     int *count_ret;
  229. {
  230.     DBUG_ENTER("_XmbTextPropertyToTextList")
  231.     int result = _XTextPropertyToTextList(lcd, dpy, text_prop, XlcNMultiByte,
  232.                     (XPointer **) list_ret, count_ret);
  233.     DBUG_RETURN(result);
  234. }
  235.  
  236. int
  237. _XwcTextPropertyToTextList(lcd, dpy, text_prop, list_ret, count_ret)
  238.     XLCd lcd;
  239.     Display *dpy;
  240.     XTextProperty *text_prop;
  241.     wchar_t ***list_ret;
  242.     int *count_ret;
  243. {
  244.     DBUG_ENTER("_XwcTextPropertyToTextList")
  245.     int result = _XTextPropertyToTextList(lcd, dpy, text_prop, XlcNWideChar,
  246.                     (XPointer **) list_ret, count_ret);
  247.     DBUG_RETURN(result);
  248. }
  249.  
  250. void
  251. _XwcFreeStringList(lcd, list)
  252.     XLCd lcd;
  253.     wchar_t **list;
  254. {
  255.     DBUG_ENTER("_XwcFreeStringList")
  256.     if (list) {
  257.         if (*list)
  258.          Xfree(*list);
  259.         Xfree(list);
  260.     }
  261.     DBUG_VOID_RETURN;
  262. }
  263.