1/* $OpenBSD: lib_inwstr.c,v 1.2 2023/10/17 09:52:09 nicm Exp $ */
2
3/****************************************************************************
4 * Copyright 2020 Thomas E. Dickey                                          *
5 * Copyright 2002-2016,2017 Free Software Foundation, Inc.                  *
6 *                                                                          *
7 * Permission is hereby granted, free of charge, to any person obtaining a  *
8 * copy of this software and associated documentation files (the            *
9 * "Software"), to deal in the Software without restriction, including      *
10 * without limitation the rights to use, copy, modify, merge, publish,      *
11 * distribute, distribute with modifications, sublicense, and/or sell       *
12 * copies of the Software, and to permit persons to whom the Software is    *
13 * furnished to do so, subject to the following conditions:                 *
14 *                                                                          *
15 * The above copyright notice and this permission notice shall be included  *
16 * in all copies or substantial portions of the Software.                   *
17 *                                                                          *
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS  *
19 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF               *
20 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.   *
21 * IN NO EVENT SHALL THE ABOVE COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,   *
22 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR    *
23 * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR    *
24 * THE USE OR OTHER DEALINGS IN THE SOFTWARE.                               *
25 *                                                                          *
26 * Except as contained in this notice, the name(s) of the above copyright   *
27 * holders shall not be used in advertising or otherwise to promote the     *
28 * sale, use or other dealings in this Software without prior written       *
29 * authorization.                                                           *
30 ****************************************************************************/
31
32/****************************************************************************
33 * Author: Thomas Dickey                                                    *
34 ****************************************************************************/
35
36/*
37**	lib_inwstr.c
38**
39**	The routines winnwstr() and winwstr().
40**
41*/
42
43#include <curses.priv.h>
44
45MODULE_ID("$Id: lib_inwstr.c,v 1.2 2023/10/17 09:52:09 nicm Exp $")
46
47NCURSES_EXPORT(int)
48winnwstr(WINDOW *win, wchar_t *wstr, int n)
49{
50    int count = 0;
51    cchar_t *text;
52
53    T((T_CALLED("winnwstr(%p,%p,%d)"), (void *) win, (void *) wstr, n));
54    if (wstr != 0) {
55	if (win) {
56	    int row, col;
57	    int last = 0;
58	    bool done = FALSE;
59
60	    getyx(win, row, col);
61
62	    text = win->_line[row].text;
63	    while (count < n && !done && count != ERR) {
64
65		if (!isWidecExt(text[col])) {
66		    int inx;
67		    wchar_t wch;
68
69		    for (inx = 0; (inx < CCHARW_MAX)
70			 && ((wch = text[col].chars[inx]) != 0);
71			 ++inx) {
72			if (count + 1 > n) {
73			    done = TRUE;
74			    if (last == 0) {
75				count = ERR;	/* error if we store nothing */
76			    } else {
77				count = last;	/* only store complete chars */
78			    }
79			    break;
80			}
81			wstr[count++] = wch;
82		    }
83		}
84		last = count;
85		if (++col > win->_maxx) {
86		    break;
87		}
88	    }
89	}
90	if (count > 0) {
91	    wstr[count] = '\0';
92	    T(("winnwstr returns %s", _nc_viswbuf(wstr)));
93	}
94    }
95    returnCode(count);
96}
97
98/*
99 * X/Open says winwstr() returns OK if not ERR.  If that is not a blunder, it
100 * must have a null termination on the string (see above).  Unlike winnstr(),
101 * it does not define what happens for a negative count with winnwstr().
102 */
103NCURSES_EXPORT(int)
104winwstr(WINDOW *win, wchar_t *wstr)
105{
106    int result = OK;
107
108    T((T_CALLED("winwstr(%p,%p)"), (void *) win, (void *) wstr));
109    if (win == 0) {
110	result = ERR;
111    } else if (winnwstr(win, wstr,
112			CCHARW_MAX * (win->_maxx - win->_curx + 1)) == ERR) {
113	result = ERR;
114    }
115    returnCode(result);
116}
117