Home | History | Annotate | Line # | Download | only in libcurses
addchnstr.c revision 1.3
      1  1.3  jdc /*	$NetBSD: addchnstr.c,v 1.3 2007/01/21 13:25:36 jdc Exp $	*/
      2  1.1  jdc 
      3  1.1  jdc /*
      4  1.1  jdc  * Copyright (c) 2003 The NetBSD Foundation, Inc.
      5  1.1  jdc  * All rights reserved.
      6  1.1  jdc  *
      7  1.1  jdc  * This code is derived from software contributed to The NetBSD Foundation
      8  1.1  jdc  * by Douwe Kiela (virtus (at) wanadoo.nl).
      9  1.1  jdc  *
     10  1.1  jdc  * Redistribution and use in source and binary forms, with or without
     11  1.1  jdc  * modification, are permitted provided that the following conditions
     12  1.1  jdc  * are met:
     13  1.1  jdc  * 1. Redistributions of source code must retain the above copyright
     14  1.1  jdc  *    notice, this list of conditions and the following disclaimer.
     15  1.1  jdc  * 2. Redistributions in binary form must reproduce the above copyright
     16  1.1  jdc  *    notice, this list of conditions and the following disclaimer in the
     17  1.1  jdc  *    documentation and/or other materials provided with the distribution.
     18  1.1  jdc  * 3. All advertising materials mentioning features or use of this software
     19  1.1  jdc  *    must display the following acknowledgement:
     20  1.1  jdc  *        This product includes software developed by the NetBSD
     21  1.1  jdc  *        Foundation, Inc. and its contributors.
     22  1.1  jdc  * 4. Neither the name of The NetBSD Foundation nor the names of its
     23  1.1  jdc  *    contributors may be used to endorse or promote products derived
     24  1.1  jdc  *    from this software without specific prior written permission.
     25  1.1  jdc  *
     26  1.1  jdc  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     27  1.1  jdc  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     28  1.1  jdc  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     29  1.1  jdc  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     30  1.1  jdc  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     31  1.1  jdc  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     32  1.1  jdc  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     33  1.1  jdc  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     34  1.1  jdc  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     35  1.1  jdc  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     36  1.1  jdc  * POSSIBILITY OF SUCH DAMAGE.
     37  1.1  jdc  */
     38  1.1  jdc 
     39  1.1  jdc #include <sys/cdefs.h>
     40  1.1  jdc #ifndef lint
     41  1.3  jdc __RCSID("$NetBSD: addchnstr.c,v 1.3 2007/01/21 13:25:36 jdc Exp $");
     42  1.1  jdc #endif				/* not lint */
     43  1.1  jdc 
     44  1.1  jdc #include <stdlib.h>
     45  1.1  jdc 
     46  1.1  jdc #include "curses.h"
     47  1.1  jdc #include "curses_private.h"
     48  1.1  jdc 
     49  1.1  jdc #ifndef _CURSES_USE_MACROS
     50  1.1  jdc 
     51  1.1  jdc /*
     52  1.1  jdc  * addchstr --
     53  1.1  jdc  *      Add a string to stdscr starting at (_cury, _curx).
     54  1.1  jdc  */
     55  1.1  jdc int
     56  1.1  jdc addchstr(const chtype *chstr)
     57  1.1  jdc {
     58  1.1  jdc 	return waddchnstr(stdscr, chstr, -1);
     59  1.1  jdc }
     60  1.1  jdc 
     61  1.1  jdc /*
     62  1.1  jdc  * waddchstr --
     63  1.1  jdc  *      Add a string to the given window starting at (_cury, _curx).
     64  1.1  jdc  */
     65  1.1  jdc int
     66  1.1  jdc waddchstr(WINDOW *win, const chtype *chstr)
     67  1.1  jdc {
     68  1.1  jdc 	return waddchnstr(win, chstr, -1);
     69  1.1  jdc }
     70  1.1  jdc 
     71  1.1  jdc /*
     72  1.1  jdc  * addchnstr --
     73  1.1  jdc  *      Add a string (at most n characters) to stdscr starting
     74  1.1  jdc  *	at (_cury, _curx).  If n is negative, add the entire string.
     75  1.1  jdc  */
     76  1.1  jdc int
     77  1.1  jdc addchnstr(const chtype *chstr, int n)
     78  1.1  jdc {
     79  1.1  jdc 	return waddchnstr(stdscr, chstr, n);
     80  1.1  jdc }
     81  1.1  jdc 
     82  1.1  jdc /*
     83  1.1  jdc  * mvaddchstr --
     84  1.1  jdc  *      Add a string to stdscr starting at (y, x)
     85  1.1  jdc  */
     86  1.1  jdc int
     87  1.1  jdc mvaddchstr(int y, int x, const chtype *chstr)
     88  1.1  jdc {
     89  1.1  jdc 	return mvwaddchnstr(stdscr, y, x, chstr, -1);
     90  1.1  jdc }
     91  1.1  jdc 
     92  1.1  jdc /*
     93  1.1  jdc  * mvwaddchstr --
     94  1.1  jdc  *      Add a string to the given window starting at (y, x)
     95  1.1  jdc  */
     96  1.1  jdc int
     97  1.1  jdc mvwaddchstr(WINDOW *win, int y, int x, const chtype *chstr)
     98  1.1  jdc {
     99  1.1  jdc 	return mvwaddchnstr(win, y, x, chstr, -1);
    100  1.1  jdc }
    101  1.1  jdc 
    102  1.1  jdc /*
    103  1.1  jdc  * mvaddchnstr --
    104  1.1  jdc  *      Add a string of at most n characters to stdscr
    105  1.1  jdc  *      starting at (y, x).
    106  1.1  jdc  */
    107  1.1  jdc int
    108  1.1  jdc mvaddchnstr(int y, int x, const chtype *chstr, int n)
    109  1.1  jdc {
    110  1.1  jdc 	return mvwaddchnstr(stdscr, y, x, chstr, n);
    111  1.1  jdc }
    112  1.1  jdc 
    113  1.1  jdc /*
    114  1.1  jdc  * mvwaddchnstr --
    115  1.1  jdc  *      Add a string of at most n characters to the given window
    116  1.1  jdc  *      starting at (y, x).
    117  1.1  jdc  */
    118  1.1  jdc int
    119  1.1  jdc mvwaddchnstr(WINDOW *win, int y, int x, const chtype *chstr, int n)
    120  1.1  jdc {
    121  1.1  jdc 	if (wmove(win, y, x) == ERR)
    122  1.1  jdc 		return ERR;
    123  1.1  jdc 
    124  1.1  jdc 	return waddchnstr(win, chstr, n);
    125  1.1  jdc }
    126  1.1  jdc 
    127  1.1  jdc #endif
    128  1.1  jdc 
    129  1.1  jdc /*
    130  1.1  jdc  * waddchnstr --
    131  1.1  jdc  *	Add a string (at most n characters) to the given window
    132  1.1  jdc  *	starting at (_cury, _curx).  If n is negative, add the
    133  1.1  jdc  *	entire string.
    134  1.1  jdc  */
    135  1.1  jdc int
    136  1.1  jdc waddchnstr(WINDOW *win, const chtype *chstr, int n)
    137  1.1  jdc {
    138  1.2  wiz 	size_t  len;
    139  1.1  jdc 	const chtype *chp;
    140  1.1  jdc 	attr_t	attr;
    141  1.1  jdc 	char	*ocp, *cp, *start;
    142  1.2  wiz 	int i, ret;
    143  1.1  jdc 
    144  1.1  jdc #ifdef DEBUG
    145  1.3  jdc 	__CTRACE(__CTRACE_INPUT, "waddchnstr: win = %p, chstr = %p, n = %d\n",
    146  1.3  jdc 	    win, chstr, n);
    147  1.1  jdc #endif
    148  1.1  jdc 
    149  1.1  jdc 	if (n >= 0)
    150  1.1  jdc 		for (chp = chstr, len = 0; n-- && *chp++; ++len);
    151  1.1  jdc 	else
    152  1.1  jdc 		for (chp = chstr, len = 0; *chp++; ++len);
    153  1.1  jdc 
    154  1.1  jdc 	if ((ocp = malloc(len + 1)) == NULL)
    155  1.1  jdc 		return ERR;
    156  1.1  jdc 	chp = chstr;
    157  1.1  jdc 	cp = ocp;
    158  1.1  jdc 	start = ocp;
    159  1.1  jdc 	i = 0;
    160  1.1  jdc 	attr = (*chp) & __ATTRIBUTES;
    161  1.1  jdc 	while (len) {
    162  1.1  jdc 		*cp = (*chp) & __CHARTEXT;
    163  1.1  jdc 		cp++;
    164  1.1  jdc 		chp++;
    165  1.1  jdc 		i++;
    166  1.1  jdc 		len--;
    167  1.1  jdc 		if (((*chp) & __ATTRIBUTES) != attr) {
    168  1.1  jdc 			*cp = '\0';
    169  1.1  jdc 			if (__waddbytes(win, start, i, attr) == ERR) {
    170  1.1  jdc 				free(ocp);
    171  1.1  jdc 				return ERR;
    172  1.1  jdc 			}
    173  1.1  jdc 			attr = (*chp) & __ATTRIBUTES;
    174  1.1  jdc 			start = cp;
    175  1.1  jdc 			i = 0;
    176  1.1  jdc 		}
    177  1.1  jdc 	}
    178  1.1  jdc 	*cp = '\0';
    179  1.2  wiz 	ret = __waddbytes(win, start, i, attr);
    180  1.1  jdc 	free(ocp);
    181  1.1  jdc 	return ret;
    182  1.1  jdc }
    183