Back to index

tetex-bin  3.0
lib_cchar.c
Go to the documentation of this file.
00001 /****************************************************************************
00002  * Copyright (c) 2001-2003,2004 Free Software Foundation, Inc.              *
00003  *                                                                          *
00004  * Permission is hereby granted, free of charge, to any person obtaining a  *
00005  * copy of this software and associated documentation files (the            *
00006  * "Software"), to deal in the Software without restriction, including      *
00007  * without limitation the rights to use, copy, modify, merge, publish,      *
00008  * distribute, distribute with modifications, sublicense, and/or sell       *
00009  * copies of the Software, and to permit persons to whom the Software is    *
00010  * furnished to do so, subject to the following conditions:                 *
00011  *                                                                          *
00012  * The above copyright notice and this permission notice shall be included  *
00013  * in all copies or substantial portions of the Software.                   *
00014  *                                                                          *
00015  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS  *
00016  * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF               *
00017  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.   *
00018  * IN NO EVENT SHALL THE ABOVE COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,   *
00019  * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR    *
00020  * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR    *
00021  * THE USE OR OTHER DEALINGS IN THE SOFTWARE.                               *
00022  *                                                                          *
00023  * Except as contained in this notice, the name(s) of the above copyright   *
00024  * holders shall not be used in advertising or otherwise to promote the     *
00025  * sale, use or other dealings in this Software without prior written       *
00026  * authorization.                                                           *
00027  ****************************************************************************/
00028 
00029 /*
00030 **     lib_cchar.c
00031 **
00032 **     The routines setcchar() and getcchar().
00033 **
00034 */
00035 
00036 #include <curses.priv.h>
00037 
00038 MODULE_ID("$Id: lib_cchar.c,v 1.10 2004/10/02 23:44:34 Marcin.Kowalczyk Exp $")
00039 
00040 /* 
00041  * The SuSv2 description leaves some room for interpretation.  We'll assume wch
00042  * points to a string which is L'\0' terminated, contains at least one
00043  * character with strictly positive width, which must be the first, and
00044  * contains no characters of negative width.
00045  */
00046 NCURSES_EXPORT(int)
00047 setcchar(cchar_t *wcval,
00048         const wchar_t *wch,
00049         const attr_t attrs,
00050         short color_pair,
00051         const void *opts)
00052 {
00053     int i;
00054     int len;
00055     int code = OK;
00056 
00057     TR(TRACE_CCALLS, (T_CALLED("setcchar(%p,%s,%ld,%d,%p)"),
00058                     wcval, _nc_viswbuf(wch), attrs, color_pair, opts));
00059 
00060     len = wcslen(wch);
00061     if (opts != NULL
00062        || (len > 1 && wcwidth(wch[0]) < 0)) {
00063        code = ERR;
00064     } else {
00065        if (len > CCHARW_MAX)
00066            len = CCHARW_MAX;
00067 
00068        /*
00069         * If we have a following spacing-character, stop at that point.  We
00070         * are only interested in adding non-spacing characters.
00071         */
00072        for (i = 1; i < len; ++i) {
00073            if (wcwidth(wch[i]) != 0) {
00074               len = i;
00075               break;
00076            }
00077        }
00078 
00079        memset(wcval, 0, sizeof(*wcval));
00080 
00081        if (len != 0) {
00082            SetAttr(*wcval, attrs | COLOR_PAIR(color_pair));
00083            memcpy(&wcval->chars, wch, len * sizeof(wchar_t));
00084            TR(TRACE_CCALLS, ("copy %d wchars, first is %s", len,
00085                            _tracecchar_t(wcval)));
00086        }
00087     }
00088 
00089     TR(TRACE_CCALLS, (T_RETURN("%d"), code));
00090     return (code);
00091 }
00092 
00093 NCURSES_EXPORT(int)
00094 getcchar(const cchar_t *wcval,
00095         wchar_t *wch,
00096         attr_t *attrs,
00097         short *color_pair,
00098         void *opts)
00099 {
00100     wchar_t *wp;
00101     int len;
00102     int code = ERR;
00103 
00104     TR(TRACE_CCALLS, (T_CALLED("getcchar(%p,%p,%p,%p,%p)"),
00105                     wcval, wch, attrs, color_pair, opts));
00106 
00107     if (opts == NULL) {
00108        len = (wp = wmemchr(wcval->chars, L'\0', CCHARW_MAX))
00109            ? wp - wcval->chars
00110            : CCHARW_MAX;
00111 
00112        if (wch == NULL) {
00113            code = len;
00114        } else if (attrs == 0 || color_pair == 0) {
00115            code = ERR;
00116        } else if (len >= 0) {
00117            *attrs = AttrOf(*wcval) & A_ATTRIBUTES;
00118            *color_pair = PAIR_NUMBER(AttrOf(*wcval));
00119            wmemcpy(wch, wcval->chars, (unsigned) len);
00120            wch[len] = L'\0';
00121            code = OK;
00122        }
00123     }
00124 
00125     TR(TRACE_CCALLS, (T_RETURN("%d"), code));
00126     return (code);
00127 }