1 /****************************************************************************
2  * Copyright 2020 Thomas E. Dickey                                          *
3  * Copyright 2002-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: Thomas Dickey                                                    *
32  ****************************************************************************/
33 
34 /*
35 **	lib_inwstr.c
36 **
37 **	The routines winnwstr() and winwstr().
38 **
39 */
40 
41 #include <curses.priv.h>
42 
43 MODULE_ID("$Id: lib_inwstr.c,v 1.9 2020/02/02 23:34:34 tom Exp $")
44 
NCURSES_EXPORT(int)45 NCURSES_EXPORT(int)
46 winnwstr(WINDOW *win, wchar_t *wstr, int n)
47 {
48     int count = 0;
49     cchar_t *text;
50 
51     T((T_CALLED("winnwstr(%p,%p,%d)"), (void *) win, (void *) wstr, n));
52     if (wstr != 0) {
53 	if (win) {
54 	    int row, col;
55 	    int last = 0;
56 	    bool done = FALSE;
57 
58 	    getyx(win, row, col);
59 
60 	    text = win->_line[row].text;
61 	    while (count < n && !done && count != ERR) {
62 
63 		if (!isWidecExt(text[col])) {
64 		    int inx;
65 		    wchar_t wch;
66 
67 		    for (inx = 0; (inx < CCHARW_MAX)
68 			 && ((wch = text[col].chars[inx]) != 0);
69 			 ++inx) {
70 			if (count + 1 > n) {
71 			    done = TRUE;
72 			    if (last == 0) {
73 				count = ERR;	/* error if we store nothing */
74 			    } else {
75 				count = last;	/* only store complete chars */
76 			    }
77 			    break;
78 			}
79 			wstr[count++] = wch;
80 		    }
81 		}
82 		last = count;
83 		if (++col > win->_maxx) {
84 		    break;
85 		}
86 	    }
87 	}
88 	if (count > 0) {
89 	    wstr[count] = '\0';
90 	    T(("winnwstr returns %s", _nc_viswbuf(wstr)));
91 	}
92     }
93     returnCode(count);
94 }
95 
96 /*
97  * X/Open says winwstr() returns OK if not ERR.  If that is not a blunder, it
98  * must have a null termination on the string (see above).  Unlike winnstr(),
99  * it does not define what happens for a negative count with winnwstr().
100  */
101 NCURSES_EXPORT(int)
winwstr(WINDOW * win,wchar_t * wstr)102 winwstr(WINDOW *win, wchar_t *wstr)
103 {
104     int result = OK;
105 
106     T((T_CALLED("winwstr(%p,%p)"), (void *) win, (void *) wstr));
107     if (win == 0) {
108 	result = ERR;
109     } else if (winnwstr(win, wstr,
110 			CCHARW_MAX * (win->_maxx - win->_curx + 1)) == ERR) {
111 	result = ERR;
112     }
113     returnCode(result);
114 }
115