197049Speter/****************************************************************************
2174993Srafan * Copyright (c) 2002-2004-2007 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/****************************************************************************
30174993Srafan * Author: Thomas Dickey                                                    *
3197049Speter ****************************************************************************/
3297049Speter
3397049Speter/*
3497049Speter**	lib_in_wchnstr.c
3597049Speter**
3697049Speter**	The routine win_wchnstr().
3797049Speter**
3897049Speter*/
3997049Speter
4097049Speter#include <curses.priv.h>
4197049Speter
42174993SrafanMODULE_ID("$Id: lib_in_wchnstr.c,v 1.7 2007/02/11 01:00:00 tom Exp $")
4397049Speter
4497049SpeterNCURSES_EXPORT(int)
45174993Srafanwin_wchnstr(WINDOW *win, cchar_t *wchstr, int n)
4697049Speter{
4797049Speter    int code = OK;
4897049Speter
49174993Srafan    T((T_CALLED("win_wchnstr(%p,%p,%d)"), win, wchstr, n));
5097049Speter    if (win != 0
5197049Speter	&& wchstr != 0) {
52174993Srafan	NCURSES_CH_T *src;
53166124Srafan	int row, col;
54174993Srafan	int j, k, limit;
5597049Speter
56166124Srafan	getyx(win, row, col);
57174993Srafan	limit = getmaxx(win) - col;
58174993Srafan	src = &(win->_line[row].text[col]);
59166124Srafan
6097049Speter	if (n < 0) {
61174993Srafan	    n = limit;
62174993Srafan	} else if (n > limit) {
63174993Srafan	    n = limit;
6497049Speter	}
65174993Srafan	for (j = k = 0; j < n; ++j) {
66174993Srafan	    if (j == 0 || !WidecExt(src[j]) || isWidecBase(src[j])) {
67174993Srafan		wchstr[k++] = src[j];
68174993Srafan	    }
69166124Srafan	}
70174993Srafan	memset(&(wchstr[k]), 0, sizeof(*wchstr));
71166124Srafan	T(("result = %s", _nc_viscbuf(wchstr, n)));
7297049Speter    } else {
7397049Speter	code = ERR;
7497049Speter    }
7597049Speter    returnCode(code);
7697049Speter}
77