xref: /minix/lib/libcurses/addwstr.c (revision 51ffecc1)
1 /*   $NetBSD: addwstr.c,v 1.2 2007/05/28 15:01:54 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@arl.wustl.edu,ruibiao@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.2 2007/05/28 15:01:54 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
addwstr(const wchar_t * s)52 addwstr(const wchar_t *s)
53 {
54 #ifndef HAVE_WCHAR
55 	return ERR;
56 #else
57 	return waddnwstr(stdscr, s, -1);
58 #endif /* HAVE_WCHAR */
59 }
60 
61 /*
62  * waddwstr --
63  *	  Add a string to the given window starting at (_cury, _curx).
64  */
65 int
waddwstr(WINDOW * win,const wchar_t * s)66 waddwstr(WINDOW *win, const wchar_t *s)
67 {
68 #ifndef HAVE_WCHAR
69 	return ERR;
70 #else
71 	return waddnwstr(win, s, -1);
72 #endif /* HAVE_WCHAR */
73 }
74 
75 /*
76  * addnwstr --
77  *	  Add a string (at most n characters) to stdscr starting
78  *	at (_cury, _curx).  If n is negative, add the entire string.
79  */
80 int
addnwstr(const wchar_t * str,int n)81 addnwstr(const wchar_t *str, int n)
82 {
83 #ifndef HAVE_WCHAR
84 	return ERR;
85 #else
86 	return waddnwstr(stdscr, str, n);
87 #endif /* HAVE_WCHAR */
88 }
89 
90 /*
91  * mvaddwstr --
92  *	  Add a string to stdscr starting at (y, x)
93  */
94 int
mvaddwstr(int y,int x,const wchar_t * str)95 mvaddwstr(int y, int x, const wchar_t *str)
96 {
97 #ifndef HAVE_WCHAR
98 	return ERR;
99 #else
100 	return mvwaddnwstr(stdscr, y, x, str, -1);
101 #endif /* HAVE_WCHAR */
102 }
103 
104 /*
105  * mvwaddwstr --
106  *	  Add a string to the given window starting at (y, x)
107  */
108 int
mvwaddwstr(WINDOW * win,int y,int x,const wchar_t * str)109 mvwaddwstr(WINDOW *win, int y, int x, const wchar_t *str)
110 {
111 #ifndef HAVE_WCHAR
112 	return ERR;
113 #else
114 	return mvwaddnwstr(win, y, x, str, -1);
115 #endif /* HAVE_WCHAR */
116 }
117 
118 /*
119  * mvaddnwstr --
120  *	  Add a string of at most n characters to stdscr
121  *	  starting at (y, x).
122  */
123 int
mvaddnwstr(int y,int x,const wchar_t * str,int count)124 mvaddnwstr(int y, int x, const wchar_t *str, int count)
125 {
126 #ifndef HAVE_WCHAR
127 	return ERR;
128 #else
129 	return mvwaddnwstr(stdscr, y, x, str, count);
130 #endif /* HAVE_WCHAR */
131 }
132 
133 /*
134  * mvwaddnwstr --
135  *	  Add a string of at most n characters to the given window
136  *	  starting at (y, x).
137  */
138 int
mvwaddnwstr(WINDOW * win,int y,int x,const wchar_t * str,int count)139 mvwaddnwstr(WINDOW *win, int y, int x, const wchar_t *str, int count)
140 {
141 #ifndef HAVE_WCHAR
142 	return ERR;
143 #else
144 	if (wmove(win, y, x) == ERR)
145 		return ERR;
146 
147 	return waddnwstr(win, str, count);
148 #endif /* HAVE_WCHAR */
149 }
150 
151 /*
152  * waddnwstr --
153  *	Add a string (at most n characters) to the given window
154  *	starting at (_cury, _curx).  If n is negative, add the
155  *	entire string.
156  */
157 int
waddnwstr(WINDOW * win,const wchar_t * s,int n)158 waddnwstr(WINDOW *win, const wchar_t *s, int n)
159 {
160 #ifndef HAVE_WCHAR
161 	return ERR;
162 #else
163 	size_t  len;
164 	const wchar_t *p;
165 	cchar_t cc;
166 	wchar_t wc[ 2 ];
167 
168 	/*
169 	 * BSD curses: if (n > 0) then "at most n", else "len = strlen(s)"
170 	 * ncurses: if (n >= 0) then "at most n", else "len = strlen(s)"
171 	 * XCURSES: if (n != -1) then "at most n", else "len = strlen(s)"
172 	 */
173 	/* compute the length and column width of string */
174 	if ( n < -1 )
175 		return ERR;
176 	if (n >= 0)
177 		for (p = s, len = 0; n-- && *p++; ++len );
178 	else
179 		len = wcslen(s);
180 #ifdef DEBUG
181 	__CTRACE(__CTRACE_INPUT, "waddnwstr: string len=%ld\n", (long) len);
182 #endif /* DEBUG */
183 
184 	p = s;
185 	while ( len ) {
186 		wc[ 0 ] = *p;
187 		wc[ 1 ] = L'\0';
188 		if ( setcchar( &cc, wc, win->wattr, 0, NULL ) == ERR )
189 			return ERR;
190 		if ( wadd_wch( win, &cc ) == ERR )
191 			return ERR;
192 #ifdef DEBUG
193 		__CTRACE(__CTRACE_INPUT, "waddnwstr: (%x,%x,%d) added\n",
194 				cc.vals[ 0 ], cc.attributes, cc.elements );
195 #endif /* DEBUG */
196 		p++, len--;
197 	}
198 
199 	return OK;
200 #endif /* HAVE_WCHAR */
201 }
202