1 /****************************************************************************
2 * Copyright (c) 2009 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_addchstr.c,v 1.5 2009/10/10 16:01:41 tom Exp $
31 * Demonstrate the waddchstr() and waddch functions.
32 * Thomas Dickey - 2009/9/12
35 #include <test.priv.h>
39 #define AddNStr addchnstr
40 #define AddStr addchstr
41 #define MvAddNStr mvaddchnstr
42 #define MvAddStr mvaddchstr
43 #define MvWAddNStr mvwaddchnstr
44 #define MvWAddStr mvwaddchstr
45 #define WAddNStr waddchnstr
46 #define WAddStr waddchstr
49 #define MvAddCh mvaddch
50 #define MvWAddCh mvwaddch
62 static bool m_opt = FALSE;
63 static bool w_opt = FALSE;
64 static int n_opt = -1;
66 static chtype *temp_buffer;
67 static size_t temp_length;
69 #define TempBuffer(source_cast)
72 ChStr(const char *source)
75 size_t need = strlen(source) + 1;
79 if (need > temp_length) {
80 temp_length = need * 2;
81 temp_buffer = typeRealloc(chtype, temp_length, temp_buffer);
86 temp_buffer[n++] = UChar(*source++);
87 } while (have[0] != 0);
88 } else if (temp_buffer != 0) {
97 legend(WINDOW *win, int level, Options state, char *buffer, int length)
99 NCURSES_CONST char *showstate;
107 showstate = " (mvXXX)";
110 showstate = " (winXXX)";
113 showstate = " (mvwinXXX)";
119 "The Strings/Chars displays should match. Enter any characters, except:\n");
121 "down-arrow or ^N to repeat on next line, 'w' for inner window, 'q' to exit.\n");
123 wprintw(win, "Level %d,%s added %d characters <%s>", level,
124 showstate, length, buffer);
128 ColOf(char *buffer, int length, int margin)
133 for (n = 0, result = margin + 1; n < length; ++n) {
134 int ch = UChar(buffer[n]);
137 /* actually newline should clear the remainder of the line
138 * and move to the next line - but that seems a little awkward
149 result += (MY_TABSIZE - (result % MY_TABSIZE));
164 #define LEN(n) ((length - (n) > n_opt) ? n_opt : (length - (n)))
168 static bool first = TRUE;
180 int margin = (2 * MY_TABSIZE) - 1;
181 Options option = (Options) ((unsigned) (m_opt
184 | (unsigned) ((w_opt || (level > 0))
190 setlocale(LC_ALL, "");
192 putenv(strcpy(cmd, "TABSIZE=8"));
195 (void) cbreak(); /* take input chars one at a time, no wait for \n */
196 (void) noecho(); /* don't echo input */
197 keypad(stdscr, TRUE);
202 look = newwin(limit, COLS - (2 * (level - 1)), 0, level - 1);
203 work = newwin(limit - 2, COLS - (2 * level), 1, level);
204 show = newwin(4, COLS, limit + 1, 0);
210 show = derwin(stdscr, 4, COLS, limit + 1, 0);
214 for (col = margin + 1; col < COLS; col += MY_TABSIZE)
215 mvwvline(work, row, col, '.', limit - 2);
217 mvwvline(work, row, margin, ACS_VLINE, limit - 2);
218 mvwvline(work, row, margin + 1, ACS_VLINE, limit - 2);
221 mvwaddchstr(work, 1, 2, ChStr("String"));
222 mvwaddchstr(work, limit + 1, 2, ChStr("Chars"));
225 buffer[length = 0] = '\0';
226 legend(show, level, option, buffer, length);
232 * Show the characters added in color, to distinguish from those that
237 init_pair(1, COLOR_WHITE, COLOR_BLUE);
238 wbkgdset(work, COLOR_PAIR(1) | ' ');
241 while ((ch = read_linedata(work)) != ERR && !isQUIT(ch)) {
242 wmove(work, row, margin + 1);
245 test_adds(level + 1);
260 /* put the whole string in, all at once */
265 for (col = 0; col < length; col += n_opt) {
266 col2 = ColOf(buffer, col, margin);
267 if (move(row, col2) != ERR) {
268 AddNStr(ChStr(buffer + col), LEN(col));
272 if (move(row, col2) != ERR) {
273 AddStr(ChStr(buffer));
279 for (col = 0; col < length; col += n_opt) {
280 col2 = ColOf(buffer, col, margin);
281 MvAddNStr(row, col2, ChStr(buffer + col), LEN(col));
284 MvAddStr(row, col2, ChStr(buffer));
289 for (col = 0; col < length; col += n_opt) {
290 col2 = ColOf(buffer, col, margin);
291 if (wmove(work, row, col2) != ERR) {
292 WAddNStr(work, ChStr(buffer + col), LEN(col));
296 if (wmove(work, row, col2) != ERR) {
297 WAddStr(work, ChStr(buffer));
303 for (col = 0; col < length; col += n_opt) {
304 col2 = ColOf(buffer, col, margin);
305 MvWAddNStr(work, row, col2, ChStr(buffer + col),
309 MvWAddStr(work, row, col2, ChStr(buffer));
314 /* do the corresponding single-character add */
316 for (col = 0; col < length; ++col) {
317 col2 = ColOf(buffer, col, margin);
320 if (move(row2, col2) != ERR) {
321 AddCh(UChar(buffer[col]));
325 MvAddCh(row2, col2, UChar(buffer[col]));
328 if (wmove(work, row2, col2) != ERR) {
329 WAddCh(work, UChar(buffer[col]));
333 MvWAddCh(work, row2, col2, UChar(buffer[col]));
345 if (ch <= 0 || ch > 255) {
349 buffer[length++] = (char) ch;
350 buffer[length] = '\0';
352 /* put the string in, one character at a time */
353 col = ColOf(buffer, length - 1, margin);
356 if (move(row, col) != ERR) {
357 AddStr(ChStr(buffer + length - 1));
361 MvAddStr(row, col, ChStr(buffer + length - 1));
364 if (wmove(work, row, col) != ERR) {
365 WAddStr(work, ChStr(buffer + length - 1));
369 MvWAddStr(work, row, col, ChStr(buffer + length - 1));
373 /* do the corresponding single-character add */
376 if (move(limit + row, col) != ERR) {
381 MvAddCh(limit + row, col, UChar(ch));
384 if (wmove(work, limit + row, col) != ERR) {
385 WAddCh(work, UChar(ch));
389 MvWAddCh(work, limit + row, col, UChar(ch));
395 legend(show, level, option, buffer, length);
412 static const char *tbl[] =
414 "Usage: test_addchstr [options]"
417 ," -f FILE read data from given file"
418 ," -n NUM limit string-adds to NUM bytes on ^N replay"
419 ," -m perform wmove/move separately from add-functions"
420 ," -w use window-parameter even when stdscr would be implied"
423 for (n = 0; n < SIZEOF(tbl); ++n)
424 fprintf(stderr, "%s\n", tbl[n]);
425 ExitProgram(EXIT_FAILURE);
429 main(int argc GCC_UNUSED, char *argv[]GCC_UNUSED)
433 setlocale(LC_ALL, "");
435 while ((ch = getopt(argc, argv, "f:mn:w")) != -1) {
438 init_linedata(optarg);
444 n_opt = atoi(optarg);
461 ExitProgram(EXIT_SUCCESS);