1 /****************************************************************************
2 * Copyright (c) 2009-2010,2012 Free Software Foundation, Inc. *
4 * Permission is hereby granted, free of charge, to any person obtaining a *
5 * copy of this software and associated documentation files (the *
6 * "Software"), to deal in the Software without restriction, including *
7 * without limitation the rights to use, copy, modify, merge, publish, *
8 * distribute, distribute with modifications, sublicense, and/or sell *
9 * copies of the Software, and to permit persons to whom the Software is *
10 * furnished to do so, subject to the following conditions: *
12 * The above copyright notice and this permission notice shall be included *
13 * in all copies or substantial portions of the Software. *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS *
16 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF *
17 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. *
18 * IN NO EVENT SHALL THE ABOVE COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, *
19 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR *
20 * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR *
21 * THE USE OR OTHER DEALINGS IN THE SOFTWARE. *
23 * Except as contained in this notice, the name(s) of the above copyright *
24 * holders shall not be used in advertising or otherwise to promote the *
25 * sale, use or other dealings in this Software without prior written *
27 ****************************************************************************/
29 * $Id: test_addstr.c,v 1.10 2012/12/16 00:14:10 tom Exp $
31 * Demonstrate the waddstr() and waddch functions.
32 * Thomas Dickey - 2009/9/12
35 #include <test.priv.h>
39 #define AddNStr addnstr
41 #define MvAddNStr (void) mvaddnstr
42 #define MvWAddNStr (void) mvwaddnstr
43 #define WAddNStr waddnstr
44 #define WAddStr waddstr
58 static bool m_opt = FALSE;
59 static bool w_opt = FALSE;
60 static int n_opt = -1;
63 legend(WINDOW *win, int level, Options state, char *buffer, int length)
65 const char *showstate;
73 showstate = " (mvXXX)";
76 showstate = " (winXXX)";
79 showstate = " (mvwinXXX)";
85 "The Strings/Chars displays should match. Enter any characters, except:\n");
87 "down-arrow or ^N to repeat on next line, ^W for inner window, ESC to exit.\n");
89 wprintw(win, "Level %d,%s added %d characters <%s>", level,
90 showstate, length, buffer);
94 ColOf(char *buffer, int length, int margin)
99 for (n = 0, result = margin + 1; n < length; ++n) {
100 int ch = UChar(buffer[n]);
103 /* actually newline should clear the remainder of the line
104 * and move to the next line - but that seems a little awkward
115 result += (MY_TABSIZE - (result % MY_TABSIZE));
130 #define LEN(n) ((length - (n) > n_opt) ? n_opt : (length - (n)))
134 static bool first = TRUE;
146 int margin = (2 * MY_TABSIZE) - 1;
147 Options option = (Options) ((unsigned) (m_opt
150 | (unsigned) ((w_opt || (level > 0))
156 setlocale(LC_ALL, "");
158 putenv(strcpy(cmd, "TABSIZE=8"));
161 (void) cbreak(); /* take input chars one at a time, no wait for \n */
162 (void) noecho(); /* don't echo input */
163 keypad(stdscr, TRUE);
166 * Show the characters added in color, to distinguish from those that
171 init_pair(1, COLOR_WHITE, COLOR_BLUE);
177 look = newwin(limit, COLS - (2 * (level - 1)), 0, level - 1);
178 work = newwin(limit - 2, COLS - (2 * level), 1, level);
179 show = newwin(4, COLS, limit + 1, 0);
185 show = derwin(stdscr, 4, COLS, limit + 1, 0);
189 for (col = margin + 1; col < COLS; col += MY_TABSIZE)
190 MvWVLine(work, row, col, '.', limit - 2);
192 MvWVLine(work, row, margin, ACS_VLINE, limit - 2);
193 MvWVLine(work, row, margin + 1, ACS_VLINE, limit - 2);
196 MvWAddStr(work, 1, 2, "String");
197 MvWAddStr(work, limit + 1, 2, "Chars");
200 buffer[length = 0] = '\0';
201 legend(show, level, option, buffer, length);
207 wbkgdset(work, (chtype) (COLOR_PAIR(1) | ' '));
210 while ((ch = read_linedata(work)) != ERR && !isQUIT(ch)) {
211 wmove(work, row, margin + 1);
214 test_adds(level + 1);
231 /* put the whole string in, all at once */
236 for (col = 0; col < length; col += n_opt) {
237 col2 = ColOf(buffer, col, margin);
238 if (move(row, col2) != ERR) {
239 AddNStr(buffer + col, LEN(col));
243 if (move(row, col2) != ERR) {
250 for (col = 0; col < length; col += n_opt) {
251 col2 = ColOf(buffer, col, margin);
252 MvAddNStr(row, col2, buffer + col, LEN(col));
255 MvAddStr(row, col2, buffer);
260 for (col = 0; col < length; col += n_opt) {
261 col2 = ColOf(buffer, col, margin);
262 if (wmove(work, row, col2) != ERR) {
263 WAddNStr(work, buffer + col, LEN(col));
267 if (wmove(work, row, col2) != ERR) {
268 WAddStr(work, buffer);
274 for (col = 0; col < length; col += n_opt) {
275 col2 = ColOf(buffer, col, margin);
276 MvWAddNStr(work, row, col2, buffer + col, LEN(col));
279 MvWAddStr(work, row, col2, buffer);
284 /* do the corresponding single-character add */
286 for (col = 0; col < length; ++col) {
287 col2 = ColOf(buffer, col, margin);
290 if (move(row2, col2) != ERR) {
291 AddCh(UChar(buffer[col]));
295 MvAddCh(row2, col2, UChar(buffer[col]));
298 if (wmove(work, row2, col2) != ERR) {
299 WAddCh(work, UChar(buffer[col]));
303 MvWAddCh(work, row2, col2, UChar(buffer[col]));
312 if (ch <= 0 || ch > 255) {
316 buffer[length++] = (char) ch;
317 buffer[length] = '\0';
319 /* put the string in, one character at a time */
320 col = ColOf(buffer, length - 1, margin);
323 if (move(row, col) != ERR) {
324 AddStr(buffer + length - 1);
328 MvAddStr(row, col, buffer + length - 1);
331 if (wmove(work, row, col) != ERR) {
332 WAddStr(work, buffer + length - 1);
336 MvWAddStr(work, row, col, buffer + length - 1);
340 /* do the corresponding single-character add */
343 if (move(limit + row, col) != ERR) {
348 MvAddCh(limit + row, col, UChar(ch));
351 if (wmove(work, limit + row, col) != ERR) {
352 WAddCh(work, UChar(ch));
356 MvWAddCh(work, limit + row, col, UChar(ch));
362 legend(show, level, option, buffer, length);
379 static const char *tbl[] =
381 "Usage: test_addstr [options]"
384 ," -f FILE read data from given file"
385 ," -n NUM limit string-adds to NUM bytes on ^N replay"
386 ," -m perform wmove/move separately from add-functions"
387 ," -w use window-parameter even when stdscr would be implied"
390 for (n = 0; n < SIZEOF(tbl); ++n)
391 fprintf(stderr, "%s\n", tbl[n]);
392 ExitProgram(EXIT_FAILURE);
396 main(int argc GCC_UNUSED, char *argv[]GCC_UNUSED)
400 setlocale(LC_ALL, "");
402 while ((ch = getopt(argc, argv, "f:mn:w")) != -1) {
405 init_linedata(optarg);
411 n_opt = atoi(optarg);
428 ExitProgram(EXIT_SUCCESS);