1/* $NetBSD: addwstr.c,v 1.5 2019/05/20 22:17:41 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.5 2019/05/20 22:17:41 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 */
51int
52addwstr(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 */
61int
62waddwstr(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 */
72int
73addnwstr(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 */
82int
83mvaddwstr(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 */
92int
93mvwaddwstr(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 */
103int
104mvaddnwstr(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 */
114int
115mvwaddnwstr(WINDOW *win, int y, int x, const wchar_t *str, int count)
116{
117 if (_cursesi_wmove(win, y, x, 0) == 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 */
129int
130waddnwstr(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 /*
138 * BSD curses: if (n > 0) then "at most n", else "len = strlen(s)"
139 * ncurses: if (n >= 0) then "at most n", else "len = strlen(s)"
140 * XCURSES: if (n != -1) then "at most n", else "len = strlen(s)"
141 */
142 /* compute the length and column width of string */
143 if (n < -1)
144 return ERR;
145 if (n >= 0)
146 for (p = s, len = 0; n-- && *p++; ++len);
147 else
148 len = wcslen(s);
149#ifdef DEBUG
150 __CTRACE(__CTRACE_INPUT, "waddnwstr: string len=%ld\n", (long) len);
151#endif /* DEBUG */
152
153 p = s;
154 while (len) {
155 wc[0] = *p;
156 wc[1] = L'\0';
157 if (setcchar( &cc, wc, win->wattr, 0, NULL ) == ERR)
158 return ERR;
159 if (wadd_wch( win, &cc ) == ERR)
160 return ERR;
161#ifdef DEBUG
162 __CTRACE(__CTRACE_INPUT, "waddnwstr: (%x,%x,%d) added\n",
163 cc.vals[ 0 ], cc.attributes, cc.elements );
164#endif /* DEBUG */
165 p++, len--;
166 }
167
168 return OK;
169}
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.5 2019/05/20 22:17:41 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 */
51int
52addwstr(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 */
61int
62waddwstr(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 */
72int
73addnwstr(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 */
82int
83mvaddwstr(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 */
92int
93mvwaddwstr(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 */
103int
104mvaddnwstr(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 */
114int
115mvwaddnwstr(WINDOW *win, int y, int x, const wchar_t *str, int count)
116{
117 if (_cursesi_wmove(win, y, x, 0) == 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 */
129int
130waddnwstr(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 /*
138 * BSD curses: if (n > 0) then "at most n", else "len = strlen(s)"
139 * ncurses: if (n >= 0) then "at most n", else "len = strlen(s)"
140 * XCURSES: if (n != -1) then "at most n", else "len = strlen(s)"
141 */
142 /* compute the length and column width of string */
143 if (n < -1)
144 return ERR;
145 if (n >= 0)
146 for (p = s, len = 0; n-- && *p++; ++len);
147 else
148 len = wcslen(s);
149#ifdef DEBUG
150 __CTRACE(__CTRACE_INPUT, "waddnwstr: string len=%ld\n", (long) len);
151#endif /* DEBUG */
152
153 p = s;
154 while (len) {
155 wc[0] = *p;
156 wc[1] = L'\0';
157 if (setcchar( &cc, wc, win->wattr, 0, NULL ) == ERR)
158 return ERR;
159 if (wadd_wch( win, &cc ) == ERR)
160 return ERR;
161#ifdef DEBUG
162 __CTRACE(__CTRACE_INPUT, "waddnwstr: (%x,%x,%d) added\n",
163 cc.vals[ 0 ], cc.attributes, cc.elements );
164#endif /* DEBUG */
165 p++, len--;
166 }
167
168 return OK;
169}