xref: /openbsd-src/lib/libcurses/base/lib_slkset.c (revision a28daedfc357b214be5c701aa8ba8adb29a7f1c2)
1 /*	$OpenBSD: lib_slkset.c,v 1.3 2001/01/22 18:05:43 millert Exp $	*/
2 
3 /****************************************************************************
4  * Copyright (c) 1998,2000 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: Zeyd M. Ben-Halim <zmbenhal@netcom.com> 1992,1995               *
33  *     and: Eric S. Raymond <esr@snark.thyrsus.com>                         *
34  ****************************************************************************/
35 
36 /*
37  *	lib_slkset.c
38  *      Set soft label text.
39  */
40 #include <curses.priv.h>
41 #include <ctype.h>
42 
43 MODULE_ID("$From: lib_slkset.c,v 1.6 2000/12/10 02:43:27 tom Exp $")
44 
45 NCURSES_EXPORT(int)
46 slk_set(int i, const char *astr, int format)
47 {
48     SLK *slk = SP->_slk;
49     size_t len;
50     const char *str = astr;
51     const char *p;
52 
53     T((T_CALLED("slk_set(%d, \"%s\", %d)"), i, str, format));
54 
55     if (slk == NULL || i < 1 || i > slk->labcnt || format < 0 || format > 2)
56 	returnCode(ERR);
57     if (str == NULL)
58 	str = "";
59 
60     while (isspace(CharOf(*str)))
61 	str++;			/* skip over leading spaces  */
62     p = str;
63     while (isprint(CharOf(*p)))
64 	p++;			/* The first non-print stops */
65 
66     --i;			/* Adjust numbering of labels */
67 
68     len = (size_t) (p - str);
69     if (len > (unsigned) slk->maxlen)
70 	len = slk->maxlen;
71     if (len == 0)
72 	slk->ent[i].text[0] = 0;
73     else
74 	(void) strlcpy(slk->ent[i].text, str, len+1);
75     memset(slk->ent[i].form_text, ' ', (unsigned) slk->maxlen);
76     /* len = strlen(slk->ent[i].text); */
77 
78     switch (format) {
79     case 0:			/* left-justified */
80 	memcpy(slk->ent[i].form_text,
81 	       slk->ent[i].text,
82 	       len);
83 	break;
84     case 1:			/* centered */
85 	memcpy(slk->ent[i].form_text + (slk->maxlen - len) / 2,
86 	       slk->ent[i].text,
87 	       len);
88 	break;
89     case 2:			/* right-justified */
90 	memcpy(slk->ent[i].form_text + slk->maxlen - len,
91 	       slk->ent[i].text,
92 	       len);
93 	break;
94     }
95     slk->ent[i].form_text[slk->maxlen] = 0;
96     slk->ent[i].dirty = TRUE;
97     returnCode(OK);
98 }
99