Merge branch 'vendor/NCURSES'
[dragonfly.git] / contrib / ncurses / ncurses / base / lib_addstr.c
1 /****************************************************************************
2  * Copyright (c) 1998-2002,2003 Free Software Foundation, Inc.              *
3  *                                                                          *
4  * Permission is hereby granted, free of charge, to any person obtaining a  *
5  * copy of this software and associated documentation files (the            *
6  * "Software"), to deal in the Software without restriction, including      *
7  * without limitation the rights to use, copy, modify, merge, publish,      *
8  * distribute, distribute with modifications, sublicense, and/or sell       *
9  * copies of the Software, and to permit persons to whom the Software is    *
10  * furnished to do so, subject to the following conditions:                 *
11  *                                                                          *
12  * The above copyright notice and this permission notice shall be included  *
13  * in all copies or substantial portions of the Software.                   *
14  *                                                                          *
15  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS  *
16  * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF               *
17  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.   *
18  * IN NO EVENT SHALL THE ABOVE COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,   *
19  * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR    *
20  * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR    *
21  * THE USE OR OTHER DEALINGS IN THE SOFTWARE.                               *
22  *                                                                          *
23  * Except as contained in this notice, the name(s) of the above copyright   *
24  * holders shall not be used in advertising or otherwise to promote the     *
25  * sale, use or other dealings in this Software without prior written       *
26  * authorization.                                                           *
27  ****************************************************************************/
28
29 /****************************************************************************
30  *  Author: Zeyd M. Ben-Halim <zmbenhal@netcom.com> 1992,1995               *
31  *     and: Eric S. Raymond <esr@snark.thyrsus.com>                         *
32  *                                                                          *
33  *  Rewritten 2001-2002 to support wide-characters by                       *
34  *      Sven Verdoolaege                                                    *
35  *      Thomas Dickey                                                       *
36  ****************************************************************************/
37
38 /*
39 **      lib_addstr.c
40 *
41 **      The routines waddnstr(), waddchnstr().
42 **
43 */
44
45 #include <curses.priv.h>
46
47 MODULE_ID("$Id: lib_addstr.c,v 1.39 2003/12/06 18:04:33 tom Exp $")
48
49 NCURSES_EXPORT(int)
50 waddnstr(WINDOW *win, const char *astr, int n)
51 {
52     const char *str = astr;
53     int code = ERR;
54
55     T((T_CALLED("waddnstr(%p,%s,%d)"), win, _nc_visbufn(astr, n), n));
56
57     if (win && (str != 0)) {
58         TR(TRACE_VIRTPUT | TRACE_ATTRS, ("... current %s", _traceattr(win->_attrs)));
59         code = OK;
60         if (n < 0)
61             n = (int) strlen(astr);
62
63         TR(TRACE_VIRTPUT, ("str is not null, length = %d", n));
64         while ((n-- > 0) && (*str != '\0')) {
65             NCURSES_CH_T ch;
66             TR(TRACE_VIRTPUT, ("*str = %#o", UChar(*str)));
67             SetChar(ch, UChar(*str++), A_NORMAL);
68             if (_nc_waddch_nosync(win, ch) == ERR) {
69                 code = ERR;
70                 break;
71             }
72         }
73         _nc_synchook(win);
74     }
75     TR(TRACE_VIRTPUT, ("waddnstr returns %d", code));
76     returnCode(code);
77 }
78
79 NCURSES_EXPORT(int)
80 waddchnstr(WINDOW *win, const chtype *astr, int n)
81 {
82     NCURSES_SIZE_T y = win->_cury;
83     NCURSES_SIZE_T x = win->_curx;
84     int code = OK;
85     struct ldat *line;
86
87     T((T_CALLED("waddchnstr(%p,%p,%d)"), win, astr, n));
88
89     if (!win)
90         returnCode(ERR);
91
92     if (n < 0) {
93         const chtype *str;
94         n = 0;
95         for (str = (const chtype *) astr; *str != 0; str++)
96             n++;
97     }
98     if (n > win->_maxx - x + 1)
99         n = win->_maxx - x + 1;
100     if (n == 0)
101         returnCode(code);
102
103     line = &(win->_line[y]);
104 #if USE_WIDEC_SUPPORT
105     {
106         int i;
107         for (i = 0; i < n; ++i)
108             SetChar(line->text[i + x], ChCharOf(astr[i]), ChAttrOf(astr[i]));
109     }
110 #else
111     memcpy(line->text + x, astr, n * sizeof(*astr));
112 #endif
113     CHANGED_RANGE(line, x, x + n - 1);
114
115     _nc_synchook(win);
116     returnCode(code);
117 }
118
119 #if USE_WIDEC_SUPPORT
120
121 NCURSES_EXPORT(int)
122 _nc_wchstrlen(const cchar_t * s)
123 {
124     int result = 0;
125     while (CharOf(s[result]) != L'\0') {
126         result++;
127     }
128     return result;
129 }
130
131 NCURSES_EXPORT(int)
132 wadd_wchnstr(WINDOW *win, const cchar_t * astr, int n)
133 {
134     NCURSES_SIZE_T y = win->_cury;
135     NCURSES_SIZE_T x = win->_curx;
136     int code = OK;
137     struct ldat *line;
138     int i, start, end;
139
140     T((T_CALLED("wadd_wchnstr(%p,%s,%d)"), win, _nc_viscbuf(astr, n), n));
141
142     if (!win)
143         returnCode(ERR);
144
145     if (n < 0) {
146         n = _nc_wchstrlen(astr);
147     }
148     if (n > win->_maxx - x + 1)
149         n = win->_maxx - x + 1;
150     if (n == 0)
151         returnCode(code);
152
153     line = &(win->_line[y]);
154     start = x;
155     end = x + n - 1;
156     if (isnac(line->text[x])) {
157         line->text[x - 1] = win->_nc_bkgd;
158         --start;
159     }
160     for (i = 0; i < n && x <= win->_maxx; ++i) {
161         line->text[x++] = astr[i];
162         if (wcwidth(CharOf(astr[i])) > 1) {
163             if (x <= win->_maxx)
164                 AddAttr(line->text[x++], WA_NAC);
165             else
166                 line->text[x - 1] = win->_nc_bkgd;
167         }
168     }
169     if (x <= win->_maxx && isnac(line->text[x])) {
170         line->text[x] = win->_nc_bkgd;
171         ++end;
172     }
173     CHANGED_RANGE(line, start, end);
174
175     _nc_synchook(win);
176     returnCode(code);
177 }
178
179 NCURSES_EXPORT(int)
180 waddnwstr(WINDOW *win, const wchar_t * str, int n)
181 {
182     int code = ERR;
183
184     T((T_CALLED("waddnwstr(%p,%s,%d)"), win, _nc_viswbufn(str, n), n));
185
186     if (win && (str != 0)) {
187         TR(TRACE_VIRTPUT | TRACE_ATTRS, ("... current %s", _traceattr(win->_attrs)));
188         code = OK;
189         if (n < 0)
190             n = (int) wcslen(str);
191
192         TR(TRACE_VIRTPUT, ("str is not null, length = %d", n));
193         while ((n-- > 0) && (*str != L('\0'))) {
194             NCURSES_CH_T ch;
195             TR(TRACE_VIRTPUT, ("*str[0] = %#lx", (unsigned long) *str));
196             SetChar(ch, *str++, A_NORMAL);
197             if (wadd_wch(win, &ch) == ERR) {
198                 code = ERR;
199                 break;
200             }
201         }
202         _nc_synchook(win);
203     }
204     TR(TRACE_VIRTPUT, ("waddnwstr returns %d", code));
205     returnCode(code);
206 }
207
208 #endif