xref: /openbsd/lib/libcurses/widechar/lib_inwstr.c (revision e5dd7070)
1 /* $OpenBSD: lib_inwstr.c,v 1.1 2010/09/06 17:26:17 nicm Exp $ */
2 
3 /****************************************************************************
4  * Copyright (c) 2002,2004 Free Software Foundation, Inc.                   *
5  *                                                                          *
6  * Permission is hereby granted, free of charge, to any person obtaining a  *
7  * copy of this software and associated documentation files (the            *
8  * "Software"), to deal in the Software without restriction, including      *
9  * without limitation the rights to use, copy, modify, merge, publish,      *
10  * distribute, distribute with modifications, sublicense, and/or sell       *
11  * copies of the Software, and to permit persons to whom the Software is    *
12  * furnished to do so, subject to the following conditions:                 *
13  *                                                                          *
14  * The above copyright notice and this permission notice shall be included  *
15  * in all copies or substantial portions of the Software.                   *
16  *                                                                          *
17  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS  *
18  * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF               *
19  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.   *
20  * IN NO EVENT SHALL THE ABOVE COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,   *
21  * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR    *
22  * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR    *
23  * THE USE OR OTHER DEALINGS IN THE SOFTWARE.                               *
24  *                                                                          *
25  * Except as contained in this notice, the name(s) of the above copyright   *
26  * holders shall not be used in advertising or otherwise to promote the     *
27  * sale, use or other dealings in this Software without prior written       *
28  * authorization.                                                           *
29  ****************************************************************************/
30 
31 /****************************************************************************
32  * Author: Thomas Dickey                                                    *
33  ****************************************************************************/
34 
35 /*
36 **	lib_inwstr.c
37 **
38 **	The routines winnwstr() and winwstr().
39 **
40 */
41 
42 #include <curses.priv.h>
43 
44 MODULE_ID("$Id: lib_inwstr.c,v 1.1 2010/09/06 17:26:17 nicm Exp $")
45 
46 NCURSES_EXPORT(int)
47 winnwstr(WINDOW *win, wchar_t *wstr, int n)
48 {
49     int row, col, inx;
50     int count = 0;
51     int last = 0;
52     cchar_t *text;
53     wchar_t wch;
54 
55     T((T_CALLED("winnwstr(%p,%p,%d)"), win, wstr, n));
56     if (wstr != 0) {
57 	if (win) {
58 	    getyx(win, row, col);
59 
60 	    text = win->_line[row].text;
61 	    while (count < n && count != ERR) {
62 		if (!isWidecExt(text[col])) {
63 		    for (inx = 0; (inx < CCHARW_MAX)
64 			 && ((wch = text[col].chars[inx]) != 0);
65 			 ++inx) {
66 			if (count + 1 > n) {
67 			    if ((count = last) == 0) {
68 				count = ERR;	/* error if we store nothing */
69 			    }
70 			    break;
71 			}
72 			wstr[count++] = wch;
73 		    }
74 		}
75 		last = count;
76 		if (++col > win->_maxx) {
77 		    break;
78 		}
79 	    }
80 	}
81 	if (count > 0) {
82 	    wstr[count] = '\0';
83 	    T(("winnwstr returns %s", _nc_viswbuf(wstr)));
84 	}
85     }
86     returnCode(count);
87 }
88 
89 /*
90  * X/Open says winwstr() returns OK if not ERR.  If that is not a blunder, it
91  * must have a null termination on the string (see above).  Unlike winnstr(),
92  * it does not define what happens for a negative count with winnwstr().
93  */
94 NCURSES_EXPORT(int)
95 winwstr(WINDOW *win, wchar_t *wstr)
96 {
97     int result = OK;
98     T((T_CALLED("winwstr(%p,%p)"), win, wstr));
99     if (winnwstr(win, wstr, CCHARW_MAX * (win->_maxx - win->_curx + 1)) == ERR)
100 	result = ERR;
101     returnCode(result);
102 }
103