]> ncurses.scripts.mit.edu Git - ncurses.git/blob - ncurses/base/lib_addstr.c
ea449794b1ab8b7d3c99c5dad8f4e7754696b7ec
[ncurses.git] / ncurses / base / lib_addstr.c
1 /****************************************************************************
2  * Copyright 2019-2020,2022 Thomas E. Dickey                                *
3  * Copyright 1998-2016,2017 Free Software Foundation, Inc.                  *
4  *                                                                          *
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:                 *
12  *                                                                          *
13  * The above copyright notice and this permission notice shall be included  *
14  * in all copies or substantial portions of the Software.                   *
15  *                                                                          *
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.                               *
23  *                                                                          *
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       *
27  * authorization.                                                           *
28  ****************************************************************************/
29
30 /****************************************************************************
31  *  Author: Zeyd M. Ben-Halim <zmbenhal@netcom.com> 1992,1995               *
32  *     and: Eric S. Raymond <esr@snark.thyrsus.com>                         *
33  *                                                                          *
34  *  Rewritten 2001-2004 to support wide-characters by                       *
35  *      Sven Verdoolaege                                                    *
36  *      Thomas Dickey                                                       *
37  ****************************************************************************/
38
39 /*
40 **      lib_addstr.c
41 *
42 **      The routines waddnstr(), waddchnstr().
43 **
44 */
45
46 #include <curses.priv.h>
47
48 MODULE_ID("$Id: lib_addstr.c,v 1.58 2022/06/11 20:12:04 tom Exp $")
49
50 NCURSES_EXPORT(int)
51 waddnstr(WINDOW *win, const char *astr, int n)
52 {
53     const char *str = astr;
54     int code = ERR;
55
56     T((T_CALLED("waddnstr(%p,%s,%d)"), (void *) win, _nc_visbufn(astr, n), n));
57
58     if (win && (str != 0)) {
59         TR(TRACE_VIRTPUT | TRACE_ATTRS,
60            ("... current %s", _traceattr(WINDOW_ATTRS(win))));
61         code = OK;
62
63         TR(TRACE_VIRTPUT, ("str is not null, length = %d",
64                            ((n > 0) ? n : (int) strlen(str))));
65         if (n < 0)
66             n = INT_MAX;
67         while ((*str != '\0') && (n-- > 0)) {
68             NCURSES_CH_T ch;
69             TR(TRACE_VIRTPUT, ("*str = %#o", UChar(*str)));
70             SetChar(ch, UChar(*str++), A_NORMAL);
71             if (_nc_waddch_nosync(win, ch) == ERR) {
72                 code = ERR;
73                 break;
74             }
75         }
76         _nc_synchook(win);
77     }
78     TR(TRACE_VIRTPUT, ("waddnstr returns %d", code));
79     returnCode(code);
80 }
81
82 NCURSES_EXPORT(int)
83 waddchnstr(WINDOW *win, const chtype *astr, int n)
84 {
85     NCURSES_SIZE_T y, x;
86     int code = OK;
87     int i;
88     struct ldat *line;
89
90     T((T_CALLED("waddchnstr(%p,%p,%d)"), (void *) win, (const void *) astr, n));
91
92     if (!win || !astr)
93         returnCode(ERR);
94
95     y = win->_cury;
96     x = win->_curx;
97     if (n < 0) {
98         const chtype *str;
99         n = 0;
100         for (str = (const chtype *) astr; *str != 0; str++)
101             n++;
102     }
103     if (n > win->_maxx - x + 1)
104         n = win->_maxx - x + 1;
105     if (n == 0)
106         returnCode(code);
107
108     line = &(win->_line[y]);
109     for (i = 0; i < n && ChCharOf(astr[i]) != '\0'; ++i) {
110         SetChar2(line->text[i + x], astr[i]);
111     }
112     CHANGED_RANGE(line, x, (NCURSES_SIZE_T) (x + n - 1));
113
114     _nc_synchook(win);
115     returnCode(code);
116 }
117
118 #if USE_WIDEC_SUPPORT
119
120 NCURSES_EXPORT(int)
121 _nc_wchstrlen(const cchar_t *s)
122 {
123     int result = 0;
124     if (s != 0) {
125         while (CharOf(s[result]) != L'\0') {
126             result++;
127         }
128     }
129     return result;
130 }
131
132 NCURSES_EXPORT(int)
133 wadd_wchnstr(WINDOW *win, const cchar_t *astr, int n)
134 {
135     static const NCURSES_CH_T blank = NewChar(BLANK_TEXT);
136     NCURSES_SIZE_T y;
137     NCURSES_SIZE_T x;
138     int code = OK;
139     struct ldat *line;
140     int i, j, start, len, end;
141
142     T((T_CALLED("wadd_wchnstr(%p,%s,%d)"),
143        (void *) win,
144        _nc_viscbuf(astr, n),
145        n));
146
147     if (!win)
148         returnCode(ERR);
149
150     y = win->_cury;
151     x = win->_curx;
152     if (n < 0) {
153         n = _nc_wchstrlen(astr);
154     }
155     if (n > win->_maxx - x + 1)
156         n = win->_maxx - x + 1;
157     if (n == 0)
158         returnCode(code);
159
160     line = &(win->_line[y]);
161     start = x;
162     end = x + n - 1;
163
164     /*
165      * Reset orphaned cells of multi-column characters that extend up to the
166      * new string's location to blanks.
167      */
168     if (x > 0 && isWidecExt(line->text[x])) {
169         for (i = 0; i <= x; ++i) {
170             if (!isWidecExt(line->text[x - i])) {
171                 /* must be isWidecBase() */
172                 start -= i;
173                 while (i > 0) {
174                     line->text[x - i--] = _nc_render(win, blank);
175                 }
176                 break;
177             }
178         }
179     }
180
181     /*
182      * Copy the new string to the window.
183      */
184     for (i = 0; i < n && CharOf(astr[i]) != L'\0' && x <= win->_maxx; ++i) {
185         if (isWidecExt(astr[i]))
186             continue;
187
188         len = _nc_wacs_width(CharOf(astr[i]));
189
190         if (x + len - 1 <= win->_maxx) {
191             line->text[x] = _nc_render(win, astr[i]);
192             if (len > 1) {
193                 for (j = 0; j < len; ++j) {
194                     if (j != 0) {
195                         line->text[x + j] = line->text[x];
196                     }
197                     SetWidecExt(line->text[x + j], j);
198                 }
199             } else {
200                 len = 1;
201             }
202             x = (NCURSES_SIZE_T) (x + len);
203             end += len - 1;
204         } else {
205             break;
206         }
207     }
208
209     /*
210      * Set orphaned cells of multi-column characters which lie after the new
211      * string to blanks.
212      */
213     while (x <= win->_maxx && isWidecExt(line->text[x])) {
214         line->text[x] = _nc_render(win, blank);
215         ++end;
216         ++x;
217     }
218     CHANGED_RANGE(line, start, end);
219
220     _nc_synchook(win);
221     returnCode(code);
222 }
223
224 NCURSES_EXPORT(int)
225 waddnwstr(WINDOW *win, const wchar_t *str, int n)
226 {
227     int code = ERR;
228
229     T((T_CALLED("waddnwstr(%p,%s,%d)"), (void *) win, _nc_viswbufn(str, n), n));
230
231     if (win && (str != 0)) {
232         TR(TRACE_VIRTPUT | TRACE_ATTRS,
233            ("... current %s", _traceattr(WINDOW_ATTRS(win))));
234         code = OK;
235
236         TR(TRACE_VIRTPUT, ("str is not null, length = %d",
237                            ((n > 0) ? n : (int) wcslen(str))));
238         if (n < 0)
239             n = INT_MAX;
240         while ((*str != L('\0')) && (n-- > 0)) {
241             NCURSES_CH_T ch;
242             TR(TRACE_VIRTPUT, ("*str[0] = %#lx", (unsigned long) *str));
243             SetChar(ch, *str++, A_NORMAL);
244             if (wadd_wch(win, &ch) == ERR) {
245                 code = ERR;
246                 break;
247             }
248         }
249         _nc_synchook(win);
250     }
251     TR(TRACE_VIRTPUT, ("waddnwstr returns %d", code));
252     returnCode(code);
253 }
254
255 #endif