blob: b2fdaeac71712de4be58ff6b6cdfb7e640dd16ad [file] [log] [blame]
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +05301/****************************************************************************
micky3879b9f5e72025-07-08 18:04:53 -04002 * Copyright 2020 Thomas E. Dickey *
3 * Copyright 2002-2016,2017 Free Software Foundation, Inc. *
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +05304 * *
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
micky3879b9f5e72025-07-08 18:04:53 -040043MODULE_ID("$Id: lib_inwstr.c,v 1.9 2020/02/02 23:34:34 tom Exp $")
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +053044
45NCURSES_EXPORT(int)
46winnwstr(WINDOW *win, wchar_t *wstr, int n)
47{
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +053048 int count = 0;
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +053049 cchar_t *text;
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +053050
Steve Kondikae271bc2015-11-15 02:50:53 +010051 T((T_CALLED("winnwstr(%p,%p,%d)"), (void *) win, (void *) wstr, n));
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +053052 if (wstr != 0) {
53 if (win) {
micky3879b9f5e72025-07-08 18:04:53 -040054 int row, col;
55 int last = 0;
56 bool done = FALSE;
57
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +053058 getyx(win, row, col);
59
60 text = win->_line[row].text;
micky3879b9f5e72025-07-08 18:04:53 -040061 while (count < n && !done && count != ERR) {
62
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +053063 if (!isWidecExt(text[col])) {
micky3879b9f5e72025-07-08 18:04:53 -040064 int inx;
65 wchar_t wch;
66
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +053067 for (inx = 0; (inx < CCHARW_MAX)
68 && ((wch = text[col].chars[inx]) != 0);
69 ++inx) {
70 if (count + 1 > n) {
micky3879b9f5e72025-07-08 18:04:53 -040071 done = TRUE;
72 if (last == 0) {
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +053073 count = ERR; /* error if we store nothing */
micky3879b9f5e72025-07-08 18:04:53 -040074 } else {
75 count = last; /* only store complete chars */
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +053076 }
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 */
101NCURSES_EXPORT(int)
102winwstr(WINDOW *win, wchar_t *wstr)
103{
104 int result = OK;
Steve Kondikae271bc2015-11-15 02:50:53 +0100105
106 T((T_CALLED("winwstr(%p,%p)"), (void *) win, (void *) wstr));
107 if (win == 0) {
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530108 result = ERR;
Steve Kondikae271bc2015-11-15 02:50:53 +0100109 } else if (winnwstr(win, wstr,
110 CCHARW_MAX * (win->_maxx - win->_curx + 1)) == ERR) {
111 result = ERR;
112 }
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530113 returnCode(result);
114}