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