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