Back to index

tetex-bin  3.0
lib_addstr.c
Go to the documentation of this file.
00001 /****************************************************************************
00002  * Copyright (c) 1998-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  *  Author: Zeyd M. Ben-Halim <zmbenhal@netcom.com> 1992,1995               *
00031  *     and: Eric S. Raymond <esr@snark.thyrsus.com>                         *
00032  *                                                                          *
00033  *  Rewritten 2001-2002 to support wide-characters by                       *
00034  *     Sven Verdoolaege                                                    *
00035  *     Thomas Dickey                                                       *
00036  ****************************************************************************/
00037 
00038 /*
00039 **     lib_addstr.c
00040 *
00041 **     The routines waddnstr(), waddchnstr().
00042 **
00043 */
00044 
00045 #include <curses.priv.h>
00046 
00047 MODULE_ID("$Id: lib_addstr.c,v 1.40 2004/07/10 20:39:03 tom Exp $")
00048 
00049 NCURSES_EXPORT(int)
00050 waddnstr(WINDOW *win, const char *astr, int n)
00051 {
00052     const char *str = astr;
00053     int code = ERR;
00054 
00055     T((T_CALLED("waddnstr(%p,%s,%d)"), win, _nc_visbufn(astr, n), n));
00056 
00057     if (win && (str != 0)) {
00058        TR(TRACE_VIRTPUT | TRACE_ATTRS, ("... current %s", _traceattr(win->_attrs)));
00059        code = OK;
00060        if (n < 0)
00061            n = (int) strlen(astr);
00062 
00063        TR(TRACE_VIRTPUT, ("str is not null, length = %d", n));
00064        while ((n-- > 0) && (*str != '\0')) {
00065            NCURSES_CH_T ch;
00066            TR(TRACE_VIRTPUT, ("*str = %#o", UChar(*str)));
00067            SetChar(ch, UChar(*str++), A_NORMAL);
00068            if (_nc_waddch_nosync(win, ch) == ERR) {
00069               code = ERR;
00070               break;
00071            }
00072        }
00073        _nc_synchook(win);
00074     }
00075     TR(TRACE_VIRTPUT, ("waddnstr returns %d", code));
00076     returnCode(code);
00077 }
00078 
00079 NCURSES_EXPORT(int)
00080 waddchnstr(WINDOW *win, const chtype *astr, int n)
00081 {
00082     NCURSES_SIZE_T y = win->_cury;
00083     NCURSES_SIZE_T x = win->_curx;
00084     int code = OK;
00085     struct ldat *line;
00086 
00087     T((T_CALLED("waddchnstr(%p,%p,%d)"), win, astr, n));
00088 
00089     if (!win)
00090        returnCode(ERR);
00091 
00092     if (n < 0) {
00093        const chtype *str;
00094        n = 0;
00095        for (str = (const chtype *) astr; *str != 0; str++)
00096            n++;
00097     }
00098     if (n > win->_maxx - x + 1)
00099        n = win->_maxx - x + 1;
00100     if (n == 0)
00101        returnCode(code);
00102 
00103     line = &(win->_line[y]);
00104 #if USE_WIDEC_SUPPORT
00105     {
00106        int i;
00107        for (i = 0; i < n; ++i)
00108            SetChar(line->text[i + x], ChCharOf(astr[i]), ChAttrOf(astr[i]));
00109     }
00110 #else
00111     memcpy(line->text + x, astr, n * sizeof(*astr));
00112 #endif
00113     CHANGED_RANGE(line, x, x + n - 1);
00114 
00115     _nc_synchook(win);
00116     returnCode(code);
00117 }
00118 
00119 #if USE_WIDEC_SUPPORT
00120 
00121 NCURSES_EXPORT(int)
00122 _nc_wchstrlen(const cchar_t * s)
00123 {
00124     int result = 0;
00125     while (CharOf(s[result]) != L'\0') {
00126        result++;
00127     }
00128     return result;
00129 }
00130 
00131 NCURSES_EXPORT(int)
00132 wadd_wchnstr(WINDOW *win, const cchar_t * astr, int n)
00133 {
00134     NCURSES_CH_T blank = NewChar(BLANK_TEXT);
00135     NCURSES_SIZE_T y = win->_cury;
00136     NCURSES_SIZE_T x = win->_curx;
00137     int code = OK;
00138     struct ldat *line;
00139     int i, start, end;
00140 
00141     T((T_CALLED("wadd_wchnstr(%p,%s,%d)"), win, _nc_viscbuf(astr, n), n));
00142 
00143     if (!win)
00144        returnCode(ERR);
00145 
00146     if (n < 0) {
00147        n = _nc_wchstrlen(astr);
00148     }
00149     if (n > win->_maxx - x + 1)
00150        n = win->_maxx - x + 1;
00151     if (n == 0)
00152        returnCode(code);
00153 
00154     line = &(win->_line[y]);
00155     start = x;
00156     end = x + n - 1;
00157     if (isnac(line->text[x])) {
00158        line->text[x - 1] = _nc_render(win, blank);
00159        --start;
00160     }
00161     for (i = 0; i < n && x <= win->_maxx; ++i) {
00162        line->text[x++] = _nc_render(win, astr[i]);
00163        if (wcwidth(CharOf(astr[i])) > 1) {
00164            if (x <= win->_maxx)
00165               AddAttr(line->text[x++], WA_NAC);
00166            else
00167               line->text[x - 1] = _nc_render(win, blank);
00168        }
00169     }
00170     if (x <= win->_maxx && isnac(line->text[x])) {
00171        line->text[x] = _nc_render(win, blank);
00172        ++end;
00173     }
00174     CHANGED_RANGE(line, start, end);
00175 
00176     _nc_synchook(win);
00177     returnCode(code);
00178 }
00179 
00180 NCURSES_EXPORT(int)
00181 waddnwstr(WINDOW *win, const wchar_t * str, int n)
00182 {
00183     int code = ERR;
00184 
00185     T((T_CALLED("waddnwstr(%p,%s,%d)"), win, _nc_viswbufn(str, n), n));
00186 
00187     if (win && (str != 0)) {
00188        TR(TRACE_VIRTPUT | TRACE_ATTRS, ("... current %s", _traceattr(win->_attrs)));
00189        code = OK;
00190        if (n < 0)
00191            n = (int) wcslen(str);
00192 
00193        TR(TRACE_VIRTPUT, ("str is not null, length = %d", n));
00194        while ((n-- > 0) && (*str != L('\0'))) {
00195            NCURSES_CH_T ch;
00196            TR(TRACE_VIRTPUT, ("*str[0] = %#lx", (unsigned long) *str));
00197            SetChar(ch, *str++, A_NORMAL);
00198            if (wadd_wch(win, &ch) == ERR) {
00199               code = ERR;
00200               break;
00201            }
00202        }
00203        _nc_synchook(win);
00204     }
00205     TR(TRACE_VIRTPUT, ("waddnwstr returns %d", code));
00206     returnCode(code);
00207 }
00208 
00209 #endif