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