197049Speter/****************************************************************************
2166124Srafan * Copyright (c) 2002,2004 Free Software Foundation, Inc.                   *
397049Speter *                                                                          *
497049Speter * Permission is hereby granted, free of charge, to any person obtaining a  *
597049Speter * copy of this software and associated documentation files (the            *
697049Speter * "Software"), to deal in the Software without restriction, including      *
797049Speter * without limitation the rights to use, copy, modify, merge, publish,      *
897049Speter * distribute, distribute with modifications, sublicense, and/or sell       *
997049Speter * copies of the Software, and to permit persons to whom the Software is    *
1097049Speter * furnished to do so, subject to the following conditions:                 *
1197049Speter *                                                                          *
1297049Speter * The above copyright notice and this permission notice shall be included  *
1397049Speter * in all copies or substantial portions of the Software.                   *
1497049Speter *                                                                          *
1597049Speter * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS  *
1697049Speter * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF               *
1797049Speter * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.   *
1897049Speter * IN NO EVENT SHALL THE ABOVE COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,   *
1997049Speter * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR    *
2097049Speter * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR    *
2197049Speter * THE USE OR OTHER DEALINGS IN THE SOFTWARE.                               *
2297049Speter *                                                                          *
2397049Speter * Except as contained in this notice, the name(s) of the above copyright   *
2497049Speter * holders shall not be used in advertising or otherwise to promote the     *
2597049Speter * sale, use or other dealings in this Software without prior written       *
2697049Speter * authorization.                                                           *
2797049Speter ****************************************************************************/
2897049Speter
2997049Speter/****************************************************************************
30166124Srafan * Author: Thomas Dickey                                                    *
3197049Speter ****************************************************************************/
3297049Speter
3397049Speter/*
3497049Speter**	lib_inwstr.c
3597049Speter**
3697049Speter**	The routines winnwstr() and winwstr().
3797049Speter**
3897049Speter*/
3997049Speter
4097049Speter#include <curses.priv.h>
4197049Speter
42166124SrafanMODULE_ID("$Id: lib_inwstr.c,v 1.4 2004/10/23 20:41:28 tom Exp $")
4397049Speter
4497049SpeterNCURSES_EXPORT(int)
45166124Srafanwinnwstr(WINDOW *win, wchar_t *wstr, int n)
4697049Speter{
4797049Speter    int row, col, inx;
4897049Speter    int count = 0;
4997049Speter    int last = 0;
5097049Speter    cchar_t *text;
5197049Speter    wchar_t wch;
5297049Speter
53166124Srafan    T((T_CALLED("winnwstr(%p,%p,%d)"), win, wstr, n));
5497049Speter    if (wstr != 0) {
5597049Speter	if (win) {
5697049Speter	    getyx(win, row, col);
5797049Speter
5897049Speter	    text = win->_line[row].text;
5997049Speter	    while (count < n && count != ERR) {
60166124Srafan		if (!isWidecExt(text[col])) {
61166124Srafan		    for (inx = 0; (inx < CCHARW_MAX)
62166124Srafan			 && ((wch = text[col].chars[inx]) != 0);
63166124Srafan			 ++inx) {
64166124Srafan			if (count + 1 > n) {
65166124Srafan			    if ((count = last) == 0) {
66166124Srafan				count = ERR;	/* error if we store nothing */
67166124Srafan			    }
68166124Srafan			    break;
6997049Speter			}
70166124Srafan			wstr[count++] = wch;
7197049Speter		    }
7297049Speter		}
7397049Speter		last = count;
7497049Speter		if (++col > win->_maxx) {
7597049Speter		    break;
7697049Speter		}
7797049Speter	    }
7897049Speter	}
79166124Srafan	if (count > 0) {
8097049Speter	    wstr[count] = '\0';
81166124Srafan	    T(("winnwstr returns %s", _nc_viswbuf(wstr)));
82166124Srafan	}
8397049Speter    }
8497049Speter    returnCode(count);
8597049Speter}
8697049Speter
8797049Speter/*
8897049Speter * X/Open says winwstr() returns OK if not ERR.  If that is not a blunder, it
8997049Speter * must have a null termination on the string (see above).  Unlike winnstr(),
9097049Speter * it does not define what happens for a negative count with winnwstr().
9197049Speter */
9297049SpeterNCURSES_EXPORT(int)
93166124Srafanwinwstr(WINDOW *win, wchar_t *wstr)
9497049Speter{
9597049Speter    int result = OK;
96166124Srafan    T((T_CALLED("winwstr(%p,%p)"), win, wstr));
9797049Speter    if (winnwstr(win, wstr, CCHARW_MAX * (win->_maxx - win->_curx + 1)) == ERR)
9897049Speter	result = ERR;
9997049Speter    returnCode(result);
10097049Speter}
101