1 /****************************************************************************
2 * Copyright 2019-2022,2023 Thomas E. Dickey *
3 * Copyright 1998-2016,2017 Free Software Foundation, Inc. *
5 * Permission is hereby granted, free of charge, to any person obtaining a *
6 * copy of this software and associated documentation files (the *
7 * "Software"), to deal in the Software without restriction, including *
8 * without limitation the rights to use, copy, modify, merge, publish, *
9 * distribute, distribute with modifications, sublicense, and/or sell *
10 * copies of the Software, and to permit persons to whom the Software is *
11 * furnished to do so, subject to the following conditions: *
13 * The above copyright notice and this permission notice shall be included *
14 * in all copies or substantial portions of the Software. *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS *
17 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF *
18 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. *
19 * IN NO EVENT SHALL THE ABOVE COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, *
20 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR *
21 * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR *
22 * THE USE OR OTHER DEALINGS IN THE SOFTWARE. *
24 * Except as contained in this notice, the name(s) of the above copyright *
25 * holders shall not be used in advertising or otherwise to promote the *
26 * sale, use or other dealings in this Software without prior written *
28 ****************************************************************************/
30 /****************************************************************************
31 * Author: Zeyd M. Ben-Halim <zmbenhal@netcom.com> 1992,1995 *
32 * and: Eric S. Raymond <esr@snark.thyrsus.com> *
34 * Rewritten 2001-2004 to support wide-characters by *
37 ****************************************************************************/
42 ** The routines waddnstr(), waddchnstr().
46 #include <curses.priv.h>
48 MODULE_ID("$Id: lib_addstr.c,v 1.62 2023/11/21 21:47:23 tom Exp $")
51 waddnstr(WINDOW *win, const char *astr, int n)
53 const char *str = astr;
56 T((T_CALLED("waddnstr(%p,%s,%d)"), (void *) win, _nc_visbufn(astr, n), n));
58 if (win && (str != 0) && (n != 0)) {
59 bool explicit = (n > 0);
61 TR(TRACE_VIRTPUT | TRACE_ATTRS,
62 ("... current %s", _traceattr(WINDOW_ATTRS(win))));
65 TR(TRACE_VIRTPUT, ("str is not null, length = %d",
66 (explicit ? n : (int) strlen(str))));
69 while ((n-- > 0) && (*str != '\0')) {
71 TR(TRACE_VIRTPUT, ("*str = %#o", UChar(*str)));
72 SetChar(ch, UChar(*str++), A_NORMAL);
73 if (_nc_waddch_nosync(win, ch) == ERR) {
80 TR(TRACE_VIRTPUT, ("waddnstr returns %d", code));
85 waddchnstr(WINDOW *win, const chtype *astr, int n)
92 T((T_CALLED("waddchnstr(%p,%p,%d)"), (void *) win, (const void *) astr, n));
102 for (str = (const chtype *) astr; *str != 0; str++)
105 if (n > win->_maxx - x + 1)
106 n = win->_maxx - x + 1;
110 line = &(win->_line[y]);
111 for (i = 0; i < n && ChCharOf(astr[i]) != '\0'; ++i) {
112 SetChar2(line->text[i + x], astr[i]);
114 CHANGED_RANGE(line, x, (NCURSES_SIZE_T) (x + n - 1));
120 #if USE_WIDEC_SUPPORT
123 _nc_wchstrlen(const cchar_t *s)
127 while (CharOf(s[result]) != L'\0') {
135 wadd_wchnstr(WINDOW *win, const cchar_t *astr, int n)
137 static const NCURSES_CH_T blank = NewChar(BLANK_TEXT);
142 int i, j, start, len, end;
144 T((T_CALLED("wadd_wchnstr(%p,%s,%d)"),
146 _nc_viscbuf(astr, n),
155 n = _nc_wchstrlen(astr);
157 if (n > win->_maxx - x + 1)
158 n = win->_maxx - x + 1;
162 line = &(win->_line[y]);
167 * Reset orphaned cells of multi-column characters that extend up to the
168 * new string's location to blanks.
170 if (x > 0 && isWidecExt(line->text[x])) {
171 for (i = 0; i <= x; ++i) {
172 if (!isWidecExt(line->text[x - i])) {
173 /* must be isWidecBase() */
176 line->text[x - i--] = _nc_render(win, blank);
184 * Copy the new string to the window.
186 for (i = 0; i < n && CharOf(astr[i]) != L'\0' && x <= win->_maxx; ++i) {
187 if (isWidecExt(astr[i]))
190 len = _nc_wacs_width(CharOf(astr[i]));
192 if (x + len - 1 <= win->_maxx) {
193 line->text[x] = _nc_render(win, astr[i]);
195 for (j = 0; j < len; ++j) {
197 line->text[x + j] = line->text[x];
199 SetWidecExt(line->text[x + j], j);
204 x = (NCURSES_SIZE_T) (x + len);
212 * Set orphaned cells of multi-column characters which lie after the new
215 while (x <= win->_maxx && isWidecExt(line->text[x])) {
216 line->text[x] = _nc_render(win, blank);
220 CHANGED_RANGE(line, start, end);
227 waddnwstr(WINDOW *win, const wchar_t *str, int n)
231 T((T_CALLED("waddnwstr(%p,%s,%d)"), (void *) win, _nc_viswbufn(str, n), n));
233 if (win && (str != 0) && (n != 0)) {
234 bool explicit = (n > 0);
236 TR(TRACE_VIRTPUT | TRACE_ATTRS,
237 ("... current %s", _traceattr(WINDOW_ATTRS(win))));
240 TR(TRACE_VIRTPUT, ("str is not null, length = %d",
241 (explicit ? n : (int) wcslen(str))));
244 while ((n-- > 0) && (*str != L('\0'))) {
246 TR(TRACE_VIRTPUT, ("*str[0] = %#lx", (unsigned long) *str));
247 SetChar(ch, *str++, A_NORMAL);
248 if (wadd_wch(win, &ch) == ERR) {
255 TR(TRACE_VIRTPUT, ("waddnwstr returns %d", code));