Home | History | Annotate | Line # | Download | only in libcurses
addwstr.c revision 1.8.4.1
      1  1.8.4.1  perseant /*   $NetBSD: addwstr.c,v 1.8.4.1 2025/08/02 05:54:45 perseant Exp $ */
      2      1.1     blymn 
      3      1.1     blymn /*
      4      1.1     blymn  * Copyright (c) 2005 The NetBSD Foundation Inc.
      5      1.1     blymn  * All rights reserved.
      6      1.1     blymn  *
      7      1.1     blymn  * This code is derived from code donated to the NetBSD Foundation
      8      1.1     blymn  * by Ruibiao Qiu <ruibiao (at) arl.wustl.edu,ruibiao (at) gmail.com>.
      9      1.1     blymn  *
     10      1.1     blymn  *
     11      1.1     blymn  * Redistribution and use in source and binary forms, with or without
     12      1.1     blymn  * modification, are permitted provided that the following conditions
     13      1.1     blymn  * are met:
     14      1.1     blymn  * 1. Redistributions of source code must retain the above copyright
     15      1.1     blymn  *	notice, this list of conditions and the following disclaimer.
     16      1.1     blymn  * 2. Redistributions in binary form must reproduce the above copyright
     17      1.1     blymn  *	notice, this list of conditions and the following disclaimer in the
     18      1.1     blymn  *	documentation and/or other materials provided with the distribution.
     19      1.1     blymn  * 3. Neither the name of the NetBSD Foundation nor the names of its
     20      1.1     blymn  *	contributors may be used to endorse or promote products derived
     21      1.1     blymn  *	from this software without specific prior written permission.
     22      1.1     blymn  *
     23      1.1     blymn  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND
     24      1.1     blymn  * CONTRIBUTORS ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
     25      1.1     blymn  * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
     26      1.1     blymn  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     27      1.1     blymn  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     28      1.1     blymn  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     29      1.1     blymn  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     30      1.1     blymn  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     31      1.1     blymn  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     32      1.1     blymn  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     33      1.1     blymn  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     34      1.1     blymn  * SUCH DAMAGE.
     35      1.1     blymn  */
     36      1.1     blymn 
     37      1.1     blymn #include <sys/cdefs.h>
     38      1.1     blymn #ifndef lint
     39  1.8.4.1  perseant __RCSID("$NetBSD: addwstr.c,v 1.8.4.1 2025/08/02 05:54:45 perseant Exp $");
     40      1.1     blymn #endif						  /* not lint */
     41      1.1     blymn 
     42      1.1     blymn #include <string.h>
     43      1.1     blymn 
     44      1.1     blymn #include "curses.h"
     45      1.1     blymn #include "curses_private.h"
     46      1.1     blymn 
     47      1.1     blymn /*
     48      1.1     blymn  * addwstr --
     49      1.1     blymn  *	  Add a string to stdscr starting at (_cury, _curx).
     50      1.1     blymn  */
     51      1.1     blymn int
     52      1.1     blymn addwstr(const wchar_t *s)
     53      1.1     blymn {
     54      1.1     blymn 	return waddnwstr(stdscr, s, -1);
     55      1.1     blymn }
     56      1.1     blymn 
     57      1.1     blymn /*
     58      1.1     blymn  * waddwstr --
     59      1.1     blymn  *	  Add a string to the given window starting at (_cury, _curx).
     60      1.1     blymn  */
     61      1.1     blymn int
     62      1.1     blymn waddwstr(WINDOW *win, const wchar_t *s)
     63      1.1     blymn {
     64      1.1     blymn 	return waddnwstr(win, s, -1);
     65      1.1     blymn }
     66      1.1     blymn 
     67      1.1     blymn /*
     68      1.1     blymn  * addnwstr --
     69      1.1     blymn  *	  Add a string (at most n characters) to stdscr starting
     70      1.1     blymn  *	at (_cury, _curx).  If n is negative, add the entire string.
     71      1.1     blymn  */
     72      1.1     blymn int
     73      1.1     blymn addnwstr(const wchar_t *str, int n)
     74      1.1     blymn {
     75      1.1     blymn 	return waddnwstr(stdscr, str, n);
     76      1.1     blymn }
     77      1.1     blymn 
     78      1.1     blymn /*
     79      1.1     blymn  * mvaddwstr --
     80      1.1     blymn  *	  Add a string to stdscr starting at (y, x)
     81      1.1     blymn  */
     82      1.1     blymn int
     83      1.1     blymn mvaddwstr(int y, int x, const wchar_t *str)
     84      1.1     blymn {
     85      1.1     blymn 	return mvwaddnwstr(stdscr, y, x, str, -1);
     86      1.1     blymn }
     87      1.1     blymn 
     88      1.1     blymn /*
     89      1.1     blymn  * mvwaddwstr --
     90      1.1     blymn  *	  Add a string to the given window starting at (y, x)
     91      1.1     blymn  */
     92      1.1     blymn int
     93      1.1     blymn mvwaddwstr(WINDOW *win, int y, int x, const wchar_t *str)
     94      1.1     blymn {
     95      1.1     blymn 	return mvwaddnwstr(win, y, x, str, -1);
     96      1.1     blymn }
     97      1.1     blymn 
     98      1.1     blymn /*
     99      1.1     blymn  * mvaddnwstr --
    100      1.1     blymn  *	  Add a string of at most n characters to stdscr
    101      1.1     blymn  *	  starting at (y, x).
    102      1.1     blymn  */
    103      1.1     blymn int
    104      1.1     blymn mvaddnwstr(int y, int x, const wchar_t *str, int count)
    105      1.1     blymn {
    106      1.1     blymn 	return mvwaddnwstr(stdscr, y, x, str, count);
    107      1.1     blymn }
    108      1.1     blymn 
    109      1.1     blymn /*
    110      1.1     blymn  * mvwaddnwstr --
    111      1.1     blymn  *	  Add a string of at most n characters to the given window
    112      1.1     blymn  *	  starting at (y, x).
    113      1.1     blymn  */
    114      1.1     blymn int
    115      1.1     blymn mvwaddnwstr(WINDOW *win, int y, int x, const wchar_t *str, int count)
    116      1.1     blymn {
    117      1.6     blymn 	if (wmove(win, y, x) == ERR)
    118      1.1     blymn 		return ERR;
    119      1.1     blymn 
    120      1.1     blymn 	return waddnwstr(win, str, count);
    121      1.1     blymn }
    122      1.1     blymn 
    123      1.1     blymn /*
    124      1.1     blymn  * waddnwstr --
    125      1.1     blymn  *	Add a string (at most n characters) to the given window
    126      1.1     blymn  *	starting at (_cury, _curx).  If n is negative, add the
    127      1.1     blymn  *	entire string.
    128      1.1     blymn  */
    129      1.1     blymn int
    130      1.1     blymn waddnwstr(WINDOW *win, const wchar_t *s, int n)
    131      1.1     blymn {
    132      1.1     blymn 	size_t  len;
    133      1.1     blymn 	const wchar_t *p;
    134      1.1     blymn 	cchar_t cc;
    135      1.3       roy 	wchar_t wc[2];
    136      1.1     blymn 
    137  1.8.4.1  perseant 	if (__predict_false(win == NULL))
    138  1.8.4.1  perseant 		return ERR;
    139  1.8.4.1  perseant 
    140      1.1     blymn 	/*
    141      1.1     blymn 	 * BSD curses: if (n > 0) then "at most n", else "len = strlen(s)"
    142      1.1     blymn 	 * ncurses: if (n >= 0) then "at most n", else "len = strlen(s)"
    143      1.1     blymn 	 * XCURSES: if (n != -1) then "at most n", else "len = strlen(s)"
    144      1.1     blymn 	 */
    145      1.1     blymn 	/* compute the length and column width of string */
    146      1.3       roy 	if (n < -1)
    147      1.1     blymn 		return ERR;
    148      1.1     blymn 	if (n >= 0)
    149      1.3       roy 		for (p = s, len = 0; n-- && *p++; ++len);
    150      1.1     blymn 	else
    151      1.1     blymn 		len = wcslen(s);
    152      1.8       rin 	__CTRACE(__CTRACE_INPUT, "waddnwstr: string len=%zu\n", len);
    153      1.1     blymn 
    154      1.1     blymn 	p = s;
    155      1.3       roy 	while (len) {
    156      1.3       roy 		wc[0] = *p;
    157      1.3       roy 		wc[1] = L'\0';
    158      1.3       roy 		if (setcchar( &cc, wc, win->wattr, 0, NULL ) == ERR)
    159      1.1     blymn 			return ERR;
    160      1.3       roy 		if (wadd_wch( win, &cc ) == ERR)
    161      1.1     blymn 			return ERR;
    162      1.3       roy 		__CTRACE(__CTRACE_INPUT, "waddnwstr: (%x,%x,%d) added\n",
    163      1.8       rin 		    cc.vals[0], cc.attributes, cc.elements);
    164      1.1     blymn 		p++, len--;
    165      1.1     blymn 	}
    166      1.1     blymn 
    167      1.1     blymn 	return OK;
    168      1.1     blymn }
    169