Home | History | Annotate | Line # | Download | only in libcurses
addchnstr.c revision 1.4
      1  1.4  martin /*	$NetBSD: addchnstr.c,v 1.4 2008/04/28 20:23:01 martin 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  *
     19  1.1     jdc  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     20  1.1     jdc  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     21  1.1     jdc  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     22  1.1     jdc  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     23  1.1     jdc  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     24  1.1     jdc  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     25  1.1     jdc  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     26  1.1     jdc  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     27  1.1     jdc  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     28  1.1     jdc  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     29  1.1     jdc  * POSSIBILITY OF SUCH DAMAGE.
     30  1.1     jdc  */
     31  1.1     jdc 
     32  1.1     jdc #include <sys/cdefs.h>
     33  1.1     jdc #ifndef lint
     34  1.4  martin __RCSID("$NetBSD: addchnstr.c,v 1.4 2008/04/28 20:23:01 martin Exp $");
     35  1.1     jdc #endif				/* not lint */
     36  1.1     jdc 
     37  1.1     jdc #include <stdlib.h>
     38  1.1     jdc 
     39  1.1     jdc #include "curses.h"
     40  1.1     jdc #include "curses_private.h"
     41  1.1     jdc 
     42  1.1     jdc #ifndef _CURSES_USE_MACROS
     43  1.1     jdc 
     44  1.1     jdc /*
     45  1.1     jdc  * addchstr --
     46  1.1     jdc  *      Add a string to stdscr starting at (_cury, _curx).
     47  1.1     jdc  */
     48  1.1     jdc int
     49  1.1     jdc addchstr(const chtype *chstr)
     50  1.1     jdc {
     51  1.1     jdc 	return waddchnstr(stdscr, chstr, -1);
     52  1.1     jdc }
     53  1.1     jdc 
     54  1.1     jdc /*
     55  1.1     jdc  * waddchstr --
     56  1.1     jdc  *      Add a string to the given window starting at (_cury, _curx).
     57  1.1     jdc  */
     58  1.1     jdc int
     59  1.1     jdc waddchstr(WINDOW *win, const chtype *chstr)
     60  1.1     jdc {
     61  1.1     jdc 	return waddchnstr(win, chstr, -1);
     62  1.1     jdc }
     63  1.1     jdc 
     64  1.1     jdc /*
     65  1.1     jdc  * addchnstr --
     66  1.1     jdc  *      Add a string (at most n characters) to stdscr starting
     67  1.1     jdc  *	at (_cury, _curx).  If n is negative, add the entire string.
     68  1.1     jdc  */
     69  1.1     jdc int
     70  1.1     jdc addchnstr(const chtype *chstr, int n)
     71  1.1     jdc {
     72  1.1     jdc 	return waddchnstr(stdscr, chstr, n);
     73  1.1     jdc }
     74  1.1     jdc 
     75  1.1     jdc /*
     76  1.1     jdc  * mvaddchstr --
     77  1.1     jdc  *      Add a string to stdscr starting at (y, x)
     78  1.1     jdc  */
     79  1.1     jdc int
     80  1.1     jdc mvaddchstr(int y, int x, const chtype *chstr)
     81  1.1     jdc {
     82  1.1     jdc 	return mvwaddchnstr(stdscr, y, x, chstr, -1);
     83  1.1     jdc }
     84  1.1     jdc 
     85  1.1     jdc /*
     86  1.1     jdc  * mvwaddchstr --
     87  1.1     jdc  *      Add a string to the given window starting at (y, x)
     88  1.1     jdc  */
     89  1.1     jdc int
     90  1.1     jdc mvwaddchstr(WINDOW *win, int y, int x, const chtype *chstr)
     91  1.1     jdc {
     92  1.1     jdc 	return mvwaddchnstr(win, y, x, chstr, -1);
     93  1.1     jdc }
     94  1.1     jdc 
     95  1.1     jdc /*
     96  1.1     jdc  * mvaddchnstr --
     97  1.1     jdc  *      Add a string of at most n characters to stdscr
     98  1.1     jdc  *      starting at (y, x).
     99  1.1     jdc  */
    100  1.1     jdc int
    101  1.1     jdc mvaddchnstr(int y, int x, const chtype *chstr, int n)
    102  1.1     jdc {
    103  1.1     jdc 	return mvwaddchnstr(stdscr, y, x, chstr, n);
    104  1.1     jdc }
    105  1.1     jdc 
    106  1.1     jdc /*
    107  1.1     jdc  * mvwaddchnstr --
    108  1.1     jdc  *      Add a string of at most n characters to the given window
    109  1.1     jdc  *      starting at (y, x).
    110  1.1     jdc  */
    111  1.1     jdc int
    112  1.1     jdc mvwaddchnstr(WINDOW *win, int y, int x, const chtype *chstr, int n)
    113  1.1     jdc {
    114  1.1     jdc 	if (wmove(win, y, x) == ERR)
    115  1.1     jdc 		return ERR;
    116  1.1     jdc 
    117  1.1     jdc 	return waddchnstr(win, chstr, n);
    118  1.1     jdc }
    119  1.1     jdc 
    120  1.1     jdc #endif
    121  1.1     jdc 
    122  1.1     jdc /*
    123  1.1     jdc  * waddchnstr --
    124  1.1     jdc  *	Add a string (at most n characters) to the given window
    125  1.1     jdc  *	starting at (_cury, _curx).  If n is negative, add the
    126  1.1     jdc  *	entire string.
    127  1.1     jdc  */
    128  1.1     jdc int
    129  1.1     jdc waddchnstr(WINDOW *win, const chtype *chstr, int n)
    130  1.1     jdc {
    131  1.2     wiz 	size_t  len;
    132  1.1     jdc 	const chtype *chp;
    133  1.1     jdc 	attr_t	attr;
    134  1.1     jdc 	char	*ocp, *cp, *start;
    135  1.2     wiz 	int i, ret;
    136  1.1     jdc 
    137  1.1     jdc #ifdef DEBUG
    138  1.3     jdc 	__CTRACE(__CTRACE_INPUT, "waddchnstr: win = %p, chstr = %p, n = %d\n",
    139  1.3     jdc 	    win, chstr, n);
    140  1.1     jdc #endif
    141  1.1     jdc 
    142  1.1     jdc 	if (n >= 0)
    143  1.1     jdc 		for (chp = chstr, len = 0; n-- && *chp++; ++len);
    144  1.1     jdc 	else
    145  1.1     jdc 		for (chp = chstr, len = 0; *chp++; ++len);
    146  1.1     jdc 
    147  1.1     jdc 	if ((ocp = malloc(len + 1)) == NULL)
    148  1.1     jdc 		return ERR;
    149  1.1     jdc 	chp = chstr;
    150  1.1     jdc 	cp = ocp;
    151  1.1     jdc 	start = ocp;
    152  1.1     jdc 	i = 0;
    153  1.1     jdc 	attr = (*chp) & __ATTRIBUTES;
    154  1.1     jdc 	while (len) {
    155  1.1     jdc 		*cp = (*chp) & __CHARTEXT;
    156  1.1     jdc 		cp++;
    157  1.1     jdc 		chp++;
    158  1.1     jdc 		i++;
    159  1.1     jdc 		len--;
    160  1.1     jdc 		if (((*chp) & __ATTRIBUTES) != attr) {
    161  1.1     jdc 			*cp = '\0';
    162  1.1     jdc 			if (__waddbytes(win, start, i, attr) == ERR) {
    163  1.1     jdc 				free(ocp);
    164  1.1     jdc 				return ERR;
    165  1.1     jdc 			}
    166  1.1     jdc 			attr = (*chp) & __ATTRIBUTES;
    167  1.1     jdc 			start = cp;
    168  1.1     jdc 			i = 0;
    169  1.1     jdc 		}
    170  1.1     jdc 	}
    171  1.1     jdc 	*cp = '\0';
    172  1.2     wiz 	ret = __waddbytes(win, start, i, attr);
    173  1.1     jdc 	free(ocp);
    174  1.1     jdc 	return ret;
    175  1.1     jdc }
    176